@@ -3,14 +3,15 @@ should = chai.should()
3
3
spawn = require (" child_process" ).spawn
4
4
Promise = require (" bluebird" )
5
5
6
+ verbose = 0
7
+
6
8
# cross platform compatibility
7
9
if process .platform == " win32"
8
10
sh = " cmd"
9
- shFlag = " /c"
11
+ shArg = " /c"
10
12
else
11
13
sh = " sh"
12
- shFlag = " -c"
13
-
14
+ shArg = " -c"
14
15
15
16
# children
16
17
waitingProcess = " \" node -e 'setTimeout(function(){},10000);'\" "
@@ -22,53 +23,61 @@ usageInfo = """
22
23
-w, --wait will not close sibling processes on error
23
24
""" .split (" \n " )
24
25
26
+ cmdWrapper = (cmd ) ->
27
+ if process .platform != " win32"
28
+ cmd = " exec " + cmd
29
+ if verbose
30
+ console .log " Calling: " + cmd
31
+ return cmd
32
+
25
33
spawnParallelshell = (cmd ) ->
26
- return spawn sh, [shFlag, " node './index.js' " + cmd], {
27
- detached : true ,
34
+ return spawn sh, [shArg, cmdWrapper (" node ./index.js " + cmd )], {
28
35
cwd : process .cwd
29
36
}
30
37
31
38
killPs = (ps ) ->
32
- if process .platform == ' win32'
33
- ps .kill " SIGINT"
34
- else
35
- spawn (sh,[shFlag," kill -INT -" + ps .pid ])
36
-
37
- spyOnPs = (ps ) ->
38
- ps .stdout .setEncoding (" utf8" )
39
- ps .stdout .on " data" , (data ) ->
40
- console .log data
41
- ps .stderr .setEncoding (" utf8" )
42
- ps .stderr .on " data" , (data ) ->
43
- console .log " err: " + data
39
+ ps .kill " SIGINT"
40
+
41
+ spyOnPs = (ps , verbosity = 1 ) ->
42
+ if verbose >= verbosity
43
+ ps .stdout .setEncoding (" utf8" )
44
+ ps .stdout .on " data" , (data ) ->
45
+ console .log data
46
+ ps .stderr .setEncoding (" utf8" )
47
+ ps .stderr .on " data" , (data ) ->
48
+ console .log " err: " + data
44
49
45
50
testOutput = (cmd , expectedOutput ) ->
46
51
return new Promise (resolve ) ->
47
52
ps = spawnParallelshell (cmd)
53
+ spyOnPs ps, 3
48
54
ps .stdout .setEncoding (" utf8" )
49
55
output = []
50
56
ps .stdout .on " data" , (data ) ->
51
57
lines = data .split (" \n " )
52
58
lines .pop () if lines[lines .length - 1 ] == " "
53
59
output = output .concat (lines)
54
60
ps .stdout .on " end" , () ->
55
- for line,i in output
56
- line .should .equal expectedOutput [i]
61
+ for line,i in expectedOutput
62
+ line .should .equal output [i]
57
63
resolve ()
58
64
59
65
describe " parallelshell" , ->
60
66
it " should print on -h and --help" , (done ) ->
61
67
Promise .all ([testOutput (" -h" , usageInfo), testOutput (" --help" , usageInfo)])
62
- .finally done
68
+ .then -> done ()
69
+ .catch done
63
70
64
71
it " should close with exitCode 1 on child error" , (done ) ->
65
72
ps = spawnParallelshell (failingProcess)
73
+ spyOnPs ps, 2
66
74
ps .on " close" , () ->
67
75
ps .exitCode .should .equal 1
68
76
done ()
69
77
70
78
it " should run with a normal child" , (done ) ->
71
79
ps = spawnParallelshell (waitingProcess)
80
+ spyOnPs ps, 1
72
81
ps .on " close" , () ->
73
82
ps .signalCode .should .equal " SIGINT"
74
83
done ()
@@ -81,13 +90,16 @@ describe "parallelshell", ->
81
90
82
91
it " should close sibling processes on child error" , (done ) ->
83
92
ps = spawnParallelshell ([waitingProcess,failingProcess,waitingProcess].join (" " ))
93
+ spyOnPs ps,2
84
94
ps .on " close" , () ->
85
95
ps .exitCode .should .equal 1
86
96
done ()
87
97
88
98
it " should wait for sibling processes on child error when called with -w or --wait" , (done ) ->
89
99
ps = spawnParallelshell ([" -w" ,waitingProcess,failingProcess,waitingProcess].join (" " ))
90
100
ps2 = spawnParallelshell ([" --wait" ,waitingProcess,failingProcess,waitingProcess].join (" " ))
101
+ spyOnPs ps,2
102
+ spyOnPs ps2,2
91
103
setTimeout (() ->
92
104
should .not .exist (ps .signalCode )
93
105
should .not .exist (ps2 .signalCode )
@@ -96,9 +108,11 @@ describe "parallelshell", ->
96
108
),50
97
109
Promise .all [new Promise ((resolve ) -> ps .on (" close" ,resolve)),
98
110
new Promise (resolve ) -> ps2 .on (" close" ,resolve)]
99
- .finally done
111
+ .then -> done ()
112
+ .catch done
100
113
it " should close on CTRL+C / SIGINT" , (done ) ->
101
114
ps = spawnParallelshell ([" -w" ,waitingProcess,failingProcess,waitingProcess].join (" " ))
115
+ spyOnPs ps,2
102
116
ps .on " close" , () ->
103
117
ps .signalCode .should .equal " SIGINT"
104
118
done ()
0 commit comments