|
| 1 | +import { afterEach, expect, test } from "bun:test"; |
| 2 | +import { execFileSync } from "node:child_process"; |
| 3 | +import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join } from "node:path"; |
| 6 | + |
| 7 | +import { initTemporaryGitRepo } from "./temporary-git-repo.js"; |
| 8 | + |
| 9 | +const tempRoots: string[] = []; |
| 10 | + |
| 11 | +afterEach(() => { |
| 12 | + while (tempRoots.length > 0) { |
| 13 | + const dir = tempRoots.pop(); |
| 14 | + if (dir !== undefined) rmSync(dir, { recursive: true, force: true }); |
| 15 | + } |
| 16 | +}); |
| 17 | + |
| 18 | +function tempRoot(): string { |
| 19 | + const dir = mkdtempSync(join(tmpdir(), "corbits-temp-git-")); |
| 20 | + tempRoots.push(dir); |
| 21 | + return dir; |
| 22 | +} |
| 23 | + |
| 24 | +function rejectingHostConfig(root: string): { configPath: string; env: NodeJS.ProcessEnv } { |
| 25 | + const hooksDir = join(root, "host-hooks"); |
| 26 | + mkdirSync(hooksDir); |
| 27 | + const preCommit = join(hooksDir, "pre-commit"); |
| 28 | + writeFileSync(preCommit, "#!/bin/sh\necho 'host hook rejected fixture commit' >&2\nexit 1\n"); |
| 29 | + chmodSync(preCommit, 0o755); |
| 30 | + const configPath = join(root, "host-gitconfig"); |
| 31 | + writeFileSync(configPath, `[core]\n\thooksPath = ${hooksDir}\n`); |
| 32 | + return { |
| 33 | + configPath, |
| 34 | + env: { |
| 35 | + ...process.env, |
| 36 | + GIT_CONFIG_GLOBAL: configPath, |
| 37 | + GIT_CONFIG_NOSYSTEM: "1", |
| 38 | + }, |
| 39 | + }; |
| 40 | +} |
| 41 | + |
| 42 | +function gitStatus( |
| 43 | + cwd: string, |
| 44 | + args: string[], |
| 45 | + env: NodeJS.ProcessEnv, |
| 46 | +): { status: number; stderr: string } { |
| 47 | + const proc = Bun.spawnSync(["git", ...args], { cwd, env, stdout: "pipe", stderr: "pipe" }); |
| 48 | + return { |
| 49 | + status: proc.exitCode ?? 1, |
| 50 | + stderr: proc.stderr.toString(), |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +test("a naive fixture commit fails when the host config points at a rejecting hook", () => { |
| 55 | + const root = tempRoot(); |
| 56 | + const { env } = rejectingHostConfig(root); |
| 57 | + const repo = join(root, "naive"); |
| 58 | + mkdirSync(repo); |
| 59 | + execFileSync("git", ["init"], { cwd: repo, env, stdio: "ignore" }); |
| 60 | + execFileSync("git", ["config", "--local", "user.email", "t@t.test"], { |
| 61 | + cwd: repo, |
| 62 | + env, |
| 63 | + stdio: "ignore", |
| 64 | + }); |
| 65 | + execFileSync("git", ["config", "--local", "user.name", "t"], { |
| 66 | + cwd: repo, |
| 67 | + env, |
| 68 | + stdio: "ignore", |
| 69 | + }); |
| 70 | + const commit = gitStatus(repo, ["commit", "--allow-empty", "-m", "init"], env); |
| 71 | + expect(commit.status).not.toBe(0); |
| 72 | + expect(commit.stderr).toContain("host hook rejected fixture commit"); |
| 73 | +}); |
| 74 | + |
| 75 | +test("initTemporaryGitRepo fixture commit succeeds under a rejecting host hooksPath", () => { |
| 76 | + const root = tempRoot(); |
| 77 | + const { configPath, env } = rejectingHostConfig(root); |
| 78 | + const before = readFileSync(configPath, "utf8"); |
| 79 | + const repo = join(root, "hermetic"); |
| 80 | + mkdirSync(repo); |
| 81 | + |
| 82 | + initTemporaryGitRepo(repo); |
| 83 | + |
| 84 | + const commit = gitStatus(repo, ["commit", "--allow-empty", "-m", "init"], env); |
| 85 | + expect(commit.status).toBe(0); |
| 86 | + expect(commit.stderr).not.toContain("host hook rejected fixture commit"); |
| 87 | + execFileSync("git", ["rev-parse", "HEAD"], { cwd: repo, env, stdio: "ignore" }); |
| 88 | + expect(readFileSync(configPath, "utf8")).toBe(before); |
| 89 | +}); |
| 90 | + |
| 91 | +test("initTemporaryGitRepo sets local identity and core.hooksPath without touching global config", () => { |
| 92 | + const root = tempRoot(); |
| 93 | + const { configPath, env } = rejectingHostConfig(root); |
| 94 | + const before = readFileSync(configPath, "utf8"); |
| 95 | + const repo = join(root, "local-only"); |
| 96 | + mkdirSync(repo); |
| 97 | + |
| 98 | + initTemporaryGitRepo(repo); |
| 99 | + |
| 100 | + const email = execFileSync("git", ["config", "--local", "--get", "user.email"], { |
| 101 | + cwd: repo, |
| 102 | + env, |
| 103 | + encoding: "utf8", |
| 104 | + }).trim(); |
| 105 | + const name = execFileSync("git", ["config", "--local", "--get", "user.name"], { |
| 106 | + cwd: repo, |
| 107 | + env, |
| 108 | + encoding: "utf8", |
| 109 | + }).trim(); |
| 110 | + const hooksPath = execFileSync("git", ["config", "--local", "--get", "core.hooksPath"], { |
| 111 | + cwd: repo, |
| 112 | + env, |
| 113 | + encoding: "utf8", |
| 114 | + }).trim(); |
| 115 | + expect(email).toBe("t@t.test"); |
| 116 | + expect(name).toBe("t"); |
| 117 | + expect(hooksPath.length).toBeGreaterThan(0); |
| 118 | + expect(readFileSync(configPath, "utf8")).toBe(before); |
| 119 | +}); |
0 commit comments