Skip to content
Myr

kill-descendants.js

This file

Path

test/terminate/kill-descendants.js

318 lines · 11.0 KiB · JavaScript

Last changed by

Report `null` pipe options as a rejectionSindre Sorhusbefore adoptionecda56b

History of this file

Take it

TweaksSindre Sorhusbefore adoption698a1b8

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

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

lines 2–4why, one step back
2import childProcess from 'node:child_process';
3import {syncBuiltinESMExports} from 'node:module';4import path from 'node:path/win32';

Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec

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

TweaksSindre Sorhusbefore adoption698a1b8

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

Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec

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

TweaksSindre Sorhusbefore adoption698a1b8

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

Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec

line 10why, one step back
10import {execa, execaSync} from '../../index.js';

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

line 11why, one step back
11import {getTaskkillFile} from '../../lib/terminate/kill-descendants.js';

Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec

lines 12–36why, one step back
12import {setFixtureDirectory} from '../helpers/fixtures-directory.js';
1314setFixtureDirectory();1516const isWindows = process.platform === 'win32';1718const pollForSubprocessExit = async pid => {19 while (isRunning(pid)) {20 // eslint-disable-next-line no-await-in-loop21 await setTimeout(100);22 }23};2425// `ipc-send-pid.js` spawns a descendant process (`forever.js`) and sends back its PID.26const spawnDescendant = async (killDescendants, options) => {27 const subprocess = execa('ipc-send-pid.js', ['false', 'false'], {28 stdio: 'ignore',29 ipc: true,30 killDescendants,31 ...options,32 });33 const descendantPid = await subprocess.getOneMessage();34 return {subprocess, descendantPid};35};36

TweaksSindre Sorhusbefore adoption698a1b8

line 37why, one step back
37test('killDescendants terminates descendant processes', async () => {

Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec

line 38why, one step back
38 const {subprocess, descendantPid} = await spawnDescendant(true);

TweaksSindre Sorhusbefore adoption698a1b8

line 39why, one step back
39 assert.ok(isRunning(descendantPid));

Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec

lines 40–41why, one step back
40
41 subprocess.kill();

TweaksSindre Sorhusbefore adoption698a1b8

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

Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec

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

TweaksSindre Sorhusbefore adoption698a1b8

line 49why, one step back
49 assert.ok(!isRunning(descendantPid));

Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec

lines 50–51why, one step back
50});
51

TweaksSindre Sorhusbefore adoption698a1b8

line 52why, one step back
52test('killDescendants also terminates descendant processes when the subprocess times out', async () => {

Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec

line 53why, one step back
53 const {subprocess, descendantPid} = await spawnDescendant(true, {timeout: 1000});

TweaksSindre Sorhusbefore adoption698a1b8

line 54why, one step back
54 assert.ok(isRunning(descendantPid));

Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec

line 55why, one step back
55

TweaksSindre Sorhusbefore adoption698a1b8

lines 56–57why, one step back
56 const {timedOut} = await assertRejects(subprocess);
57 assert.equal(timedOut, true);

Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec

lines 58–62why, one step back
58
59 await Promise.race([60 setTimeout(1e4, undefined, {ref: false}),61 pollForSubprocessExit(descendantPid),62 ]);

TweaksSindre Sorhusbefore adoption698a1b8

line 63why, one step back
63 assert.ok(!isRunning(descendantPid));

Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec

lines 64–68why, one step back
64});
6566// On Windows, terminating the direct subprocess already terminates its descendants, so this67// only asserts the default Unix behavior of leaving descendants running.68if (!isWindows) {

TweaksSindre Sorhusbefore adoption698a1b8

line 69why, one step back
69 test('descendant processes are not terminated without killDescendants', async () => {

Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec

line 70why, one step back
70 const {subprocess, descendantPid} = await spawnDescendant(false);

TweaksSindre Sorhusbefore adoption698a1b8

line 71why, one step back
71 assert.ok(isRunning(descendantPid));

Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec

lines 72–73why, one step back
72
73 subprocess.kill();

TweaksSindre Sorhusbefore adoption698a1b8

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

Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec

line 76why, one step back
76

TweaksSindre Sorhusbefore adoption698a1b8

line 77why, one step back
77 assert.ok(isRunning(descendantPid));

Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec

lines 78–80why, one step back
78 process.kill(descendantPid, 'SIGKILL');
79 });80

TweaksSindre Sorhusbefore adoption698a1b8

line 81why, one step back
81 test('timeout does not terminate descendants without killDescendants', async () => {

Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec

line 82why, one step back
82 const {subprocess, descendantPid} = await spawnDescendant(false, {timeout: 1000});

TweaksSindre Sorhusbefore adoption698a1b8

line 83why, one step back
83 assert.ok(isRunning(descendantPid));

Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec

line 84why, one step back
84

TweaksSindre Sorhusbefore adoption698a1b8

lines 85–87why, one step back
85 const {timedOut} = await assertRejects(subprocess);
86 assert.equal(timedOut, true);87 assert.ok(isRunning(descendantPid));

Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec

lines 88–91why, one step back
88 process.kill(descendantPid, 'SIGKILL');
89 });90}91

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

lines 92–96why, one step back
92/*
93The `forceKillAfterDelay` escalation happens after the subprocess exited, since that is when its termination has been noticed.94A descendant which ignores `SIGTERM` is still running then, so the escalation must still reach it.95Otherwise, since that descendant keeps the subprocess' `stdout` open, awaiting the subprocess would never settle.96*/

TweaksSindre Sorhusbefore adoption698a1b8

line 97why, one step back
97test('killDescendants escalates to descendants which outlive the subprocess', async () => {

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

lines 98–103why, one step back
98 const subprocess = execa('ipc-send-pid-stubborn.js', {
99 ipc: true,100 killDescendants: true,101 forceKillAfterDelay: 1000,102 });103 const descendantPid = await subprocess.getOneMessage();

TweaksSindre Sorhusbefore adoption698a1b8

line 104why, one step back
104 assert.ok(isRunning(descendantPid));

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

lines 105–107why, one step back
105
106 subprocess.kill();107 const settled = await Promise.race([

TweaksSindre Sorhusbefore adoption698a1b8

line 108why, one step back
108 assertRejects(subprocess).then(() => 'settled'),

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

lines 109–110why, one step back
109 setTimeout(1e4, 'pending', {ref: false}),
110 ]);

TweaksSindre Sorhusbefore adoption698a1b8

lines 111–112why, one step back
111 assert.equal(settled, 'settled');
112 assert.ok(!isRunning(descendantPid));

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

lines 113–115why, one step back
113});
114115if (!isWindows) {

TweaksSindre Sorhusbefore adoption698a1b8

lines 116–120why, one step back
116 // The process group ID stays reserved while a descendant is running, so the descendants which outlive the subprocess can still be terminated once Execa is done with it
117 test('killDescendants terminates background descendants once Execa is done with the subprocess', async () => {118 const subprocess = execa({shell: true, killDescendants: true})`sleep 100 > /dev/null 2>&1 & echo $!`;119 const {stdout} = await subprocess;120 const descendantPid = Number(stdout);

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

line 121why, one step back
121

TweaksSindre Sorhusbefore adoption698a1b8

lines 122–124why, one step back
122 try {
123 assert.ok(isRunning(descendantPid));124 assert.ok(subprocess.kill());

Report `null` pipe options as a rejectionSindre Sorhusbefore adoptionecda56b

lines 125–129why, one step back
125 await Promise.race([
126 setTimeout(1e4, undefined, {ref: false}),127 pollForSubprocessExit(descendantPid),128 ]);129 assert.ok(!isRunning(descendantPid));

TweaksSindre Sorhusbefore adoption698a1b8

lines 130–134why, one step back
130 } finally {
131 if (isRunning(descendantPid)) {132 process.kill(descendantPid, 'SIGKILL');133 }134 }

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

lines 135–136why, one step back
135 });
136

TweaksSindre Sorhusbefore adoption698a1b8

lines 137–141why, one step back
137 // Once the whole process group exited, its PID is not signaled since `process.kill()` fails
138 test('killDescendants returns false once the whole process group exited', async () => {139 const subprocess = execa('empty.js', {killDescendants: true});140 await subprocess;141 assert.equal(subprocess.kill(), false);

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

lines 142–144why, one step back
142 });
143144 // While Execa is still waiting on the subprocess, its process group is still its own

TweaksSindre Sorhusbefore adoption698a1b8

line 145why, one step back
145 test('killDescendants signals the process group while Execa is not done with the subprocess', async t => {

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

lines 146–148why, one step back
146 const subprocess = execa('forever.js', {killDescendants: true});
147148 const originalKill = process.kill;

TweaksSindre Sorhusbefore adoption698a1b8

line 149why, one step back
149 t.after(() => {

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

lines 150–158why, one step back
150 process.kill = originalKill;
151 });152153 const signaledProcesses = [];154 process.kill = (pid, signal) => {155 signaledProcesses.push([pid, signal]);156 return originalKill(pid, signal);157 };158

TweaksSindre Sorhusbefore adoption698a1b8

lines 159–160why, one step back
159 assert.ok(subprocess.kill('SIGTERM'));
160 assert.deepEqual(signaledProcesses, [[-subprocess.pid, 'SIGTERM']]);

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

line 161why, one step back
161 process.kill = originalKill;

TweaksSindre Sorhusbefore adoption698a1b8

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

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

lines 163–165why, one step back
163 });
164}165

TweaksSindre Sorhusbefore adoption698a1b8

lines 166–167why, one step back
166test('Cannot use "killDescendants" option, sync', () => {
167 assertThrows(() => {

Add `killDescendants` option to also terminate descendant processes (#1256)Sindre Sorhusbefore adoption84fa0ec

lines 168–170why, one step back
168 execaSync('empty.js', {killDescendants: true});
169 }, {message: /The "killDescendants: true" option cannot be used/});170});

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

line 171why, one step back
171

TweaksSindre Sorhusbefore adoption698a1b8

line 172why, one step back
172test('taskkill is resolved from the Windows directory when available', t => {

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

line 173why, one step back
173 const {SystemRoot, windir} = process.env;

TweaksSindre Sorhusbefore adoption698a1b8

line 174why, one step back
174 t.after(() => {

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

lines 175–180why, one step back
175 restoreEnvironment('SystemRoot', SystemRoot);
176 restoreEnvironment('windir', windir);177 });178179 process.env.SystemRoot = 'C:\\Windows';180 process.env.windir = 'D:\\Windows';

TweaksSindre Sorhusbefore adoption698a1b8

line 181why, one step back
181 assert.equal(getTaskkillFile(), path.join('C:\\Windows', 'System32', 'taskkill.exe'));

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

lines 182–183why, one step back
182
183 process.env.SystemRoot = 'C:/Windows';

TweaksSindre Sorhusbefore adoption698a1b8

line 184why, one step back
184 assert.equal(getTaskkillFile(), path.join('C:/Windows', 'System32', 'taskkill.exe'));

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

lines 185–186why, one step back
185
186 process.env.SystemRoot = 'Windows';

TweaksSindre Sorhusbefore adoption698a1b8

line 187why, one step back
187 assert.equal(getTaskkillFile(), path.join('D:\\Windows', 'System32', 'taskkill.exe'));

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

lines 188–189why, one step back
188
189 process.env.SystemRoot = '\\Windows';

TweaksSindre Sorhusbefore adoption698a1b8

line 190why, one step back
190 assert.equal(getTaskkillFile(), path.join('D:\\Windows', 'System32', 'taskkill.exe'));

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

lines 191–192why, one step back
191
192 delete process.env.SystemRoot;

TweaksSindre Sorhusbefore adoption698a1b8

line 193why, one step back
193 assert.equal(getTaskkillFile(), path.join('D:\\Windows', 'System32', 'taskkill.exe'));

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

lines 194–195why, one step back
194
195 process.env.windir = 'Windows';

TweaksSindre Sorhusbefore adoption698a1b8

line 196why, one step back
196 assert.equal(getTaskkillFile(), undefined);

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

lines 197–198why, one step back
197
198 process.env.windir = '\\Windows';

TweaksSindre Sorhusbefore adoption698a1b8

line 199why, one step back
199 assert.equal(getTaskkillFile(), undefined);

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

lines 200–201why, one step back
200
201 process.env.windir = '\\\\server\\share\\Windows';

TweaksSindre Sorhusbefore adoption698a1b8

line 202why, one step back
202 assert.equal(getTaskkillFile(), undefined);

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

lines 203–204why, one step back
203
204 process.env.windir = '';

TweaksSindre Sorhusbefore adoption698a1b8

line 205why, one step back
205 assert.equal(getTaskkillFile(), undefined);

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

lines 206–207why, one step back
206
207 delete process.env.windir;

TweaksSindre Sorhusbefore adoption698a1b8

line 208why, one step back
208 assert.equal(getTaskkillFile(), undefined);

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

lines 209–210why, one step back
209});
210

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

lines 211–217why, one step back
211/*
212The `taskkill` logic only runs on Windows, so the platform, the Windows directory and `taskkill` itself are faked to test it on any OS.213`lib/terminate/kill-descendants.js` reads `process.platform` when it is loaded, so it must be re-imported once the platform has been faked.214*/215let fakeWindowsCount = 0;216217const fakeWindows = async (t, systemRoot, execFile = () => {}) => {

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

line 218why, one step back
218 const platformDescriptor = Object.getOwnPropertyDescriptor(process, 'platform');

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

line 219why, one step back
219 const originalExecFile = childProcess.execFile;

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

line 220why, one step back
220 const {SystemRoot, windir} = process.env;

TweaksSindre Sorhusbefore adoption698a1b8

line 221why, one step back
221 t.after(() => {

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

line 222why, one step back
222 Object.defineProperty(process, 'platform', platformDescriptor);

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

lines 223–224why, one step back
223 childProcess.execFile = originalExecFile;
224 syncBuiltinESMExports();

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

lines 225–229why, one step back
225 restoreEnvironment('SystemRoot', SystemRoot);
226 restoreEnvironment('windir', windir);227 });228229 Object.defineProperty(process, 'platform', {value: 'win32'});

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

line 230why, one step back
230 process.env.SystemRoot = systemRoot;

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

line 231why, one step back
231 delete process.env.windir;

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

lines 232–233why, one step back
232 childProcess.execFile = execFile;
233 syncBuiltinESMExports();

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

line 234why, one step back
234

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

lines 235–241why, one step back
235 fakeWindowsCount += 1;
236 const {getKillFunction} = await import(`../../lib/terminate/kill-descendants.js?fake-windows=${fakeWindowsCount}`);237 return getKillFunction;238};239240// Mimics the `ChildProcess` properties used to send the signal and to detect whether the subprocess is still running241const createFakeSubprocess = ({exitCode = null, signalCode = null} = {}) => {

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

lines 242–243why, one step back
242 const subprocess = {
243 pid: 123,

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

lines 244–245why, one step back
244 exitCode,
245 signalCode,

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

line 246why, one step back
246 kill(signal) {

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

line 247why, one step back
247 subprocess.killedWith = signal;

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

lines 248–250why, one step back
248 return true;
249 },250 };

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

lines 251–252why, one step back
251 return subprocess;
252};

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

line 253why, one step back
253

TweaksSindre Sorhusbefore adoption698a1b8

line 254why, one step back
254test('taskkill fallback uses direct subprocess kill when Windows directory is unavailable', async t => {

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

lines 255–257why, one step back
255 const getKillFunction = await fakeWindows(t, 'Windows');
256 const subprocess = createFakeSubprocess();257

TweaksSindre Sorhusbefore adoption698a1b8

lines 258–260why, one step back
258 const kill = getKillFunction(subprocess, {killDescendants: true});
259 assert.ok(kill('SIGTERM'));260 assert.equal(subprocess.killedWith, 'SIGTERM');

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

lines 261–262why, one step back
261});
262

TweaksSindre Sorhusbefore adoption698a1b8

line 263why, one step back
263test('taskkill fallback uses direct subprocess kill when taskkill cannot be spawned', async t => {

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

line 264why, one step back
264 const taskkillFailure = Promise.withResolvers();

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

line 265why, one step back
265 const getKillFunction = await fakeWindows(t, 'C:\\MissingWindows', (file, arguments_, callback) => {

TweaksSindre Sorhusbefore adoption698a1b8

lines 266–267why, one step back
266 assert.equal(file, path.join('C:\\MissingWindows', 'System32', 'taskkill.exe'));
267 assert.deepEqual(arguments_, ['/pid', '123', '/T', '/F']);

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

lines 268–271why, one step back
268 queueMicrotask(() => {
269 callback(new Error('spawn failed'));270 taskkillFailure.resolve();271 });

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

lines 272–273why, one step back
272 });
273 const subprocess = createFakeSubprocess();

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

line 274why, one step back
274

TweaksSindre Sorhusbefore adoption698a1b8

lines 275–277why, one step back
275 const kill = getKillFunction(subprocess, {killDescendants: true});
276 assert.ok(kill('SIGTERM'));277 assert.equal(subprocess.killedWith, undefined);

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

line 278why, one step back
278

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

line 279why, one step back
279 await taskkillFailure.promise;

TweaksSindre Sorhusbefore adoption698a1b8

line 280why, one step back
280 assert.equal(subprocess.killedWith, 'SIGTERM');

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

line 281why, one step back
281});

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

line 282why, one step back
282

TweaksSindre Sorhusbefore adoption698a1b8

lines 283–284why, one step back
283// On Windows, the PID is not reserved anymore once the subprocess exited, even while Execa is still waiting on it
284test('taskkill is not spawned once the subprocess exited', async t => {

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

lines 285–290why, one step back
285 let isTaskkillSpawned = false;
286 const getKillFunction = await fakeWindows(t, 'C:\\Windows', () => {287 isTaskkillSpawned = true;288 });289 const subprocess = createFakeSubprocess({exitCode: 0});290

TweaksSindre Sorhusbefore adoption698a1b8

lines 291–294why, one step back
291 const kill = getKillFunction(subprocess, {killDescendants: true});
292 assert.equal(kill('SIGTERM'), false);293 assert.equal(isTaskkillSpawned, false);294 assert.equal(subprocess.killedWith, undefined);

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

lines 295–297why, one step back
295});
296297// A subprocess which never spawned has no PID to signal

TweaksSindre Sorhusbefore adoption698a1b8

line 298why, one step back
298test('taskkill is not spawned when the subprocess has no PID', async t => {

Fix `killDescendants` signaling unrelated processes once Execa is done with the subprocessSindre Sorhusbefore adoption9c5c71c

lines 299–305why, one step back
299 let isTaskkillSpawned = false;
300 const getKillFunction = await fakeWindows(t, 'C:\\Windows', () => {301 isTaskkillSpawned = true;302 });303 const subprocess = createFakeSubprocess();304 subprocess.pid = undefined;305

TweaksSindre Sorhusbefore adoption698a1b8

lines 306–309why, one step back
306 const kill = getKillFunction(subprocess, {killDescendants: true});
307 assert.equal(kill('SIGTERM'), false);308 assert.equal(isTaskkillSpawned, false);309 assert.equal(subprocess.killedWith, undefined);

Improve `taskkill` usage (#1258)Sindre Sorhusbefore adoption3d9d820

lines 310–318why, one step back
310});
311312const restoreEnvironment = (name, value) => {313 if (value === undefined) {314 delete process.env[name];315 } else {316 process.env[name] = value;317 }318};