Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dderidder/x mochajs pr review part2 #5134

Closed
13 changes: 10 additions & 3 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,9 +110,15 @@ if (mochaArgs['node-option'] || Object.keys(nodeArgs).length || hasInspect) {
proc.on('exit', (code, signal) => {
process.on('exit', () => {
if (signal) {
process.kill(process.pid, signal);
const numericSignal =
typeof signal === 'string' ? os.constants.signals[signal] : signal;
if (mochaArgs['posix-exit-codes'] === true) {
process.exit(128 + numericSignal);
} else {
process.kill(process.pid, signal);
}
} else {
process.exit(code);
process.exit((mochaArgs['posix-exit-codes'] === true) ? 0 : code);
}
});
});
Expand All @@ -126,7 +133,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
4 changes: 4 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,10 @@ Define a global variable name. For example, suppose your app deliberately expose

By using this option in conjunction with `--check-leaks`, you can specify a whitelist of known global variables that you _expect_ to leak into global scope.

### `--posix-exit-codes`

Exits with standard POSIX exit codes instead of the number of failed tests.

### `--retries <n>`

Retries failed tests `n` times.
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/signals-sigabrt.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');
});
});
7 changes: 7 additions & 0 deletions test/integration/fixtures/signals-sigterm.fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

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

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

describe('--posix-exit-codes', function () {
describe('when enabled with node options', function () {
var args = ['--no-warnings', '--posix-exit-codes'];

it('should exit with code 134 on SIGABRT', function (done) {
var fixture = 'signals-sigabrt.fixture.js';
runMocha(fixture, args, function postmortem(err, res) {
if (err) {
return done(err);
}
expect(res.code, 'to be', 134);
done();
});
});

it('should exit with code 143 on SIGTERM', function (done) {
var fixture = 'signals-sigterm.fixture.js';
runMocha(fixture, args, function postmortem(err, res) {
if (err) {
return done(err);
}
expect(res.code, 'to be', 143);
done();
});
});
});

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

it('should exit with the number of failed tests', function (done) {
var fixture = 'failing.fixture.js'; // one failing test
var args = ['--no-warnings'];
runMocha(fixture, args, function postmortem(err, res) {
if (err) {
return done(err);
}
expect(res.code, 'to be', 1);
done();
});
});
});
});