windows-taskkill.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import type { ChildProcess } from "node:child_process";
  2. import { EventEmitter } from "node:events";
  3. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  4. const childProcessMocks = vi.hoisted(() => ({
  5. spawn: vi.fn(),
  6. spawnSync: vi.fn(),
  7. }));
  8. vi.mock("child_process", () => ({
  9. spawn: childProcessMocks.spawn,
  10. spawnSync: childProcessMocks.spawnSync,
  11. }));
  12. import {
  13. resolveWindowsTaskkillPath,
  14. spawnWindowsTaskkill,
  15. terminateProcessTreeGracefully,
  16. } from "../src/utils/shell.ts";
  17. function createFakeChild(): { child: ChildProcess; unref: ReturnType<typeof vi.fn> } {
  18. const child = new EventEmitter() as ChildProcess;
  19. const unref = vi.fn();
  20. child.unref = unref;
  21. return { child, unref };
  22. }
  23. describe("Windows taskkill process-tree termination", () => {
  24. beforeEach(() => {
  25. childProcessMocks.spawn.mockReset();
  26. vi.spyOn(process, "kill").mockReturnValue(true);
  27. });
  28. afterEach(() => {
  29. vi.useRealTimers();
  30. vi.restoreAllMocks();
  31. });
  32. it("resolves taskkill.exe from SystemRoot without depending on PATH", () => {
  33. expect(resolveWindowsTaskkillPath({ SystemRoot: "D:\\Windows", PATH: "C:\\tools" })).toBe(
  34. "D:\\Windows\\System32\\taskkill.exe",
  35. );
  36. expect(resolveWindowsTaskkillPath({ SYSTEMROOT: "E:\\WinNT" })).toBe("E:\\WinNT\\System32\\taskkill.exe");
  37. });
  38. it("falls back to taskkill.exe when Windows root variables are unavailable", () => {
  39. expect(resolveWindowsTaskkillPath({ PATH: "C:\\tools" })).toBe("taskkill.exe");
  40. });
  41. it("uses the absolute executable and protects the graceful error event", () => {
  42. const { child, unref } = createFakeChild();
  43. childProcessMocks.spawn.mockReturnValue(child);
  44. spawnWindowsTaskkill(4321, false, { SystemRoot: "C:\\Windows" });
  45. expect(childProcessMocks.spawn).toHaveBeenCalledWith(
  46. "C:\\Windows\\System32\\taskkill.exe",
  47. ["/T", "/PID", "4321"],
  48. {
  49. stdio: "ignore",
  50. detached: true,
  51. windowsHide: true,
  52. },
  53. );
  54. expect(unref).toHaveBeenCalledOnce();
  55. expect(() => child.emit("error", Object.assign(new Error("spawn failed"), { code: "ENOENT" }))).not.toThrow();
  56. expect(process.kill).toHaveBeenCalledWith(4321, "SIGKILL");
  57. });
  58. it("protects the forced taskkill path and falls back when spawn throws synchronously", () => {
  59. childProcessMocks.spawn.mockImplementation(() => {
  60. throw Object.assign(new Error("spawn failed"), { code: "ENOENT" });
  61. });
  62. expect(() => spawnWindowsTaskkill(9876, true, { windir: "C:\\Windows" })).not.toThrow();
  63. expect(childProcessMocks.spawn).toHaveBeenCalledWith(
  64. "C:\\Windows\\System32\\taskkill.exe",
  65. ["/F", "/T", "/PID", "9876"],
  66. {
  67. stdio: "ignore",
  68. detached: true,
  69. windowsHide: true,
  70. },
  71. );
  72. expect(process.kill).toHaveBeenCalledWith(9876, "SIGKILL");
  73. });
  74. it("does not rethrow when taskkill and the root-process fallback both fail", () => {
  75. const { child } = createFakeChild();
  76. childProcessMocks.spawn.mockReturnValue(child);
  77. vi.mocked(process.kill).mockImplementation(() => {
  78. throw Object.assign(new Error("kill failed"), { code: "ESRCH" });
  79. });
  80. spawnWindowsTaskkill(2468, true, {});
  81. expect(() => child.emit("error", Object.assign(new Error("spawn failed"), { code: "ENOENT" }))).not.toThrow();
  82. expect(process.kill).toHaveBeenCalledWith(2468, "SIGKILL");
  83. });
  84. it("keeps the forced /F /T escalation observable until its grace timer fires", async () => {
  85. vi.useFakeTimers();
  86. vi.spyOn(process, "platform", "get").mockReturnValue("win32");
  87. childProcessMocks.spawn.mockImplementation(() => createFakeChild().child);
  88. const termination = terminateProcessTreeGracefully(1357, 3000);
  89. let settled = false;
  90. void termination.settled.then(() => {
  91. settled = true;
  92. });
  93. expect(childProcessMocks.spawn.mock.calls[0]?.[1]).toEqual(["/T", "/PID", "1357"]);
  94. expect(settled).toBe(false);
  95. await vi.advanceTimersByTimeAsync(3000);
  96. expect(childProcessMocks.spawn.mock.calls[1]?.[1]).toEqual(["/F", "/T", "/PID", "1357"]);
  97. await termination.settled;
  98. expect(settled).toBe(true);
  99. });
  100. });