|
| 1 | +// The unedited-file fast path and merge temp-file cleanup, at the decideAndWrite level. |
| 2 | +// |
| 3 | +// An unedited file (on disk === .gen-state snapshot) must take the fresh output |
| 4 | +// without spawning `git merge-file`, and advance the snapshot and its hash as a clean |
| 5 | +// merge does; an edited file still merges or conflicts as before; and no merge input |
| 6 | +// file may outlive its merge, including when the merge throws. |
| 7 | +// |
| 8 | +// "Never spawns git" is proven by pointing META_GEN_GIT at a binary that does not |
| 9 | +// exist: any spawn would throw GitMissingError. Leftover temporaries are read from |
| 10 | +// mergeTempDir(), pointed at a private directory through DEV_SCRATCH. |
| 11 | + |
| 12 | +import { describe, test, expect, beforeEach, afterEach } from "bun:test"; |
| 13 | +import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 14 | +import { tmpdir } from "node:os"; |
| 15 | +import { join } from "node:path"; |
| 16 | +import { |
| 17 | + contentHash, |
| 18 | + decideAndWrite, |
| 19 | + GitMissingError, |
| 20 | + mergeTempDir, |
| 21 | + readGeneratedHash, |
| 22 | +} from "../../src/overwrite-policy.js"; |
| 23 | + |
| 24 | +const NO_GIT = "/usr/bin/totally-not-a-real-binary-xyz123"; |
| 25 | + |
| 26 | +let root: string; |
| 27 | +let out: string; |
| 28 | +let state: string; |
| 29 | +let prevGit: string | undefined; |
| 30 | +let prevScratch: string | undefined; |
| 31 | + |
| 32 | +beforeEach(() => { |
| 33 | + root = mkdtempSync(join(process.env.DEV_SCRATCH || tmpdir(), "codegen-fastpath-")); |
| 34 | + out = join(root, "out"); |
| 35 | + state = join(root, "state"); |
| 36 | + prevGit = process.env.META_GEN_GIT; |
| 37 | + prevScratch = process.env.DEV_SCRATCH; |
| 38 | + process.env.DEV_SCRATCH = join(root, "scratch"); |
| 39 | +}); |
| 40 | + |
| 41 | +afterEach(() => { |
| 42 | + if (prevGit === undefined) delete process.env.META_GEN_GIT; |
| 43 | + else process.env.META_GEN_GIT = prevGit; |
| 44 | + if (prevScratch === undefined) delete process.env.DEV_SCRATCH; |
| 45 | + else process.env.DEV_SCRATCH = prevScratch; |
| 46 | + rmSync(root, { recursive: true, force: true }); |
| 47 | +}); |
| 48 | + |
| 49 | +const opts = () => ({ genStateDir: state, outputRelPath: "User.ts" }); |
| 50 | +const userPath = () => join(out, "User.ts"); |
| 51 | +const tmpLeft = () => (existsSync(mergeTempDir()) ? readdirSync(mergeTempDir()) : []); |
| 52 | + |
| 53 | +describe("unedited file fast path", () => { |
| 54 | + test("never spawns git merge-file and gets the fresh content, snapshot and hash advanced", () => { |
| 55 | + decideAndWrite(userPath(), "v1\n", opts()); |
| 56 | + process.env.META_GEN_GIT = NO_GIT; |
| 57 | + |
| 58 | + const result = decideAndWrite(userPath(), "v2\n", opts()); |
| 59 | + |
| 60 | + expect(result.status).toBe("overwrite"); |
| 61 | + expect(readFileSync(userPath(), "utf-8")).toBe("v2\n"); |
| 62 | + expect(readFileSync(join(state, "User.ts"), "utf-8")).toBe("v2\n"); |
| 63 | + expect(readGeneratedHash(state, "User.ts")).toBe(contentHash("v2\n")); |
| 64 | + expect(tmpLeft()).toEqual([]); |
| 65 | + }); |
| 66 | +}); |
| 67 | + |
| 68 | +describe("edited file still merges", () => { |
| 69 | + const base = "line1\nline2\nline3\nline4\nline5\n"; |
| 70 | + |
| 71 | + test("a non-overlapping edit merges cleanly, and no temp file remains", () => { |
| 72 | + decideAndWrite(userPath(), base, opts()); |
| 73 | + writeFileSync(userPath(), "line1 EDITED\nline2\nline3\nline4\nline5\n"); |
| 74 | + |
| 75 | + const result = decideAndWrite(userPath(), "line1\nline2\nline3\nline4\nline5 FRESH\n", opts()); |
| 76 | + |
| 77 | + expect(result.status).toBe("merged"); |
| 78 | + expect(readFileSync(userPath(), "utf-8")).toBe("line1 EDITED\nline2\nline3\nline4\nline5 FRESH\n"); |
| 79 | + expect(readFileSync(join(state, "User.ts"), "utf-8")).toBe("line1\nline2\nline3\nline4\nline5 FRESH\n"); |
| 80 | + expect(tmpLeft()).toEqual([]); |
| 81 | + }); |
| 82 | + |
| 83 | + test("an overlapping edit conflicts without advancing the snapshot, and no temp file remains", () => { |
| 84 | + decideAndWrite(userPath(), base, opts()); |
| 85 | + writeFileSync(userPath(), "line1 MINE\nline2\nline3\nline4\nline5\n"); |
| 86 | + |
| 87 | + const result = decideAndWrite(userPath(), "line1 THEIRS\nline2\nline3\nline4\nline5\n", opts()); |
| 88 | + |
| 89 | + expect(result.status).toBe("conflict"); |
| 90 | + expect(readFileSync(userPath(), "utf-8")).toContain("<<<<<<<"); |
| 91 | + expect(readFileSync(join(state, "User.ts"), "utf-8")).toBe(base); |
| 92 | + expect(tmpLeft()).toEqual([]); |
| 93 | + }); |
| 94 | + |
| 95 | + test("a merge that throws leaves no temp file behind", () => { |
| 96 | + decideAndWrite(userPath(), base, opts()); |
| 97 | + writeFileSync(userPath(), "edited\n"); |
| 98 | + process.env.META_GEN_GIT = NO_GIT; |
| 99 | + |
| 100 | + expect(() => decideAndWrite(userPath(), "fresh\n", opts())).toThrow(GitMissingError); |
| 101 | + expect(tmpLeft()).toEqual([]); |
| 102 | + }); |
| 103 | +}); |
0 commit comments