Skip to content
Myr

Improve `cleanup` option (code quality, tests, documentation) (#225)

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 06f21c6 · Browse files · 6 files+42 added−15 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+42 added−15 removed

  • other:+42 added−15 removed

6 files+42 added−15 removed

modified:fixtures/sub-process+2 added-1 removed
1 unmodified line
2'use strict';3const execa = require('..');45const cleanup = process.argv[2] === 'true';6const detached = process.argv[3] === 'true';6const subprocess = execa('node', ['./fixtures/forever'], {cleanup});7const subprocess = execa('node', ['./fixtures/forever'], {cleanup, detached});8process.send(subprocess.pid);
added:fixtures/sub-process-exit+7 added
1#!/usr/bin/env node2'use strict';3const execa = require('..');45const cleanup = process.argv[2] === 'true';6const detached = process.argv[3] === 'true';7execa('node', ['./fixtures/noop'], {cleanup, detached});
modified:index.d.ts+3 added-1 removed
104 unmodified lines
105 */106 readonly reject?: boolean;107108 /**109 Keep track of the spawned process and `kill` it when the parent process exits.109 Kill the spawned process when the parent process exits unless either:110 - the spawned process is [`detached`](https://nodejs.org/api/child_process.html#child_process_options_detached)111 - the parent process is terminated abruptly, for example, with `SIGKILL` as opposed to `SIGTERM` or a normal exit112113 @default true114 */115 readonly cleanup?: boolean;
309 unmodified lines
modified:index.js+2 added-6 removed
91 unmodified lines
92 }9394 options.stdio = stdio(options);9596 if (options.detached) {97 // #11598 options.cleanup = false;99 }10096 if (process.platform === 'win32' && path.basename(command, '.exe') === 'cmd') {97 // #11698 args.unshift('/q');99 }
154 unmodified lines
254 } catch (error) {255 return Promise.reject(error);256 }257258 // #115259 let removeExitHandler;264 if (parsed.options.cleanup) {260 if (parsed.options.cleanup && !parsed.options.detached) {261 removeExitHandler = onExit(() => {262 spawned.kill();263 });264 }
211 unmodified lines
modified:readme.md+3 added-1 removed
259 unmodified lines
260261Type: `boolean`<br>262Default: `true`263264Keep track of the spawned process and `kill` it when the parent process exits.264Kill the spawned process when the parent process exits unless either:265 - the spawned process is [`detached`](https://nodejs.org/api/child_process.html#child_process_options_detached)266 - the parent process is terminated abruptly, for example, with `SIGKILL` as opposed to `SIGTERM` or a normal exit267268#### encoding269270Type: `string | null`<br>
85 unmodified lines
modified:test.js+25 added-6 removed
461 unmodified lines
462test(command, ' foo bar', 'foo', 'bar');463test(command, ' baz quz', 'baz', 'quz');464test(command, '');465466async function spawnAndKill(t, signal, cleanup, isKilled) {467 const subprocess = execa('sub-process', [cleanup], {stdio: ['ignore', 'ignore', 'ignore', 'ipc']});466// When child process exits before parent process467async function spawnAndExit(t, cleanup, detached) {468 await t.notThrowsAsync(execa('sub-process-exit', [cleanup, detached]));469}470471test('spawnAndExit', spawnAndExit, false, false);472test('spawnAndExit cleanup', spawnAndExit, true, false);473test('spawnAndExit detached', spawnAndExit, false, true);474test('spawnAndExit cleanup detached', spawnAndExit, true, true);475476// When parent process exits before child process477async function spawnAndKill(t, signal, cleanup, detached, isKilled) {478 const subprocess = execa('sub-process', [cleanup, detached], {stdio: ['ignore', 'ignore', 'ignore', 'ipc']});479480 const pid = await pEvent(subprocess, 'message');481 t.true(Number.isInteger(pid));482 t.true(isRunning(pid));483
2 unmodified lines
486 await t.throwsAsync(subprocess);487488 t.false(isRunning(subprocess.pid));489 t.is(isRunning(pid), !isKilled);490491 if (!isKilled) {492 process.kill(pid, 'SIGKILL');493 }494}495496// Without `options.cleanup`:497// - on Windows subprocesses are killed if `options.detached: false`, but not
1 unmodified line
499// - on Linux subprocesses are never killed regardless of `options.detached`500// With `options.cleanup`, subprocesses are always killed501// - `options.cleanup` with SIGKILL is a noop, since it cannot be handled502const exitIfWindows = process.platform === 'win32';488test('cleanup true - SIGTERM', spawnAndKill, 'SIGTERM', 'true', true);489test('cleanup false - SIGTERM', spawnAndKill, 'SIGTERM', 'false', exitIfWindows);490test('cleanup true - SIGKILL', spawnAndKill, 'SIGKILL', 'true', exitIfWindows);491test('cleanup false - SIGKILL', spawnAndKill, 'SIGKILL', 'false', exitIfWindows);503test('spawnAndKill SIGTERM', spawnAndKill, 'SIGTERM', false, false, exitIfWindows);504test('spawnAndKill SIGKILL', spawnAndKill, 'SIGKILL', false, false, exitIfWindows);505test('spawnAndKill cleanup SIGTERM', spawnAndKill, 'SIGTERM', true, false, true);506test('spawnAndKill cleanup SIGKILL', spawnAndKill, 'SIGKILL', true, false, exitIfWindows);507test('spawnAndKill detached SIGTERM', spawnAndKill, 'SIGTERM', false, true, false);508test('spawnAndKill detached SIGKILL', spawnAndKill, 'SIGKILL', false, true, false);509test('spawnAndKill cleanup detached SIGTERM', spawnAndKill, 'SIGTERM', true, true, false);510test('spawnAndKill cleanup detached SIGKILL', spawnAndKill, 'SIGKILL', true, true, false);511512if (process.platform !== 'win32') {513 test('write to fast-exit process', async t => {514 // Try-catch here is necessary, because this test is not 100% accurate
178 unmodified lines