diff --git a/docs/internal/agent-rules/toolchain.md b/docs/internal/agent-rules/toolchain.md index e91edab39..946f7715b 100644 --- a/docs/internal/agent-rules/toolchain.md +++ b/docs/internal/agent-rules/toolchain.md @@ -57,7 +57,9 @@ list and this one the same. e2e typecheck. A test that reads the repository's files instead of importing code is named `*.repo.test.ts` and runs in about a second: `pnpm test:changed` selects it only when the branch edits the test - itself, so the hook runs every one. + itself, so the hook runs every one, except on `bug/-repro` and + `wip/-repro`, where a triage's reproduction fails by design; CI runs + them all. - **CI:** what `pnpm check` runs, and Fallow, on every push. CI's Fallow (`fallow --ci`) checks the whole tree and fails on any dead-code finding or clone group, `warn` rules included; complexity fails above the diff --git a/lefthook.yml b/lefthook.yml index e1bd3cd63..25427fb5b 100644 --- a/lefthook.yml +++ b/lefthook.yml @@ -57,6 +57,10 @@ pre-push: # never selects them and any change can break them; together they take about a second. repo-tests: priority: 1 + # why: a triage branch pushes a reproduction that fails by design (delivery rule 8); CI runs every repo test. + skip: + - ref: bug/*-repro + - ref: wip/*-repro run: pnpm exec vitest run --project unit .repo.test.ts fail_text: "A test of the repository's own files fails (a size cap, the ADR index, a doc's audience): the output names the file and the rule." protocol-bump: diff --git a/src/push-hook.repo.test.ts b/src/push-hook.repo.test.ts new file mode 100644 index 000000000..47528761c --- /dev/null +++ b/src/push-hook.repo.test.ts @@ -0,0 +1,73 @@ +import { execFileSync, spawnSync } from "node:child_process"; +import { + chmodSync, + existsSync, + mkdirSync, + mkdtempSync, + readFileSync, + writeFileSync, +} from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; + +import { describe, expect, it } from "vitest"; + +const ROOT = join(dirname(fileURLToPath(import.meta.url)), ".."); +const LEFTHOOK = join(ROOT, "node_modules", ".bin", "lefthook"); + +// why: the stand-in pnpm fails as `vitest run` does when the branch carries a failing +// *.repo.test.ts, so the step's exit is the hook's verdict on such a push. +function pushRepoTests(branch: string) { + // why: a git hook runs with GIT_DIR set, which would aim the scratch repo's git calls at this checkout. + for (const name of Object.keys(process.env)) { + if (name.startsWith("GIT_")) delete process.env[name]; + } + const repo = mkdtempSync(join(tmpdir(), "push-hook-")); + execFileSync("git", ["init", "-q", "-b", branch, repo]); + execFileSync( + "git", + ["-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "--allow-empty", "-m", "x"], + { + cwd: repo, + }, + ); + // why: `lefthook dump` drops a skip it has judged against this checkout's branch, and `--command` ignores one, so the + // scratch repo gets the step's own block from lefthook.yml and the hook runs there on the scratch branch. + const block = /^ {4}repo-tests:\n(?: {5,}.*\n|\s*\n)*/m.exec( + readFileSync(join(ROOT, "lefthook.yml"), "utf8"), + ); + writeFileSync(join(repo, "lefthook.yml"), `pre-push:\n commands:\n${block?.[0]}`); + const bin = join(repo, ".bin"); + mkdirSync(bin); + const calls = join(repo, "pnpm-calls"); + writeFileSync(join(bin, "pnpm"), `#!/bin/sh\necho "$@" >> "${calls}"\nexit 1\n`); + chmodSync(join(bin, "pnpm"), 0o755); + const { LEFTHOOK: _off, LEFTHOOK_EXCLUDE: _excluded, ...inherited } = process.env; + const env = { ...inherited, PATH: `${bin}:${process.env.PATH}` }; + const result = spawnSync( + LEFTHOOK, + ["run", "pre-push", "--force", "--no-auto-install", "--no-tty"], + { cwd: repo, env, encoding: "utf8" }, + ); + return { + status: result.status, + pnpm: existsSync(calls) ? readFileSync(calls, "utf8") : null, + }; +} + +describe("the push hook's repo-tests step", () => { + it.each(["bug/7-repro", "wip/7-repro"])( + "does not run the repo tests on %s, whose reproduction fails by design", + (branch) => { + expect(pushRepoTests(branch)).toEqual({ status: 0, pnpm: null }); + }, + ); + + it("runs every repo test on any other branch and refuses the push when one fails", () => { + expect(pushRepoTests("task/7")).toEqual({ + status: 1, + pnpm: "exec vitest run --project unit .repo.test.ts\n", + }); + }); +});