cleanup.js
This file
Path
test/terminate/cleanup.js
122 lines · 4.5 KiB · JavaScript
Last changed by
TweaksSindre Sorhusbefore adoption698a1b8
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
TweaksSindre Sorhusbefore adoption698a1b8
line 1why, one step back1import assert from 'node:assert/strict';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 2–3why, one step back2import process from 'node:process';3import {setTimeout} from 'node:timers/promises';TweaksSindre Sorhusbefore adoption698a1b8
line 4why, one step back4import test from 'node:test';100% test coverage (#1018)ehmickybefore adoption9f65b86
line 5why, one step back5import isRunning from 'is-running';TweaksSindre Sorhusbefore adoption698a1b8
line 6why, one step back6import {assertThrows, assertRejects} from '../helpers/assert.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 7–8why, one step back7import {execa, execaSync} from '../../index.js';8import {setFixtureDirectory} from '../helpers/fixtures-directory.js';Refactor test helpers for the `verbose` option (#1128)ehmickybefore adoption18d320f
line 9why, one step back9import {nestedSubprocess} from '../helpers/nested.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 10–16why, one step back10import {foobarString} from '../helpers/input.js';1112setFixtureDirectory();1314const isWindows = process.platform === 'win32';1516// When subprocess exits before current processTweaksSindre Sorhusbefore adoption698a1b8
line 17why, one step back17const spawnAndExit = async (worker, cleanup, detached) => {Refactor test helpers for the `verbose` option (#1128)ehmickybefore adoption18d320f
line 18why, one step back18 const {nestedResult: {stdout}} = await nestedSubprocess('noop-fd.js', ['1', foobarString], {worker, cleanup, detached});TweaksSindre Sorhusbefore adoption698a1b8
line 19why, one step back19 assert.equal(stdout, foobarString);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 20–21why, one step back20};21TweaksSindre Sorhusbefore adoption698a1b8
lines 22–29why, one step back22test('spawnAndExit', () => spawnAndExit(false, false, false));23test('spawnAndExit cleanup', () => spawnAndExit(false, true, false));24test('spawnAndExit detached', () => spawnAndExit(false, false, true));25test('spawnAndExit cleanup detached', () => spawnAndExit(false, true, true));26test('spawnAndExit, worker', () => spawnAndExit(true, false, false));27test('spawnAndExit cleanup, worker', () => spawnAndExit(true, true, false));28test('spawnAndExit detached, worker', () => spawnAndExit(true, false, true));29test('spawnAndExit cleanup detached, worker', () => spawnAndExit(true, true, true));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 30–31why, one step back3031// When current process exits before subprocessTweaksSindre Sorhusbefore adoption698a1b8
line 32why, one step back32const spawnAndKill = async ([signal, cleanup, detached, isKilled]) => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 33why, one step back33 const subprocess = execa('ipc-send-pid.js', [cleanup, detached], {stdio: 'ignore', ipc: true});100% test coverage (#1018)ehmickybefore adoption9f65b86
line 34why, one step back34Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 35why, one step back35 const pid = await subprocess.getOneMessage();TweaksSindre Sorhusbefore adoption698a1b8
lines 36–37why, one step back36 assert.ok(Number.isInteger(pid));37 assert.ok(isRunning(pid));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 38–40why, one step back3839 process.kill(subprocess.pid, signal);40TweaksSindre Sorhusbefore adoption698a1b8
lines 41–42why, one step back41 await assertRejects(subprocess);42 assert.ok(!isRunning(subprocess.pid));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 43–48why, one step back4344 if (isKilled) {45 await Promise.race([46 setTimeout(1e4, undefined, {ref: false}),47 pollForSubprocessExit(pid),48 ]);TweaksSindre Sorhusbefore adoption698a1b8
line 49why, one step back49 assert.equal(isRunning(pid), false);100% test coverage (#1018)ehmickybefore adoption9f65b86
line 50why, one step back50 } else {TweaksSindre Sorhusbefore adoption698a1b8
line 51why, one step back51 assert.equal(isRunning(pid), true);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 52–68why, one step back52 process.kill(pid, 'SIGKILL');53 }54};5556const pollForSubprocessExit = async pid => {57 while (isRunning(pid)) {58 // eslint-disable-next-line no-await-in-loop59 await setTimeout(100);60 }61};6263// Without `options.cleanup`:64// - on Windows subprocesses are killed if `options.detached: false`, but not65// if `options.detached: true`66// - on Linux subprocesses are never killed regardless of `options.detached`67// With `options.cleanup`, subprocesses are always killed68// - `options.cleanup` with SIGKILL is a noop, since it cannot be handledTweaksSindre Sorhusbefore adoption698a1b8
lines 69–76why, one step back69test('spawnAndKill SIGTERM', () => spawnAndKill(['SIGTERM', false, false, isWindows]));70test('spawnAndKill SIGKILL', () => spawnAndKill(['SIGKILL', false, false, isWindows]));71test('spawnAndKill cleanup SIGTERM', () => spawnAndKill(['SIGTERM', true, false, true]));72test('spawnAndKill cleanup SIGKILL', () => spawnAndKill(['SIGKILL', true, false, isWindows]));73test('spawnAndKill detached SIGTERM', () => spawnAndKill(['SIGTERM', false, true, false]));74test('spawnAndKill detached SIGKILL', () => spawnAndKill(['SIGKILL', false, true, false]));75test('spawnAndKill cleanup detached SIGTERM', () => spawnAndKill(['SIGTERM', true, true, false]));76test('spawnAndKill cleanup detached SIGKILL', () => spawnAndKill(['SIGKILL', true, true, false]));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 77why, one step back77Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 78why, one step back78if (!isWindows) {TweaksSindre Sorhusbefore adoption698a1b8
line 79why, one step back79 test('spawnAndKill cleanup uses killSignal', async () => {Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
lines 80–82why, one step back80 const subprocess = execa('ipc-send-pid.js', ['true', 'false', 'no-killable'], {stdio: 'ignore', ipc: true});8182 const pid = await subprocess.getOneMessage();TweaksSindre Sorhusbefore adoption698a1b8
lines 83–84why, one step back83 assert.ok(Number.isInteger(pid));84 assert.ok(isRunning(pid));Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
lines 85–87why, one step back8586 process.kill(subprocess.pid, 'SIGTERM');87TweaksSindre Sorhusbefore adoption698a1b8
line 88why, one step back88 await assertRejects(subprocess);Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
lines 89–92why, one step back89 await Promise.race([90 setTimeout(1e4, undefined, {ref: false}),91 pollForSubprocessExit(pid),92 ]);TweaksSindre Sorhusbefore adoption698a1b8
line 93why, one step back93 assert.ok(!isRunning(pid));Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
lines 94–96why, one step back94 });95}96100% test coverage (#1018)ehmickybefore adoption9f65b86
line 97why, one step back97// See #128TweaksSindre Sorhusbefore adoption698a1b8
line 98why, one step back98test('removes exit handler on exit', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 99–105why, one step back99 // @todo this relies on `signal-exit` internals100 const exitListeners = globalThis[Symbol.for('signal-exit emitter')].listeners.exit;101102 const subprocess = execa('noop.js');103 const listener = exitListeners.at(-1);104105 await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
line 106why, one step back106 assert.ok(!exitListeners.includes(listener));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 107–108why, one step back107});108TweaksSindre Sorhusbefore adoption698a1b8
line 109why, one step back109test('detach subprocess', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 110–111why, one step back110 const {stdout} = await execa('detach.js');111 const pid = Number(stdout);TweaksSindre Sorhusbefore adoption698a1b8
lines 112–113why, one step back112 assert.ok(Number.isInteger(pid));113 assert.ok(isRunning(pid));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 114–117why, one step back114115 process.kill(pid, 'SIGKILL');116});117TweaksSindre Sorhusbefore adoption698a1b8
lines 118–119why, one step back118test('Cannot use "detached" option, sync', () => {119 assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 120–122why, one step back120 execaSync('empty.js', {detached: true});121 }, {message: /The "detached: true" option cannot be used/});122});