Skip to content
Myr

kill.js

This file

Path

test/exit/kill.js

312 lines · 11.5 KiB · JavaScript

Last changed by

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

History of this file

Take it

Split files (#837)ehmickybefore adoption672c2d7

lines 1–48why, one step back
1import process from 'node:process';
2import {once, defaultMaxListeners} from 'node:events';3import {constants} from 'node:os';4import {setTimeout, setImmediate} from 'node:timers/promises';5import test from 'ava';6import {pEvent} from 'p-event';7import isRunning from 'is-running';8import {execa} from '../../index.js';9import {setFixtureDir} from '../helpers/fixtures-dir.js';10import {assertMaxListeners} from '../helpers/listeners.js';1112setFixtureDir();1314const isWindows = process.platform === 'win32';1516const spawnNoKillable = async (forceKillAfterDelay, options) => {17 const subprocess = execa('no-killable.js', {18 ipc: true,19 forceKillAfterDelay,20 ...options,21 });22 await pEvent(subprocess, 'message');23 return {subprocess};24};2526const spawnNoKillableSimple = options => execa('forever.js', {killSignal: 'SIGWINCH', forceKillAfterDelay: 1, ...options});2728const spawnNoKillableOutput = options => execa('noop-forever.js', ['.'], {killSignal: 'SIGWINCH', forceKillAfterDelay: 1, ...options});2930test('kill("SIGKILL") should terminate cleanly', async t => {31 const {subprocess} = await spawnNoKillable();3233 subprocess.kill('SIGKILL');3435 const {isTerminated, signal} = await t.throwsAsync(subprocess);36 t.true(isTerminated);37 t.is(signal, 'SIGKILL');38});3940const testInvalidForceKill = async (t, forceKillAfterDelay) => {41 t.throws(() => {42 execa('empty.js', {forceKillAfterDelay});43 }, {instanceOf: TypeError, message: /non-negative integer/});44};4546test('`forceKillAfterDelay` should not be NaN', testInvalidForceKill, Number.NaN);47test('`forceKillAfterDelay` should not be negative', testInvalidForceKill, -1);48

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 49why, one step back
49// `SIGTERM` cannot be caught on Windows, and it always aborts the subprocess (like `SIGKILL` on Unix).

Split files (#837)ehmickybefore adoption672c2d7

lines 50–100why, one step back
50// Therefore, this feature and those tests must be different on Windows.
51if (isWindows) {52 test('Can call `.kill()` with `forceKillAfterDelay` on Windows', async t => {53 const {subprocess} = await spawnNoKillable(1);54 subprocess.kill();5556 const {isTerminated, signal} = await t.throwsAsync(subprocess);57 t.true(isTerminated);58 t.is(signal, 'SIGTERM');59 });60} else {61 const testNoForceKill = async (t, forceKillAfterDelay, killArgument, options) => {62 const {subprocess} = await spawnNoKillable(forceKillAfterDelay, options);6364 subprocess.kill(killArgument);6566 await setTimeout(6e3);67 t.true(isRunning(subprocess.pid));68 subprocess.kill('SIGKILL');6970 const {isTerminated, signal} = await t.throwsAsync(subprocess);71 t.true(isTerminated);72 t.is(signal, 'SIGKILL');73 };7475 test('`forceKillAfterDelay: false` should not kill after a timeout', testNoForceKill, false);76 test('`forceKillAfterDelay` should not kill after a timeout with other signals', testNoForceKill, true, 'SIGINT');77 test('`forceKillAfterDelay` should not kill after a timeout with wrong killSignal string', testNoForceKill, true, 'SIGTERM', {killSignal: 'SIGINT'});78 test('`forceKillAfterDelay` should not kill after a timeout with wrong killSignal number', testNoForceKill, true, constants.signals.SIGTERM, {killSignal: constants.signals.SIGINT});7980 const testForceKill = async (t, forceKillAfterDelay, killArgument, options) => {81 const {subprocess} = await spawnNoKillable(forceKillAfterDelay, options);8283 subprocess.kill(killArgument);8485 const {isTerminated, signal} = await t.throwsAsync(subprocess);86 t.true(isTerminated);87 t.is(signal, 'SIGKILL');88 };8990 test('`forceKillAfterDelay: number` should kill after a timeout', testForceKill, 50);91 test('`forceKillAfterDelay: true` should kill after a timeout', testForceKill, true);92 test('`forceKillAfterDelay: undefined` should kill after a timeout', testForceKill, undefined);93 test('`forceKillAfterDelay` should kill after a timeout with SIGTERM', testForceKill, 50, 'SIGTERM');94 test('`forceKillAfterDelay` should kill after a timeout with the killSignal string', testForceKill, 50, 'SIGINT', {killSignal: 'SIGINT'});95 test('`forceKillAfterDelay` should kill after a timeout with the killSignal number', testForceKill, 50, constants.signals.SIGINT, {killSignal: constants.signals.SIGINT});96 test('`forceKillAfterDelay` should kill after a timeout with an error', testForceKill, 50, new Error('test'));97 test('`forceKillAfterDelay` should kill after a timeout with an error and a killSignal', testForceKill, 50, new Error('test'), {killSignal: 'SIGINT'});9899 test('`forceKillAfterDelay` works with the "signal" option', async t => {100 const abortController = new AbortController();

Rename `signal` option to `cancelSignal` (#880)ehmickybefore adoption231beaf

line 101why, one step back
101 const subprocess = spawnNoKillableSimple({cancelSignal: abortController.signal});

Split files (#837)ehmickybefore adoption672c2d7

lines 102–181why, one step back
102 await once(subprocess, 'spawn');
103 abortController.abort();104 const {isTerminated, signal, isCanceled} = await t.throwsAsync(subprocess);105 t.true(isTerminated);106 t.is(signal, 'SIGKILL');107 t.true(isCanceled);108 });109110 test('`forceKillAfterDelay` works with the "timeout" option', async t => {111 const subprocess = spawnNoKillableSimple({timeout: 1});112 const {isTerminated, signal, timedOut} = await t.throwsAsync(subprocess);113 t.true(isTerminated);114 t.is(signal, 'SIGKILL');115 t.true(timedOut);116 });117118 test('`forceKillAfterDelay` works with the "maxBuffer" option', async t => {119 const subprocess = spawnNoKillableOutput({maxBuffer: 1});120 const {isTerminated, signal} = await t.throwsAsync(subprocess);121 t.true(isTerminated);122 t.is(signal, 'SIGKILL');123 });124125 test.serial('Can call `.kill()` with `forceKillAfterDelay` many times without triggering the maxListeners warning', async t => {126 const checkMaxListeners = assertMaxListeners(t);127128 const subprocess = spawnNoKillableSimple();129 for (let index = 0; index < defaultMaxListeners + 1; index += 1) {130 subprocess.kill();131 }132133 const {isTerminated, signal} = await t.throwsAsync(subprocess);134 t.true(isTerminated);135 t.is(signal, 'SIGKILL');136137 checkMaxListeners();138 });139140 test('Can call `.kill()` with `forceKillAfterDelay` multiple times', async t => {141 const subprocess = spawnNoKillableSimple();142 subprocess.kill();143 subprocess.kill();144145 const {isTerminated, signal} = await t.throwsAsync(subprocess);146 t.true(isTerminated);147 t.is(signal, 'SIGKILL');148 });149}150151test('Can call `.kill()` multiple times', async t => {152 const subprocess = execa('forever.js');153 subprocess.kill();154 subprocess.kill();155156 const {isTerminated, signal} = await t.throwsAsync(subprocess);157 t.true(isTerminated);158 t.is(signal, 'SIGTERM');159});160161test('execa() returns a promise with kill()', async t => {162 const subprocess = execa('noop.js', ['foo']);163 t.is(typeof subprocess.kill, 'function');164 await subprocess;165});166167const testInvalidKillArgument = async (t, killArgument, secondKillArgument) => {168 const subprocess = execa('empty.js');169 const message = secondKillArgument instanceof Error || secondKillArgument === undefined170 ? /error instance or a signal name/171 : /second argument is optional/;172 t.throws(() => {173 subprocess.kill(killArgument, secondKillArgument);174 }, {message});175 await subprocess;176};177178test('Cannot call .kill(null)', testInvalidKillArgument, null);179test('Cannot call .kill(0n)', testInvalidKillArgument, 0n);180test('Cannot call .kill(true)', testInvalidKillArgument, true);181test('Cannot call .kill(errorObject)', testInvalidKillArgument, {name: '', message: '', stack: ''});

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

line 182why, one step back
182test('Cannot call .kill(errorArray)', testInvalidKillArgument, [new Error('test')]);

Split files (#837)ehmickybefore adoption672c2d7

lines 183–189why, one step back
183test('Cannot call .kill(undefined, true)', testInvalidKillArgument, undefined, true);
184test('Cannot call .kill("SIGTERM", true)', testInvalidKillArgument, 'SIGTERM', true);185test('Cannot call .kill(true, error)', testInvalidKillArgument, true, new Error('test'));186187test('.kill(error) propagates error', async t => {188 const subprocess = execa('forever.js');189 const originalMessage = 'test';

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 190–194why, one step back
190 const cause = new Error(originalMessage);
191 t.true(subprocess.kill(cause));192 const error = await t.throwsAsync(subprocess);193 t.is(error.cause, cause);194 t.true(cause.stack.includes(import.meta.url));

Split files (#837)ehmickybefore adoption672c2d7

lines 195–204why, one step back
195 t.is(error.exitCode, undefined);
196 t.is(error.signal, 'SIGTERM');197 t.true(error.isTerminated);198 t.is(error.originalMessage, originalMessage);199 t.true(error.message.includes(originalMessage));200 t.true(error.message.includes('was killed with SIGTERM'));201});202203test('.kill(error) uses killSignal', async t => {204 const subprocess = execa('forever.js', {killSignal: 'SIGINT'});

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 205–208why, one step back
205 const cause = new Error('test');
206 subprocess.kill(cause);207 const error = await t.throwsAsync(subprocess);208 t.is(error.cause, cause);

Split files (#837)ehmickybefore adoption672c2d7

lines 209–213why, one step back
209 t.is(error.signal, 'SIGINT');
210});211212test('.kill(signal, error) uses signal', async t => {213 const subprocess = execa('forever.js');

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 214–217why, one step back
214 const cause = new Error('test');
215 subprocess.kill('SIGINT', cause);216 const error = await t.throwsAsync(subprocess);217 t.is(error.cause, cause);

Split files (#837)ehmickybefore adoption672c2d7

lines 218–220why, one step back
218 t.is(error.signal, 'SIGINT');
219});220

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 221why, one step back
221test('.kill(error) is a noop if subprocess already exited', async t => {

Split files (#837)ehmickybefore adoption672c2d7

lines 222–227why, one step back
222 const subprocess = execa('empty.js');
223 await subprocess;224 t.false(isRunning(subprocess.pid));225 t.false(subprocess.kill(new Error('test')));226});227

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 228why, one step back
228test('.kill(error) terminates but does not change the error if the subprocess already errored but did not exit yet', async t => {

Split files (#837)ehmickybefore adoption672c2d7

line 229why, one step back
229 const subprocess = execa('forever.js');

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 230–231why, one step back
230 const cause = new Error('first');
231 subprocess.stdout.destroy(cause);

Split files (#837)ehmickybefore adoption672c2d7

lines 232–234why, one step back
232 await setImmediate();
233 const secondError = new Error('second');234 t.true(subprocess.kill(secondError));

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 235–236why, one step back
235 const error = await t.throwsAsync(subprocess);
236 t.is(error.cause, cause);

Split files (#837)ehmickybefore adoption672c2d7

lines 237–244why, one step back
237 t.is(error.exitCode, undefined);
238 t.is(error.signal, 'SIGTERM');239 t.true(error.isTerminated);240 t.false(error.message.includes(secondError.message));241});242243test('.kill(error) twice in a row', async t => {244 const subprocess = execa('forever.js');

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 245–251why, one step back
245 const cause = new Error('first');
246 subprocess.kill(cause);247 const secondCause = new Error('second');248 subprocess.kill(secondCause);249 const error = await t.throwsAsync(subprocess);250 t.is(error.cause, cause);251 t.false(error.message.includes(secondCause.message));

Split files (#837)ehmickybefore adoption672c2d7

lines 252–255why, one step back
252});
253254test('.kill(error) does not emit the "error" event', async t => {255 const subprocess = execa('forever.js');

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 256–259why, one step back
256 const cause = new Error('test');
257 subprocess.kill(cause);258 const error = await Promise.race([t.throwsAsync(subprocess), once(subprocess, 'error')]);259 t.is(error.cause, cause);

Split files (#837)ehmickybefore adoption672c2d7

lines 260–261why, one step back
260});
261

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 262why, one step back
262test('subprocess errors are handled before spawn', async t => {

Split files (#837)ehmickybefore adoption672c2d7

line 263why, one step back
263 const subprocess = execa('forever.js');

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 264–265why, one step back
264 const cause = new Error('test');
265 subprocess.emit('error', cause);

Split files (#837)ehmickybefore adoption672c2d7

line 266why, one step back
266 subprocess.kill();

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 267–271why, one step back
267 const error = await t.throwsAsync(subprocess);
268 t.is(error.cause, cause);269 t.is(error.exitCode, undefined);270 t.is(error.signal, undefined);271 t.false(error.isTerminated);

Split files (#837)ehmickybefore adoption672c2d7

lines 272–273why, one step back
272});
273

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 274why, one step back
274test('subprocess errors are handled after spawn', async t => {

Split files (#837)ehmickybefore adoption672c2d7

lines 275–276why, one step back
275 const subprocess = execa('forever.js');
276 await once(subprocess, 'spawn');

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 277–278why, one step back
277 const cause = new Error('test');
278 subprocess.emit('error', cause);

Split files (#837)ehmickybefore adoption672c2d7

line 279why, one step back
279 subprocess.kill();

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 280–284why, one step back
280 const error = await t.throwsAsync(subprocess);
281 t.is(error.cause, cause);282 t.is(error.exitCode, undefined);283 t.is(error.signal, 'SIGTERM');284 t.true(error.isTerminated);

Split files (#837)ehmickybefore adoption672c2d7

lines 285–286why, one step back
285});
286

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 287why, one step back
287test('subprocess double errors are handled after spawn', async t => {

Split files (#837)ehmickybefore adoption672c2d7

line 288why, one step back
288 const abortController = new AbortController();

Rename `signal` option to `cancelSignal` (#880)ehmickybefore adoption231beaf

line 289why, one step back
289 const subprocess = execa('forever.js', {cancelSignal: abortController.signal});

Split files (#837)ehmickybefore adoption672c2d7

line 290why, one step back
290 await once(subprocess, 'spawn');

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 291–292why, one step back
291 const cause = new Error('test');
292 subprocess.emit('error', cause);

Split files (#837)ehmickybefore adoption672c2d7

lines 293–294why, one step back
293 await setImmediate();
294 abortController.abort();

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 295–299why, one step back
295 const error = await t.throwsAsync(subprocess);
296 t.is(error.cause, cause);297 t.is(error.exitCode, undefined);298 t.is(error.signal, 'SIGTERM');299 t.true(error.isTerminated);

Split files (#837)ehmickybefore adoption672c2d7

lines 300–301why, one step back
300});
301

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 302why, one step back
302test('subprocess errors use killSignal', async t => {

Split files (#837)ehmickybefore adoption672c2d7

lines 303–304why, one step back
303 const subprocess = execa('forever.js', {killSignal: 'SIGINT'});
304 await once(subprocess, 'spawn');

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 305–306why, one step back
305 const cause = new Error('test');
306 subprocess.emit('error', cause);

Split files (#837)ehmickybefore adoption672c2d7

line 307why, one step back
307 subprocess.kill();

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 308–311why, one step back
308 const error = await t.throwsAsync(subprocess);
309 t.is(error.cause, cause);310 t.true(error.isTerminated);311 t.is(error.signal, 'SIGINT');

Split files (#837)ehmickybefore adoption672c2d7

line 312why, one step back
312});