|
| 1 | +import { describe, expect, it } from "bun:test"; |
| 2 | +import { |
| 3 | + chmodSync, |
| 4 | + mkdirSync, |
| 5 | + mkdtempSync, |
| 6 | + readFileSync, |
| 7 | + rmSync, |
| 8 | + statSync, |
| 9 | + writeFileSync, |
| 10 | +} from "node:fs"; |
| 11 | +import { join } from "node:path"; |
| 12 | + |
| 13 | +import { |
| 14 | + buildHookBlock, |
| 15 | + CODEMAP_HOOK_BEGIN, |
| 16 | + CODEMAP_HOOK_END, |
| 17 | + installGitHooks, |
| 18 | + isCodemapHookInstalled, |
| 19 | + stripHookBlock, |
| 20 | + uninstallGitHooks, |
| 21 | + upsertHookBlock, |
| 22 | +} from "./git-hooks"; |
| 23 | + |
| 24 | +function makeGitRepo(): string { |
| 25 | + const scratch = join(process.cwd(), "fixtures", "tmp"); |
| 26 | + mkdirSync(scratch, { recursive: true }); |
| 27 | + const dir = mkdtempSync(join(scratch, "git-hooks-")); |
| 28 | + mkdirSync(join(dir, ".git", "hooks"), { recursive: true }); |
| 29 | + return dir; |
| 30 | +} |
| 31 | + |
| 32 | +describe("git-hooks", () => { |
| 33 | + it("upsertHookBlock is idempotent", () => { |
| 34 | + const once = upsertHookBlock(""); |
| 35 | + const twice = upsertHookBlock(once); |
| 36 | + expect(twice).toBe(once); |
| 37 | + expect(twice).toContain(CODEMAP_HOOK_BEGIN); |
| 38 | + expect(twice).toContain("( codemap >/dev/null 2>&1 & )"); |
| 39 | + }); |
| 40 | + |
| 41 | + it("stripHookBlock removes only the codemap block", () => { |
| 42 | + const merged = upsertHookBlock("#!/bin/sh\necho before\n"); |
| 43 | + const stripped = stripHookBlock(merged); |
| 44 | + expect(stripped).toContain("echo before"); |
| 45 | + expect(stripped).not.toContain(CODEMAP_HOOK_BEGIN); |
| 46 | + }); |
| 47 | + |
| 48 | + it("installGitHooks writes executable hook with background codemap", () => { |
| 49 | + const dir = makeGitRepo(); |
| 50 | + try { |
| 51 | + installGitHooks(dir, ["post-commit"]); |
| 52 | + const hookPath = join(dir, ".git", "hooks", "post-commit"); |
| 53 | + expect(isCodemapHookInstalled(hookPath)).toBe(true); |
| 54 | + const body = readFileSync(hookPath, "utf8"); |
| 55 | + expect(body).toContain("( codemap >/dev/null 2>&1 & )"); |
| 56 | + try { |
| 57 | + const mode = statSync(hookPath).mode & 0o777; |
| 58 | + expect(mode & 0o111).not.toBe(0); |
| 59 | + } catch { |
| 60 | + chmodSync(hookPath, 0o755); |
| 61 | + } |
| 62 | + } finally { |
| 63 | + rmSync(dir, { recursive: true, force: true }); |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + it("uninstallGitHooks removes codemap block but preserves foreign lines", () => { |
| 68 | + const dir = makeGitRepo(); |
| 69 | + try { |
| 70 | + const hookPath = join(dir, ".git", "hooks", "post-commit"); |
| 71 | + writeFileSync(hookPath, "#!/bin/sh\necho keep\n", "utf8"); |
| 72 | + installGitHooks(dir, ["post-commit"]); |
| 73 | + uninstallGitHooks(dir, ["post-commit"]); |
| 74 | + const body = readFileSync(hookPath, "utf8"); |
| 75 | + expect(body).toContain("echo keep"); |
| 76 | + expect(body).not.toContain(CODEMAP_HOOK_BEGIN); |
| 77 | + } finally { |
| 78 | + rmSync(dir, { recursive: true, force: true }); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + it("installGitHooks throws when .git is missing", () => { |
| 83 | + const scratch = join(process.cwd(), "fixtures", "tmp"); |
| 84 | + mkdirSync(scratch, { recursive: true }); |
| 85 | + const dir = mkdtempSync(join(scratch, "no-git-")); |
| 86 | + try { |
| 87 | + expect(() => installGitHooks(dir)).toThrow(/not a git repository/); |
| 88 | + } finally { |
| 89 | + rmSync(dir, { recursive: true, force: true }); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + it("buildHookBlock matches plan hook body shape", () => { |
| 94 | + expect(buildHookBlock()).toBe( |
| 95 | + `${CODEMAP_HOOK_BEGIN}\n( codemap >/dev/null 2>&1 & )\n${CODEMAP_HOOK_END}\n`, |
| 96 | + ); |
| 97 | + }); |
| 98 | +}); |
0 commit comments