Skip to content
Myr

cleanup.js

This file

Path

test/terminate/cleanup.js

122 lines · 4.5 KiB · JavaScript

Last changed by

TweaksSindre Sorhusbefore adoption698a1b8

History of this file

Take it

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

TweaksSindre Sorhusbefore adoption698a1b8

line 1why, one step back
1import assert from 'node:assert/strict';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 2–3why, one step back
2import process from 'node:process';
3import {setTimeout} from 'node:timers/promises';

TweaksSindre Sorhusbefore adoption698a1b8

line 4why, one step back
4import test from 'node:test';

100% test coverage (#1018)ehmickybefore adoption9f65b86

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

TweaksSindre Sorhusbefore adoption698a1b8

line 6why, one step back
6import {assertThrows, assertRejects} from '../helpers/assert.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 7–8why, one step back
7import {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 back
9import {nestedSubprocess} from '../helpers/nested.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 10–16why, one step back
10import {foobarString} from '../helpers/input.js';
1112setFixtureDirectory();1314const isWindows = process.platform === 'win32';1516// When subprocess exits before current process

TweaksSindre Sorhusbefore adoption698a1b8

line 17why, one step back
17const spawnAndExit = async (worker, cleanup, detached) => {

Refactor test helpers for the `verbose` option (#1128)ehmickybefore adoption18d320f

line 18why, one step back
18 const {nestedResult: {stdout}} = await nestedSubprocess('noop-fd.js', ['1', foobarString], {worker, cleanup, detached});

TweaksSindre Sorhusbefore adoption698a1b8

line 19why, one step back
19 assert.equal(stdout, foobarString);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 20–21why, one step back
20};
21

TweaksSindre Sorhusbefore adoption698a1b8

lines 22–29why, one step back
22test('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 back
30
31// When current process exits before subprocess

TweaksSindre Sorhusbefore adoption698a1b8

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

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 33why, one step back
33 const subprocess = execa('ipc-send-pid.js', [cleanup, detached], {stdio: 'ignore', ipc: true});

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 34why, one step back
34

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 35why, one step back
35 const pid = await subprocess.getOneMessage();

TweaksSindre Sorhusbefore adoption698a1b8

lines 36–37why, one step back
36 assert.ok(Number.isInteger(pid));
37 assert.ok(isRunning(pid));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 38–40why, one step back
38
39 process.kill(subprocess.pid, signal);40

TweaksSindre Sorhusbefore adoption698a1b8

lines 41–42why, one step back
41 await assertRejects(subprocess);
42 assert.ok(!isRunning(subprocess.pid));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 43–48why, one step back
43
44 if (isKilled) {45 await Promise.race([46 setTimeout(1e4, undefined, {ref: false}),47 pollForSubprocessExit(pid),48 ]);

TweaksSindre Sorhusbefore adoption698a1b8

line 49why, one step back
49 assert.equal(isRunning(pid), false);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 50why, one step back
50 } else {

TweaksSindre Sorhusbefore adoption698a1b8

line 51why, one step back
51 assert.equal(isRunning(pid), true);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 52–68why, one step back
52 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 handled

TweaksSindre Sorhusbefore adoption698a1b8

lines 69–76why, one step back
69test('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 back
77

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 78why, one step back
78if (!isWindows) {

TweaksSindre Sorhusbefore adoption698a1b8

line 79why, one step back
79 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 back
80 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 back
83 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 back
85
86 process.kill(subprocess.pid, 'SIGTERM');87

TweaksSindre Sorhusbefore adoption698a1b8

line 88why, one step back
88 await assertRejects(subprocess);

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

lines 89–92why, one step back
89 await Promise.race([
90 setTimeout(1e4, undefined, {ref: false}),91 pollForSubprocessExit(pid),92 ]);

TweaksSindre Sorhusbefore adoption698a1b8

line 93why, one step back
93 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 back
94 });
95}96

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 97why, one step back
97// See #128

TweaksSindre Sorhusbefore adoption698a1b8

line 98why, one step back
98test('removes exit handler on exit', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 99–105why, one step back
99 // @todo this relies on `signal-exit` internals
100 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 back
106 assert.ok(!exitListeners.includes(listener));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 107–108why, one step back
107});
108

TweaksSindre Sorhusbefore adoption698a1b8

line 109why, one step back
109test('detach subprocess', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 110–111why, one step back
110 const {stdout} = await execa('detach.js');
111 const pid = Number(stdout);

TweaksSindre Sorhusbefore adoption698a1b8

lines 112–113why, one step back
112 assert.ok(Number.isInteger(pid));
113 assert.ok(isRunning(pid));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 114–117why, one step back
114
115 process.kill(pid, 'SIGKILL');116});117

TweaksSindre Sorhusbefore adoption698a1b8

lines 118–119why, one step back
118test('Cannot use "detached" option, sync', () => {
119 assertThrows(() => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 120–122why, one step back
120 execaSync('empty.js', {detached: true});
121 }, {message: /The "detached: true" option cannot be used/});122});