Skip to content
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

fixes some signal handling #4239

Merged
merged 1 commit into from Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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