Skip to content

Commit 2296e49

Browse files
committed
Option to use posix exit code upon fatal signal
1 parent 37deed2 commit 2296e49

File tree

5 files changed

+56
-2
lines changed

5 files changed

+56
-2
lines changed

bin/mocha.js

100644100755
+8-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* @private
1111
*/
1212

13+
const os = require('os');
1314
const {loadOptions} = require('../lib/cli/options');
1415
const {
1516
unparseNodeFlags,
@@ -109,7 +110,12 @@ if (mochaArgs['node-option'] || Object.keys(nodeArgs).length || hasInspect) {
109110
proc.on('exit', (code, signal) => {
110111
process.on('exit', () => {
111112
if (signal) {
112-
process.kill(process.pid, signal);
113+
if (mochaArgs['posix-exit-codes'] === true) {
114+
process.exitCode = 128 + os.constants.signals[signal];
115+
process.exit(process.exitCode);
116+
} else {
117+
process.kill(process.pid, signal);
118+
}
113119
} else {
114120
process.exit(code);
115121
}
@@ -126,7 +132,7 @@ if (mochaArgs['node-option'] || Object.keys(nodeArgs).length || hasInspect) {
126132
// be needed.
127133
if (!args.parallel || args.jobs < 2) {
128134
// win32 does not support SIGTERM, so use next best thing.
129-
if (require('os').platform() === 'win32') {
135+
if (os.platform() === 'win32') {
130136
proc.kill('SIGKILL');
131137
} else {
132138
// using SIGKILL won't cleanly close the output streams, which can result

lib/cli/run-option-metadata.js

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const TYPES = (exports.types = {
4545
'list-reporters',
4646
'no-colors',
4747
'parallel',
48+
'posix-exit-codes',
4849
'recursive',
4950
'sort',
5051
'watch'

lib/cli/run.js

+4
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ exports.builder = yargs =>
190190
description: 'Run tests in parallel',
191191
group: GROUPS.RULES
192192
},
193+
'posix-exit-codes': {
194+
description: 'Use posix exit codes for fatal signals',
195+
group: GROUPS.RULES
196+
},
193197
recursive: {
194198
description: 'Look for tests in subdirectories',
195199
group: GROUPS.FILES
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
describe('signal suite', function () {
4+
it('test SIGABRT', function () {
5+
process.kill(process.pid, 'SIGABRT');
6+
});
7+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
var helpers = require('../helpers');
4+
var runMocha = helpers.runMocha;
5+
6+
describe('--posix-exit-codes', function () {
7+
// subprocess
8+
var mocha;
9+
10+
function killSubprocess() {
11+
mocha.kill('SIGKILL');
12+
}
13+
14+
// these two handlers deal with a ctrl-c on command-line
15+
before(function () {
16+
process.on('SIGINT', killSubprocess);
17+
});
18+
19+
after(function () {
20+
process.removeListener('SIGINT', killSubprocess);
21+
});
22+
23+
describe('when enabled with node options', function () {
24+
it('should exit with code 134 on SIGABRT', function (done) {
25+
var fixture = 'posix-exit-codes.fixture.js';
26+
var args = ['--no-warnings', '--posix-exit-codes'];
27+
mocha = runMocha(fixture, args, function postmortem(err, res) {
28+
if (err) {
29+
return done(err);
30+
}
31+
expect(res.code, 'to be', 134);
32+
done();
33+
});
34+
});
35+
});
36+
});

0 commit comments

Comments
 (0)