Properly clean up child processes created during tests (#349)
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 0cdc62c · Browse files · 4 files·+11 added−11 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+11 added−11 removed
- tests:+11 added−11 removed
Provenance
- Commit
071a815- Tree
6647ecd- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
0cdc62c
4 files changed+11 added−11 removed
modified:test/fixtures/detach+1 added-1 removed
1 unmodified line2 'use strict';3 4 const execa = require('../..');5 6-const subprocess = execa('node', ['./test/fixtures/forever'], {detached: true});6+const subprocess = execa('node', ['./test/fixtures/noop'], {detached: true});7 console.log(subprocess.pid);8 process.exit();modified:test/error.js+6 added-6 removed
56 unmodified lines575859606161626364656667686970707172737425 unmodified lines1001011021031041041051061071083 unmodified lines11211311411511611611711811912012112212312412512512612712812913013113213313413413513613713827 unmodified lines56 unmodified lines t.true(failed);});
test('error.killed is true if process was killed directly', async t => { const cp = execa('forever'); const cp = execa('noop');
cp.kill();
const {killed} = await t.throwsAsync(cp, {message: /was killed with SIGTERM/}); t.true(killed);});
test('error.killed is false if process was killed indirectly', async t => { const cp = execa('forever'); const cp = execa('noop');
process.kill(cp.pid, 'SIGINT');
// `process.kill()` is emulated by Node.js on Windows25 unmodified lines});
if (process.platform === 'darwin') { test.cb('sanity check: child_process.exec also has killed.false if killed indirectly', t => { const {pid} = childProcess.exec('forever', error => { const {pid} = childProcess.exec('noop', error => { t.truthy(error); t.false(error.killed); t.end(); });3 unmodified lines}
if (process.platform !== 'win32') { test('error.signal is SIGINT', async t => { const cp = execa('forever'); const cp = execa('noop');
process.kill(cp.pid, 'SIGINT');
const {signal} = await t.throwsAsync(cp, {message: /was killed with SIGINT/}); t.is(signal, 'SIGINT'); });
test('error.signal is SIGTERM', async t => { const cp = execa('forever'); const cp = execa('noop');
process.kill(cp.pid, 'SIGTERM');
const {signal} = await t.throwsAsync(cp, {message: /was killed with SIGTERM/}); t.is(signal, 'SIGTERM'); });
test('custom error.signal', async t => { const {signal} = await t.throwsAsync(execa('forever', {killSignal: 'SIGHUP', timeout: 1, message: TIMEOUT_REGEXP})); const {signal} = await t.throwsAsync(execa('noop', {killSignal: 'SIGHUP', timeout: 1, message: TIMEOUT_REGEXP})); t.is(signal, 'SIGHUP'); });}
27 unmodified linesmodified:test/kill.js+3 added-3 removed
78 unmodified lines79 t.is(typeof kill, 'function');80 });81 82 test('timeout kills the process if it times out', async t => {83- const {killed, timedOut} = await t.throwsAsync(execa('forever', {timeout: 1}), TIMEOUT_REGEXP);83+ const {killed, timedOut} = await t.throwsAsync(execa('noop', {timeout: 1}), TIMEOUT_REGEXP);84 t.false(killed);85 t.true(timedOut);86 });87 88 test('timeout kills the process if it times out, in sync mode', async t => {89 const {killed, timedOut} = await t.throws(() => {90- execa.sync('forever', {timeout: 1, message: TIMEOUT_REGEXP});90+ execa.sync('noop', {timeout: 1, message: TIMEOUT_REGEXP});91 });92 t.false(killed);93 t.true(timedOut);94 });56 unmodified lines151 152 t.false(isRunning(subprocess.pid));153 t.is(isRunning(pid), !isKilled);154 155- if (!isKilled) {155+ if (isRunning(pid)) {156 process.kill(pid, 'SIGKILL');157 }158 };159 103 unmodified linesmodified:test/test.js+1 added-1 removed
105 unmodified lines106 await t.throwsAsync(child, /test/);107 });108 109 test('child process errors rejects promise right away', async t => {110- const child = execa('forever');110+ const child = execa('noop');111 child.emit('error', new Error('test'));112 await t.throwsAsync(child, /test/);113 });114 106 unmodified lines