Skip to content
Myr

Improve `taskkill` usage (#1258)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sindre Sorhus · committed by GitHub · parent 5944b2d · Browse files · 2 files+156 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+156 added−6 removed

  • tests:+125 added−0 removed
  • other:+31 added−6 removed

2 files+156 added−6 removed

modified:lib/terminate/kill-descendants.js+31 added-6 removed
modified:test/terminate/kill-descendants.js+125 added
1import childProcess from 'node:child_process';2import {syncBuiltinESMExports} from 'node:module';3import path from 'node:path/win32';4import process from 'node:process';5import {setTimeout} from 'node:timers/promises';6import test from 'ava';7import isRunning from 'is-running';8import {execa, execaSync} from '../../index.js';9import {getTaskkillFile} from '../../lib/terminate/kill-descendants.js';10import {setFixtureDirectory} from '../helpers/fixtures-directory.js';1112setFixtureDirectory();13
77 unmodified lines
91 t.throws(() => {92 execaSync('empty.js', {killDescendants: true});93 }, {message: /The "killDescendants: true" option cannot be used/});94});9596test.serial('taskkill is resolved from the Windows directory when available', t => {97 const {SystemRoot, windir} = process.env;98 t.teardown(() => {99 restoreEnvironment('SystemRoot', SystemRoot);100 restoreEnvironment('windir', windir);101 });102103 process.env.SystemRoot = 'C:\\Windows';104 process.env.windir = 'D:\\Windows';105 t.is(getTaskkillFile(), path.join('C:\\Windows', 'System32', 'taskkill.exe'));106107 process.env.SystemRoot = 'C:/Windows';108 t.is(getTaskkillFile(), path.join('C:/Windows', 'System32', 'taskkill.exe'));109110 process.env.SystemRoot = 'Windows';111 t.is(getTaskkillFile(), path.join('D:\\Windows', 'System32', 'taskkill.exe'));112113 process.env.SystemRoot = '\\Windows';114 t.is(getTaskkillFile(), path.join('D:\\Windows', 'System32', 'taskkill.exe'));115116 delete process.env.SystemRoot;117 t.is(getTaskkillFile(), path.join('D:\\Windows', 'System32', 'taskkill.exe'));118119 process.env.windir = 'Windows';120 t.is(getTaskkillFile(), undefined);121122 process.env.windir = '\\Windows';123 t.is(getTaskkillFile(), undefined);124125 process.env.windir = '\\\\server\\share\\Windows';126 t.is(getTaskkillFile(), undefined);127128 process.env.windir = '';129 t.is(getTaskkillFile(), undefined);130131 delete process.env.windir;132 t.is(getTaskkillFile(), undefined);133});134135test.serial('taskkill fallback uses direct subprocess kill when Windows directory is unavailable', async t => {136 const platformDescriptor = Object.getOwnPropertyDescriptor(process, 'platform');137 const {SystemRoot, windir} = process.env;138 t.teardown(() => {139 Object.defineProperty(process, 'platform', platformDescriptor);140 restoreEnvironment('SystemRoot', SystemRoot);141 restoreEnvironment('windir', windir);142 });143144 Object.defineProperty(process, 'platform', {value: 'win32'});145 process.env.SystemRoot = 'Windows';146 delete process.env.windir;147148 const {getKillFunction} = await import(`../../lib/terminate/kill-descendants.js?taskkill-fallback=${Date.now()}`);149 let killedWith;150 const subprocess = {151 pid: 123,152 kill(signal) {153 killedWith = signal;154 return true;155 },156 };157158 const kill = getKillFunction(subprocess, {killDescendants: true});159 t.true(kill('SIGTERM'));160 t.is(killedWith, 'SIGTERM');161});162163test.serial('taskkill fallback uses direct subprocess kill when taskkill cannot be spawned', async t => {164 const platformDescriptor = Object.getOwnPropertyDescriptor(process, 'platform');165 const originalExecFile = childProcess.execFile;166 const {SystemRoot, windir} = process.env;167 t.teardown(() => {168 Object.defineProperty(process, 'platform', platformDescriptor);169 childProcess.execFile = originalExecFile;170 syncBuiltinESMExports();171 restoreEnvironment('SystemRoot', SystemRoot);172 restoreEnvironment('windir', windir);173 });174175 Object.defineProperty(process, 'platform', {value: 'win32'});176 process.env.SystemRoot = 'C:\\MissingWindows';177 delete process.env.windir;178179 const taskkillFailure = Promise.withResolvers();180 childProcess.execFile = (file, arguments_, callback) => {181 t.is(file, path.join('C:\\MissingWindows', 'System32', 'taskkill.exe'));182 t.deepEqual(arguments_, ['/pid', '123', '/T', '/F']);183 queueMicrotask(() => {184 callback(new Error('spawn failed'));185 taskkillFailure.resolve();186 });187 };188189 syncBuiltinESMExports();190191 const {getKillFunction} = await import(`../../lib/terminate/kill-descendants.js?taskkill-spawn-fallback=${Date.now()}`);192 let killedWith;193 const subprocess = {194 pid: 123,195 kill(signal) {196 killedWith = signal;197 return true;198 },199 };200201 const kill = getKillFunction(subprocess, {killDescendants: true});202 t.true(kill('SIGTERM'));203 t.is(killedWith, undefined);204205 await taskkillFailure.promise;206 t.is(killedWith, 'SIGTERM');207});208209const restoreEnvironment = (name, value) => {210 if (value === undefined) {211 delete process.env[name];212 } else {213 process.env[name] = value;214 }215};