Skip to content

Commit 55d28b9

Browse files
committed
test: reuse immutable native PR bootstrap fixtures
1 parent f1d2c5b commit 55d28b9

2 files changed

Lines changed: 110 additions & 59 deletions

File tree

‎test/scripts/pr-merge-outcome.test.ts‎

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { execFileSync, spawnSync } from "node:child_process";
22
import {
33
chmodSync,
4+
constants as fsConstants,
5+
cpSync,
46
existsSync,
57
mkdirSync,
68
readFileSync,
@@ -10,51 +12,75 @@ import {
1012
writeFileSync,
1113
} from "node:fs";
1214
import { join } from "node:path";
13-
import { afterEach, describe, expect, it } from "vitest";
15+
import { afterAll, afterEach, describe, expect, it } from "vitest";
1416
import { useAutoCleanupTempDirTracker } from "../helpers/temp-dir.js";
1517

1618
const temps = useAutoCleanupTempDirTracker(afterEach);
19+
const templateDirs = useAutoCleanupTempDirTracker(afterAll);
20+
let fixtureTemplate: ReturnType<typeof createFixtureTemplate> | undefined;
1721
const scripts = join(process.cwd(), "scripts");
1822
const outcomeRef = "refs/openclaw/pr-merge-outcomes/123";
1923
const lockRef = "refs/openclaw/pr-operation-locks/123";
2024
const describePosix = process.platform === "win32" ? describe.skip : describe;
2125
const unknownProjection = { mergeable: "UNKNOWN", mergeStateStatus: "UNKNOWN" };
2226

23-
function fixture(sourceMessage?: string, sourceVersions: Array<[string, string?]> = [["after\n"]]) {
24-
const root = realpathSync(temps.make("pr-merge-outcome-"));
25-
const remote = join(root, "remote.git");
26-
const repo = join(root, "repo");
27-
mkdirSync(repo);
27+
function createFixtureGit(repo: string) {
2828
const git = (args: string[], input?: string, cwd = repo) =>
2929
execFileSync("git", ["-c", "commit.gpgsign=false", "-c", "core.hooksPath=/dev/null", ...args], {
3030
cwd,
3131
input,
3232
encoding: "utf8",
3333
stdio: ["pipe", "pipe", "pipe"],
3434
}).trim();
35-
git(["init", "-q", "-b", "main"]);
36-
git(["config", "user.name", "Merge Fixture"]);
37-
git(["config", "user.email", "[email protected]"]);
38-
git(["init", "-q", "--bare", remote]);
39-
git(["remote", "add", "origin", remote]);
40-
// Production URLs still use the real Git transport, redirected only in this disposable repo.
41-
git(["config", `url.${remote}.insteadOf`, "https://github.com/fixture/repo"]);
42-
git(["config", "--add", `url.${remote}.insteadOf`, "https://github.com/fixture/repo.git"]);
4335
const tree = (owner: string, sibling = "stable\n") => {
4436
const a = git(["hash-object", "-w", "--stdin"], owner);
4537
const b = git(["hash-object", "-w", "--stdin"], sibling);
4638
return git(["mktree"], `100644 blob ${a}\towner.txt\n100644 blob ${b}\tsibling.txt\n`);
4739
};
4840
const commit = (contents: string, parents: string[], message = "Fixture commit\n") =>
4941
git(["commit-tree", contents, ...parents.flatMap((parent) => ["-p", parent])], message);
42+
return { git, tree, commit };
43+
}
44+
45+
function createFixtureTemplate(directory: string) {
46+
const root = realpathSync(directory);
47+
const repo = join(root, "repo");
48+
const remote = join(root, "remote.git");
49+
mkdirSync(repo);
50+
const { git, tree, commit } = createFixtureGit(repo);
51+
git(["init", "-q", "-b", "main"]);
52+
git(["config", "user.name", "Merge Fixture"]);
53+
git(["config", "user.email", "[email protected]"]);
54+
git(["init", "-q", "--bare", remote]);
5055
const base = commit(tree("before\n"), []);
56+
git(["update-ref", "refs/heads/main", base]);
57+
return { repo, remote, base };
58+
}
59+
60+
function fixture(sourceMessage?: string, sourceVersions: Array<[string, string?]> = [["after\n"]]) {
61+
const root = realpathSync(temps.make("pr-merge-outcome-"));
62+
const remote = join(root, "remote.git");
63+
const repo = join(root, "repo");
64+
const template = (fixtureTemplate ??= createFixtureTemplate(
65+
templateDirs.make("pr-merge-outcome-template-"),
66+
));
67+
// Only the common base is reused. Source commits stay fresh, loose and private
68+
// so corruption, GC, attribution and multi-commit rebase cases keep their proof.
69+
const copyOptions = { recursive: true, mode: fsConstants.COPYFILE_FICLONE };
70+
cpSync(template.repo, repo, copyOptions);
71+
cpSync(template.remote, remote, copyOptions);
72+
const { base } = template;
73+
const { git, tree, commit } = createFixtureGit(repo);
74+
git(["remote", "add", "origin", remote]);
75+
// Production URLs still use the real Git transport, redirected only in this disposable repo.
76+
git(["config", `url.${remote}.insteadOf`, "https://github.com/fixture/repo"]);
77+
git(["config", "--add", `url.${remote}.insteadOf`, "https://github.com/fixture/repo.git"]);
5178
const sourceCommits: string[] = [];
5279
let head = base;
5380
for (const [owner, sibling] of sourceVersions) {
5481
head = commit(tree(owner, sibling), [head], sourceMessage);
5582
sourceCommits.push(head);
5683
}
57-
git(["update-ref", "refs/heads/main", base]);
5884
git(["update-ref", "refs/heads/topic", head]);
5985
git(["push", "-q", "origin", "main", "topic:refs/pull/123/head", "topic"]);
6086
const worktree = join(repo, ".worktrees/pr-123");

‎test/scripts/pr-operation-lock.test.ts‎

Lines changed: 69 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { useAutoCleanupTempDirTracker } from "../helpers/temp-dir.js";
3030
import { validReview, writeReviewArtifacts } from "./pr-review-artifact-fixture.js";
3131

3232
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
33+
const freshMainTemplateDirs = useAutoCleanupTempDirTracker(afterAll);
3334
const escapedPipeHolderPidFiles = new Set<string>();
3435
afterEach(async () => {
3536
const failures: unknown[] = [];
@@ -55,6 +56,7 @@ const lockRef = "refs/openclaw/pr-operation-locks/42";
5556
const detachedChildren = new WeakSet<ChildProcess>();
5657
const goneProcessGroups = new Set<number>();
5758
let templateRepo = "";
59+
let freshMainTemplate: ReturnType<typeof createFreshMainTemplate> | undefined;
5860

5961
// Direct preload affects only the supervisor; operation fixtures keep real clocks.
6062
// The source assertions below pin the production safety durations being accelerated.
@@ -278,6 +280,67 @@ function installPrCliFixture(repoDir: string, env?: NodeJS.ProcessEnv) {
278280
return { binDir, cli };
279281
}
280282

283+
function createFreshMainTemplate() {
284+
// Freeze only the committed wrapper/tool prefix. Origins, FETCH_HEAD,
285+
// linked worktrees, and failure proxies are created in each private copy.
286+
const repoDir = freshMainTemplateDirs.make("openclaw-pr-fresh-main-template-");
287+
cpSync(templateRepo, repoDir, { recursive: true });
288+
const stateDir = join(repoDir, "fixture-state");
289+
const homeDir = join(stateDir, "home");
290+
const tmpDir = join(stateDir, "tmp");
291+
mkdirSync(homeDir, { recursive: true });
292+
mkdirSync(tmpDir, { recursive: true });
293+
const setupEnv = createPrFixtureEnv(homeDir, process.env.PATH ?? "");
294+
const { binDir } = installPrCliFixture(repoDir, setupEnv);
295+
const realGit = realpathSync(join(binDir, "git"));
296+
// Only these real tools are reachable. rg/pnpm are required by the CLI
297+
// preflight; these cases must stop before invoking either of them.
298+
for (const command of [
299+
"awk",
300+
"cat",
301+
"date",
302+
"env",
303+
"jq",
304+
"mkdir",
305+
"mktemp",
306+
"mv",
307+
"ps",
308+
"rm",
309+
"seq",
310+
"sh",
311+
"sleep",
312+
]) {
313+
symlinkSync(
314+
execFileSync("which", [command], { encoding: "utf8", env: setupEnv }).trim(),
315+
join(binDir, command),
316+
);
317+
}
318+
symlinkSync(process.execPath, join(binDir, "node"));
319+
for (const command of ["rg", "pnpm"]) {
320+
const stub = writeFixtureFile(binDir, command, [
321+
"#!/bin/sh",
322+
"echo 'unexpected command in review-init fixture' >&2",
323+
"exit 99",
324+
]);
325+
chmodSync(stub, 0o755);
326+
}
327+
const env: NodeJS.ProcessEnv = {
328+
...createPrFixtureEnv(homeDir, binDir),
329+
TMPDIR: tmpDir,
330+
};
331+
const git = (...args: string[]) =>
332+
execFileSync(realGit, args, { cwd: repoDir, env, encoding: "utf8", timeout: 5000 }).trim();
333+
writeFileSync(
334+
join(repoDir, ".git/info/exclude"),
335+
"/.local/\n/.worktrees/\n/isolated-bin/\n/fixture-state/\n",
336+
);
337+
git("add", "scripts", ".github");
338+
git("commit", "-qm", "test: unmodified public PR wrapper fixture");
339+
const cachedMain = git("rev-parse", "HEAD");
340+
const canonicalTree = git("rev-parse", "HEAD^{tree}");
341+
return { repoDir, cachedMain, canonicalTree };
342+
}
343+
281344
function installRequiredPrCommandStubs(binDir: string) {
282345
for (const command of ["gh", "jq", "pnpm", "rg"]) {
283346
const stub = join(binDir, command);
@@ -738,60 +801,22 @@ describePosix("scripts/pr per-PR operation lock", () => {
738801
])(
739802
"$command requires fresh main before replacing PR artifacts ($failure, existing=$existing)",
740803
async ({ command, failure, existing }) => {
741-
const repoDir = createRepo();
804+
const template = (freshMainTemplate ??= createFreshMainTemplate());
805+
const repoDir = tempDirs.make("openclaw-pr-fresh-main-");
806+
cpSync(template.repoDir, repoDir, { recursive: true });
807+
const { cachedMain, canonicalTree } = template;
742808
const stateDir = join(repoDir, "fixture-state");
743809
const homeDir = join(stateDir, "home");
744810
const tmpDir = join(stateDir, "tmp");
745-
mkdirSync(homeDir, { recursive: true });
746-
mkdirSync(tmpDir, { recursive: true });
747-
const setupEnv = createPrFixtureEnv(homeDir, process.env.PATH ?? "");
748-
const { binDir, cli } = installPrCliFixture(repoDir, setupEnv);
811+
const binDir = join(repoDir, "isolated-bin");
812+
const cli = join(repoDir, "scripts/pr");
749813
const realGit = realpathSync(join(binDir, "git"));
750-
// Only these real tools are reachable. rg/pnpm are required by the CLI
751-
// preflight; these cases must stop before invoking either of them.
752-
for (const command of [
753-
"awk",
754-
"cat",
755-
"date",
756-
"env",
757-
"jq",
758-
"mkdir",
759-
"mktemp",
760-
"mv",
761-
"ps",
762-
"rm",
763-
"seq",
764-
"sh",
765-
"sleep",
766-
]) {
767-
symlinkSync(
768-
execFileSync("which", [command], { encoding: "utf8", env: setupEnv }).trim(),
769-
join(binDir, command),
770-
);
771-
}
772-
symlinkSync(process.execPath, join(binDir, "node"));
773-
for (const command of ["rg", "pnpm"]) {
774-
const stub = writeFixtureFile(binDir, command, [
775-
"#!/bin/sh",
776-
"echo 'unexpected command in review-init fixture' >&2",
777-
"exit 99",
778-
]);
779-
chmodSync(stub, 0o755);
780-
}
781814
const env: NodeJS.ProcessEnv = {
782815
...createPrFixtureEnv(homeDir, binDir),
783816
TMPDIR: tmpDir,
784817
};
785818
const git = (...args: string[]) =>
786819
execFileSync(realGit, args, { cwd: repoDir, env, encoding: "utf8", timeout: 5000 }).trim();
787-
writeFileSync(
788-
join(repoDir, ".git/info/exclude"),
789-
"/.local/\n/.worktrees/\n/isolated-bin/\n/fixture-state/\n",
790-
);
791-
git("add", "scripts", ".github");
792-
git("commit", "-qm", "test: unmodified public PR wrapper fixture");
793-
const cachedMain = git("rev-parse", "HEAD");
794-
const canonicalTree = git("rev-parse", "HEAD^{tree}");
795820
const newCommit = (message: string) =>
796821
execFileSync(realGit, ["commit-tree", canonicalTree, "-p", cachedMain], {
797822
cwd: repoDir,

0 commit comments

Comments
 (0)