@@ -3,34 +3,39 @@ import { Yok } from "../../../../yok";
3
3
import { assert } from "chai" ;
4
4
import * as path from "path" ;
5
5
import * as childProcess from "child_process" ;
6
+ import * as fileSystem from "fs" ;
7
+ import { EventEmitter } from "events" ;
8
+ import stream = require( "stream" ) ;
9
+
10
+ class ChildProcessMockInstance extends EventEmitter {
11
+ public stdout : stream . Readable ;
12
+ public stderr : any ;
13
+ public processKillCallCount = 0 ;
14
+
15
+ constructor ( pathToSample : string ) {
16
+ super ( ) ;
17
+ this . stdout = fileSystem . createReadStream ( pathToSample ) ;
18
+ this . stderr = new EventEmitter ( ) ;
19
+ }
20
+
21
+ public kill ( ) : void {
22
+ this . processKillCallCount ++ ;
23
+ }
24
+ }
6
25
7
26
class ChildProcessStub {
8
27
public processSpawnCallCount = 0 ;
9
- public processKillCallCount = 0 ;
10
28
public adbProcessArgs : string [ ] = [ ] ;
11
- private isWin = / ^ w i n / . test ( process . platform ) ;
29
+ public lastSpawnedProcess : ChildProcessMockInstance = null ;
12
30
13
31
public spawn ( command : string , args ?: string [ ] , options ?: any ) : childProcess . ChildProcess {
14
32
this . adbProcessArgs = args ;
15
33
this . processSpawnCallCount ++ ;
16
- let pathToExecutable = "" ;
17
- let shell = "" ;
18
- if ( this . isWin ) {
19
- pathToExecutable = "type" ;
20
- shell = "cmd" ;
21
- } else {
22
- pathToExecutable = "cat" ;
23
- }
24
- pathToExecutable = path . join ( pathToExecutable ) ;
25
34
const pathToSample = path . join ( __dirname , "valid-sample.txt" ) ;
26
- const spawnedProcess = childProcess . spawn ( pathToExecutable , [ pathToSample ] , { shell } ) ;
27
- const spawnedProcessKill = spawnedProcess . kill ;
28
- spawnedProcess . kill = ( signal : string ) => {
29
- this . processKillCallCount ++ ;
30
- spawnedProcessKill . call ( spawnedProcessKill , signal ) ;
31
- } ;
32
-
33
- return spawnedProcess ;
35
+
36
+ this . lastSpawnedProcess = new ChildProcessMockInstance ( pathToSample ) ;
37
+
38
+ return < any > this . lastSpawnedProcess ;
34
39
}
35
40
}
36
41
@@ -207,7 +212,7 @@ describe("logcat-helper", () => {
207
212
await logcatHelper . stop ( validIdentifier ) ;
208
213
await logcatHelper . stop ( validIdentifier ) ;
209
214
210
- assert . equal ( childProcessStub . processKillCallCount , 1 ) ;
215
+ assert . equal ( childProcessStub . lastSpawnedProcess . processKillCallCount , 1 ) ;
211
216
} ) ;
212
217
213
218
it ( "should not kill the process if started with keepSingleProcess" , async ( ) => {
@@ -220,7 +225,7 @@ describe("logcat-helper", () => {
220
225
221
226
await logcatHelper . stop ( validIdentifier ) ;
222
227
await logcatHelper . stop ( validIdentifier ) ;
223
- assert . equal ( childProcessStub . processKillCallCount , 0 ) ;
228
+ assert . equal ( childProcessStub . lastSpawnedProcess . processKillCallCount , 0 ) ;
224
229
} ) ;
225
230
226
231
it ( "should do nothing if called without start" , async ( ) => {
@@ -229,7 +234,27 @@ describe("logcat-helper", () => {
229
234
await logcatHelper . stop ( validIdentifier ) ;
230
235
231
236
assert . equal ( childProcessStub . processSpawnCallCount , 0 ) ;
232
- assert . equal ( childProcessStub . processKillCallCount , 0 ) ;
233
237
} ) ;
238
+
239
+ for ( const keepSingleProcess of [ false , true ] ) {
240
+ it ( `should clear the device identifier cache when the logcat process is killed manually and keepSingleProcess is ${ keepSingleProcess } ` , async ( ) => {
241
+ const logcatHelper = injector . resolve < LogcatHelper > ( "logcatHelper" ) ;
242
+
243
+ await logcatHelper . start ( {
244
+ deviceIdentifier : validIdentifier ,
245
+ keepSingleProcess
246
+ } ) ;
247
+
248
+ assert . equal ( childProcessStub . processSpawnCallCount , 1 ) ;
249
+
250
+ childProcessStub . lastSpawnedProcess . emit ( "close" ) ;
251
+
252
+ await logcatHelper . start ( {
253
+ deviceIdentifier : validIdentifier
254
+ } ) ;
255
+
256
+ assert . equal ( childProcessStub . processSpawnCallCount , 2 ) ;
257
+ } ) ;
258
+ }
234
259
} ) ;
235
260
} ) ;
0 commit comments