




















@@ -12,6 +12,29 @@ import {
1212signalRunWithEnvChild,
1313} from "../../scripts/run-with-env.mjs";
141415+const taskkillPath = path.win32.join("C:\\Windows", "System32", "taskkill.exe");
16+17+function restoreEnvValue(key: string, value: string | undefined): void {
18+if (value === undefined) {
19+delete process.env[key];
20+return;
21+}
22+process.env[key] = value;
23+}
24+25+function withDefaultWindowsSystemRoot(run: () => void): void {
26+const originalSystemRoot = process.env.SystemRoot;
27+const originalWindir = process.env.WINDIR;
28+try {
29+process.env.SystemRoot = "C:\\Windows";
30+delete process.env.WINDIR;
31+run();
32+} finally {
33+restoreEnvValue("SystemRoot", originalSystemRoot);
34+restoreEnvValue("WINDIR", originalWindir);
35+}
36+}
37+1538async function waitFor(predicate: () => boolean, label: string, timeoutMs = 3_000): Promise<void> {
1639const startedAt = Date.now();
1740while (!predicate()) {
@@ -160,52 +183,56 @@ describe("run-with-env", () => {
160183});
161184162185it("signals Windows wrapped command trees with taskkill", () => {
163-const child = {
164-kill: vi.fn(),
165-pid: 12345,
166-};
167-const runTaskkill = vi.fn(() => ({ error: undefined, status: 0 }));
168-169-signalRunWithEnvChild(child, "SIGTERM", {
170-platform: "win32",
171- runTaskkill,
186+withDefaultWindowsSystemRoot(() => {
187+const child = {
188+kill: vi.fn(),
189+pid: 12345,
190+};
191+const runTaskkill = vi.fn(() => ({ error: undefined, status: 0 }));
192+193+signalRunWithEnvChild(child, "SIGTERM", {
194+platform: "win32",
195+ runTaskkill,
196+});
197+expect(runTaskkill).toHaveBeenNthCalledWith(1, taskkillPath, ["/PID", "12345", "/T"], {
198+stdio: "ignore",
199+});
200+201+signalRunWithEnvChild(child, "SIGKILL", {
202+platform: "win32",
203+ runTaskkill,
204+});
205+expect(runTaskkill).toHaveBeenNthCalledWith(2, taskkillPath, ["/PID", "12345", "/T", "/F"], {
206+stdio: "ignore",
207+});
208+expect(child.kill).not.toHaveBeenCalled();
172209});
173-expect(runTaskkill).toHaveBeenNthCalledWith(1, "taskkill", ["/PID", "12345", "/T"], {
174-stdio: "ignore",
175-});
176-177-signalRunWithEnvChild(child, "SIGKILL", {
178-platform: "win32",
179- runTaskkill,
180-});
181-expect(runTaskkill).toHaveBeenNthCalledWith(2, "taskkill", ["/PID", "12345", "/T", "/F"], {
182-stdio: "ignore",
183-});
184-expect(child.kill).not.toHaveBeenCalled();
185210});
186211187212it("force-kills Windows wrapped command trees when graceful taskkill fails", () => {
188-const child = {
189-kill: vi.fn(),
190-pid: 12345,
191-};
192-const runTaskkill = vi
193-.fn()
194-.mockReturnValueOnce({ error: undefined, status: 1 })
195-.mockReturnValueOnce({ error: undefined, status: 0 });
196-197-signalRunWithEnvChild(child, "SIGTERM", {
198-platform: "win32",
199- runTaskkill,
200-});
201-202-expect(runTaskkill).toHaveBeenNthCalledWith(1, "taskkill", ["/PID", "12345", "/T"], {
203-stdio: "ignore",
204-});
205-expect(runTaskkill).toHaveBeenNthCalledWith(2, "taskkill", ["/PID", "12345", "/T", "/F"], {
206-stdio: "ignore",
213+withDefaultWindowsSystemRoot(() => {
214+const child = {
215+kill: vi.fn(),
216+pid: 12345,
217+};
218+const runTaskkill = vi
219+.fn()
220+.mockReturnValueOnce({ error: undefined, status: 1 })
221+.mockReturnValueOnce({ error: undefined, status: 0 });
222+223+signalRunWithEnvChild(child, "SIGTERM", {
224+platform: "win32",
225+ runTaskkill,
226+});
227+228+expect(runTaskkill).toHaveBeenNthCalledWith(1, taskkillPath, ["/PID", "12345", "/T"], {
229+stdio: "ignore",
230+});
231+expect(runTaskkill).toHaveBeenNthCalledWith(2, taskkillPath, ["/PID", "12345", "/T", "/F"], {
232+stdio: "ignore",
233+});
234+expect(child.kill).not.toHaveBeenCalled();
207235});
208-expect(child.kill).not.toHaveBeenCalled();
209236});
210237211238it.runIf(process.platform !== "win32").each(["SIGTERM", "SIGHUP", "SIGINT"] as const)(
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。