|
| 1 | +/** |
| 2 | + * `bin/git-push-scoped` must never write to global git config: not on a |
| 3 | + * normal push, not when two pushes race, and not when one is killed |
| 4 | + * mid-flight. Every test runs against an isolated HOME/GIT_CONFIG_GLOBAL |
| 5 | + * so a bug here can never touch the real machine's config. |
| 6 | + */ |
| 7 | + |
| 8 | +import { spawn } from "node:child_process"; |
| 9 | +import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync, chmodSync } from "node:fs"; |
| 10 | +import { tmpdir } from "node:os"; |
| 11 | +import { join } from "node:path"; |
| 12 | + |
| 13 | +import { afterEach, beforeEach, describe, expect, it } from "bun:test"; |
| 14 | + |
| 15 | +const SCRIPT = join(import.meta.dir, "../../bin/git-push-scoped"); |
| 16 | + |
| 17 | +let root: string; |
| 18 | +let globalConfigPath: string; |
| 19 | +let fakeBinDir: string; |
| 20 | +let env: NodeJS.ProcessEnv; |
| 21 | + |
| 22 | +function run(args: string[], opts: { cwd?: string } = {}) { |
| 23 | + return new Promise<{ code: number | null; signal: NodeJS.Signals | null }>((resolve) => { |
| 24 | + const child = spawn(SCRIPT, args, { cwd: opts.cwd ?? root, env, stdio: "ignore" }); |
| 25 | + child.on("exit", (code, signal) => resolve({ code, signal })); |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +function initBareRemote(name: string): string { |
| 30 | + const path = join(root, name); |
| 31 | + mkdirSync(path); |
| 32 | + spawnSync("git", ["init", "--bare", "-q", path]); |
| 33 | + return path; |
| 34 | +} |
| 35 | + |
| 36 | +// node:child_process.spawnSync is used only for synchronous test setup below. |
| 37 | +import { spawnSync } from "node:child_process"; |
| 38 | + |
| 39 | +function initWorkingRepo(name: string, remotePath: string): string { |
| 40 | + const path = join(root, name); |
| 41 | + mkdirSync(path); |
| 42 | + spawnSync("git", ["init", "-q", "-b", "main", path]); |
| 43 | + spawnSync("git", ["-C", path, "config", "user.email", "test@example.com"]); |
| 44 | + spawnSync("git", ["-C", path, "config", "user.name", "Test"]); |
| 45 | + writeFileSync(join(path, "file.txt"), name); |
| 46 | + spawnSync("git", ["-C", path, "add", "file.txt"]); |
| 47 | + spawnSync("git", ["-C", path, "commit", "-q", "-m", "initial commit"]); |
| 48 | + spawnSync("git", ["-C", path, "remote", "add", "origin", remotePath]); |
| 49 | + return path; |
| 50 | +} |
| 51 | + |
| 52 | +beforeEach(() => { |
| 53 | + root = mkdtempSync(join(tmpdir(), "git-push-scoped-")); |
| 54 | + |
| 55 | + // Isolated global config: a known sentinel that must never change. |
| 56 | + globalConfigPath = join(root, "global-gitconfig"); |
| 57 | + writeFileSync(globalConfigPath, "[user]\n\tname = Sentinel\n\temail = sentinel@example.com\n"); |
| 58 | + |
| 59 | + // Stub `gh` on PATH so the script's `command -v gh` check passes without |
| 60 | + // depending on a real GitHub CLI install or credentials. |
| 61 | + fakeBinDir = join(root, "fakebin"); |
| 62 | + mkdirSync(fakeBinDir); |
| 63 | + const ghStub = join(fakeBinDir, "gh"); |
| 64 | + writeFileSync(ghStub, "#!/usr/bin/env bash\nexit 0\n"); |
| 65 | + chmodSync(ghStub, 0o755); |
| 66 | + |
| 67 | + env = { |
| 68 | + ...process.env, |
| 69 | + HOME: root, |
| 70 | + GIT_CONFIG_GLOBAL: globalConfigPath, |
| 71 | + GIT_CONFIG_NOSYSTEM: "1", |
| 72 | + PATH: `${fakeBinDir}:${process.env.PATH}`, |
| 73 | + }; |
| 74 | +}); |
| 75 | + |
| 76 | +afterEach(() => { |
| 77 | + rmSync(root, { recursive: true, force: true }); |
| 78 | +}); |
| 79 | + |
| 80 | +describe("git-push-scoped", () => { |
| 81 | + it("pushes successfully without writing to global git config", async () => { |
| 82 | + const before = readFileSync(globalConfigPath, "utf8"); |
| 83 | + const remote = initBareRemote("remote.git"); |
| 84 | + const work = initWorkingRepo("work", remote); |
| 85 | + |
| 86 | + const { code } = await run(["origin", "main"], { cwd: work }); |
| 87 | + |
| 88 | + expect(code).toBe(0); |
| 89 | + expect(readFileSync(globalConfigPath, "utf8")).toBe(before); |
| 90 | + }); |
| 91 | + |
| 92 | + it("leaves global config untouched when two pushes race", async () => { |
| 93 | + const before = readFileSync(globalConfigPath, "utf8"); |
| 94 | + const remoteA = initBareRemote("remote-a.git"); |
| 95 | + const remoteB = initBareRemote("remote-b.git"); |
| 96 | + const workA = initWorkingRepo("work-a", remoteA); |
| 97 | + const workB = initWorkingRepo("work-b", remoteB); |
| 98 | + |
| 99 | + const [resultA, resultB] = await Promise.all([ |
| 100 | + run(["origin", "main"], { cwd: workA }), |
| 101 | + run(["origin", "main"], { cwd: workB }), |
| 102 | + ]); |
| 103 | + |
| 104 | + expect(resultA.code).toBe(0); |
| 105 | + expect(resultB.code).toBe(0); |
| 106 | + expect(readFileSync(globalConfigPath, "utf8")).toBe(before); |
| 107 | + }); |
| 108 | + |
| 109 | + it("leaves global config untouched when killed mid-flight", async () => { |
| 110 | + const before = readFileSync(globalConfigPath, "utf8"); |
| 111 | + const remote = initBareRemote("remote-slow.git"); |
| 112 | + const work = initWorkingRepo("work-slow", remote); |
| 113 | + |
| 114 | + // A pre-receive hook that sleeps gives us a reliable window to kill the |
| 115 | + // push while it is in flight. |
| 116 | + const hookPath = join(remote, "hooks", "pre-receive"); |
| 117 | + writeFileSync(hookPath, "#!/usr/bin/env bash\nsleep 5\n"); |
| 118 | + chmodSync(hookPath, 0o755); |
| 119 | + |
| 120 | + const child = spawn(SCRIPT, ["origin", "main"], { cwd: work, env, stdio: "ignore" }); |
| 121 | + const exited = new Promise<{ code: number | null; signal: NodeJS.Signals | null }>( |
| 122 | + (resolve) => { |
| 123 | + child.on("exit", (code, signal) => resolve({ code, signal })); |
| 124 | + }, |
| 125 | + ); |
| 126 | + |
| 127 | + await new Promise((resolve) => setTimeout(resolve, 300)); |
| 128 | + child.kill("SIGKILL"); |
| 129 | + const { signal } = await exited; |
| 130 | + |
| 131 | + expect(signal).toBe("SIGKILL"); |
| 132 | + expect(readFileSync(globalConfigPath, "utf8")).toBe(before); |
| 133 | + }); |
| 134 | +}); |
0 commit comments