-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
feat: add option to use posix exit code upon fatal signal #4989
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
Changes from all commits
b419072
7b36d1e
702ec53
cb2ba84
d000c32
859abe6
34e196d
7c85e3a
abdd3d5
e94876f
169927a
73259a2
5938f49
b155129
5af8365
d09c2bc
9c394f0
bfe7388
b3bc5c5
f365cbc
f9ae3f5
8b3f2e3
7e0efd2
55cbd8b
6613a02
7347748
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -27,7 +27,7 @@ const {UnmatchedFile} = require('./collect-files'); | |||||
| */ | ||||||
| const exitMochaLater = clampedCode => { | ||||||
| process.on('exit', () => { | ||||||
| process.exitCode = clampedCode; | ||||||
| process.exitCode = Math.min(clampedCode, process.argv.includes('--posix-exit-codes') ? 1 : 255); | ||||||
|
||||||
| process.exitCode = Math.min(clampedCode, process.argv.includes('--posix-exit-codes') ? 1 : 255); | |
| process.exitCode = Math.min(clampedCode, usePosixExitCodes() ? 1 : 255); |
Copilot
AI
Jun 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Similar flag-check logic appears in multiple places; consider consolidating this behavior into a single helper function for consistency and easier future maintenance.
| const usePosixExitCodes = process.argv.includes('--posix-exit-codes'); | |
| const usePosixExitCodes = shouldUsePosixExitCodes(); |
73rhodes marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| }); | ||
| }); |
| 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'); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| 'use strict'; | ||
| const os = require('node:os'); | ||
|
|
||
| describe('signal suite', function () { | ||
| it('test SIGTERM', function () { | ||
| process.kill(process.pid, os.constants.signals['SIGTERM']); | ||
| }); | ||
| }); |
| 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'); | ||
| }); | ||
| }); |
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| 'use strict'; | ||
|
|
||
| var helpers = require('../helpers'); | ||
| var runMocha = helpers.runMocha; | ||
| var os = require('node:os'); | ||
|
|
||
| const EXIT_SUCCESS = 0; | ||
| const EXIT_FAILURE = 1; | ||
| const SIGNAL_OFFSET = 128; | ||
|
|
||
| describe('--posix-exit-codes', function () { | ||
| if (os.platform() !== 'win32') { | ||
| describe('when enabled', function () { | ||
| describe('when mocha is run as a child process', () => { | ||
| // 'no-warnings' node option makes mocha run as a child process | ||
| const args = ['--no-warnings', '--posix-exit-codes']; | ||
|
|
||
| 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', | ||
| SIGNAL_OFFSET + os.constants.signals.SIGABRT | ||
| ); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should exit with correct POSIX shell code on SIGTERM', function (done) { | ||
| // SIGTERM is not supported on Windows | ||
| if (os.platform() !== 'win32') { | ||
| var fixture = 'signals-sigterm.fixture.js'; | ||
| runMocha(fixture, args, function postmortem(err, res) { | ||
| if (err) { | ||
| return done(err); | ||
| } | ||
| expect( | ||
| res.code, | ||
| 'to be', | ||
| SIGNAL_OFFSET + os.constants.signals.SIGTERM | ||
| ); | ||
| done(); | ||
| }); | ||
| } else { | ||
| done(); | ||
| } | ||
| }); | ||
|
|
||
| it('should exit with the correct POSIX shell code on numeric fatal signal', function (done) { | ||
| // not supported on Windows | ||
| if (os.platform() !== 'win32') { | ||
| var fixture = 'signals-sigterm-numeric.fixture.js'; | ||
| runMocha(fixture, args, function postmortem(err, res) { | ||
| if (err) { | ||
| return done(err); | ||
| } | ||
| expect( | ||
| res.code, | ||
| 'to be', | ||
| SIGNAL_OFFSET + os.constants.signals.SIGTERM | ||
| ); | ||
| done(); | ||
| }); | ||
| } else { | ||
| 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', EXIT_FAILURE); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
73rhodes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| describe('when mocha is run in-process', () => { | ||
| // Without node-specific cli options, mocha runs in-process | ||
| const args = ['--posix-exit-codes']; | ||
|
|
||
| it('should exit with the 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', | ||
| SIGNAL_OFFSET + os.constants.signals.SIGABRT | ||
| ); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should exit with the correct POSIX shell code on SIGTERM', function (done) { | ||
| // SIGTERM is not supported on Windows | ||
| if (os.platform() !== 'win32') { | ||
| var fixture = 'signals-sigterm.fixture.js'; | ||
| runMocha(fixture, args, function postmortem(err, res) { | ||
| if (err) { | ||
| return done(err); | ||
| } | ||
| expect( | ||
| res.code, | ||
| 'to be', | ||
| SIGNAL_OFFSET + os.constants.signals.SIGTERM | ||
| ); | ||
| done(); | ||
| }); | ||
| } else { | ||
| 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', EXIT_FAILURE); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('when not enabled', function () { | ||
| describe('when mocha is run as a child process', () => { | ||
| // any node-specific option makes mocha run as a child process | ||
| var args = ['--no-warnings']; | ||
|
|
||
| it('should exit with the number of failed tests', function (done) { | ||
| var fixture = 'failing.fixture.js'; | ||
| var numFailures = 3; | ||
| runMocha(fixture, args, function postmortem(err, res) { | ||
| if (err) { | ||
| return done(err); | ||
| } | ||
| expect(res.code, 'to be', numFailures); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('when mocha is run in-process', () => { | ||
| var args = []; | ||
|
|
||
| it('should exit with the number of failed tests', function (done) { | ||
| var fixture = 'failing.fixture.js'; | ||
| var numFailures = 3; | ||
| runMocha(fixture, args, function postmortem(err, res) { | ||
| if (err) { | ||
| return done(err); | ||
| } | ||
| expect(res.code, 'to be', numFailures); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
| } | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.