Skip to content

Commit 7ed9fa4

Browse files
boneskullcraigtaub
authored andcommitted
fixes some signal handling
- `landing` reporter befouls the terminal up if `SIGINT` is issued - `--exit` test used an incorrect description, and also did not reliably handle `SIGINT` when running - `runMocha` helper returns the child process Ref: #4198
1 parent 56acffc commit 7ed9fa4

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

lib/reporters/landing.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ function Landing(runner, options) {
9898
process.stdout.write('\n');
9999
self.epilogue();
100100
});
101+
102+
// if cursor is hidden when we ctrl-C, then it will remain hidden unless...
103+
process.once('SIGINT', function() {
104+
cursor.show();
105+
process.nextTick(function() {
106+
process.kill(process.pid, 'SIGINT');
107+
});
108+
});
101109
}
102110

103111
/**

test/integration/helpers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ module.exports = {
3333
* @param {string[]} args - Extra args to mocha executable
3434
* @param {Function} fn - Callback
3535
* @param {Object} [opts] - Options for `spawn()`
36+
* @returns {ChildProcess} Mocha process
3637
*/
3738
runMocha: function(fixturePath, args, fn, opts) {
3839
if (typeof args === 'function') {
@@ -46,7 +47,7 @@ module.exports = {
4647
path = resolveFixturePath(fixturePath);
4748
args = args || [];
4849

49-
invokeSubMocha(
50+
return invokeSubMocha(
5051
args.concat(['-C', path]),
5152
function(err, res) {
5253
if (err) {

test/integration/options/exit.spec.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
'use strict';
22

3-
var helpers = require('../helpers');
4-
var runMochaJSON = helpers.runMochaJSON;
3+
var runMocha = require('../helpers').runMocha;
54

65
describe('--exit', function() {
76
var behaviors = {
87
enabled: '--exit',
98
disabled: '--no-exit'
109
};
1110

11+
// subprocess
12+
var mocha;
13+
14+
function killSubprocess() {
15+
mocha.kill('SIGKILL');
16+
}
17+
18+
// these two handlers deal with a ctrl-c on command-line
19+
before(function() {
20+
process.on('SIGINT', killSubprocess);
21+
});
22+
23+
after(function() {
24+
process.removeListener('SIGINT', killSubprocess);
25+
});
26+
1227
/**
1328
* Returns a test that executes Mocha in a subprocess with either
1429
* `--exit`, `--no-exit`, or default behavior.
1530
*
1631
* @param {boolean} shouldExit - Expected result; `true` if Mocha should
1732
* have force-killed the process.
18-
* @param {string} [behavior] - 'enabled' or 'disabled'
33+
* @param {"enabled"|"disabled"} [behavior] - 'enabled' or 'disabled'; omit for default
1934
* @returns {Function} async function implementing the test
2035
*/
2136
var runExit = function(shouldExit, behavior) {
@@ -28,8 +43,7 @@ describe('--exit', function() {
2843
var timeoutObj;
2944
var fixture = 'exit.fixture.js';
3045
var args = behaviors[behavior] ? [behaviors[behavior]] : [];
31-
32-
var mocha = runMochaJSON(fixture, args, function postmortem(err) {
46+
mocha = runMocha(fixture, args, function postmortem(err) {
3347
clearTimeout(timeoutObj);
3448
if (err) {
3549
return done(err);
@@ -41,15 +55,13 @@ describe('--exit', function() {
4155
// If this callback happens, then Mocha didn't automatically exit.
4256
timeoutObj = setTimeout(function() {
4357
didExit = false;
44-
// This is the only way to kill the child, afaik.
45-
// After the process ends, the callback to `run()` above is handled.
46-
mocha.kill('SIGINT');
58+
killSubprocess();
4759
}, timeout - 500);
4860
};
4961
};
5062

5163
describe('default behavior', function() {
52-
it('should force exit after root suite completion', runExit(false));
64+
it('should not force exit after root suite completion', runExit(false));
5365
});
5466

5567
describe('when enabled', function() {

0 commit comments

Comments
 (0)