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 fileTake 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 back1import process from 'node:process';Fix test about `SIGTERM` (#611)ehmickybefore adoption85a9821
line 2why, one step back2import {setTimeout} from 'node:timers/promises';3.2.0ehmickybefore adoptione003f9c
line 3why, one step back3import test from 'ava';Minor tweaksSindre Sorhusbefore adoption244e87a
line 4why, one step back4import {pEvent} from 'p-event';3.2.0ehmickybefore adoptione003f9c
line 5why, one step back5import isRunning from 'is-running';Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 6why, one step back6import {execa, execaSync} from '../index.js';Re-enable Windows in CI (#500)ehmickybefore adoption071d0a2
line 7why, one step back7import {setFixtureDir} from './helpers/fixtures-dir.js';3.2.0ehmickybefore adoptione003f9c
line 8why, one step back8Re-enable Windows in CI (#500)ehmickybefore adoption071d0a2
line 9why, one step back9setFixtureDir();3.2.0ehmickybefore adoptione003f9c
lines 10–13why, one step back1011const TIMEOUT_REGEXP = /timed out after/;1213test('kill("SIGKILL") should terminate cleanly', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 14why, one step back14 const subprocess = execa('no-killable.js', {stdio: ['ipc']});3.2.0ehmickybefore adoptione003f9c
lines 15–26why, one step back15 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 back27 const subprocess = execa('no-killable.js', {stdio: ['ipc']});3.2.0ehmickybefore adoptione003f9c
lines 28–38why, one step back28 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 });38Improve some tests (#655)ehmickybefore adoption6cf1c5e
line 39why, one step back39 const testForceKill = async (t, killArguments) => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 40why, one step back40 const subprocess = execa('no-killable.js', {stdio: ['ipc']});3.2.0ehmickybefore adoptione003f9c
lines 41–42why, one step back41 await pEvent(subprocess, 'message');42Improve some tests (#655)ehmickybefore adoption6cf1c5e
line 43why, one step back43 subprocess.kill(...killArguments);3.2.0ehmickybefore adoptione003f9c
lines 44–46why, one step back4445 const {signal} = await t.throwsAsync(subprocess);46 t.is(signal, 'SIGKILL');Improve some tests (#655)ehmickybefore adoption6cf1c5e
line 47why, one step back47 };3.2.0ehmickybefore adoptione003f9c
line 48why, one step back48Improve some tests (#655)ehmickybefore adoption6cf1c5e
lines 49–52why, one step back49 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 back53Improve some tests (#655)ehmickybefore adoption6cf1c5e
lines 54–55why, one step back54 const testInvalidForceKill = async (t, forceKillAfterTimeout) => {55 const childProcess = execa('noop.js');3.2.0ehmickybefore adoptione003f9c
line 56why, one step back56 t.throws(() => {Improve some tests (#655)ehmickybefore adoption6cf1c5e
line 57why, one step back57 childProcess.kill('SIGTERM', {forceKillAfterTimeout});3.2.0ehmickybefore adoptione003f9c
line 58why, one step back58 }, {instanceOf: TypeError, message: /non-negative integer/});Improve some tests (#655)ehmickybefore adoption6cf1c5e
lines 59–61why, one step back59 const {signal} = await t.throwsAsync(childProcess);60 t.is(signal, 'SIGTERM');61 };3.2.0ehmickybefore adoptione003f9c
line 62why, one step back62Improve some tests (#655)ehmickybefore adoption6cf1c5e
lines 63–64why, one step back63 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 back65}66Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
lines 67–70why, one step back67test('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 back71});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 back74 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 back76 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 back80 const {isTerminated, timedOut} = await t.throws(() => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 81why, one step back81 execaSync('noop.js', {timeout: 1, message: TIMEOUT_REGEXP});3.2.0ehmickybefore adoptione003f9c
line 82why, one step back82 });Fix `error.killed` and rename to `error.isTerminated` (#625)ehmickybefore adoption248349c
line 83why, one step back83 t.true(isTerminated);3.2.0ehmickybefore adoptione003f9c
lines 84–87why, one step back84 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 back88 const {timedOut} = await execa('delay.js', ['500'], {timeout: 1e8});3.2.0ehmickybefore adoptione003f9c
lines 89–95why, one step back89 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 back96 execa('noop.js', {timeout: -1});97 }, {message: INVALID_TIMEOUT_REGEXP});3.2.0ehmickybefore adoptione003f9c
lines 98–101why, one step back98});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 back102 execa('noop.js', {timeout: false});103 }, {message: INVALID_TIMEOUT_REGEXP});3.2.0ehmickybefore adoptione003f9c
lines 104–106why, one step back104});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 back107 const {timedOut} = await execa('noop.js');3.2.0ehmickybefore adoptione003f9c
lines 108–111why, one step back108 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 back112 const {timedOut} = await execa('noop.js', {timeout: 0});3.2.0ehmickybefore adoptione003f9c
lines 113–116why, one step back113 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 back117 const {timedOut} = execaSync('noop.js');3.2.0ehmickybefore adoptione003f9c
lines 118–122why, one step back118 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 back123 await t.notThrowsAsync(execa('sub-process-exit.js', [cleanup, detached]));3.2.0ehmickybefore adoptione003f9c
lines 124–131why, one step back124};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 processFix linting errors (#462)ehmickybefore adoption8e4f901
line 132why, one step back132const spawnAndKill = async (t, [signal, cleanup, detached, isKilled]) => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 133why, one step back133 const subprocess = execa('sub-process.js', [cleanup, detached], {stdio: ['ignore', 'ignore', 'ignore', 'ipc']});3.2.0ehmickybefore adoptione003f9c
lines 134–142why, one step back134135 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);142Fix test about `SIGTERM` (#611)ehmickybefore adoption85a9821
lines 143–148why, one step back143 // 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 }1483.2.0ehmickybefore adoptione003f9c
line 149why, one step back149 t.false(isRunning(subprocess.pid));Fix test about `SIGTERM` (#611)ehmickybefore adoption85a9821
line 150why, one step back150 t.not(isRunning(pid), isKilled);3.2.0ehmickybefore adoptione003f9c
line 151why, one step back151Fix test about `SIGTERM` (#611)ehmickybefore adoption85a9821
line 152why, one step back152 if (!isKilled) {3.2.0ehmickybefore adoptione003f9c
lines 153–163why, one step back153 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 back164test('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 back166test.serial('spawnAndKill cleanup SIGTERM', spawnAndKill, ['SIGTERM', true, false, true]);Fix linting errors (#462)ehmickybefore adoption8e4f901
lines 167–171why, one step back167test('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 back172173// See #128174test('removes exit handler on exit', async t => {Fix linting errors (#462)ehmickybefore adoption8e4f901
line 175why, one step back175 // @todo this relies on `signal-exit` internalsRefactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b
line 176why, one step back176 const exitListeners = globalThis[Symbol.for('signal-exit emitter')].listeners.exit;3.2.0ehmickybefore adoptione003f9c
line 177why, one step back177Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 178why, one step back178 const subprocess = execa('noop.js');Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b
line 179why, one step back179 const listener = exitListeners.at(-1);3.2.0ehmickybefore adoptione003f9c
line 180why, one step back180Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b
lines 181–182why, one step back181 await subprocess;182 t.false(exitListeners.includes(listener));3.2.0ehmickybefore adoptione003f9c
lines 183–184why, one step back183});184Fix `error.killed` and rename to `error.isTerminated` (#625)ehmickybefore adoption248349c
line 185why, one step back185test('cancel method kills the subprocess', async t => {3.2.0ehmickybefore adoptione003f9c
lines 186–188why, one step back186 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 back189 const {isTerminated} = await t.throwsAsync(subprocess);190 t.true(isTerminated);3.2.0ehmickybefore adoptione003f9c
lines 191–193why, one step back191});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 back194 const {isCanceled} = await execa('noop.js');3.2.0ehmickybefore adoptione003f9c
lines 195–198why, one step back195 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 back199 const {isCanceled} = await t.throwsAsync(execa('fail.js'));3.2.0ehmickybefore adoptione003f9c
lines 200–203why, one step back200 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 back204 const {isCanceled} = execaSync('noop.js');3.2.0ehmickybefore adoptione003f9c
lines 205–209why, one step back205 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 back210 execaSync('fail.js');3.2.0ehmickybefore adoptione003f9c
lines 211–215why, one step back211 });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 back216 const subprocess = execa('noop.js');3.2.0ehmickybefore adoptione003f9c
lines 217–221why, one step back217 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 back222 const subprocess = execa('noop.js');3.2.0ehmickybefore adoptione003f9c
lines 223–228why, one step back223 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 back229 const subprocess = execa('noop.js');3.2.0ehmickybefore adoptione003f9c
lines 230–235why, one step back230 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 back236 const subprocess = execa('noop.js');3.2.0ehmickybefore adoptione003f9c
lines 237–244why, one step back237 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 back245 const subprocess = execa('noop.js');3.2.0ehmickybefore adoptione003f9c
lines 246–251why, one step back246 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 back252 const subprocess = execa('noop.js');3.2.0ehmickybefore adoptione003f9c
lines 253–256why, one step back253 subprocess.kill();254 const {isCanceled} = await t.throwsAsync(subprocess);255 t.false(isCanceled);256});Support `AbortController` (#490)Gyubongbefore adoptionc6e791a
lines 257–303why, one step back257258if (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}