Skip to content
Myr

kill.js

This file

Path

test/kill.js

303 lines · 10.8 KiB · JavaScript

Last changed by

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

History of this file

Take it

The walk stopped after 200 commits: lines older than e003f9c point at it.

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

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

Fix test about `SIGTERM` (#611)ehmickybefore adoption85a9821

line 2why, one step back
2import {setTimeout} from 'node:timers/promises';

3.2.0ehmickybefore adoptione003f9c

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

Minor tweaksSindre Sorhusbefore adoption244e87a

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

3.2.0ehmickybefore adoptione003f9c

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

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

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

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

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

3.2.0ehmickybefore adoptione003f9c

line 8why, one step back
8

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

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

3.2.0ehmickybefore adoptione003f9c

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

Simplify tests (#552)ehmickybefore adoption5fa61d8

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

3.2.0ehmickybefore adoptione003f9c

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

Simplify tests (#552)ehmickybefore adoption5fa61d8

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

3.2.0ehmickybefore adoptione003f9c

lines 28–38why, one step back
28 await pEvent(subprocess, 'message');
2930 subprocess.kill('SIGTERM', {forceKillAfterTimeout: false});3132 t.true(isRunning(subprocess.pid));33 subprocess.kill('SIGKILL');3435 const {signal} = await t.throwsAsync(subprocess);36 t.is(signal, 'SIGKILL');37 });38

Improve some tests (#655)ehmickybefore adoption6cf1c5e

line 39why, one step back
39 const testForceKill = async (t, killArguments) => {

Simplify tests (#552)ehmickybefore adoption5fa61d8

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

3.2.0ehmickybefore adoptione003f9c

lines 41–42why, one step back
41 await pEvent(subprocess, 'message');
42

Improve some tests (#655)ehmickybefore adoption6cf1c5e

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

3.2.0ehmickybefore adoptione003f9c

lines 44–46why, one step back
44
45 const {signal} = await t.throwsAsync(subprocess);46 t.is(signal, 'SIGKILL');

Improve some tests (#655)ehmickybefore adoption6cf1c5e

line 47why, one step back
47 };

3.2.0ehmickybefore adoptione003f9c

line 48why, one step back
48

Improve some tests (#655)ehmickybefore adoption6cf1c5e

lines 49–52why, one step back
49 test('`forceKillAfterTimeout: number` should kill after a timeout', testForceKill, ['SIGTERM', {forceKillAfterTimeout: 50}]);
50 test('`forceKillAfterTimeout: true` should kill after a timeout', testForceKill, ['SIGTERM', {forceKillAfterTimeout: true}]);51 test('kill("SIGTERM") should kill after a timeout', testForceKill, ['SIGTERM']);52 test('kill() with no arguments should kill after a timeout', testForceKill, []);

3.2.0ehmickybefore adoptione003f9c

line 53why, one step back
53

Improve some tests (#655)ehmickybefore adoption6cf1c5e

lines 54–55why, one step back
54 const testInvalidForceKill = async (t, forceKillAfterTimeout) => {
55 const childProcess = execa('noop.js');

3.2.0ehmickybefore adoptione003f9c

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

Improve some tests (#655)ehmickybefore adoption6cf1c5e

line 57why, one step back
57 childProcess.kill('SIGTERM', {forceKillAfterTimeout});

3.2.0ehmickybefore adoptione003f9c

line 58why, one step back
58 }, {instanceOf: TypeError, message: /non-negative integer/});

Improve some tests (#655)ehmickybefore adoption6cf1c5e

lines 59–61why, one step back
59 const {signal} = await t.throwsAsync(childProcess);
60 t.is(signal, 'SIGTERM');61 };

3.2.0ehmickybefore adoptione003f9c

line 62why, one step back
62

Improve some tests (#655)ehmickybefore adoption6cf1c5e

lines 63–64why, one step back
63 test('`forceKillAfterTimeout` should not be NaN', testInvalidForceKill, Number.NaN);
64 test('`forceKillAfterTimeout` should not be negative', testInvalidForceKill, -1);

3.2.0ehmickybefore adoptione003f9c

lines 65–66why, one step back
65}
66

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 67–70why, one step back
67test('execa() returns a promise with kill()', async t => {
68 const subprocess = execa('noop.js', ['foo']);69 t.is(typeof subprocess.kill, 'function');70 await subprocess;

3.2.0ehmickybefore adoptione003f9c

lines 71–73why, one step back
71});
7273test('timeout kills the process if it times out', async t => {

Fix `error.killed` and rename to `error.isTerminated` (#625)ehmickybefore adoption248349c

lines 74–75why, one step back
74 const {isTerminated, timedOut} = await t.throwsAsync(execa('noop.js', {timeout: 1}), {message: TIMEOUT_REGEXP});
75 t.true(isTerminated);

3.2.0ehmickybefore adoptione003f9c

lines 76–79why, one step back
76 t.true(timedOut);
77});7879test('timeout kills the process if it times out, in sync mode', async t => {

Fix `error.killed` and rename to `error.isTerminated` (#625)ehmickybefore adoption248349c

line 80why, one step back
80 const {isTerminated, timedOut} = await t.throws(() => {

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

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

3.2.0ehmickybefore adoptione003f9c

line 82why, one step back
82 });

Fix `error.killed` and rename to `error.isTerminated` (#625)ehmickybefore adoption248349c

line 83why, one step back
83 t.true(isTerminated);

3.2.0ehmickybefore adoptione003f9c

lines 84–87why, one step back
84 t.true(timedOut);
85});8687test('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 88why, one step back
88 const {timedOut} = await execa('delay.js', ['500'], {timeout: 1e8});

3.2.0ehmickybefore adoptione003f9c

lines 89–95why, one step back
89 t.false(timedOut);
90});9192const INVALID_TIMEOUT_REGEXP = /`timeout` option to be a non-negative integer/;9394test('timeout must not be negative', async t => {95 await t.throws(() => {

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

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

3.2.0ehmickybefore adoptione003f9c

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

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

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

3.2.0ehmickybefore adoptione003f9c

lines 104–106why, one step back
104});
105106test('timedOut is false if timeout is undefined', async t => {

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

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

3.2.0ehmickybefore adoptione003f9c

lines 108–111why, one step back
108 t.false(timedOut);
109});110111test('timedOut is false if timeout is 0', async t => {

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

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

3.2.0ehmickybefore adoptione003f9c

lines 113–116why, one step back
113 t.false(timedOut);
114});115116test('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 117why, one step back
117 const {timedOut} = execaSync('noop.js');

3.2.0ehmickybefore adoptione003f9c

lines 118–122why, one step back
118 t.false(timedOut);
119});120121// When child process exits before parent process122const spawnAndExit = async (t, cleanup, detached) => {

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

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

3.2.0ehmickybefore adoptione003f9c

lines 124–131why, one step back
124};
125126test('spawnAndExit', spawnAndExit, false, false);127test('spawnAndExit cleanup', spawnAndExit, true, false);128test('spawnAndExit detached', spawnAndExit, false, true);129test('spawnAndExit cleanup detached', spawnAndExit, true, true);130131// When parent process exits before child process

Fix linting errors (#462)ehmickybefore adoption8e4f901

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

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

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

3.2.0ehmickybefore adoptione003f9c

lines 134–142why, one step back
134
135 const pid = await pEvent(subprocess, 'message');136 t.true(Number.isInteger(pid));137 t.true(isRunning(pid));138139 process.kill(subprocess.pid, signal);140141 await t.throwsAsync(subprocess);142

Fix test about `SIGTERM` (#611)ehmickybefore adoption85a9821

lines 143–148why, one step back
143 // The `cleanup` option can introduce a race condition in this test.
144 // This is especially true when run concurrently, so we use `test.serial()` and a manual timeout.145 if (signal === 'SIGTERM' && cleanup && !detached) {146 await setTimeout(1e3);147 }148

3.2.0ehmickybefore adoptione003f9c

line 149why, one step back
149 t.false(isRunning(subprocess.pid));

Fix test about `SIGTERM` (#611)ehmickybefore adoption85a9821

line 150why, one step back
150 t.not(isRunning(pid), isKilled);

3.2.0ehmickybefore adoptione003f9c

line 151why, one step back
151

Fix test about `SIGTERM` (#611)ehmickybefore adoption85a9821

line 152why, one step back
152 if (!isKilled) {

3.2.0ehmickybefore adoptione003f9c

lines 153–163why, one step back
153 process.kill(pid, 'SIGKILL');
154 }155};156157// Without `options.cleanup`:158// - on Windows subprocesses are killed if `options.detached: false`, but not159// if `options.detached: true`160// - on Linux subprocesses are never killed regardless of `options.detached`161// With `options.cleanup`, subprocesses are always killed162// - `options.cleanup` with SIGKILL is a noop, since it cannot be handled163const exitIfWindows = process.platform === 'win32';

Fix linting errors (#462)ehmickybefore adoption8e4f901

lines 164–165why, one step back
164test('spawnAndKill SIGTERM', spawnAndKill, ['SIGTERM', false, false, exitIfWindows]);
165test('spawnAndKill SIGKILL', spawnAndKill, ['SIGKILL', false, false, exitIfWindows]);

Fix `cleanup` option test (#601)ehmickybefore adoption9a2b00f

line 166why, one step back
166test.serial('spawnAndKill cleanup SIGTERM', spawnAndKill, ['SIGTERM', true, false, true]);

Fix linting errors (#462)ehmickybefore adoption8e4f901

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

3.2.0ehmickybefore adoptione003f9c

lines 172–174why, one step back
172
173// See #128174test('removes exit handler on exit', async t => {

Fix linting errors (#462)ehmickybefore adoption8e4f901

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

Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b

line 176why, one step back
176 const exitListeners = globalThis[Symbol.for('signal-exit emitter')].listeners.exit;

3.2.0ehmickybefore adoptione003f9c

line 177why, one step back
177

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

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

Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b

line 179why, one step back
179 const listener = exitListeners.at(-1);

3.2.0ehmickybefore adoptione003f9c

line 180why, one step back
180

Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b

lines 181–182why, one step back
181 await subprocess;
182 t.false(exitListeners.includes(listener));

3.2.0ehmickybefore adoptione003f9c

lines 183–184why, one step back
183});
184

Fix `error.killed` and rename to `error.isTerminated` (#625)ehmickybefore adoption248349c

line 185why, one step back
185test('cancel method kills the subprocess', async t => {

3.2.0ehmickybefore adoptione003f9c

lines 186–188why, one step back
186 const subprocess = execa('node');
187 subprocess.cancel();188 t.true(subprocess.killed);

Fix `error.killed` and rename to `error.isTerminated` (#625)ehmickybefore adoption248349c

lines 189–190why, one step back
189 const {isTerminated} = await t.throwsAsync(subprocess);
190 t.true(isTerminated);

3.2.0ehmickybefore adoptione003f9c

lines 191–193why, one step back
191});
192193test('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 194why, one step back
194 const {isCanceled} = await execa('noop.js');

3.2.0ehmickybefore adoptione003f9c

lines 195–198why, one step back
195 t.false(isCanceled);
196});197198test('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 199why, one step back
199 const {isCanceled} = await t.throwsAsync(execa('fail.js'));

3.2.0ehmickybefore adoptione003f9c

lines 200–203why, one step back
200 t.false(isCanceled);
201});202203test('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 204why, one step back
204 const {isCanceled} = execaSync('noop.js');

3.2.0ehmickybefore adoptione003f9c

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

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

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

3.2.0ehmickybefore adoptione003f9c

lines 211–215why, one step back
211 });
212 t.false(isCanceled);213});214215test('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 216why, one step back
216 const subprocess = execa('noop.js');

3.2.0ehmickybefore adoptione003f9c

lines 217–221why, one step back
217 subprocess.cancel();
218 await t.throwsAsync(subprocess, {message: /Command was canceled/});219});220221test('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 222why, one step back
222 const subprocess = execa('noop.js');

3.2.0ehmickybefore adoptione003f9c

lines 223–228why, one step back
223 subprocess.cancel();
224 const {isCanceled} = await t.throwsAsync(subprocess);225 t.true(isCanceled);226});227228test('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 229why, one step back
229 const subprocess = execa('noop.js');

3.2.0ehmickybefore adoptione003f9c

lines 230–235why, one step back
230 subprocess.kill();
231 const {isCanceled} = await t.throwsAsync(subprocess);232 t.false(isCanceled);233});234235test('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 236why, one step back
236 const subprocess = execa('noop.js');

3.2.0ehmickybefore adoptione003f9c

lines 237–244why, one step back
237 subprocess.cancel();
238 subprocess.cancel();239 const {isCanceled} = await t.throwsAsync(subprocess);240 t.true(isCanceled);241 t.true(subprocess.killed);242});243244test('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 245why, one step back
245 const subprocess = execa('noop.js');

3.2.0ehmickybefore adoptione003f9c

lines 246–251why, one step back
246 const {isCanceled} = await subprocess;
247 subprocess.cancel();248 t.false(isCanceled);249});250251test('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 252why, one step back
252 const subprocess = execa('noop.js');

3.2.0ehmickybefore adoptione003f9c

lines 253–256why, one step back
253 subprocess.kill();
254 const {isCanceled} = await t.throwsAsync(subprocess);255 t.false(isCanceled);256});

Support `AbortController` (#490)Gyubongbefore adoptionc6e791a

lines 257–303why, one step back
257
258if (globalThis.AbortController !== undefined) {259 test('calling abort throws an error with message "Command was canceled"', async t => {260 const abortController = new AbortController();261 const subprocess = execa('noop.js', [], {signal: abortController.signal});262 abortController.abort();263 await t.throwsAsync(subprocess, {message: /Command was canceled/});264 });265266 test('calling abort twice should show the same behaviour as calling it once', async t => {267 const abortController = new AbortController();268 const subprocess = execa('noop.js', [], {signal: abortController.signal});269 abortController.abort();270 abortController.abort();271 const {isCanceled} = await t.throwsAsync(subprocess);272 t.true(isCanceled);273 t.true(subprocess.killed);274 });275276 test('calling abort on a successfully completed process does not make result.isCanceled true', async t => {277 const abortController = new AbortController();278 const subprocess = execa('noop.js', [], {signal: abortController.signal});279 const {isCanceled} = await subprocess;280 abortController.abort();281 t.false(isCanceled);282 });283284 test('calling cancel after abort should show the same behaviour as only calling cancel', async t => {285 const abortController = new AbortController();286 const subprocess = execa('noop.js', [], {signal: abortController.signal});287 abortController.abort();288 subprocess.cancel();289 const {isCanceled} = await t.throwsAsync(subprocess);290 t.true(isCanceled);291 t.true(subprocess.killed);292 });293294 test('calling abort after cancel should show the same behaviour as only calling cancel', async t => {295 const abortController = new AbortController();296 const subprocess = execa('noop.js', [], {signal: abortController.signal});297 subprocess.cancel();298 abortController.abort();299 const {isCanceled} = await t.throwsAsync(subprocess);300 t.true(isCanceled);301 t.true(subprocess.killed);302 });303}