|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +import { collectGitDiffsWithCli, splitGitDiff } from "./gitDiffFallback"; |
| 4 | + |
| 5 | +describe("splitGitDiff", () => { |
| 6 | + it("splits combined git diff output into per-file entries", () => { |
| 7 | + const combined = [ |
| 8 | + "diff --git a/a.txt b/a.txt\n+hello", |
| 9 | + "diff --git a/b.txt b/b.txt\n+world", |
| 10 | + ].join("\n"); |
| 11 | + |
| 12 | + expect(splitGitDiff(combined)).toEqual([ |
| 13 | + "diff --git a/a.txt b/a.txt\n+hello\n", |
| 14 | + "diff --git a/b.txt b/b.txt\n+world", |
| 15 | + ]); |
| 16 | + }); |
| 17 | +}); |
| 18 | + |
| 19 | +describe("collectGitDiffsWithCli", () => { |
| 20 | + it("falls back to git CLI and returns staged and unstaged diffs", async () => { |
| 21 | + const execFn = vi.fn(async (command: string, options: { cwd: string }) => { |
| 22 | + if (command === "git rev-parse --show-toplevel") { |
| 23 | + return { stdout: "/repo\n" }; |
| 24 | + } |
| 25 | + if (command === "git diff --cached") { |
| 26 | + expect(options.cwd).toBe("/repo"); |
| 27 | + return { stdout: "diff --git a/staged.ts b/staged.ts\n+staged\n" }; |
| 28 | + } |
| 29 | + if (command === "git diff") { |
| 30 | + expect(options.cwd).toBe("/repo"); |
| 31 | + return { stdout: "diff --git a/unstaged.ts b/unstaged.ts\n+unstaged\n" }; |
| 32 | + } |
| 33 | + throw new Error("unexpected command: " + command); |
| 34 | + }); |
| 35 | + |
| 36 | + await expect( |
| 37 | + collectGitDiffsWithCli(["/repo", "/repo/subdir"], true, execFn), |
| 38 | + ).resolves.toEqual([ |
| 39 | + "diff --git a/staged.ts b/staged.ts\n+staged\n", |
| 40 | + "diff --git a/unstaged.ts b/unstaged.ts\n+unstaged\n", |
| 41 | + ]); |
| 42 | + |
| 43 | + expect(execFn).toHaveBeenCalledTimes(4); |
| 44 | + }); |
| 45 | + |
| 46 | + it("skips non-git directories and only uses staged diff when requested", async () => { |
| 47 | + const execFn = vi.fn(async (command: string, options: { cwd: string }) => { |
| 48 | + if ( |
| 49 | + command === "git rev-parse --show-toplevel" && |
| 50 | + options.cwd === "/not-a-repo" |
| 51 | + ) { |
| 52 | + throw new Error("fatal: not a git repository"); |
| 53 | + } |
| 54 | + if (command === "git rev-parse --show-toplevel" && options.cwd === "/repo") { |
| 55 | + return { stdout: "/repo\n" }; |
| 56 | + } |
| 57 | + if (command === "git diff --cached") { |
| 58 | + return { stdout: "diff --git a/file.ts b/file.ts\n+only-staged\n" }; |
| 59 | + } |
| 60 | + throw new Error("unexpected command: " + command); |
| 61 | + }); |
| 62 | + |
| 63 | + await expect( |
| 64 | + collectGitDiffsWithCli(["/not-a-repo", "/repo"], false, execFn), |
| 65 | + ).resolves.toEqual(["diff --git a/file.ts b/file.ts\n+only-staged\n"]); |
| 66 | + |
| 67 | + expect(execFn).toHaveBeenCalledTimes(3); |
| 68 | + }); |
| 69 | +}); |
0 commit comments