From d2991a18c1b02c7f23e8abc62631b06d6dfa3b3a Mon Sep 17 00:00:00 2001 From: Darren DeRidder Date: Mon, 15 Apr 2024 13:44:54 -0400 Subject: [PATCH] Address PR comments; use os.constants --- bin/mocha.js | 9 ++++++-- lib/cli/run.js | 2 +- test/integration/fixtures/failing.fixture.js | 23 +++++++++++++++++++ .../options/posixExitCodes.spec.js | 22 +++++++++++------- 4 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 test/integration/fixtures/failing.fixture.js diff --git a/bin/mocha.js b/bin/mocha.js index d5f914a462..27e02fd5e9 100755 --- a/bin/mocha.js +++ b/bin/mocha.js @@ -23,6 +23,9 @@ const {aliases} = require('../lib/cli/run-option-metadata'); const mochaArgs = {}; const nodeArgs = {}; +const EXIT_SUCCESS = 0; +const EXIT_FAILURE = 1; +const SIGNAL_OFFSET= 128; let hasInspect = false; const opts = loadOptions(process.argv.slice(2)); @@ -113,12 +116,14 @@ if (mochaArgs['node-option'] || Object.keys(nodeArgs).length || hasInspect) { const numericSignal = typeof signal === 'string' ? os.constants.signals[signal] : signal; if (mochaArgs['posix-exit-codes'] === true) { - process.exit(128 + numericSignal); + process.exit(SIGNAL_OFFSET + numericSignal); } else { process.kill(process.pid, signal); } + } else if (code !== 0 && mochaArgs['posix-exit-codes'] === true) { + process.exit(EXIT_FAILURE); } else { - process.exit((mochaArgs['posix-exit-codes'] === true) ? 0 : code); + process.exit(code); } }); }); diff --git a/lib/cli/run.js b/lib/cli/run.js index a4830e97e6..a50418d3f7 100644 --- a/lib/cli/run.js +++ b/lib/cli/run.js @@ -191,7 +191,7 @@ exports.builder = yargs => group: GROUPS.RULES }, 'posix-exit-codes': { - description: 'Use posix exit codes for fatal signals', + description: 'Use POSIX and UNIX shell exit codes as Mocha\'s return value', group: GROUPS.RULES }, recursive: { diff --git a/test/integration/fixtures/failing.fixture.js b/test/integration/fixtures/failing.fixture.js new file mode 100644 index 0000000000..337d628991 --- /dev/null +++ b/test/integration/fixtures/failing.fixture.js @@ -0,0 +1,23 @@ +'use strict'; + +// One passing test and three failing tests + +var assert = require('assert'); + +describe('suite', function () { + it('test1', function () { + assert(true); + }); + + it('test2', function () { + assert(false); + }); + + it('test3', function () { + assert(false); + }); + + it('test4', function () { + assert(false); + }); +}); diff --git a/test/integration/options/posixExitCodes.spec.js b/test/integration/options/posixExitCodes.spec.js index 4510a7c42c..c86a7105b3 100644 --- a/test/integration/options/posixExitCodes.spec.js +++ b/test/integration/options/posixExitCodes.spec.js @@ -2,40 +2,45 @@ var helpers = require('../helpers'); var runMocha = helpers.runMocha; +var os = require('os'); + +const EXIT_SUCCESS = 0; +const EXIT_FAILURE = 1; +const SIGNAL_OFFSET = 128; describe('--posix-exit-codes', function () { describe('when enabled, and with node options', function () { var args = ['--no-warnings', '--posix-exit-codes']; - it('should exit with code 134 on SIGABRT', function (done) { + it('should exit with correct POSIX shell code 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); + expect(res.code, 'to be', SIGNAL_OFFSET + os.constants.signals.SIGABRT); done(); }); }); - it('should exit with code 143 on SIGTERM', function (done) { + it('should exit with correct POSIX shell code 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); + expect(res.code, 'to be', SIGNAL_OFFSET + os.constants.signals.SIGTERM); done(); }); }); - it('should exit with code 0 even if there are test failures', function (done) { + it('should exit with code 1 if there are test failures', function (done) { var fixture = 'failing.fixture.js'; runMocha(fixture, args, function postmortem(err, res) { if (err) { return done(err); } - expect(res.code, 'to be', 0); + expect(res.code, 'to be', EXIT_FAILURE); done(); }); }); @@ -55,13 +60,14 @@ describe('--posix-exit-codes', function () { }); it('should exit with the number of failed tests', function (done) { - var fixture = 'failing.fixture.js'; // one failing test + var fixture = 'failing.fixture.js'; // contains three failing tests + var numFailures = 3; var args = ['--no-warnings']; runMocha(fixture, args, function postmortem(err, res) { if (err) { return done(err); } - expect(res.code, 'to be', 1); + expect(res.code, 'to be', numFailures); done(); }); });