File tree 5 files changed +66
-2
lines changed 5 files changed +66
-2
lines changed Original file line number Diff line number Diff line change @@ -189,9 +189,9 @@ module.exports = function runTask(task, options) {
189
189
cp = null
190
190
reject ( err )
191
191
} )
192
- cp . on ( "close" , ( code ) => {
192
+ cp . on ( "close" , ( code , signal ) => {
193
193
cp = null
194
- resolve ( { task, code } )
194
+ resolve ( { task, code, signal } )
195
195
} )
196
196
} )
197
197
Original file line number Diff line number Diff line change @@ -32,6 +32,39 @@ function remove(array, x) {
32
32
}
33
33
}
34
34
35
+ const signals = {
36
+ SIGABRT : 6 ,
37
+ SIGALRM : 14 ,
38
+ SIGBUS : 10 ,
39
+ SIGCHLD : 20 ,
40
+ SIGCONT : 19 ,
41
+ SIGFPE : 8 ,
42
+ SIGHUP : 1 ,
43
+ SIGILL : 4 ,
44
+ SIGINT : 2 ,
45
+ SIGKILL : 9 ,
46
+ SIGPIPE : 13 ,
47
+ SIGQUIT : 3 ,
48
+ SIGSEGV : 11 ,
49
+ SIGSTOP : 17 ,
50
+ SIGTERM : 15 ,
51
+ SIGTRAP : 5 ,
52
+ SIGTSTP : 18 ,
53
+ SIGTTIN : 21 ,
54
+ SIGTTOU : 22 ,
55
+ SIGUSR1 : 30 ,
56
+ SIGUSR2 : 31 ,
57
+ }
58
+
59
+ /**
60
+ * Converts a signal name to a number.
61
+ * @param {string } signal - the signal name to convert into a number
62
+ * @returns {number } - the return code for the signal
63
+ */
64
+ function convert ( signal ) {
65
+ return signals [ signal ] || 0
66
+ }
67
+
35
68
//------------------------------------------------------------------------------
36
69
// Public Interface
37
70
//------------------------------------------------------------------------------
@@ -133,6 +166,15 @@ module.exports = function runTasks(tasks, options) {
133
166
originalOutputStream . write ( writer . toString ( ) )
134
167
}
135
168
169
+ // Check if the task failed as a result of a signal, and
170
+ // amend the exit code as a result.
171
+ if ( result . code === null && result . signal !== null ) {
172
+ // An exit caused by a signal must return a status code
173
+ // of 128 plus the value of the signal code.
174
+ // Ref: https://nodejs.org/api/process.html#process_exit_codes
175
+ result . code = 128 + convert ( result . signal )
176
+ }
177
+
136
178
// Save the result.
137
179
results [ task . index ] . code = result . code
138
180
Original file line number Diff line number Diff line change 26
26
"test-task:append:b" : " node tasks/append2.js b" ,
27
27
"test-task:append1" : " node tasks/append1.js" ,
28
28
"test-task:append2" : " node tasks/append2.js" ,
29
+ "test-task:abort" : " node tasks/abort.js" ,
29
30
"test-task:error" : " node tasks/error.js" ,
30
31
"test-task:stdout" : " node tasks/stdout.js > test.txt" ,
31
32
"test-task:stderr" : " node tasks/stderr.js 2> test.txt" ,
Original file line number Diff line number Diff line change
1
+ "use strict"
2
+
3
+ setTimeout ( ( ) => {
4
+ process . abort ( )
5
+ } , 500 )
Original file line number Diff line number Diff line change @@ -101,4 +101,20 @@ describe("[fail] it should fail", () => {
101
101
it ( "run-s command" , ( ) => shouldFail ( runSeq ( [ "test-task:error" ] ) ) )
102
102
it ( "run-p command" , ( ) => shouldFail ( runPar ( [ "test-task:error" ] ) ) )
103
103
} )
104
+
105
+ describe ( "if tasks exited via a signal:" , ( ) => {
106
+ it ( "Node API" , ( ) => shouldFail ( nodeApi ( "test-task:abort" ) ) )
107
+ it ( "npm-run-all command" , ( ) => shouldFail ( runAll ( [ "test-task:abort" ] ) ) )
108
+ it ( "run-s command" , ( ) => shouldFail ( runSeq ( [ "test-task:abort" ] ) ) )
109
+ it ( "run-p command" , ( ) => shouldFail ( runPar ( [ "test-task:abort" ] ) ) )
110
+ it ( "with correct exit code" , ( ) => nodeApi ( "test-task:abort" ) . then ( ( ) =>
111
+ assert ( false , "should fail" )
112
+ ) . catch ( err => {
113
+ // In NodeJS versions > 6, the child process correctly sends back
114
+ // the signal + code of null. In NodeJS versions <= 6, the child
115
+ // process does not set the signal, and sets the code to 1.
116
+ const code = Number ( process . version . match ( / ^ v ( \d + ) / ) [ 1 ] ) > 6 ? 134 : 1
117
+ assert ( err . code === code , "should have correct exit code" )
118
+ } ) )
119
+ } )
104
120
} )
You can’t perform that action at this time.
0 commit comments