Skip to content

Commit a05faec

Browse files
committed
fix(ci): isolate coverage hash probes
1 parent e491855 commit a05faec

1 file changed

Lines changed: 35 additions & 9 deletions

File tree

‎src/scripts/verify-coverage-cache-inputs.mjs‎

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,49 @@
11
import { spawnSync } from "node:child_process"
2-
import { readFileSync, writeFileSync } from "node:fs"
2+
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"
3+
import { tmpdir } from "node:os"
34
import { resolve } from "node:path"
45
import process from "node:process"
5-
import { test } from "node:test"
6-
7-
if (process.env.GITHUB_ACTIONS !== "true" && process.env.ZOO_ALLOW_COVERAGE_INPUT_MUTATION !== "true")
8-
throw new Error("Coverage cache input verification requires an ephemeral runner or explicit local opt-in")
6+
import { after, test } from "node:test"
97

108
const root = resolve(import.meta.dirname, "../..")
119
const pnpm = process.platform === "win32" ? process.env.npm_execpath : "pnpm"
1210
if (!pnpm) throw new Error("pnpm executable path is unavailable")
1311
const command = process.platform === "win32" ? process.execPath : pnpm
1412
const args = process.platform === "win32" ? [pnpm] : []
1513
const lanes = ["api", "core", "services", "misc", "tree-sitter"]
14+
const probeRoot = mkdtempSync(resolve(tmpdir(), "zoo-code-coverage-cache-inputs-"))
15+
16+
const git = (gitArgs) => {
17+
const result = spawnSync("git", gitArgs, { cwd: root, encoding: "utf8" })
18+
if (result.status !== 0) {
19+
const details = [result.error?.message, result.signal, result.stderr, result.stdout].filter(Boolean).join("\n")
20+
throw new Error(details || `git exited with status ${result.status ?? "unknown"}`)
21+
}
22+
}
23+
24+
git(["worktree", "add", "--detach", probeRoot, "HEAD"])
25+
after(() => {
26+
try {
27+
git(["worktree", "remove", "--force", probeRoot])
28+
} finally {
29+
rmSync(probeRoot, { recursive: true, force: true })
30+
}
31+
})
1632

1733
const coverageTasks = () => {
1834
const result = spawnSync(
1935
command,
20-
[...args, "turbo", "run", ...lanes.map((lane) => `test:coverage:${lane}`), "--filter=zoo-code", "--dry=json"],
36+
[
37+
...args,
38+
"turbo",
39+
"--cwd",
40+
probeRoot,
41+
"run",
42+
...lanes.map((lane) => `test:coverage:${lane}`),
43+
"--filter=zoo-code",
44+
"--dry=json",
45+
"--no-daemon",
46+
],
2147
{ cwd: root, encoding: "utf8" },
2248
)
2349
if (result.status !== 0) {
@@ -35,13 +61,13 @@ const coverageTasks = () => {
3561
const hashes = () => Object.fromEntries(coverageTasks().map((task) => [task.task.split(":").at(-1), task.hash]))
3662

3763
const withChangedFiles = (paths, run) => {
38-
const originals = paths.map((path) => [path, readFileSync(resolve(root, path), "utf8")])
64+
const originals = paths.map((path) => [path, readFileSync(resolve(probeRoot, path), "utf8")])
3965
try {
4066
for (const [path, contents] of originals)
41-
writeFileSync(resolve(root, path), `${contents}\n// cache-input-test\n`)
67+
writeFileSync(resolve(probeRoot, path), `${contents}\n// cache-input-test\n`)
4268
return run()
4369
} finally {
44-
for (const [path, contents] of originals) writeFileSync(resolve(root, path), contents)
70+
for (const [path, contents] of originals) writeFileSync(resolve(probeRoot, path), contents)
4571
}
4672
}
4773

0 commit comments

Comments
 (0)