Skip to content

Commit

Permalink
Option to use posix exit code upon fatal signal
Browse files Browse the repository at this point in the history
  • Loading branch information
73rhodes committed Jan 11, 2024
1 parent 53a4baf commit 8999b7b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
10 changes: 8 additions & 2 deletions bin/mocha.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @private
*/

const os = require('os');
const {loadOptions} = require('../lib/cli/options');
const {
unparseNodeFlags,
Expand Down Expand Up @@ -109,7 +110,12 @@ if (mochaArgs['node-option'] || Object.keys(nodeArgs).length || hasInspect) {
proc.on('exit', (code, signal) => {
process.on('exit', () => {
if (signal) {
process.kill(process.pid, signal);
if (mochaArgs['posix-exit-codes'] === true) {
process.exitCode = 128 + os.constants.signals[signal];
process.exit(process.exitCode);
} else {
process.kill(process.pid, signal);
}
} else {
process.exit(code);
}
Expand All @@ -126,7 +132,7 @@ if (mochaArgs['node-option'] || Object.keys(nodeArgs).length || hasInspect) {
// be needed.
if (!args.parallel || args.jobs < 2) {
// win32 does not support SIGTERM, so use next best thing.
if (require('os').platform() === 'win32') {
if (os.platform() === 'win32') {
proc.kill('SIGKILL');
} else {
// using SIGKILL won't cleanly close the output streams, which can result
Expand Down
1 change: 1 addition & 0 deletions lib/cli/run-option-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const TYPES = (exports.types = {
'list-reporters',
'no-colors',
'parallel',
'posix-exit-codes',
'recursive',
'sort',
'watch'
Expand Down
4 changes: 4 additions & 0 deletions lib/cli/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ exports.builder = yargs =>
description: 'Run tests in parallel',
group: GROUPS.RULES
},
'posix-exit-codes': {
description: 'Use posix exit codes for fatal signals',
group: GROUPS.RULES
},
recursive: {
description: 'Look for tests in subdirectories',
group: GROUPS.FILES
Expand Down
7 changes: 7 additions & 0 deletions test/integration/fixtures/posix-exit-codes.fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

describe('signal suite', function () {
it('test SIGABRT', function () {
process.kill(process.pid, 'SIGABRT');
});
});
36 changes: 36 additions & 0 deletions test/integration/options/posixExitCodes.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

var helpers = require('../helpers');
var runMocha = helpers.runMocha;

describe('--posix-exit-codes', function () {
// subprocess
var mocha;

function killSubprocess() {
mocha.kill('SIGKILL');
}

// these two handlers deal with a ctrl-c on command-line
before(function () {
process.on('SIGINT', killSubprocess);
});

after(function () {
process.removeListener('SIGINT', killSubprocess);
});

describe('when enabled with node options', function () {
it('should exit with code 134 on SIGABRT', function (done) {
var fixture = 'posix-exit-codes.fixture.js';
var args = ['--no-warnings', '--posix-exit-codes'];
mocha = runMocha(fixture, args, function postmortem(err, res) {
if (err) {
return done(err);
}
expect(res.code, 'to be', 134);
done();
});
});
});
});

0 comments on commit 8999b7b

Please sign in to comment.