|
| 1 | +import childProcess from 'node:child_process'; |
| 2 | +import {syncBuiltinESMExports} from 'node:module'; |
| 3 | +import path from 'node:path/win32'; |
1 | 4 | import process from 'node:process'; |
2 | 5 | import {setTimeout} from 'node:timers/promises'; |
3 | 6 | import test from 'ava'; |
4 | 7 | import isRunning from 'is-running'; |
5 | 8 | import {execa, execaSync} from '../../index.js'; |
| 9 | +import {getTaskkillFile} from '../../lib/terminate/kill-descendants.js'; |
6 | 10 | import {setFixtureDirectory} from '../helpers/fixtures-directory.js'; |
7 | 11 |
|
8 | 12 | setFixtureDirectory(); |
@@ -88,3 +92,124 @@ test('Cannot use "killDescendants" option, sync', t => { |
88 | 92 | execaSync('empty.js', {killDescendants: true}); |
89 | 93 | }, {message: /The "killDescendants: true" option cannot be used/}); |
90 | 94 | }); |
| 95 | + |
| 96 | +test.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 | + }); |
| 102 | + |
| 103 | + process.env.SystemRoot = 'C:\\Windows'; |
| 104 | + process.env.windir = 'D:\\Windows'; |
| 105 | + t.is(getTaskkillFile(), path.join('C:\\Windows', 'System32', 'taskkill.exe')); |
| 106 | + |
| 107 | + process.env.SystemRoot = 'C:/Windows'; |
| 108 | + t.is(getTaskkillFile(), path.join('C:/Windows', 'System32', 'taskkill.exe')); |
| 109 | + |
| 110 | + process.env.SystemRoot = 'Windows'; |
| 111 | + t.is(getTaskkillFile(), path.join('D:\\Windows', 'System32', 'taskkill.exe')); |
| 112 | + |
| 113 | + process.env.SystemRoot = '\\Windows'; |
| 114 | + t.is(getTaskkillFile(), path.join('D:\\Windows', 'System32', 'taskkill.exe')); |
| 115 | + |
| 116 | + delete process.env.SystemRoot; |
| 117 | + t.is(getTaskkillFile(), path.join('D:\\Windows', 'System32', 'taskkill.exe')); |
| 118 | + |
| 119 | + process.env.windir = 'Windows'; |
| 120 | + t.is(getTaskkillFile(), undefined); |
| 121 | + |
| 122 | + process.env.windir = '\\Windows'; |
| 123 | + t.is(getTaskkillFile(), undefined); |
| 124 | + |
| 125 | + process.env.windir = '\\\\server\\share\\Windows'; |
| 126 | + t.is(getTaskkillFile(), undefined); |
| 127 | + |
| 128 | + process.env.windir = ''; |
| 129 | + t.is(getTaskkillFile(), undefined); |
| 130 | + |
| 131 | + delete process.env.windir; |
| 132 | + t.is(getTaskkillFile(), undefined); |
| 133 | +}); |
| 134 | + |
| 135 | +test.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 | + }); |
| 143 | + |
| 144 | + Object.defineProperty(process, 'platform', {value: 'win32'}); |
| 145 | + process.env.SystemRoot = 'Windows'; |
| 146 | + delete process.env.windir; |
| 147 | + |
| 148 | + 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 | + }; |
| 157 | + |
| 158 | + const kill = getKillFunction(subprocess, {killDescendants: true}); |
| 159 | + t.true(kill('SIGTERM')); |
| 160 | + t.is(killedWith, 'SIGTERM'); |
| 161 | +}); |
| 162 | + |
| 163 | +test.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 | + }); |
| 174 | + |
| 175 | + Object.defineProperty(process, 'platform', {value: 'win32'}); |
| 176 | + process.env.SystemRoot = 'C:\\MissingWindows'; |
| 177 | + delete process.env.windir; |
| 178 | + |
| 179 | + 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 | + }; |
| 188 | + |
| 189 | + syncBuiltinESMExports(); |
| 190 | + |
| 191 | + 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 | + }; |
| 200 | + |
| 201 | + const kill = getKillFunction(subprocess, {killDescendants: true}); |
| 202 | + t.true(kill('SIGTERM')); |
| 203 | + t.is(killedWith, undefined); |
| 204 | + |
| 205 | + await taskkillFailure.promise; |
| 206 | + t.is(killedWith, 'SIGTERM'); |
| 207 | +}); |
| 208 | + |
| 209 | +const restoreEnvironment = (name, value) => { |
| 210 | + if (value === undefined) { |
| 211 | + delete process.env[name]; |
| 212 | + } else { |
| 213 | + process.env[name] = value; |
| 214 | + } |
| 215 | +}; |
0 commit comments