|
| 1 | +import { assert, beforeEach, describe, it, vi } from "vitest"; |
| 2 | + |
| 3 | +import type { ProcessRunOptions, ProcessRunResult } from "./processRunner"; |
| 4 | + |
| 5 | +const { runProcessMock } = vi.hoisted(() => ({ |
| 6 | + runProcessMock: |
| 7 | + vi.fn< |
| 8 | + ( |
| 9 | + command: string, |
| 10 | + args: readonly string[], |
| 11 | + options?: ProcessRunOptions, |
| 12 | + ) => Promise<ProcessRunResult> |
| 13 | + >(), |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock("./processRunner", () => ({ |
| 17 | + runProcess: runProcessMock, |
| 18 | +})); |
| 19 | + |
| 20 | +function processResult( |
| 21 | + overrides: Partial<ProcessRunResult> & Pick<ProcessRunResult, "stdout" | "code">, |
| 22 | +): ProcessRunResult { |
| 23 | + return { |
| 24 | + stdout: overrides.stdout, |
| 25 | + code: overrides.code, |
| 26 | + stderr: overrides.stderr ?? "", |
| 27 | + signal: overrides.signal ?? null, |
| 28 | + timedOut: overrides.timedOut ?? false, |
| 29 | + stdoutTruncated: overrides.stdoutTruncated ?? false, |
| 30 | + stderrTruncated: overrides.stderrTruncated ?? false, |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +describe("gitIgnore", () => { |
| 35 | + beforeEach(() => { |
| 36 | + runProcessMock.mockReset(); |
| 37 | + vi.resetModules(); |
| 38 | + }); |
| 39 | + |
| 40 | + it("chunks large git check-ignore requests and filters ignored matches", async () => { |
| 41 | + const ignoredPaths = Array.from( |
| 42 | + { length: 320 }, |
| 43 | + (_, index) => `ignored/${index.toString().padStart(4, "0")}/${"x".repeat(1024)}.ts`, |
| 44 | + ); |
| 45 | + const keptPaths = ["src/keep.ts", "docs/readme.md"]; |
| 46 | + const relativePaths = [...ignoredPaths, ...keptPaths]; |
| 47 | + let checkIgnoreCalls = 0; |
| 48 | + |
| 49 | + runProcessMock.mockImplementation(async (_command, args, options) => { |
| 50 | + if (args[0] === "check-ignore") { |
| 51 | + checkIgnoreCalls += 1; |
| 52 | + const chunkPaths = (options?.stdin ?? "").split("\0").filter((value) => value.length > 0); |
| 53 | + const chunkIgnored = chunkPaths.filter((value) => value.startsWith("ignored/")); |
| 54 | + return processResult({ |
| 55 | + code: chunkIgnored.length > 0 ? 0 : 1, |
| 56 | + stdout: chunkIgnored.length > 0 ? `${chunkIgnored.join("\0")}\0` : "", |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + throw new Error(`Unexpected command: git ${args.join(" ")}`); |
| 61 | + }); |
| 62 | + |
| 63 | + const { filterGitIgnoredPaths } = await import("./gitIgnore"); |
| 64 | + const result = await filterGitIgnoredPaths("/virtual/workspace", relativePaths); |
| 65 | + |
| 66 | + assert.isAbove(checkIgnoreCalls, 1); |
| 67 | + assert.deepEqual(result, keptPaths); |
| 68 | + }); |
| 69 | + |
| 70 | + it("fails open when git check-ignore cannot complete", async () => { |
| 71 | + const relativePaths = ["src/keep.ts", "ignored.txt"]; |
| 72 | + |
| 73 | + runProcessMock.mockRejectedValueOnce(new Error("spawn failed")); |
| 74 | + |
| 75 | + const { filterGitIgnoredPaths } = await import("./gitIgnore"); |
| 76 | + const result = await filterGitIgnoredPaths("/virtual/workspace", relativePaths); |
| 77 | + |
| 78 | + assert.deepEqual(result, relativePaths); |
| 79 | + }); |
| 80 | +}); |
0 commit comments