kill.js
This file
Path
test/kill.js
262 lines · 8.6 KiB · JavaScript
Last changed by
Properly clean up child processes created during tests (#349)ehmickybefore adoption071a815
History of this fileTake it
Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 1–82why, one step back1import path from 'path';2import test from 'ava';3import pEvent from 'p-event';4import isRunning from 'is-running';5import execa from '..';67process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;89const TIMEOUT_REGEXP = /timed out after/;1011test('kill("SIGKILL") should terminate cleanly', async t => {12 const subprocess = execa('node', ['./test/fixtures/no-killable'], {stdio: ['ipc']});13 await pEvent(subprocess, 'message');1415 subprocess.kill('SIGKILL');1617 const {signal} = await t.throwsAsync(subprocess);18 t.is(signal, 'SIGKILL');19});2021// `SIGTERM` cannot be caught on Windows, and it always aborts the process (like `SIGKILL` on Unix).22// Therefore, this feature and those tests do not make sense on Windows.23if (process.platform !== 'win32') {24 test('`forceKillAfterTimeout: false` should not kill after a timeout', async t => {25 const subprocess = execa('node', ['./test/fixtures/no-killable'], {stdio: ['ipc']});26 await pEvent(subprocess, 'message');2728 subprocess.kill('SIGTERM', {forceKillAfterTimeout: false});2930 t.true(isRunning(subprocess.pid));31 subprocess.kill('SIGKILL');32 });3334 test('`forceKillAfterTimeout: number` should kill after a timeout', async t => {35 const subprocess = execa('node', ['./test/fixtures/no-killable'], {stdio: ['ipc']});36 await pEvent(subprocess, 'message');3738 subprocess.kill('SIGTERM', {forceKillAfterTimeout: 50});3940 const {signal} = await t.throwsAsync(subprocess);41 t.is(signal, 'SIGKILL');42 });4344 test('`forceKillAfterTimeout: true` should kill after a timeout', async t => {45 const subprocess = execa('node', ['./test/fixtures/no-killable'], {stdio: ['ipc']});46 await pEvent(subprocess, 'message');4748 subprocess.kill('SIGTERM', {forceKillAfterTimeout: true});4950 const {signal} = await t.throwsAsync(subprocess);51 t.is(signal, 'SIGKILL');52 });5354 test('kill() with no arguments should kill after a timeout', async t => {55 const subprocess = execa('node', ['./test/fixtures/no-killable'], {stdio: ['ipc']});56 await pEvent(subprocess, 'message');5758 subprocess.kill();5960 const {signal} = await t.throwsAsync(subprocess);61 t.is(signal, 'SIGKILL');62 });6364 test('`forceKillAfterTimeout` should not be a float', t => {65 t.throws(() => {66 execa('noop').kill('SIGTERM', {forceKillAfterTimeout: 0.5});67 }, {instanceOf: TypeError, message: /non-negative integer/});68 });6970 test('`forceKillAfterTimeout` should not be negative', t => {71 t.throws(() => {72 execa('noop').kill('SIGTERM', {forceKillAfterTimeout: -1});73 }, {instanceOf: TypeError, message: /non-negative integer/});74 });75}7677test('execa() returns a promise with kill()', t => {78 const {kill} = execa('noop', ['foo']);79 t.is(typeof kill, 'function');80});8182test('timeout kills the process if it times out', async t => {Properly clean up child processes created during tests (#349)ehmickybefore adoption071a815
line 83why, one step back83 const {killed, timedOut} = await t.throwsAsync(execa('noop', {timeout: 1}), TIMEOUT_REGEXP);Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 84–89why, one step back84 t.false(killed);85 t.true(timedOut);86});8788test('timeout kills the process if it times out, in sync mode', async t => {89 const {killed, timedOut} = await t.throws(() => {Properly clean up child processes created during tests (#349)ehmickybefore adoption071a815
line 90why, one step back90 execa.sync('noop', {timeout: 1, message: TIMEOUT_REGEXP});Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 91–100why, one step back91 });92 t.false(killed);93 t.true(timedOut);94});9596test('timeout does not kill the process if it does not time out', async t => {97 const {timedOut} = await execa('delay', ['500'], {timeout: 1e8});98 t.false(timedOut);99});100Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f
lines 101–114why, one step back101const INVALID_TIMEOUT_REGEXP = /`timeout` option to be a non-negative integer/;102103test('timeout must not be negative', async t => {104 await t.throws(() => {105 execa('noop', {timeout: -1});106 }, INVALID_TIMEOUT_REGEXP);107});108109test('timeout must be an integer', async t => {110 await t.throws(() => {111 execa('noop', {timeout: false});112 }, INVALID_TIMEOUT_REGEXP);113});114Improve tests for the `timeout` option (#332)ehmickybefore adoption0241d6e
line 115why, one step back115test('timedOut is false if timeout is undefined', async t => {Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 116–119why, one step back116 const {timedOut} = await execa('noop');117 t.false(timedOut);118});119Improve tests for the `timeout` option (#332)ehmickybefore adoption0241d6e
lines 120–125why, one step back120test('timedOut is false if timeout is 0', async t => {121 const {timedOut} = await execa('noop', {timeout: 0});122 t.false(timedOut);123});124125test('timedOut is false if timeout is undefined and exit code is 0 in sync mode', t => {Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 126–154why, one step back126 const {timedOut} = execa.sync('noop');127 t.false(timedOut);128});129130// When child process exits before parent process131const spawnAndExit = async (t, cleanup, detached) => {132 await t.notThrowsAsync(execa('sub-process-exit', [cleanup, detached]));133};134135test('spawnAndExit', spawnAndExit, false, false);136test('spawnAndExit cleanup', spawnAndExit, true, false);137test('spawnAndExit detached', spawnAndExit, false, true);138test('spawnAndExit cleanup detached', spawnAndExit, true, true);139140// When parent process exits before child process141const spawnAndKill = async (t, signal, cleanup, detached, isKilled) => {142 const subprocess = execa('sub-process', [cleanup, detached], {stdio: ['ignore', 'ignore', 'ignore', 'ipc']});143144 const pid = await pEvent(subprocess, 'message');145 t.true(Number.isInteger(pid));146 t.true(isRunning(pid));147148 process.kill(subprocess.pid, signal);149150 await t.throwsAsync(subprocess);151152 t.false(isRunning(subprocess.pid));153 t.is(isRunning(pid), !isKilled);154Properly clean up child processes created during tests (#349)ehmickybefore adoption071a815
line 155why, one step back155 if (isRunning(pid)) {Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 156–262why, one step back156 process.kill(pid, 'SIGKILL');157 }158};159160// Without `options.cleanup`:161// - on Windows subprocesses are killed if `options.detached: false`, but not162// if `options.detached: true`163// - on Linux subprocesses are never killed regardless of `options.detached`164// With `options.cleanup`, subprocesses are always killed165// - `options.cleanup` with SIGKILL is a noop, since it cannot be handled166const exitIfWindows = process.platform === 'win32';167test('spawnAndKill SIGTERM', spawnAndKill, 'SIGTERM', false, false, exitIfWindows);168test('spawnAndKill SIGKILL', spawnAndKill, 'SIGKILL', false, false, exitIfWindows);169test('spawnAndKill cleanup SIGTERM', spawnAndKill, 'SIGTERM', true, false, true);170test('spawnAndKill cleanup SIGKILL', spawnAndKill, 'SIGKILL', true, false, exitIfWindows);171test('spawnAndKill detached SIGTERM', spawnAndKill, 'SIGTERM', false, true, false);172test('spawnAndKill detached SIGKILL', spawnAndKill, 'SIGKILL', false, true, false);173test('spawnAndKill cleanup detached SIGTERM', spawnAndKill, 'SIGTERM', true, true, false);174test('spawnAndKill cleanup detached SIGKILL', spawnAndKill, 'SIGKILL', true, true, false);175176// See #128177test('removes exit handler on exit', async t => {178 // FIXME: This relies on `signal-exit` internals179 const ee = process.__signal_exit_emitter__;180181 const child = execa('noop');182 const listener = ee.listeners('exit').pop();183184 await new Promise((resolve, reject) => {185 child.on('error', reject);186 child.on('exit', resolve);187 });188189 const included = ee.listeners('exit').includes(listener);190 t.false(included);191});192193test('cancel method kills the subprocess', t => {194 const subprocess = execa('node');195 subprocess.cancel();196 t.true(subprocess.killed);197});198199test('result.isCanceled is false when spawned.cancel() isn\'t called (success)', async t => {200 const {isCanceled} = await execa('noop');201 t.false(isCanceled);202});203204test('result.isCanceled is false when spawned.cancel() isn\'t called (failure)', async t => {205 const {isCanceled} = await t.throwsAsync(execa('fail'));206 t.false(isCanceled);207});208209test('result.isCanceled is false when spawned.cancel() isn\'t called in sync mode (success)', t => {210 const {isCanceled} = execa.sync('noop');211 t.false(isCanceled);212});213214test('result.isCanceled is false when spawned.cancel() isn\'t called in sync mode (failure)', t => {215 const {isCanceled} = t.throws(() => {216 execa.sync('fail');217 });218 t.false(isCanceled);219});220221test('calling cancel method throws an error with message "Command was canceled"', async t => {222 const subprocess = execa('noop');223 subprocess.cancel();224 await t.throwsAsync(subprocess, {message: /Command was canceled/});225});226227test('error.isCanceled is true when cancel method is used', async t => {228 const subprocess = execa('noop');229 subprocess.cancel();230 const {isCanceled} = await t.throwsAsync(subprocess);231 t.true(isCanceled);232});233234test('error.isCanceled is false when kill method is used', async t => {235 const subprocess = execa('noop');236 subprocess.kill();237 const {isCanceled} = await t.throwsAsync(subprocess);238 t.false(isCanceled);239});240241test('calling cancel method twice should show the same behaviour as calling it once', async t => {242 const subprocess = execa('noop');243 subprocess.cancel();244 subprocess.cancel();245 const {isCanceled} = await t.throwsAsync(subprocess);246 t.true(isCanceled);247 t.true(subprocess.killed);248});249250test('calling cancel method on a successfully completed process does not make result.isCanceled true', async t => {251 const subprocess = execa('noop');252 const {isCanceled} = await subprocess;253 subprocess.cancel();254 t.false(isCanceled);255});256257test('calling cancel method on a process which has been killed does not make error.isCanceled true', async t => {258 const subprocess = execa('noop');259 subprocess.kill();260 const {isCanceled} = await t.throwsAsync(subprocess);261 t.false(isCanceled);262});