-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun-setup.ts
More file actions
93 lines (91 loc) · 2.75 KB
/
Copy pathrun-setup.ts
File metadata and controls
93 lines (91 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { existsSync, readFileSync } from "node:fs";
import { resolve, sep } from "node:path";
import {
ingestIstanbul,
ingestLcov,
} from "../../src/application/coverage-engine";
import { closeDb, openDb, replaceFileChurn } from "../../src/db";
import type { FileChurnRow } from "../../src/db";
import type { GoldenSetupStep } from "./schema";
/**
* Run one-time setup steps after the corpus is indexed and before the first
* scenario. Today: `ingest-coverage` (Istanbul / LCOV — auto-detected by
* extension, mirrors the CLI verb). Extend the dispatch as more one-shot
* ingest verbs land.
*/
export function runGoldenSetup(
steps: GoldenSetupStep[],
fixtureRoot: string,
): void {
const fixtureAbs = resolve(fixtureRoot);
const db = openDb();
try {
for (const step of steps) {
if (step.kind === "clear-coverage") {
db.run("DELETE FROM coverage");
continue;
}
if (step.kind === "seed-file-churn") {
const absPath = resolve(fixtureRoot, step.path);
if (
absPath !== fixtureAbs &&
!absPath.startsWith(`${fixtureAbs}${sep}`)
) {
throw new Error(
`query-golden setup: path must stay under fixture root (${step.path})`,
);
}
if (!existsSync(absPath)) {
throw new Error(
`query-golden setup: missing file-churn seed ${absPath}`,
);
}
const rows = JSON.parse(
readFileSync(absPath, "utf-8"),
) as FileChurnRow[];
replaceFileChurn(db, rows);
continue;
}
if (step.kind !== "ingest-coverage") continue;
const absPath = resolve(fixtureRoot, step.path);
if (
absPath !== fixtureAbs &&
!absPath.startsWith(`${fixtureAbs}${sep}`)
) {
throw new Error(
`query-golden setup: path must stay under fixture root (${step.path})`,
);
}
if (!existsSync(absPath)) {
console.warn(
` query-golden setup: skipping missing coverage file ${absPath}`,
);
continue;
}
if (absPath.endsWith(".json")) {
const payload = JSON.parse(
readFileSync(absPath, "utf-8"),
) as Parameters<typeof ingestIstanbul>[0]["payload"];
ingestIstanbul({
db,
projectRoot: fixtureRoot,
payload,
sourcePath: absPath,
});
} else if (absPath.endsWith(".info")) {
ingestLcov({
db,
projectRoot: fixtureRoot,
payload: readFileSync(absPath, "utf-8"),
sourcePath: absPath,
});
} else {
throw new Error(
`query-golden setup: cannot auto-detect coverage format from ${absPath}`,
);
}
}
} finally {
closeDb(db);
}
}