mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 05:57:09 +01:00
The pre-push hook runs the unit suite with git's hook environment, and from a linked worktree GIT_DIR is absolute; an absolute GIT_DIR overrides 'git -C <sandbox>'. lane-base.ts (new on main today) made its throwaway repo that way. Pushing from a worktree on 2026-11-12, its 'init' commit (author test) replaced every file of the branch being pushed, and its git init and git config set core.bare, core.hooksPath /dev/null and user.name test on the real repository. The push's hook failed, so nothing reached GitHub. worktree-workspace.ts and the shell fixtures each guarded themselves; a new test did not. The runner now strips git's own repository variables (git rev-parse --local-env-vars) from every test it spawns (scripts/test/lib/test-env.ts). scripts/test/test-runner-git-env.ts replays the hazard between two throwaway repositories and checks the guard; run against lane-base.ts, an inherited GIT_DIR got its commit unguarded and nothing guarded. Claude-Session: https://claude.ai/code/session_019MDtmtCjm3CvrFm3kYaTgy Co-authored-by: Claude <noreply@anthropic.com>
66 lines
2.8 KiB
TypeScript
66 lines
2.8 KiB
TypeScript
/**
|
|
* The unit runner gives every test an environment with no pointer to a real
|
|
* repository (scripts/test/lib/test-env.ts).
|
|
*
|
|
* On 2026-11-12 the pre-push hook, pushing from a linked worktree, ran
|
|
* lane-base.ts with GIT_DIR set to the real repository. Its throwaway
|
|
* `git init`, `git config` and "init" commit landed in the real one. This
|
|
* replays the hazard between two throwaway repositories and checks the guard.
|
|
*/
|
|
import assert from "node:assert/strict";
|
|
import { execFileSync } from "node:child_process";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { testEnv } from "./lib/test-env";
|
|
|
|
const root = fs.mkdtempSync(path.join(os.tmpdir(), "loki-runner-git-env-"));
|
|
const real = path.join(root, "real");
|
|
const sandbox = path.join(root, "sandbox");
|
|
fs.mkdirSync(real);
|
|
fs.mkdirSync(sandbox);
|
|
|
|
// A clean environment for setting up the stand-in for the real repository,
|
|
// whatever this process inherited.
|
|
const base = testEnv(process.env);
|
|
const git = (env: NodeJS.ProcessEnv, dir: string, args: string[]) =>
|
|
execFileSync("git", ["-C", dir, ...args], { env, encoding: "utf8" }).trim();
|
|
git(base, real, ["init", "-q"]);
|
|
|
|
// What a hook hands down from a linked worktree: an absolute GIT_DIR.
|
|
const hooked = { ...base, GIT_DIR: path.join(real, ".git") };
|
|
|
|
// The hazard: the sandbox's own config write lands in the "real" repository.
|
|
git(hooked, sandbox, ["config", "user.name", "leaked"]);
|
|
assert.equal(
|
|
git(base, real, ["config", "--local", "--get", "user.name"]),
|
|
"leaked",
|
|
"with GIT_DIR inherited, `git -C <sandbox>` writes the real repository",
|
|
);
|
|
git(base, real, ["config", "--local", "--unset", "user.name"]);
|
|
|
|
// The guard: the same steps under the runner's environment stay in the sandbox.
|
|
const guarded = testEnv(hooked);
|
|
assert.equal(guarded.GIT_DIR, undefined, "GIT_DIR is not passed to a test");
|
|
git(guarded, sandbox, ["init", "-q"]);
|
|
git(guarded, sandbox, ["config", "user.name", "sandboxed"]);
|
|
assert.equal(git(base, sandbox, ["config", "--local", "--get", "user.name"]), "sandboxed");
|
|
assert.throws(
|
|
() => git(base, real, ["config", "--local", "--get", "user.name"]),
|
|
"the real repository is untouched",
|
|
);
|
|
assert.equal(git(base, real, ["config", "--local", "--get", "core.bare"]), "false");
|
|
|
|
for (const name of ["GIT_WORK_TREE", "GIT_INDEX_FILE", "GIT_COMMON_DIR"]) {
|
|
assert.equal(testEnv({ [name]: "/x", PATH: process.env.PATH })[name], undefined, name);
|
|
}
|
|
assert.equal(testEnv({ HOME: "/h", PATH: process.env.PATH }).HOME, "/h", "everything else stays");
|
|
|
|
// And the runner uses it for every test it spawns.
|
|
const runner = fs.readFileSync("scripts/test-unit.ts", "utf8");
|
|
assert.match(runner, /const CHILD_ENV = testEnv\(\);/);
|
|
assert.match(runner, /env: CHILD_ENV,/);
|
|
|
|
fs.rmSync(root, { recursive: true, force: true });
|
|
console.log("test-runner-git-env ok");
|