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

Fix test for options.cleanup #224

Merged
merged 2 commits into from May 7, 2019
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
6 changes: 4 additions & 2 deletions fixtures/sub-process
@@ -1,5 +1,7 @@
#!/usr/bin/env node
'use strict';
const m = require('../');
const execa = require('..');

console.log(m('forever').pid);
const cleanup = process.argv[2] === 'true';
const subprocess = execa('node', ['./fixtures/forever'], {cleanup});
process.send(subprocess.pid);
5 changes: 0 additions & 5 deletions fixtures/sub-process-false

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -52,9 +52,9 @@
"ava": "^1.4.1",
"cat-names": "^2.0.0",
"coveralls": "^3.0.3",
"delay": "^4.1.0",
"is-running": "^2.1.0",
"nyc": "^14.1.0",
"p-event": "^4.1.0",
"tempfile": "^3.0.0",
"tsd": "^0.7.1",
"xo": "^0.24.0"
Expand Down
48 changes: 21 additions & 27 deletions test.js
Expand Up @@ -5,8 +5,8 @@ import childProcess from 'child_process';
import test from 'ava';
import getStream from 'get-stream';
import isRunning from 'is-running';
import delay from 'delay';
import tempfile from 'tempfile';
import pEvent from 'p-event';
import execa from '.';

process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;
Expand Down Expand Up @@ -486,38 +486,32 @@ test(command, ' foo bar', 'foo', 'bar');
test(command, ' baz quz', 'baz', 'quz');
test(command, '');

async function spawnAndKill(t, signal, cleanup) {
const name = cleanup ? 'sub-process' : 'sub-process-false';
const cp = execa(name);
let pid;
async function spawnAndKill(t, signal, cleanup, isKilled) {
const subprocess = execa('sub-process', [cleanup], {stdio: ['ignore', 'ignore', 'ignore', 'ipc']});

cp.stdout.setEncoding('utf8');
cp.stdout.on('data', chunk => {
pid = parseInt(chunk, 10);
t.is(typeof pid, 'number');

setTimeout(() => {
process.kill(cp.pid, signal);
}, 100);
});
const pid = await pEvent(subprocess, 'message');
t.true(Number.isInteger(pid));
t.true(isRunning(pid));

await t.throwsAsync(cp);
process.kill(subprocess.pid, signal);

// Give everybody some time to breath and kill things
await delay(200);
await t.throwsAsync(subprocess);

t.false(isRunning(cp.pid));
t.is(isRunning(pid), !cleanup);
t.false(isRunning(subprocess.pid));
t.is(isRunning(pid), !isKilled);
}

test('cleanup - SIGINT', spawnAndKill, 'SIGINT', true);
test('cleanup - SIGKILL', spawnAndKill, 'SIGTERM', true);

if (process.platform !== 'win32') {
// On Windows the subprocesses are actually always killed
test('cleanup false - SIGINT', spawnAndKill, 'SIGTERM', false);
test('cleanup false - SIGKILL', spawnAndKill, 'SIGKILL', false);
}
// Without `options.cleanup`:
// - on Windows subprocesses are killed if `options.detached: false`, but not
// if `options.detached: true`
// - on Linux subprocesses are never killed regardless of `options.detached`
// With `options.cleanup`, subprocesses are always killed
// - `options.cleanup` with SIGKILL is a noop, since it cannot be handled
const exitIfWindows = process.platform === 'win32';
test('cleanup true - SIGTERM', spawnAndKill, 'SIGTERM', 'true', true);
test('cleanup false - SIGTERM', spawnAndKill, 'SIGTERM', 'false', exitIfWindows);
test('cleanup true - SIGKILL', spawnAndKill, 'SIGKILL', 'true', exitIfWindows);
test('cleanup false - SIGKILL', spawnAndKill, 'SIGKILL', 'false', exitIfWindows);

test('execa.shell() supports the `shell` option', async t => {
const {stdout} = await execa.shell('node fixtures/noop foo', {
Expand Down