-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
feat: add option to use posix exit code upon fatal signal #4989
base: main
Are you sure you want to change the base?
Changes from 16 commits
8999b7b
3b99517
2150c30
0fb6ca3
62528c5
8a4f2bd
817d890
ad30057
592f071
34f2822
3438da6
b6e6d30
d2991a1
7cf79d8
609f94f
b71eaf3
26bebac
0aa939f
59e471d
6d237fc
ca78dbc
1809576
df38431
9ee844a
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 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -10,6 +10,7 @@ | |||||||||||||
* @private | ||||||||||||||
*/ | ||||||||||||||
|
||||||||||||||
const os = require('os'); | ||||||||||||||
const {loadOptions} = require('../lib/cli/options'); | ||||||||||||||
const { | ||||||||||||||
unparseNodeFlags, | ||||||||||||||
|
@@ -22,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)); | ||||||||||||||
|
@@ -109,7 +113,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(SIGNAL_OFFSET + numericSignal); | ||||||||||||||
} else { | ||||||||||||||
process.kill(process.pid, signal); | ||||||||||||||
} | ||||||||||||||
Comment on lines
+118
to
+122
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the Else the Though the logic of Mocha here is kind of weird – the |
||||||||||||||
} else if (code !== 0 && mochaArgs['posix-exit-codes'] === true) { | ||||||||||||||
process.exit(EXIT_FAILURE); | ||||||||||||||
} else { | ||||||||||||||
process.exit(code); | ||||||||||||||
73rhodes marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+123
to
126
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could probably make it this:
Suggested change
That reuses the Not sure the |
||||||||||||||
} | ||||||||||||||
|
@@ -126,7 +138,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 | ||||||||||||||
|
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,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,63 @@ | ||
'use strict'; | ||
|
||
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']; | ||
73rhodes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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) { | ||
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(); | ||
}); | ||
}); | ||
|
||
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 not enabled, and with node options', function () { | ||
it('should exit with the number of failed tests', function (done) { | ||
73rhodes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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', numFailures); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
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.
According to the documentation the
signal
argument of theexit
event onchild_process.spawn()
is always a string – when is it ever something else?