Skip to content

Commit 9422c3f

Browse files
cliffhallclaude
andcommitted
test: pin main-source.yml's safety properties; name the guard as a gate exception
Addresses Copilot round 1 on #5120. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent a8a13d3 commit 9422c3f

2 files changed

Lines changed: 66 additions & 6 deletions

File tree

‎docs/quality-gate.md‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@ your machine. It is the strongest predictor of a green CI there is here, though
2222
not a proof: CI runs on Linux, on a fresh `npm ci`, with Node 22 and each
2323
Python server's pinned interpreter.
2424

25-
**The one exception is the Windows legs** (#1149): `test-windows` in both
25+
**There are two exceptions.** The first is the Windows legs (#1149): `test-windows` in both
2626
workflows runs each server's test suite (`npm test`, `pytest`) on a
2727
`windows-latest` runner. The gate runs the same suites, but only on your
2828
machine's OS, so a Windows-only failure (a drive letter, a UNC path, a missing
2929
`O_NOFOLLOW`, a locked file) shows up first in CI. When a PR touches
3030
platform-specific code, wait for those legs, not just the gate.
3131

32+
The second is **Source is v2/main** (`main-source.yml`), which runs only on a
33+
pull request into `main` and checks that its head is already on `v2/main`. No
34+
local push targets `main`, so the gate has nothing to check there and no stage
35+
mirrors it.
36+
3237
The inner-loop commands are subsets. They are for speed while working, and are
3338
never a substitute for the gate.
3439

‎scripts/verify-main-source.test.mjs‎

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
// Tests for `verify-main-source.mjs` (#5119): the arguments and the report,
2-
// then the guard end to end against throwaway repositories shaped like the
3-
// release flow: a merge PR from a branch pushed off `v2/main` passes, as does
2+
// the guard end to end against throwaway repositories shaped like the
3+
// release flow, a merge PR from a branch pushed off `v2/main` passes, as does
44
// one `v2/main` has since moved past; a branch with a commit of its own, or a
5-
// merge of `v2/main` into something else, fails. Run via `npm run
6-
// test:scripts`.
5+
// merge of `v2/main` into something else, fails. Last, the properties of
6+
// `main-source.yml` its safety rests on. Run via `npm run test:scripts`.
77

88
import { test } from "node:test";
99
import assert from "node:assert/strict";
1010
import { spawnSync } from "node:child_process";
11-
import { mkdtempSync, rmSync } from "node:fs";
11+
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
1212
import { devNull, tmpdir } from "node:os";
1313
import path from "node:path";
14+
import { parse } from "yaml";
1415
import {
1516
DEFAULT_SOURCE,
1617
MAX_LISTED,
@@ -162,3 +163,57 @@ test("verifyMainSource: run as a script, a bad argument exits 2", () => {
162163
assert.equal(r.status, 2);
163164
assert.match(r.stderr, /--head is required/);
164165
});
166+
167+
// The workflow's safety rests on properties of its YAML; pin them, so an edit
168+
// that would run the PR's code, or stop wiring the guard, fails here.
169+
const workflow = parse(
170+
readFileSync(
171+
path.join(import.meta.dirname, "..", ".github/workflows/main-source.yml"),
172+
"utf8",
173+
),
174+
);
175+
const job = workflow.jobs["main-source"];
176+
177+
test("main-source.yml: runs main's copy, on PRs into main only, read-only", () => {
178+
assert.deepEqual(Object.keys(workflow.on), ["pull_request_target"]);
179+
assert.deepEqual(workflow.on.pull_request_target.branches, ["main"]);
180+
assert.ok(workflow.on.pull_request_target.types.includes("edited"));
181+
assert.deepEqual(workflow.permissions, { contents: "read" });
182+
assert.equal(job.permissions, undefined);
183+
assert.deepEqual(Object.keys(workflow.jobs), ["main-source"]);
184+
assert.equal(job.name, "Source is v2/main");
185+
assert.ok(job["timeout-minutes"]);
186+
});
187+
188+
test("main-source.yml: checks out main, never the PR, and fetches the PR as objects only", () => {
189+
const checkout = job.steps.filter((s) =>
190+
/actions\/checkout/.test(s.uses ?? ""),
191+
);
192+
assert.equal(checkout.length, 1);
193+
assert.equal(checkout[0].with?.ref, undefined);
194+
assert.equal(checkout[0].with?.repository, undefined);
195+
assert.equal(checkout[0].with["persist-credentials"], false);
196+
const runs = job.steps.map((s) => s.run ?? "").join("\n");
197+
const refspec = '"+refs/pull/${PR_NUMBER}/head:refs/remotes/pr/head"';
198+
assert.ok(runs.includes(refspec), "the PR head is fetched");
199+
// Past that fetch, nothing names the PR's ref, or switches to, installs or
200+
// executes a tree.
201+
const rest = runs.replace(refspec, "");
202+
assert.doesNotMatch(rest, /pr\/head|refs\/pull/);
203+
assert.doesNotMatch(rest, /git (checkout|switch|reset|worktree)|npm |npx /);
204+
// No step reads a secret; expressions reach the shell only through env.
205+
for (const step of job.steps) {
206+
assert.doesNotMatch(JSON.stringify(step), /secrets\./);
207+
assert.doesNotMatch(step.run ?? "", /\$\{\{/);
208+
}
209+
});
210+
211+
test("main-source.yml: hands the event's head SHA to the guard", () => {
212+
const step = job.steps.find((s) =>
213+
/verify-main-source\.mjs/.test(s.run ?? ""),
214+
);
215+
assert.ok(step, "a step runs the guard");
216+
assert.equal(step.env.HEAD_SHA, "${{ github.event.pull_request.head.sha }}");
217+
assert.match(step.run, /--head "\$HEAD_SHA"/);
218+
assert.doesNotMatch(step.run, /--source/);
219+
});

0 commit comments

Comments
 (0)