Skip to content

Commit

Permalink
Fix test for options.cleanup (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored and sindresorhus committed May 7, 2019
1 parent 4cd3d35 commit c0d058c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 35 deletions.
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 @@ -51,9 +51,9 @@
"@types/node": "^11.12.2",
"ava": "^1.4.1",
"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 @@ -477,38 +477,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

0 comments on commit c0d058c

Please sign in to comment.