Skip to content
Myr

kill.js

This file

Path

test/kill.js

310 lines · 10.7 KiB · JavaScript

Last changed by

Re-enable Windows in CI (#500)ehmickybefore adoption071d0a2

History of this file

Take it

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 1why, one step back
1import process from 'node:process';

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

line 2why, one step back
2import test from 'ava';

Minor tweaksSindre Sorhusbefore adoption244e87a

line 3why, one step back
3import {pEvent} from 'p-event';

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

line 4why, one step back
4import isRunning from 'is-running';

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 5why, one step back
5import {execa, execaSync} from '../index.js';

Re-enable Windows in CI (#500)ehmickybefore adoption071d0a2

line 6why, one step back
6import {setFixtureDir} from './helpers/fixtures-dir.js';

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

line 7why, one step back
7

Re-enable Windows in CI (#500)ehmickybefore adoption071d0a2

line 8why, one step back
8setFixtureDir();

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 9–12why, one step back
9
10const TIMEOUT_REGEXP = /timed out after/;1112test('kill("SIGKILL") should terminate cleanly', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 13why, one step back
13 const subprocess = execa('node', ['./test/fixtures/no-killable.js'], {stdio: ['ipc']});

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 14–25why, one step back
14 await pEvent(subprocess, 'message');
1516 subprocess.kill('SIGKILL');1718 const {signal} = await t.throwsAsync(subprocess);19 t.is(signal, 'SIGKILL');20});2122// `SIGTERM` cannot be caught on Windows, and it always aborts the process (like `SIGKILL` on Unix).23// Therefore, this feature and those tests do not make sense on Windows.24if (process.platform !== 'win32') {25 test('`forceKillAfterTimeout: false` should not kill after a timeout', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 26why, one step back
26 const subprocess = execa('node', ['./test/fixtures/no-killable.js'], {stdio: ['ipc']});

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 27–35why, one step back
27 await pEvent(subprocess, 'message');
2829 subprocess.kill('SIGTERM', {forceKillAfterTimeout: false});3031 t.true(isRunning(subprocess.pid));32 subprocess.kill('SIGKILL');33 });3435 test('`forceKillAfterTimeout: number` should kill after a timeout', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 36why, one step back
36 const subprocess = execa('node', ['./test/fixtures/no-killable.js'], {stdio: ['ipc']});

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 37–45why, one step back
37 await pEvent(subprocess, 'message');
3839 subprocess.kill('SIGTERM', {forceKillAfterTimeout: 50});4041 const {signal} = await t.throwsAsync(subprocess);42 t.is(signal, 'SIGKILL');43 });4445 test('`forceKillAfterTimeout: true` should kill after a timeout', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 46why, one step back
46 const subprocess = execa('node', ['./test/fixtures/no-killable.js'], {stdio: ['ipc']});

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 47–55why, one step back
47 await pEvent(subprocess, 'message');
4849 subprocess.kill('SIGTERM', {forceKillAfterTimeout: true});5051 const {signal} = await t.throwsAsync(subprocess);52 t.is(signal, 'SIGKILL');53 });5455 test('kill() with no arguments should kill after a timeout', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 56why, one step back
56 const subprocess = execa('node', ['./test/fixtures/no-killable.js'], {stdio: ['ipc']});

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 57–64why, one step back
57 await pEvent(subprocess, 'message');
5859 subprocess.kill();6061 const {signal} = await t.throwsAsync(subprocess);62 t.is(signal, 'SIGKILL');63 });64

Fix use of floating number for the `timeout` and `forceKillAfterTimeout` options (#431)Kiko Beatsbefore adoption9a157b3

line 65why, one step back
65 test('`forceKillAfterTimeout` should not be NaN', t => {

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

line 66why, one step back
66 t.throws(() => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 67why, one step back
67 execa('noop.js').kill('SIGTERM', {forceKillAfterTimeout: Number.NaN});

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 68–72why, one step back
68 }, {instanceOf: TypeError, message: /non-negative integer/});
69 });7071 test('`forceKillAfterTimeout` should not be negative', t => {72 t.throws(() => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 73why, one step back
73 execa('noop.js').kill('SIGTERM', {forceKillAfterTimeout: -1});

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 74–78why, one step back
74 }, {instanceOf: TypeError, message: /non-negative integer/});
75 });76}7778test('execa() returns a promise with kill()', t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 79why, one step back
79 const {kill} = execa('noop.js', ['foo']);

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 80–83why, one step back
80 t.is(typeof kill, 'function');
81});8283test('timeout kills the process if it times out', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 84why, one step back
84 const {killed, timedOut} = await t.throwsAsync(execa('noop.js', {timeout: 1}), {message: TIMEOUT_REGEXP});

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 85–90why, one step back
85 t.false(killed);
86 t.true(timedOut);87});8889test('timeout kills the process if it times out, in sync mode', async t => {90 const {killed, timedOut} = await t.throws(() => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 91why, one step back
91 execaSync('noop.js', {timeout: 1, message: TIMEOUT_REGEXP});

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 92–97why, one step back
92 });
93 t.false(killed);94 t.true(timedOut);95});9697test('timeout does not kill the process if it does not time out', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 98why, one step back
98 const {timedOut} = await execa('delay.js', ['500'], {timeout: 1e8});

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 99–101why, one step back
99 t.false(timedOut);
100});101

Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f

lines 102–105why, one step back
102const INVALID_TIMEOUT_REGEXP = /`timeout` option to be a non-negative integer/;
103104test('timeout must not be negative', async t => {105 await t.throws(() => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

lines 106–107why, one step back
106 execa('noop.js', {timeout: -1});
107 }, {message: INVALID_TIMEOUT_REGEXP});

Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f

lines 108–111why, one step back
108});
109110test('timeout must be an integer', async t => {111 await t.throws(() => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

lines 112–113why, one step back
112 execa('noop.js', {timeout: false});
113 }, {message: INVALID_TIMEOUT_REGEXP});

Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f

lines 114–115why, one step back
114});
115

Improve tests for the `timeout` option (#332)ehmickybefore adoption0241d6e

line 116why, one step back
116test('timedOut is false if timeout is undefined', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 117why, one step back
117 const {timedOut} = await execa('noop.js');

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 118–120why, one step back
118 t.false(timedOut);
119});120

Improve tests for the `timeout` option (#332)ehmickybefore adoption0241d6e

line 121why, one step back
121test('timedOut is false if timeout is 0', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 122why, one step back
122 const {timedOut} = await execa('noop.js', {timeout: 0});

Improve tests for the `timeout` option (#332)ehmickybefore adoption0241d6e

lines 123–126why, one step back
123 t.false(timedOut);
124});125126test('timedOut is false if timeout is undefined and exit code is 0 in sync mode', t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 127why, one step back
127 const {timedOut} = execaSync('noop.js');

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 128–132why, one step back
128 t.false(timedOut);
129});130131// When child process exits before parent process132const spawnAndExit = async (t, cleanup, detached) => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 133why, one step back
133 await t.notThrowsAsync(execa('sub-process-exit.js', [cleanup, detached]));

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 134–141why, one step back
134};
135136test('spawnAndExit', spawnAndExit, false, false);137test('spawnAndExit cleanup', spawnAndExit, true, false);138test('spawnAndExit detached', spawnAndExit, false, true);139test('spawnAndExit cleanup detached', spawnAndExit, true, true);140141// When parent process exits before child process

Fix linting errors (#462)ehmickybefore adoption8e4f901

line 142why, one step back
142const spawnAndKill = async (t, [signal, cleanup, detached, isKilled]) => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 143why, one step back
143 const subprocess = execa('sub-process.js', [cleanup, detached], {stdio: ['ignore', 'ignore', 'ignore', 'ipc']});

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 144–155why, one step back
144
145 const pid = await pEvent(subprocess, 'message');146 t.true(Number.isInteger(pid));147 t.true(isRunning(pid));148149 process.kill(subprocess.pid, signal);150151 await t.throwsAsync(subprocess);152153 t.false(isRunning(subprocess.pid));154 t.is(isRunning(pid), !isKilled);155

Properly clean up child processes created during tests (#349)ehmickybefore adoption071a815

line 156why, one step back
156 if (isRunning(pid)) {

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 157–167why, one step back
157 process.kill(pid, 'SIGKILL');
158 }159};160161// Without `options.cleanup`:162// - on Windows subprocesses are killed if `options.detached: false`, but not163// if `options.detached: true`164// - on Linux subprocesses are never killed regardless of `options.detached`165// With `options.cleanup`, subprocesses are always killed166// - `options.cleanup` with SIGKILL is a noop, since it cannot be handled167const exitIfWindows = process.platform === 'win32';

Fix linting errors (#462)ehmickybefore adoption8e4f901

lines 168–175why, one step back
168test('spawnAndKill SIGTERM', spawnAndKill, ['SIGTERM', false, false, exitIfWindows]);
169test('spawnAndKill SIGKILL', spawnAndKill, ['SIGKILL', false, false, exitIfWindows]);170test('spawnAndKill cleanup SIGTERM', spawnAndKill, ['SIGTERM', true, false, true]);171test('spawnAndKill cleanup SIGKILL', spawnAndKill, ['SIGKILL', true, false, exitIfWindows]);172test('spawnAndKill detached SIGTERM', spawnAndKill, ['SIGTERM', false, true, false]);173test('spawnAndKill detached SIGKILL', spawnAndKill, ['SIGKILL', false, true, false]);174test('spawnAndKill cleanup detached SIGTERM', spawnAndKill, ['SIGTERM', true, true, false]);175test('spawnAndKill cleanup detached SIGKILL', spawnAndKill, ['SIGKILL', true, true, false]);

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 176–178why, one step back
176
177// See #128178test('removes exit handler on exit', async t => {

Fix linting errors (#462)ehmickybefore adoption8e4f901

line 179why, one step back
179 // @todo this relies on `signal-exit` internals

Rename internal variables (#379)ehmickybefore adoption1ac56ea

line 180why, one step back
180 const emitter = process.__signal_exit_emitter__;

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

line 181why, one step back
181

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

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

Rename internal variables (#379)ehmickybefore adoption1ac56ea

line 183why, one step back
183 const listener = emitter.listeners('exit').pop();

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 184–185why, one step back
184
185 await new Promise((resolve, reject) => {

Rename internal variables (#379)ehmickybefore adoption1ac56ea

lines 186–187why, one step back
186 subprocess.on('error', reject);
187 subprocess.on('exit', resolve);

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 188–189why, one step back
188 });
189

Rename internal variables (#379)ehmickybefore adoption1ac56ea

line 190why, one step back
190 const included = emitter.listeners('exit').includes(listener);

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 191–200why, one step back
191 t.false(included);
192});193194test('cancel method kills the subprocess', t => {195 const subprocess = execa('node');196 subprocess.cancel();197 t.true(subprocess.killed);198});199200test('result.isCanceled is false when spawned.cancel() isn\'t called (success)', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 201why, one step back
201 const {isCanceled} = await execa('noop.js');

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 202–205why, one step back
202 t.false(isCanceled);
203});204205test('result.isCanceled is false when spawned.cancel() isn\'t called (failure)', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 206why, one step back
206 const {isCanceled} = await t.throwsAsync(execa('fail.js'));

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 207–210why, one step back
207 t.false(isCanceled);
208});209210test('result.isCanceled is false when spawned.cancel() isn\'t called in sync mode (success)', t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 211why, one step back
211 const {isCanceled} = execaSync('noop.js');

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 212–216why, one step back
212 t.false(isCanceled);
213});214215test('result.isCanceled is false when spawned.cancel() isn\'t called in sync mode (failure)', t => {216 const {isCanceled} = t.throws(() => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 217why, one step back
217 execaSync('fail.js');

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 218–222why, one step back
218 });
219 t.false(isCanceled);220});221222test('calling cancel method throws an error with message "Command was canceled"', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

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

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 224–228why, one step back
224 subprocess.cancel();
225 await t.throwsAsync(subprocess, {message: /Command was canceled/});226});227228test('error.isCanceled is true when cancel method is used', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

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

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 230–235why, one step back
230 subprocess.cancel();
231 const {isCanceled} = await t.throwsAsync(subprocess);232 t.true(isCanceled);233});234235test('error.isCanceled is false when kill method is used', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

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

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 237–242why, one step back
237 subprocess.kill();
238 const {isCanceled} = await t.throwsAsync(subprocess);239 t.false(isCanceled);240});241242test('calling cancel method twice should show the same behaviour as calling it once', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

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

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 244–251why, one step back
244 subprocess.cancel();
245 subprocess.cancel();246 const {isCanceled} = await t.throwsAsync(subprocess);247 t.true(isCanceled);248 t.true(subprocess.killed);249});250251test('calling cancel method on a successfully completed process does not make result.isCanceled true', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

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

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 253–258why, one step back
253 const {isCanceled} = await subprocess;
254 subprocess.cancel();255 t.false(isCanceled);256});257258test('calling cancel method on a process which has been killed does not make error.isCanceled true', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

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

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 260–263why, one step back
260 subprocess.kill();
261 const {isCanceled} = await t.throwsAsync(subprocess);262 t.false(isCanceled);263});

Support `AbortController` (#490)Gyubongbefore adoptionc6e791a

lines 264–310why, one step back
264
265if (globalThis.AbortController !== undefined) {266 test('calling abort throws an error with message "Command was canceled"', async t => {267 const abortController = new AbortController();268 const subprocess = execa('noop.js', [], {signal: abortController.signal});269 abortController.abort();270 await t.throwsAsync(subprocess, {message: /Command was canceled/});271 });272273 test('calling abort twice should show the same behaviour as calling it once', async t => {274 const abortController = new AbortController();275 const subprocess = execa('noop.js', [], {signal: abortController.signal});276 abortController.abort();277 abortController.abort();278 const {isCanceled} = await t.throwsAsync(subprocess);279 t.true(isCanceled);280 t.true(subprocess.killed);281 });282283 test('calling abort on a successfully completed process does not make result.isCanceled true', async t => {284 const abortController = new AbortController();285 const subprocess = execa('noop.js', [], {signal: abortController.signal});286 const {isCanceled} = await subprocess;287 abortController.abort();288 t.false(isCanceled);289 });290291 test('calling cancel after abort should show the same behaviour as only calling cancel', async t => {292 const abortController = new AbortController();293 const subprocess = execa('noop.js', [], {signal: abortController.signal});294 abortController.abort();295 subprocess.cancel();296 const {isCanceled} = await t.throwsAsync(subprocess);297 t.true(isCanceled);298 t.true(subprocess.killed);299 });300301 test('calling abort after cancel should show the same behaviour as only calling cancel', async t => {302 const abortController = new AbortController();303 const subprocess = execa('noop.js', [], {signal: abortController.signal});304 subprocess.cancel();305 abortController.abort();306 const {isCanceled} = await t.throwsAsync(subprocess);307 t.true(isCanceled);308 t.true(subprocess.killed);309 });310}