























@@ -15,6 +15,28 @@ import { createScriptTestHarness } from "./test-helpers.js";
15151616const { createTempDir } = createScriptTestHarness();
1717const posixIt = process.platform === "win32" ? it.skip : it;
18+const taskkillPath = path.win32.join("C:\\Windows", "System32", "taskkill.exe");
19+20+function restoreEnvValue(key: string, value: string | undefined): void {
21+if (value === undefined) {
22+delete process.env[key];
23+return;
24+}
25+process.env[key] = value;
26+}
27+28+function withDefaultWindowsSystemRoot(run: () => void): void {
29+const originalSystemRoot = process.env.SystemRoot;
30+const originalWindir = process.env.WINDIR;
31+try {
32+process.env.SystemRoot = "C:\\Windows";
33+delete process.env.WINDIR;
34+run();
35+} finally {
36+restoreEnvValue("SystemRoot", originalSystemRoot);
37+restoreEnvValue("WINDIR", originalWindir);
38+}
39+}
18401941function expectProcessPid(pid: number | undefined): number {
2042if (pid == null) {
@@ -112,52 +134,56 @@ describe("managed-child-process", () => {
112134});
113135114136it("signals Windows managed process trees with taskkill", () => {
115-const child = {
116-kill: vi.fn(),
117-pid: 12345,
118-};
119-const runTaskkill = vi.fn(() => ({ error: undefined, status: 0 }));
137+withDefaultWindowsSystemRoot(() => {
138+const child = {
139+kill: vi.fn(),
140+pid: 12345,
141+};
142+const runTaskkill = vi.fn(() => ({ error: undefined, status: 0 }));
120143121-terminateManagedChild(child, "SIGTERM", {
122-platform: "win32",
123- runTaskkill,
124-});
125-expect(runTaskkill).toHaveBeenNthCalledWith(1, "taskkill", ["/PID", "12345", "/T"], {
126-stdio: "ignore",
127-});
144+ terminateManagedChild(child, "SIGTERM", {
145+ platform: "win32",
146+ runTaskkill,
147+ });
148+ expect(runTaskkill).toHaveBeenNthCalledWith(1, taskkillPath, ["/PID", "12345", "/T"], {
149+ stdio: "ignore",
150+ });
128151129-terminateManagedChild(child, "SIGKILL", {
130-platform: "win32",
131- runTaskkill,
132-});
133-expect(runTaskkill).toHaveBeenNthCalledWith(2, "taskkill", ["/PID", "12345", "/T", "/F"], {
134-stdio: "ignore",
152+terminateManagedChild(child, "SIGKILL", {
153+platform: "win32",
154+ runTaskkill,
155+});
156+expect(runTaskkill).toHaveBeenNthCalledWith(2, taskkillPath, ["/PID", "12345", "/T", "/F"], {
157+stdio: "ignore",
158+});
159+expect(child.kill).not.toHaveBeenCalled();
135160});
136-expect(child.kill).not.toHaveBeenCalled();
137161});
138162139163it("force-kills Windows managed process trees when graceful taskkill fails", () => {
140-const child = {
141-kill: vi.fn(),
142-pid: 12345,
143-};
144-const runTaskkill = vi
145-.fn()
146-.mockReturnValueOnce({ error: undefined, status: 1 })
147-.mockReturnValueOnce({ error: undefined, status: 0 });
164+withDefaultWindowsSystemRoot(() => {
165+const child = {
166+kill: vi.fn(),
167+pid: 12345,
168+};
169+const runTaskkill = vi
170+.fn()
171+.mockReturnValueOnce({ error: undefined, status: 1 })
172+.mockReturnValueOnce({ error: undefined, status: 0 });
148173149-terminateManagedChild(child, "SIGTERM", {
150-platform: "win32",
151- runTaskkill,
152-});
174+ terminateManagedChild(child, "SIGTERM", {
175+ platform: "win32",
176+ runTaskkill,
177+ });
153178154-expect(runTaskkill).toHaveBeenNthCalledWith(1, "taskkill", ["/PID", "12345", "/T"], {
155-stdio: "ignore",
156-});
157-expect(runTaskkill).toHaveBeenNthCalledWith(2, "taskkill", ["/PID", "12345", "/T", "/F"], {
158-stdio: "ignore",
179+expect(runTaskkill).toHaveBeenNthCalledWith(1, taskkillPath, ["/PID", "12345", "/T"], {
180+stdio: "ignore",
181+});
182+expect(runTaskkill).toHaveBeenNthCalledWith(2, taskkillPath, ["/PID", "12345", "/T", "/F"], {
183+stdio: "ignore",
184+});
185+expect(child.kill).not.toHaveBeenCalled();
159186});
160-expect(child.kill).not.toHaveBeenCalled();
161187});
162188163189it("shares process signal listeners across parallel managed commands", async () => {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。