gitCommandProvider.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { EventEmitter } from "node:events";
  2. import { PassThrough } from "node:stream";
  3. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  4. import type { GitEnvironmentProvider } from "../src/git/providers/gitEnvironmentProvider.js";
  5. const { spawnMock } = vi.hoisted(() => ({
  6. spawnMock: vi.fn(),
  7. }));
  8. vi.mock("node:child_process", () => ({
  9. spawn: spawnMock,
  10. }));
  11. import { createGitCommandProvider } from "../src/git/providers/gitCommandProvider.js";
  12. function createMockChild(pid = 12345) {
  13. const child = new EventEmitter() as EventEmitter & {
  14. stdout: PassThrough;
  15. stderr: PassThrough;
  16. kill: ReturnType<typeof vi.fn>;
  17. unref: ReturnType<typeof vi.fn>;
  18. pid: number;
  19. };
  20. child.stdout = new PassThrough();
  21. child.stderr = new PassThrough();
  22. child.kill = vi.fn(() => true);
  23. child.unref = vi.fn();
  24. child.pid = pid;
  25. return child;
  26. }
  27. function createEnvironmentProvider(binaryPath = "git"): GitEnvironmentProvider {
  28. return {
  29. resolveGitBinary: vi.fn().mockResolvedValue(binaryPath),
  30. createCommandEnv: vi.fn(() => ({})),
  31. };
  32. }
  33. beforeEach(() => {
  34. vi.useFakeTimers();
  35. vi.setSystemTime(new Date(0));
  36. spawnMock.mockReset();
  37. });
  38. afterEach(() => {
  39. vi.useRealTimers();
  40. vi.restoreAllMocks();
  41. });
  42. describe("gitCommandProvider timeout cleanup", () => {
  43. it("returns after bounded cleanup when a timed-out child never closes", async () => {
  44. const child = createMockChild();
  45. spawnMock.mockReturnValue(child);
  46. const provider = createGitCommandProvider({
  47. environmentProvider: createEnvironmentProvider(),
  48. platform: "darwin",
  49. timeoutKillGraceMs: 5,
  50. timeoutForceKillGraceMs: 5,
  51. });
  52. const resultPromise = provider.run({
  53. cwd: "/repo",
  54. args: ["status"],
  55. timeoutMs: 10,
  56. });
  57. await vi.advanceTimersByTimeAsync(10);
  58. expect(child.kill).toHaveBeenCalledWith();
  59. await vi.advanceTimersByTimeAsync(5);
  60. expect(child.kill).toHaveBeenCalledWith("SIGKILL");
  61. await vi.advanceTimersByTimeAsync(5);
  62. const result = await resultPromise;
  63. expect(result).toMatchObject({
  64. timedOut: true,
  65. timeoutMs: 10,
  66. timeoutElapsedMs: 10,
  67. timeoutCloseDelayMs: 10,
  68. forceKillAttempted: true,
  69. orphaned: true,
  70. durationMs: 20,
  71. });
  72. expect(child.unref).toHaveBeenCalledTimes(1);
  73. });
  74. it("uses taskkill to force-kill the process tree on Windows timeouts", async () => {
  75. const child = createMockChild(24680);
  76. const taskkillChild = createMockChild(13579);
  77. spawnMock.mockImplementation((command: string) => {
  78. return command === "taskkill" ? taskkillChild : child;
  79. });
  80. const provider = createGitCommandProvider({
  81. environmentProvider: createEnvironmentProvider(),
  82. platform: "win32",
  83. timeoutKillGraceMs: 5,
  84. timeoutForceKillGraceMs: 5,
  85. });
  86. const resultPromise = provider.run({
  87. cwd: "/repo",
  88. args: ["status"],
  89. timeoutMs: 10,
  90. });
  91. await vi.advanceTimersByTimeAsync(15);
  92. expect(spawnMock).toHaveBeenCalledWith(
  93. "taskkill",
  94. ["/PID", "24680", "/T", "/F"],
  95. {
  96. stdio: "ignore",
  97. windowsHide: true,
  98. },
  99. );
  100. taskkillChild.emit("close", 0, null);
  101. await vi.advanceTimersByTimeAsync(5);
  102. const result = await resultPromise;
  103. expect(result.timedOut).toBe(true);
  104. expect(result.forceKillAttempted).toBe(true);
  105. expect(result.orphaned).toBe(true);
  106. expect(child.kill).toHaveBeenCalledTimes(1);
  107. });
  108. it("uses the child close result when the process exits during the grace period", async () => {
  109. const child = createMockChild();
  110. spawnMock.mockReturnValue(child);
  111. const provider = createGitCommandProvider({
  112. environmentProvider: createEnvironmentProvider(),
  113. platform: "darwin",
  114. timeoutKillGraceMs: 5,
  115. timeoutForceKillGraceMs: 5,
  116. });
  117. const resultPromise = provider.run({
  118. cwd: "/repo",
  119. args: ["status"],
  120. timeoutMs: 10,
  121. });
  122. await vi.advanceTimersByTimeAsync(10);
  123. child.emit("close", null, "SIGTERM");
  124. const result = await resultPromise;
  125. expect(result).toMatchObject({
  126. timedOut: true,
  127. timeoutMs: 10,
  128. timeoutElapsedMs: 10,
  129. timeoutCloseDelayMs: 0,
  130. forceKillAttempted: false,
  131. orphaned: false,
  132. signal: "SIGTERM",
  133. durationMs: 10,
  134. });
  135. });
  136. });