Fix test for `options.cleanup` (#224)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by Sindre Sorhus · parent 4cd3d35 · Browse files · 4 files·+26 added−35 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+26 added−35 removed
- other:+26 added−35 removed
Provenance
- Commit
c0d058c- Tree
182468d- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
4cd3d35
4 files changed+26 added−35 removed
modified:fixtures/sub-process+4 added-2 removed
1 #!/usr/bin/env node2 'use strict';3-const m = require('../');3+const execa = require('..');4 5-console.log(m('forever').pid);5+const cleanup = process.argv[2] === 'true';6+const subprocess = execa('node', ['./fixtures/forever'], {cleanup});7+process.send(subprocess.pid);deleted:fixtures/sub-process-false-5 removed
1-#!/usr/bin/env node2-'use strict';3-const m = require('../');4-5-console.log(m('forever', {cleanup: false}).pid);modified:package.json+1 added-1 removed
49 unmodified lines5051525354545556575859608 unmodified lines49 unmodified lines "devDependencies": { "@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" },8 unmodified linesmodified:test.js+21 added-27 removed
3 unmodified lines4 import childProcess from 'child_process';5 import test from 'ava';6 import getStream from 'get-stream';7 import isRunning from 'is-running';8-import delay from 'delay';8 import tempfile from 'tempfile';9+import pEvent from 'p-event';10 import execa from '.';11 12 process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;13 process.env.FOO = 'foo';462 unmodified lines476 test(command, ' foo bar', 'foo', 'bar');477 test(command, ' baz quz', 'baz', 'quz');478 test(command, '');479 480-async function spawnAndKill(t, signal, cleanup) {481- const name = cleanup ? 'sub-process' : 'sub-process-false';482- const cp = execa(name);483- let pid;480+async function spawnAndKill(t, signal, cleanup, isKilled) {481+ const subprocess = execa('sub-process', [cleanup], {stdio: ['ignore', 'ignore', 'ignore', 'ipc']});482 485- cp.stdout.setEncoding('utf8');486- cp.stdout.on('data', chunk => {487- pid = parseInt(chunk, 10);488- t.is(typeof pid, 'number');483+ const pid = await pEvent(subprocess, 'message');484+ t.true(Number.isInteger(pid));485+ t.true(isRunning(pid));486 490- setTimeout(() => {491- process.kill(cp.pid, signal);487+ process.kill(subprocess.pid, signal);492- }, 100);493- });488 495- await t.throwsAsync(cp);489+ await t.throwsAsync(subprocess);490 497- // Give everybody some time to breath and kill things498- await delay(200);499-500- t.false(isRunning(cp.pid));501- t.is(isRunning(pid), !cleanup);491+ t.false(isRunning(subprocess.pid));492+ t.is(isRunning(pid), !isKilled);493 }494 504-test('cleanup - SIGINT', spawnAndKill, 'SIGINT', true);505-test('cleanup - SIGKILL', spawnAndKill, 'SIGTERM', true);495+// Without `options.cleanup`:496+// - on Windows subprocesses are killed if `options.detached: false`, but not497+// if `options.detached: true`498+// - on Linux subprocesses are never killed regardless of `options.detached`499+// With `options.cleanup`, subprocesses are always killed500+// - `options.cleanup` with SIGKILL is a noop, since it cannot be handled501+const exitIfWindows = process.platform === 'win32';502+test('cleanup true - SIGTERM', spawnAndKill, 'SIGTERM', 'true', true);503+test('cleanup false - SIGTERM', spawnAndKill, 'SIGTERM', 'false', exitIfWindows);504+test('cleanup true - SIGKILL', spawnAndKill, 'SIGKILL', 'true', exitIfWindows);505+test('cleanup false - SIGKILL', spawnAndKill, 'SIGKILL', 'false', exitIfWindows);506 507-if (process.platform !== 'win32') {508- // On Windows the subprocesses are actually always killed509- test('cleanup false - SIGINT', spawnAndKill, 'SIGTERM', false);510- test('cleanup false - SIGKILL', spawnAndKill, 'SIGKILL', false);511-}512-507 test('execa.shell() supports the `shell` option', async t => {508 const {stdout} = await execa.shell('node fixtures/noop foo', {509 shell: process.platform === 'win32' ? 'cmd.exe' : '/bin/bash'510 });172 unmodified lines