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 fileTake it
Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 1why, one step back1import process from 'node:process';Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 2why, one step back2import test from 'ava';Minor tweaksSindre Sorhusbefore adoption244e87a
line 3why, one step back3import {pEvent} from 'p-event';Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 4why, one step back4import isRunning from 'is-running';Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 5why, one step back5import {execa, execaSync} from '../index.js';Re-enable Windows in CI (#500)ehmickybefore adoption071d0a2
line 6why, one step back6import {setFixtureDir} from './helpers/fixtures-dir.js';Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 7why, one step back7Re-enable Windows in CI (#500)ehmickybefore adoption071d0a2
line 8why, one step back8setFixtureDir();Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 9–12why, one step back910const 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 back13 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 back14 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 back26 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 back27 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 back36 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 back37 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 back46 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 back47 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 back56 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 back57 await pEvent(subprocess, 'message');5859 subprocess.kill();6061 const {signal} = await t.throwsAsync(subprocess);62 t.is(signal, 'SIGKILL');63 });64Fix use of floating number for the `timeout` and `forceKillAfterTimeout` options (#431)Kiko Beatsbefore adoption9a157b3
line 65why, one step back65 test('`forceKillAfterTimeout` should not be NaN', t => {Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 66why, one step back66 t.throws(() => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 67why, one step back67 execa('noop.js').kill('SIGTERM', {forceKillAfterTimeout: Number.NaN});Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 68–72why, one step back68 }, {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 back73 execa('noop.js').kill('SIGTERM', {forceKillAfterTimeout: -1});Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 74–78why, one step back74 }, {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 back79 const {kill} = execa('noop.js', ['foo']);Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 80–83why, one step back80 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 back84 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 back85 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 back91 execaSync('noop.js', {timeout: 1, message: TIMEOUT_REGEXP});Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 92–97why, one step back92 });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 back98 const {timedOut} = await execa('delay.js', ['500'], {timeout: 1e8});Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 99–101why, one step back99 t.false(timedOut);100});101Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f
lines 102–105why, one step back102const 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 back106 execa('noop.js', {timeout: -1});107 }, {message: INVALID_TIMEOUT_REGEXP});Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f
lines 108–111why, one step back108});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 back112 execa('noop.js', {timeout: false});113 }, {message: INVALID_TIMEOUT_REGEXP});Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f
lines 114–115why, one step back114});115Improve tests for the `timeout` option (#332)ehmickybefore adoption0241d6e
line 116why, one step back116test('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 back117 const {timedOut} = await execa('noop.js');Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 118–120why, one step back118 t.false(timedOut);119});120Improve tests for the `timeout` option (#332)ehmickybefore adoption0241d6e
line 121why, one step back121test('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 back122 const {timedOut} = await execa('noop.js', {timeout: 0});Improve tests for the `timeout` option (#332)ehmickybefore adoption0241d6e
lines 123–126why, one step back123 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 back127 const {timedOut} = execaSync('noop.js');Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 128–132why, one step back128 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 back133 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 back134};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 processFix linting errors (#462)ehmickybefore adoption8e4f901
line 142why, one step back142const spawnAndKill = async (t, [signal, cleanup, detached, isKilled]) => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 143why, one step back143 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 back144145 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);155Properly clean up child processes created during tests (#349)ehmickybefore adoption071a815
line 156why, one step back156 if (isRunning(pid)) {Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 157–167why, one step back157 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 back168test('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 back176177// See #128178test('removes exit handler on exit', async t => {Fix linting errors (#462)ehmickybefore adoption8e4f901
line 179why, one step back179 // @todo this relies on `signal-exit` internalsRename internal variables (#379)ehmickybefore adoption1ac56ea
line 180why, one step back180 const emitter = process.__signal_exit_emitter__;Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 181why, one step back181Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 182why, one step back182 const subprocess = execa('noop.js');Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 183why, one step back183 const listener = emitter.listeners('exit').pop();Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 184–185why, one step back184185 await new Promise((resolve, reject) => {Rename internal variables (#379)ehmickybefore adoption1ac56ea
lines 186–187why, one step back186 subprocess.on('error', reject);187 subprocess.on('exit', resolve);Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 188–189why, one step back188 });189Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 190why, one step back190 const included = emitter.listeners('exit').includes(listener);Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 191–200why, one step back191 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 back201 const {isCanceled} = await execa('noop.js');Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 202–205why, one step back202 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 back206 const {isCanceled} = await t.throwsAsync(execa('fail.js'));Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 207–210why, one step back207 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 back211 const {isCanceled} = execaSync('noop.js');Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 212–216why, one step back212 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 back217 execaSync('fail.js');Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 218–222why, one step back218 });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 back223 const subprocess = execa('noop.js');Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 224–228why, one step back224 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 back229 const subprocess = execa('noop.js');Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 230–235why, one step back230 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 back236 const subprocess = execa('noop.js');Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 237–242why, one step back237 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 back243 const subprocess = execa('noop.js');Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 244–251why, one step back244 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 back252 const subprocess = execa('noop.js');Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 253–258why, one step back253 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 back259 const subprocess = execa('noop.js');Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 260–263why, one step back260 subprocess.kill();261 const {isCanceled} = await t.throwsAsync(subprocess);262 t.false(isCanceled);263});Support `AbortController` (#490)Gyubongbefore adoptionc6e791a
lines 264–310why, one step back264265if (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}