|
1 | 1 | import { spawnSync } from "node:child_process" |
2 | | -import fs from "node:fs" |
3 | | -import path from "node:path" |
4 | 2 | import process from "node:process" |
5 | | -import { fileURLToPath } from "node:url" |
6 | 3 |
|
7 | | -import { assertMatchingFiles } from "./verify-wasm-files.mjs" |
8 | | - |
9 | | -const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..") |
10 | 4 | const pnpm = process.platform === "win32" ? process.env.npm_execpath : "pnpm" |
11 | 5 | if (!pnpm) throw new Error("pnpm executable path is unavailable") |
12 | | -const pnpmPrefix = process.platform === "win32" ? [pnpm] : [] |
13 | | -const run = (args, options = {}) => { |
14 | | - const { includeStderr = true, ...spawnOptions } = options |
15 | | - const command = process.platform === "win32" ? process.execPath : pnpm |
16 | | - const result = spawnSync(command, [...pnpmPrefix, ...args], { cwd: root, encoding: "utf8", ...spawnOptions }) |
17 | | - if (result.status !== 0) { |
18 | | - const details = [ |
19 | | - result.error?.message, |
20 | | - result.signal ? `terminated by ${result.signal}` : undefined, |
21 | | - result.stderr, |
22 | | - result.stdout, |
23 | | - ] |
24 | | - .filter(Boolean) |
25 | | - .join("\n") |
26 | | - throw new Error(details || `pnpm exited with status ${result.status ?? "unknown"}`) |
27 | | - } |
28 | | - return `${result.stdout || ""}${includeStderr ? result.stderr || "" : ""}` |
| 6 | +const command = process.platform === "win32" ? process.execPath : pnpm |
| 7 | +const args = process.platform === "win32" ? [pnpm] : [] |
| 8 | +const result = spawnSync( |
| 9 | + command, |
| 10 | + [...args, "turbo", "run", "test:coverage:unit", "test:dist", "--filter=zoo-code", "--dry=json"], |
| 11 | + { encoding: "utf8" }, |
| 12 | +) |
| 13 | +if (result.status !== 0) { |
| 14 | + const details = [result.error?.message, result.signal, result.stderr, result.stdout].filter(Boolean).join("\n") |
| 15 | + throw new Error(details || `pnpm exited with status ${result.status ?? "unknown"}`) |
29 | 16 | } |
30 | 17 |
|
31 | | -const graph = JSON.parse( |
32 | | - run(["turbo", "run", "test:coverage:unit", "--filter=zoo-code", "--dry=json"], { includeStderr: false }), |
33 | | -) |
| 18 | +const graph = JSON.parse(result.stdout) |
34 | 19 | const coverageTask = graph.tasks.find(({ taskId }) => taskId === "zoo-code#test:coverage:unit") |
35 | | -const preparationTask = graph.tasks.find(({ taskId }) => taskId === "zoo-code#prepare:tree-sitter-wasms") |
36 | | -if (!coverageTask?.dependencies.includes("zoo-code#prepare:tree-sitter-wasms")) |
37 | | - throw new Error("WASM prerequisite missing") |
| 20 | +const distTask = graph.tasks.find(({ taskId }) => taskId === "zoo-code#test:dist") |
| 21 | +if (!coverageTask) throw new Error("Unit coverage task missing") |
| 22 | +if (graph.tasks.some(({ taskId }) => taskId === "zoo-code#prepare:tree-sitter-wasms")) |
| 23 | + throw new Error("Removed WASM preparation task remains in the graph") |
38 | 24 | if (coverageTask.dependencies.includes("zoo-code#bundle")) throw new Error("Unit coverage must not depend on bundle") |
39 | | -if (JSON.stringify(preparationTask?.outputs) !== JSON.stringify(["generated/tree-sitter-wasms/**"])) |
40 | | - throw new Error("WASM prerequisite outputs changed") |
41 | | - |
42 | | -const generated = path.join(root, "src", "generated", "tree-sitter-wasms") |
43 | | -const cacheDir = path.join(root, ".turbo", "coverage-contract") |
44 | | -fs.rmSync(cacheDir, { recursive: true, force: true }) |
45 | | - |
46 | | -try { |
47 | | - run(["turbo", "run", "prepare:tree-sitter-wasms", "--filter=zoo-code", "--cache-dir=.turbo/coverage-contract"]) |
48 | | - run(["--dir", "src", "exec", "vitest", "run", "services/tree-sitter/__tests__"], { stdio: "inherit" }) |
49 | | - |
50 | | - const source = fs |
51 | | - .readdirSync(path.join(root, "src", "node_modules", "tree-sitter-wasms", "out")) |
52 | | - .filter((filename) => /^tree-sitter-.*\.wasm$/.test(filename)) |
53 | | - .sort() |
54 | | - const prepared = fs |
55 | | - .readdirSync(generated) |
56 | | - .filter((filename) => /^tree-sitter-.*\.wasm$/.test(filename)) |
57 | | - .sort() |
58 | | - if (source.length === 0) throw new Error("Dependency contains no tree-sitter WASMs") |
59 | | - if (JSON.stringify(source) !== JSON.stringify(prepared)) |
60 | | - throw new Error("Prepared WASM set does not match dependency") |
61 | | - assertMatchingFiles( |
62 | | - path.join(root, "src", "node_modules", "tree-sitter-wasms", "out"), |
63 | | - generated, |
64 | | - source, |
65 | | - "Prepared WASM content does not match dependency", |
66 | | - ) |
67 | | - |
68 | | - fs.rmSync(generated, { recursive: true, force: true }) |
69 | | - const warmGraph = JSON.parse( |
70 | | - run( |
71 | | - [ |
72 | | - "turbo", |
73 | | - "run", |
74 | | - "prepare:tree-sitter-wasms", |
75 | | - "--filter=zoo-code", |
76 | | - "--cache-dir=.turbo/coverage-contract", |
77 | | - "--dry=json", |
78 | | - ], |
79 | | - { includeStderr: false }, |
80 | | - ), |
81 | | - ) |
82 | | - const warmTask = warmGraph.tasks.find(({ taskId }) => taskId === "zoo-code#prepare:tree-sitter-wasms") |
83 | | - if (warmTask?.cache.status !== "HIT") throw new Error("WASM prerequisite is not available in the isolated cache") |
84 | | - run(["turbo", "run", "prepare:tree-sitter-wasms", "--filter=zoo-code", "--cache-dir=.turbo/coverage-contract"]) |
85 | | - const restored = fs |
86 | | - .readdirSync(generated) |
87 | | - .filter((filename) => /^tree-sitter-.*\.wasm$/.test(filename)) |
88 | | - .sort() |
89 | | - if (JSON.stringify(source) !== JSON.stringify(restored)) throw new Error("WASM cache did not restore exact outputs") |
90 | | - assertMatchingFiles( |
91 | | - path.join(root, "src", "node_modules", "tree-sitter-wasms", "out"), |
92 | | - generated, |
93 | | - source, |
94 | | - "WASM cache restored corrupted output", |
95 | | - ) |
96 | | -} finally { |
97 | | - fs.rmSync(cacheDir, { recursive: true, force: true }) |
98 | | -} |
| 25 | +if (!Object.hasOwn(coverageTask.inputs, "package.json")) throw new Error("Unit coverage must hash package.json") |
| 26 | +if (!coverageTask.hashOfExternalDependencies) throw new Error("Unit coverage must hash external dependencies") |
| 27 | +if (!distTask?.dependencies.includes("zoo-code#bundle")) throw new Error("Dist smoke test must depend on bundle") |
0 commit comments