Skip to content

Commit

Permalink
fixes some signal handling
Browse files Browse the repository at this point in the history
- `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
  • Loading branch information
boneskull authored and craigtaub committed May 21, 2020
1 parent 56acffc commit 7ed9fa4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
8 changes: 8 additions & 0 deletions lib/reporters/landing.js
Expand Up @@ -98,6 +98,14 @@ function Landing(runner, options) {
process.stdout.write('\n');
self.epilogue();
});

// if cursor is hidden when we ctrl-C, then it will remain hidden unless...
process.once('SIGINT', function() {
cursor.show();
process.nextTick(function() {
process.kill(process.pid, 'SIGINT');
});
});
}

/**
Expand Down
3 changes: 2 additions & 1 deletion test/integration/helpers.js
Expand Up @@ -33,6 +33,7 @@ module.exports = {
* @param {string[]} args - Extra args to mocha executable
* @param {Function} fn - Callback
* @param {Object} [opts] - Options for `spawn()`
* @returns {ChildProcess} Mocha process
*/
runMocha: function(fixturePath, args, fn, opts) {
if (typeof args === 'function') {
Expand All @@ -46,7 +47,7 @@ module.exports = {
path = resolveFixturePath(fixturePath);
args = args || [];

invokeSubMocha(
return invokeSubMocha(
args.concat(['-C', path]),
function(err, res) {
if (err) {
Expand Down
30 changes: 21 additions & 9 deletions test/integration/options/exit.spec.js
@@ -1,21 +1,36 @@
'use strict';

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

describe('--exit', function() {
var behaviors = {
enabled: '--exit',
disabled: '--no-exit'
};

// subprocess
var mocha;

function killSubprocess() {
mocha.kill('SIGKILL');
}

// these two handlers deal with a ctrl-c on command-line
before(function() {
process.on('SIGINT', killSubprocess);
});

after(function() {
process.removeListener('SIGINT', killSubprocess);
});

/**
* Returns a test that executes Mocha in a subprocess with either
* `--exit`, `--no-exit`, or default behavior.
*
* @param {boolean} shouldExit - Expected result; `true` if Mocha should
* have force-killed the process.
* @param {string} [behavior] - 'enabled' or 'disabled'
* @param {"enabled"|"disabled"} [behavior] - 'enabled' or 'disabled'; omit for default
* @returns {Function} async function implementing the test
*/
var runExit = function(shouldExit, behavior) {
Expand All @@ -28,8 +43,7 @@ describe('--exit', function() {
var timeoutObj;
var fixture = 'exit.fixture.js';
var args = behaviors[behavior] ? [behaviors[behavior]] : [];

var mocha = runMochaJSON(fixture, args, function postmortem(err) {
mocha = runMocha(fixture, args, function postmortem(err) {
clearTimeout(timeoutObj);
if (err) {
return done(err);
Expand All @@ -41,15 +55,13 @@ describe('--exit', function() {
// If this callback happens, then Mocha didn't automatically exit.
timeoutObj = setTimeout(function() {
didExit = false;
// This is the only way to kill the child, afaik.
// After the process ends, the callback to `run()` above is handled.
mocha.kill('SIGINT');
killSubprocess();
}, timeout - 500);
};
};

describe('default behavior', function() {
it('should force exit after root suite completion', runExit(false));
it('should not force exit after root suite completion', runExit(false));
});

describe('when enabled', function() {
Expand Down

0 comments on commit 7ed9fa4

Please sign in to comment.