supercov 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +159 -30
- package/bin/supercov.js +10 -0
- package/dist/atomic.d.ts +6 -0
- package/dist/atomic.d.ts.map +1 -0
- package/dist/atomic.js +47 -0
- package/dist/atomic.js.map +1 -0
- package/dist/buildCache.d.ts +14 -0
- package/dist/buildCache.d.ts.map +1 -0
- package/dist/buildCache.js +84 -0
- package/dist/buildCache.js.map +1 -0
- package/dist/cli.js +536 -215
- package/dist/cli.js.map +1 -1
- package/dist/directInstrumenter.d.ts +8 -0
- package/dist/directInstrumenter.d.ts.map +1 -0
- package/dist/directInstrumenter.js +70 -0
- package/dist/directInstrumenter.js.map +1 -0
- package/dist/evidenceArchive.d.ts +35 -0
- package/dist/evidenceArchive.d.ts.map +1 -0
- package/dist/evidenceArchive.js +101 -0
- package/dist/evidenceArchive.js.map +1 -0
- package/dist/instrumenter.d.ts.map +1 -1
- package/dist/instrumenter.js +251 -41
- package/dist/instrumenter.js.map +1 -1
- package/dist/integrity.d.ts.map +1 -1
- package/dist/integrity.js +27 -0
- package/dist/integrity.js.map +1 -1
- package/dist/launchSupervisor.d.ts +24 -0
- package/dist/launchSupervisor.d.ts.map +1 -0
- package/dist/launchSupervisor.js +397 -0
- package/dist/launchSupervisor.js.map +1 -0
- package/dist/playwright.d.ts +0 -4
- package/dist/playwright.d.ts.map +1 -1
- package/dist/playwright.js +33 -17
- package/dist/playwright.js.map +1 -1
- package/dist/playwrightReporter.d.ts.map +1 -1
- package/dist/playwrightReporter.js +3 -2
- package/dist/playwrightReporter.js.map +1 -1
- package/dist/project.d.ts +5 -2
- package/dist/project.d.ts.map +1 -1
- package/dist/project.js +192 -27
- package/dist/project.js.map +1 -1
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +29 -21
- package/dist/query.js.map +1 -1
- package/dist/register.mjs +18 -13
- package/dist/register.mjs.map +1 -1
- package/dist/reporter.d.ts +6 -10
- package/dist/reporter.d.ts.map +1 -1
- package/dist/reporter.js +23 -82
- package/dist/reporter.js.map +1 -1
- package/dist/resolve-loader.d.mts.map +1 -1
- package/dist/resolve-loader.mjs +11 -3
- package/dist/resolve-loader.mjs.map +1 -1
- package/dist/runAnalysis.d.ts +19 -0
- package/dist/runAnalysis.d.ts.map +1 -0
- package/dist/runAnalysis.js +112 -0
- package/dist/runAnalysis.js.map +1 -0
- package/dist/runtime.d.ts +2 -2
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +35 -7
- package/dist/runtime.js.map +1 -1
- package/dist/transport.d.ts +2 -0
- package/dist/transport.d.ts.map +1 -1
- package/dist/transport.js +13 -5
- package/dist/transport.js.map +1 -1
- package/dist/types.d.ts +2 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/vitePlugin.d.ts +1 -0
- package/dist/vitePlugin.d.ts.map +1 -1
- package/dist/vitePlugin.js +35 -3
- package/dist/vitePlugin.js.map +1 -1
- package/dist/vitest.js +3 -2
- package/dist/vitest.js.map +1 -1
- package/dist/vitestReporter.d.ts +17 -2
- package/dist/vitestReporter.d.ts.map +1 -1
- package/dist/vitestReporter.js +51 -2
- package/dist/vitestReporter.js.map +1 -1
- package/dist/workspace.d.ts +81 -0
- package/dist/workspace.d.ts.map +1 -0
- package/dist/workspace.js +501 -0
- package/dist/workspace.js.map +1 -0
- package/docs/performance.md +107 -0
- package/docs/workspace-isolation.md +118 -0
- package/package.json +21 -5
package/dist/cli.js
CHANGED
|
@@ -1,18 +1,545 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { copyFileSync, existsSync, mkdirSync, readFileSync, rmSync, } from "node:fs";
|
|
3
|
+
import { relative, resolve, sep } from "node:path";
|
|
4
|
+
import { performance } from "node:perf_hooks";
|
|
5
5
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
6
|
-
import {
|
|
6
|
+
import { atomicRenameSync, atomicWriteFileSync } from "./atomic.js";
|
|
7
|
+
import { printCoverageSummary } from "./reporter.js";
|
|
8
|
+
import { analyzeCoverageArchive } from "./runAnalysis.js";
|
|
7
9
|
import { coverageQueryCommands, runQueryCommand } from "./query.js";
|
|
8
10
|
import { discoverCoverageProject } from "./project.js";
|
|
9
11
|
import { createRunIntegrity } from "./integrity.js";
|
|
12
|
+
import { writeEvidenceArchive } from "./evidenceArchive.js";
|
|
13
|
+
import { buildCacheReusePaths, instrumentedBuildCacheKey, readInstrumentedBuildCache, writeInstrumentedBuildCache, } from "./buildCache.js";
|
|
14
|
+
import { instrumentDirectWorkspace } from "./directInstrumenter.js";
|
|
15
|
+
import { acquireProjectLock, cachedWorkspacePath, cleanCoverageStorage, finalizePublishedRunStorage, prepareCachedWorkspace, pruneCoverageStorage, recoverAbandonedRuns, updateRunState, writeRunState, } from "./workspace.js";
|
|
16
|
+
function roundedTimings(timings) {
|
|
17
|
+
return Object.fromEntries(Object.entries(timings).map(([phase, duration]) => [
|
|
18
|
+
phase,
|
|
19
|
+
Math.round(duration * 10) / 10,
|
|
20
|
+
]));
|
|
21
|
+
}
|
|
22
|
+
function formatTimings(timings, totalMs) {
|
|
23
|
+
const rounded = roundedTimings(timings);
|
|
24
|
+
return [
|
|
25
|
+
`initialization=${rounded.initializationMs}ms`,
|
|
26
|
+
`workspace=${rounded.workspacePreparationMs}ms`,
|
|
27
|
+
`setup=${rounded.adapterSetupMs}ms`,
|
|
28
|
+
`build=${rounded.instrumentedBuildMs}ms`,
|
|
29
|
+
`tests=${rounded.testCommandMs}ms`,
|
|
30
|
+
`evidence=${rounded.evidencePublicationMs}ms`,
|
|
31
|
+
`total=${Math.round(totalMs * 10) / 10}ms`,
|
|
32
|
+
].join(" ");
|
|
33
|
+
}
|
|
34
|
+
let activeChild;
|
|
35
|
+
let signalEscalation;
|
|
36
|
+
function terminateChild(signal) {
|
|
37
|
+
const child = activeChild;
|
|
38
|
+
if (!child?.pid)
|
|
39
|
+
return;
|
|
40
|
+
try {
|
|
41
|
+
if (process.platform !== "win32")
|
|
42
|
+
process.kill(-child.pid, signal);
|
|
43
|
+
else
|
|
44
|
+
child.kill(signal);
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
try {
|
|
48
|
+
child.kill(signal);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
// The child may have exited between the signal and cleanup.
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function runChild(command, args, options) {
|
|
56
|
+
return new Promise((resolveChild) => {
|
|
57
|
+
let error;
|
|
58
|
+
const child = spawn(command, args, {
|
|
59
|
+
cwd: options.cwd,
|
|
60
|
+
env: options.env,
|
|
61
|
+
stdio: "inherit",
|
|
62
|
+
detached: process.platform !== "win32",
|
|
63
|
+
});
|
|
64
|
+
activeChild = child;
|
|
65
|
+
child.once("error", (failure) => {
|
|
66
|
+
error = failure;
|
|
67
|
+
});
|
|
68
|
+
child.once("close", (status, signal) => {
|
|
69
|
+
if (activeChild === child)
|
|
70
|
+
activeChild = undefined;
|
|
71
|
+
resolveChild({ status, signal, ...(error ? { error } : {}) });
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
function exitCode(result) {
|
|
76
|
+
if (!result)
|
|
77
|
+
return 0;
|
|
78
|
+
if (result.error)
|
|
79
|
+
return 1;
|
|
80
|
+
if (result.status !== null)
|
|
81
|
+
return result.status;
|
|
82
|
+
return result.signal ? 128 : 1;
|
|
83
|
+
}
|
|
84
|
+
function signalExitCode(signal) {
|
|
85
|
+
if (signal === "SIGHUP")
|
|
86
|
+
return 129;
|
|
87
|
+
if (signal === "SIGINT")
|
|
88
|
+
return 130;
|
|
89
|
+
if (signal === "SIGTERM")
|
|
90
|
+
return 143;
|
|
91
|
+
return 128;
|
|
92
|
+
}
|
|
93
|
+
function parseRetentionOptions(command, args) {
|
|
94
|
+
let keep = 20;
|
|
95
|
+
let dryRun = false;
|
|
96
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
97
|
+
const argument = args[index];
|
|
98
|
+
if (argument === "--dry-run")
|
|
99
|
+
dryRun = true;
|
|
100
|
+
else if (argument === "--keep") {
|
|
101
|
+
const value = Number(args[++index]);
|
|
102
|
+
if (!Number.isSafeInteger(value) || value < 0)
|
|
103
|
+
throw new Error("--keep must be a non-negative integer");
|
|
104
|
+
keep = value;
|
|
105
|
+
}
|
|
106
|
+
else
|
|
107
|
+
throw new Error(`Unknown ${command} option: ${argument}`);
|
|
108
|
+
}
|
|
109
|
+
return { keep, dryRun };
|
|
110
|
+
}
|
|
111
|
+
function cleanCommand(args) {
|
|
112
|
+
const options = parseRetentionOptions("clean", args);
|
|
113
|
+
const result = cleanCoverageStorage(process.cwd(), options);
|
|
114
|
+
console.log(`[supercov] ${options.dryRun ? "would remove" : "removed"} ${result.removedRuns.length} stored run(s), ${result.removedWorkspaces.length} per-run workspace(s), and ${result.removedBuildCache ? "the" : "no"} isolated build cache; keeping ${options.keep} newest run(s)`);
|
|
115
|
+
for (const id of result.removedRuns)
|
|
116
|
+
console.log(id);
|
|
117
|
+
}
|
|
118
|
+
function pruneCommand(args) {
|
|
119
|
+
const options = parseRetentionOptions("prune", args);
|
|
120
|
+
const result = pruneCoverageStorage(process.cwd(), options);
|
|
121
|
+
console.log(`[supercov] ${options.dryRun ? "would remove" : "removed"} ${result.removedRuns.length} stored run(s), ${result.removedWorkspaces.length} terminal/orphan work director${result.removedWorkspaces.length === 1 ? "y" : "ies"}, and ${result.removedEvidence.length} loose evidence director${result.removedEvidence.length === 1 ? "y" : "ies"}; keeping ${options.keep} newest run(s) and preserving the shared cache`);
|
|
122
|
+
for (const id of result.removedRuns)
|
|
123
|
+
console.log(id);
|
|
124
|
+
}
|
|
125
|
+
async function createCoverageRun(command) {
|
|
126
|
+
const root = process.cwd();
|
|
127
|
+
const runId = new Date().toISOString().replace(/[:.]/g, "-");
|
|
128
|
+
const runStartedAt = Date.now();
|
|
129
|
+
const runStartedMonotonic = performance.now();
|
|
130
|
+
const timings = {
|
|
131
|
+
initializationMs: 0,
|
|
132
|
+
workspacePreparationMs: 0,
|
|
133
|
+
adapterSetupMs: 0,
|
|
134
|
+
instrumentedBuildMs: 0,
|
|
135
|
+
testCommandMs: 0,
|
|
136
|
+
evidencePublicationMs: 0,
|
|
137
|
+
};
|
|
138
|
+
const recovered = recoverAbandonedRuns(root);
|
|
139
|
+
if (recovered.length > 0)
|
|
140
|
+
console.error(`[supercov] recovered abandoned run(s): ${recovered.join(", ")}`);
|
|
141
|
+
const lock = acquireProjectLock(root, runId);
|
|
142
|
+
const project = discoverCoverageProject(root, process.env, command);
|
|
143
|
+
const packageSource = fileURLToPath(new URL(".", import.meta.url));
|
|
144
|
+
const runIntegrity = createRunIntegrity(root, project, packageSource);
|
|
145
|
+
const workspace = cachedWorkspacePath(root);
|
|
146
|
+
const buildCacheKey = instrumentedBuildCacheKey(runIntegrity, project);
|
|
147
|
+
const reusableBuild = project.buildAdapter === "vite"
|
|
148
|
+
? readInstrumentedBuildCache(workspace, buildCacheKey)
|
|
149
|
+
: undefined;
|
|
150
|
+
const serverEvidenceRoot = resolve(workspace, ".supercov/server-evidence");
|
|
151
|
+
const runStagingDirectory = resolve(root, ".supercov/work", runId, "run-publication");
|
|
152
|
+
const storedRunDirectory = resolve(root, ".supercov/runs", runId);
|
|
153
|
+
const startedAt = new Date(runStartedAt).toISOString();
|
|
154
|
+
writeRunState(root, runId, {
|
|
155
|
+
id: runId,
|
|
156
|
+
pid: process.pid,
|
|
157
|
+
root,
|
|
158
|
+
workspace,
|
|
159
|
+
startedAt,
|
|
160
|
+
status: "preparing",
|
|
161
|
+
});
|
|
162
|
+
let receivedSignal;
|
|
163
|
+
const signalHandlers = new Map();
|
|
164
|
+
for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"]) {
|
|
165
|
+
const handler = () => {
|
|
166
|
+
if (receivedSignal)
|
|
167
|
+
return;
|
|
168
|
+
receivedSignal = signal;
|
|
169
|
+
try {
|
|
170
|
+
updateRunState(root, runId, { status: "interrupted", signal });
|
|
171
|
+
}
|
|
172
|
+
catch {
|
|
173
|
+
// State recovery on the next invocation remains the fallback.
|
|
174
|
+
}
|
|
175
|
+
terminateChild(signal);
|
|
176
|
+
signalEscalation = setTimeout(() => terminateChild("SIGKILL"), 5_000);
|
|
177
|
+
signalEscalation.unref();
|
|
178
|
+
};
|
|
179
|
+
signalHandlers.set(signal, handler);
|
|
180
|
+
process.once(signal, handler);
|
|
181
|
+
}
|
|
182
|
+
let buildResult;
|
|
183
|
+
let testResult;
|
|
184
|
+
let publicationFailed = false;
|
|
185
|
+
let runPublished = false;
|
|
186
|
+
let timingsPrinted = false;
|
|
187
|
+
timings.initializationMs = performance.now() - runStartedMonotonic;
|
|
188
|
+
try {
|
|
189
|
+
let phaseStarted = performance.now();
|
|
190
|
+
const isolatedRoot = prepareCachedWorkspace(root, {
|
|
191
|
+
...(reusableBuild
|
|
192
|
+
? { reusePaths: buildCacheReusePaths(reusableBuild) }
|
|
193
|
+
: {}),
|
|
194
|
+
});
|
|
195
|
+
timings.workspacePreparationMs = performance.now() - phaseStarted;
|
|
196
|
+
if (receivedSignal)
|
|
197
|
+
throw new Error(`Interrupted by ${receivedSignal}`);
|
|
198
|
+
phaseStarted = performance.now();
|
|
199
|
+
const generatedDirectory = resolve(isolatedRoot, ".supercov");
|
|
200
|
+
const evidenceDirectoryRelative = `.supercov/evidence/${runId}`;
|
|
201
|
+
const isolatedEvidenceDirectory = resolve(isolatedRoot, evidenceDirectoryRelative);
|
|
202
|
+
const persistedEvidenceDirectory = resolve(root, ".supercov/evidence", runId);
|
|
203
|
+
const generatedPlaywrightConfig = resolve(generatedDirectory, "playwright.config.mjs");
|
|
204
|
+
const generatedViteConfig = resolve(generatedDirectory, "vite.config.mjs");
|
|
205
|
+
const generatedVitestConfig = resolve(generatedDirectory, "vitest.config.mjs");
|
|
206
|
+
const manifestPath = resolve(generatedDirectory, "manifest.json");
|
|
207
|
+
const buildOutputMetadataPath = resolve(generatedDirectory, "build-outputs.json");
|
|
208
|
+
const isolatedPlaywrightConfig = project.playwrightConfig
|
|
209
|
+
? resolve(isolatedRoot, relative(root, project.playwrightConfig))
|
|
210
|
+
: undefined;
|
|
211
|
+
const isolatedVitestConfig = project.vitestConfig
|
|
212
|
+
? resolve(isolatedRoot, relative(root, project.vitestConfig))
|
|
213
|
+
: undefined;
|
|
214
|
+
mkdirSync(generatedDirectory, { recursive: true });
|
|
215
|
+
mkdirSync(isolatedEvidenceDirectory, { recursive: true });
|
|
216
|
+
for (const file of [
|
|
217
|
+
"atomic.js",
|
|
218
|
+
"launchSupervisor.js",
|
|
219
|
+
"playwright.js",
|
|
220
|
+
"playwrightReporter.js",
|
|
221
|
+
"provenance.js",
|
|
222
|
+
"register.mjs",
|
|
223
|
+
"resolve-loader.mjs",
|
|
224
|
+
"runtime.js",
|
|
225
|
+
"transport.js",
|
|
226
|
+
"types.js",
|
|
227
|
+
]) {
|
|
228
|
+
copyFileSync(resolve(packageSource, file), resolve(generatedDirectory, file));
|
|
229
|
+
}
|
|
230
|
+
const generatedPlaywrightAdapter = resolve(generatedDirectory, "playwright.js");
|
|
231
|
+
const generatedPlaywrightReporter = resolve(generatedDirectory, "playwrightReporter.js");
|
|
232
|
+
atomicWriteFileSync(generatedPlaywrightAdapter, readFileSync(generatedPlaywrightAdapter, "utf8")
|
|
233
|
+
.replace("__SUPERCOV_EVIDENCE_DIRECTORY__", evidenceDirectoryRelative)
|
|
234
|
+
.replace("__SUPERCOV_PLAYWRIGHT_MODULE__", project.playwrightModule)
|
|
235
|
+
.replace("__SUPERCOV_PLAYWRIGHT_TEST_EXPORT__", project.playwrightTestExport)
|
|
236
|
+
.replace("/*__SUPERCOV_ADAPTER_EXPORTS__*/", [
|
|
237
|
+
...(project.playwrightTestExport === "test"
|
|
238
|
+
? []
|
|
239
|
+
: [
|
|
240
|
+
`export { instrumentedTest as ${project.playwrightTestExport} };`,
|
|
241
|
+
]),
|
|
242
|
+
...project.playwrightExports
|
|
243
|
+
.filter((name) => name !== "test" &&
|
|
244
|
+
name !== "expect" &&
|
|
245
|
+
name !== project.playwrightTestExport)
|
|
246
|
+
.map((name) => `export const ${name} = adapter[${JSON.stringify(name)}];`),
|
|
247
|
+
].join("\n"))
|
|
248
|
+
.replace("__SUPERCOV_RUN_ID__", runId));
|
|
249
|
+
atomicWriteFileSync(generatedPlaywrightReporter, readFileSync(generatedPlaywrightReporter, "utf8").replace("__SUPERCOV_EVIDENCE_DIRECTORY__", evidenceDirectoryRelative));
|
|
250
|
+
const generatedResolveLoader = resolve(generatedDirectory, "resolve-loader.mjs");
|
|
251
|
+
atomicWriteFileSync(generatedResolveLoader, readFileSync(generatedResolveLoader, "utf8").replace("__SUPERCOV_PLAYWRIGHT_MODULE__", project.playwrightModule));
|
|
252
|
+
if (isolatedPlaywrightConfig) {
|
|
253
|
+
// Keep this import inside Playwright's own transform graph. In older
|
|
254
|
+
// supported releases, a native ESM file-URL import of a TypeScript
|
|
255
|
+
// config enters the synchronous transform bridge recursively and can
|
|
256
|
+
// deadlock in Atomics.wait before test discovery starts.
|
|
257
|
+
const configImport = `../${relative(isolatedRoot, isolatedPlaywrightConfig).split(sep).join("/")}`;
|
|
258
|
+
atomicWriteFileSync(generatedPlaywrightConfig, [
|
|
259
|
+
`import './register.mjs';`,
|
|
260
|
+
`import { dirname, isAbsolute, relative, resolve } from 'node:path';`,
|
|
261
|
+
`import { fileURLToPath } from 'node:url';`,
|
|
262
|
+
`import original from '${configImport}';`,
|
|
263
|
+
`const resolved = typeof original === 'function' ? await original({ command: 'test', mode: 'test' }) : original;`,
|
|
264
|
+
`const runtimeProjectRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..');`,
|
|
265
|
+
`const originalDirectory = dirname(fileURLToPath(new URL(${JSON.stringify(configImport)}, import.meta.url)));`,
|
|
266
|
+
`const sourceProjectRoot = process.env.SUPERCOV_SOURCE_PROJECT_ROOT;`,
|
|
267
|
+
`const runtimePath = value => {`,
|
|
268
|
+
` if (!value) return value;`,
|
|
269
|
+
` const absolute = isAbsolute(value) ? value : resolve(originalDirectory, value);`,
|
|
270
|
+
` const local = relative(runtimeProjectRoot, absolute);`,
|
|
271
|
+
` if (local === '' || (!local.startsWith('..') && !isAbsolute(local))) return absolute;`,
|
|
272
|
+
` if (sourceProjectRoot) {`,
|
|
273
|
+
` const sourceLocal = relative(sourceProjectRoot, absolute);`,
|
|
274
|
+
` if (sourceLocal === '' || (!sourceLocal.startsWith('..') && !isAbsolute(sourceLocal))) return resolve(runtimeProjectRoot, sourceLocal);`,
|
|
275
|
+
` }`,
|
|
276
|
+
` throw new Error('Supercov refuses a Playwright output/cwd outside the isolated project: ' + absolute);`,
|
|
277
|
+
`};`,
|
|
278
|
+
`const normalizeWebServer = server => server ? ({ ...server, cwd: runtimePath(server.cwd ?? originalDirectory) }) : server;`,
|
|
279
|
+
`const normalized = { ...resolved,`,
|
|
280
|
+
` testDir: runtimePath(resolved?.testDir),`,
|
|
281
|
+
` outputDir: runtimePath(resolved?.outputDir),`,
|
|
282
|
+
` snapshotDir: runtimePath(resolved?.snapshotDir),`,
|
|
283
|
+
` projects: resolved?.projects?.map(project => ({ ...project, testDir: runtimePath(project.testDir), outputDir: runtimePath(project.outputDir), snapshotDir: runtimePath(project.snapshotDir) })),`,
|
|
284
|
+
` webServer: Array.isArray(resolved?.webServer) ? resolved.webServer.map(normalizeWebServer) : normalizeWebServer(resolved?.webServer),`,
|
|
285
|
+
`};`,
|
|
286
|
+
`const configuredReporters = normalized.reporter;`,
|
|
287
|
+
`const reporters = configuredReporters`,
|
|
288
|
+
` ? (typeof configuredReporters === 'string' ? [[configuredReporters]] : (Array.isArray(configuredReporters[0]) ? configuredReporters : [configuredReporters]))`,
|
|
289
|
+
` : [['list']];`,
|
|
290
|
+
`const coverageReporter = resolve(runtimeProjectRoot, '.supercov/playwrightReporter.js');`,
|
|
291
|
+
`export default { ...normalized, reporter: [...reporters, [coverageReporter]] };`,
|
|
292
|
+
"",
|
|
293
|
+
].join("\n"));
|
|
294
|
+
}
|
|
295
|
+
atomicWriteFileSync(generatedViteConfig, [
|
|
296
|
+
`import { loadConfigFromFile, mergeConfig } from 'vite';`,
|
|
297
|
+
`import { isAbsolute, relative, resolve } from 'node:path';`,
|
|
298
|
+
`import { mcdcVitePlugin } from '${pathToFileURL(resolve(packageSource, "vitePlugin.js")).href}';`,
|
|
299
|
+
`export default async function supercovViteConfig(env) {`,
|
|
300
|
+
` const loaded = await loadConfigFromFile(env, undefined, process.cwd());`,
|
|
301
|
+
` const originalRoot = ${JSON.stringify(root)};`,
|
|
302
|
+
` const isolatedRoot = ${JSON.stringify(isolatedRoot)};`,
|
|
303
|
+
` const relocate = (value, label) => {`,
|
|
304
|
+
` const absolute = isAbsolute(value) ? value : resolve(isolatedRoot, value);`,
|
|
305
|
+
` const alreadyIsolated = relative(isolatedRoot, absolute);`,
|
|
306
|
+
` if (alreadyIsolated === '' || (!alreadyIsolated.startsWith('..') && !isAbsolute(alreadyIsolated))) return absolute;`,
|
|
307
|
+
` const local = relative(originalRoot, absolute);`,
|
|
308
|
+
` if (local.startsWith('..') || isAbsolute(local)) throw new Error('Supercov refuses ' + label + ' outside the isolated project: ' + absolute);`,
|
|
309
|
+
` return resolve(isolatedRoot, local);`,
|
|
310
|
+
` };`,
|
|
311
|
+
` const config = loaded?.config ?? {};`,
|
|
312
|
+
` const relocateOutput = output => output ? ({ ...output, dir: output.dir ? relocate(output.dir, 'Rollup output') : output.dir, file: output.file ? relocate(output.file, 'Rollup output') : output.file }) : output;`,
|
|
313
|
+
` const rollupOutput = config.build?.rollupOptions?.output;`,
|
|
314
|
+
` const safe = { ...config, cacheDir: resolve(isolatedRoot, '.supercov/vite-cache'), build: { ...config.build, outDir: relocate(config.build?.outDir ?? 'dist', 'Vite build output'), rollupOptions: { ...config.build?.rollupOptions, output: Array.isArray(rollupOutput) ? rollupOutput.map(relocateOutput) : relocateOutput(rollupOutput) } } };`,
|
|
315
|
+
` return mergeConfig(safe, { plugins: [mcdcVitePlugin(${JSON.stringify({ root: isolatedRoot, sourceRoots: project.sourceRoots, manifestPath, buildOutputMetadataPath })})] });`,
|
|
316
|
+
`}`,
|
|
317
|
+
"",
|
|
318
|
+
].join("\n"));
|
|
319
|
+
atomicWriteFileSync(generatedVitestConfig, [
|
|
320
|
+
`import { loadConfigFromFile, mergeConfig } from 'vite';`,
|
|
321
|
+
`import { resolve } from 'node:path';`,
|
|
322
|
+
`import { mcdcVitePlugin } from '${pathToFileURL(resolve(packageSource, "vitePlugin.js")).href}';`,
|
|
323
|
+
`import SupercovVitestReporter from '${pathToFileURL(resolve(packageSource, "vitestReporter.js")).href}';`,
|
|
324
|
+
`const discoveredConfig = ${JSON.stringify(isolatedVitestConfig)};`,
|
|
325
|
+
`export default async function supercovVitestConfig(env) {`,
|
|
326
|
+
` const originalPath = process.env.SUPERCOV_ORIGINAL_VITEST_CONFIG || discoveredConfig;`,
|
|
327
|
+
` const loaded = originalPath ? await loadConfigFromFile(env, originalPath, process.cwd()) : undefined;`,
|
|
328
|
+
` const config = mergeConfig(loaded?.config ?? {}, {`,
|
|
329
|
+
` cacheDir: resolve(process.cwd(), '.supercov/vitest-cache'),`,
|
|
330
|
+
` plugins: ${project.buildAdapter === "vite" ? `[mcdcVitePlugin(${JSON.stringify({ root: isolatedRoot, sourceRoots: project.sourceRoots, manifestPath })})]` : "[]"},`,
|
|
331
|
+
` test: { setupFiles: [${JSON.stringify(resolve(packageSource, "vitest.js"))}], maxConcurrency: 1 },`,
|
|
332
|
+
` });`,
|
|
333
|
+
` const configuredReporters = loaded?.config?.test?.reporters;`,
|
|
334
|
+
` config.test ??= {};`,
|
|
335
|
+
` config.test.reporters = configuredReporters`,
|
|
336
|
+
` ? [...(Array.isArray(configuredReporters) ? configuredReporters : [configuredReporters]), new SupercovVitestReporter()]`,
|
|
337
|
+
` : ['default', new SupercovVitestReporter()];`,
|
|
338
|
+
` return config;`,
|
|
339
|
+
`}`,
|
|
340
|
+
"",
|
|
341
|
+
].join("\n"));
|
|
342
|
+
const coverageEnv = {
|
|
343
|
+
...process.env,
|
|
344
|
+
SUPERCOV_EVIDENCE_DIR: evidenceDirectoryRelative,
|
|
345
|
+
SUPERCOV_EXECUTION_FINGERPRINT: runIntegrity.fingerprint.execution,
|
|
346
|
+
SUPERCOV_EXECUTION_LOG: resolve(isolatedEvidenceDirectory, "execution.jsonl"),
|
|
347
|
+
SUPERCOV_RUN_ID: runId,
|
|
348
|
+
SUPERCOV_SERVER_EVIDENCE_ROOT: serverEvidenceRoot,
|
|
349
|
+
SUPERCOV_MANIFEST: manifestPath,
|
|
350
|
+
SUPERCOV_PLAYWRIGHT_MODULE: project.playwrightModule,
|
|
351
|
+
SUPERCOV_PLAYWRIGHT_TEST_EXPORT: project.playwrightTestExport,
|
|
352
|
+
SUPERCOV_PROJECT_ROOT: isolatedRoot,
|
|
353
|
+
SUPERCOV_SOURCE_PROJECT_ROOT: root,
|
|
354
|
+
...(project.buildAdapter === "direct"
|
|
355
|
+
? { SUPERCOV_DIRECT_INSTRUMENTATION: "1" }
|
|
356
|
+
: {}),
|
|
357
|
+
SUPERCOV_GENERATED_VITEST_CONFIG: generatedVitestConfig,
|
|
358
|
+
SUPERCOV_GENERATED_PLAYWRIGHT_CONFIG: generatedPlaywrightConfig,
|
|
359
|
+
...(isolatedPlaywrightConfig
|
|
360
|
+
? { SUPERCOV_ORIGINAL_PLAYWRIGHT_CONFIG: isolatedPlaywrightConfig }
|
|
361
|
+
: {}),
|
|
362
|
+
};
|
|
363
|
+
const testNodeOptions = [
|
|
364
|
+
process.env["NODE_OPTIONS"],
|
|
365
|
+
`--import=${pathToFileURL(resolve(generatedDirectory, "register.mjs")).href}`,
|
|
366
|
+
]
|
|
367
|
+
.filter(Boolean)
|
|
368
|
+
.join(" ");
|
|
369
|
+
timings.adapterSetupMs = performance.now() - phaseStarted;
|
|
370
|
+
updateRunState(root, runId, { status: "building" });
|
|
371
|
+
console.error(`[supercov] instrumenting isolated workspace ${isolatedRoot}`);
|
|
372
|
+
if (Object.keys(project.buildEnvironment).length > 0)
|
|
373
|
+
console.error(`[supercov] inferred build mode from command/config: ${Object.entries(project.buildEnvironment)
|
|
374
|
+
.map(([key, value]) => `${key}=${value}`)
|
|
375
|
+
.join(" ")}`);
|
|
376
|
+
phaseStarted = performance.now();
|
|
377
|
+
if (reusableBuild) {
|
|
378
|
+
console.error(`[supercov] reusing exact-fingerprint instrumented build ${buildCacheKey.slice(0, 12)}`);
|
|
379
|
+
buildResult = { status: 0, signal: null };
|
|
380
|
+
}
|
|
381
|
+
else if (project.buildAdapter === "direct") {
|
|
382
|
+
instrumentDirectWorkspace(isolatedRoot, project.sourceRoots, manifestPath);
|
|
383
|
+
buildResult = { status: 0, signal: null };
|
|
384
|
+
}
|
|
385
|
+
else {
|
|
386
|
+
buildResult = await runChild(project.buildCommand[0], [
|
|
387
|
+
...project.buildCommand.slice(1),
|
|
388
|
+
"--",
|
|
389
|
+
"--config",
|
|
390
|
+
".supercov/vite.config.mjs",
|
|
391
|
+
], {
|
|
392
|
+
cwd: isolatedRoot,
|
|
393
|
+
env: {
|
|
394
|
+
...coverageEnv,
|
|
395
|
+
...project.buildEnvironment,
|
|
396
|
+
NODE_ENV: "production",
|
|
397
|
+
},
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
if (buildResult.status === 0 &&
|
|
401
|
+
project.buildAdapter === "vite" &&
|
|
402
|
+
!reusableBuild) {
|
|
403
|
+
writeInstrumentedBuildCache(isolatedRoot, buildCacheKey);
|
|
404
|
+
}
|
|
405
|
+
timings.instrumentedBuildMs = performance.now() - phaseStarted;
|
|
406
|
+
if (receivedSignal)
|
|
407
|
+
throw new Error(`Interrupted by ${receivedSignal}`);
|
|
408
|
+
if (buildResult.error)
|
|
409
|
+
throw buildResult.error;
|
|
410
|
+
if (buildResult.status === 0) {
|
|
411
|
+
updateRunState(root, runId, { status: "testing" });
|
|
412
|
+
console.error(`[supercov] running in isolated workspace: ${command.join(" ")}`);
|
|
413
|
+
phaseStarted = performance.now();
|
|
414
|
+
testResult = await runChild(command[0], command.slice(1), {
|
|
415
|
+
cwd: isolatedRoot,
|
|
416
|
+
env: {
|
|
417
|
+
...coverageEnv,
|
|
418
|
+
NODE_OPTIONS: testNodeOptions,
|
|
419
|
+
SUPERCOV_CJS_INTERCEPT: "1",
|
|
420
|
+
},
|
|
421
|
+
});
|
|
422
|
+
timings.testCommandMs = performance.now() - phaseStarted;
|
|
423
|
+
if (receivedSignal)
|
|
424
|
+
throw new Error(`Interrupted by ${receivedSignal}`);
|
|
425
|
+
if (testResult.error)
|
|
426
|
+
throw testResult.error;
|
|
427
|
+
updateRunState(root, runId, { status: "publishing" });
|
|
428
|
+
phaseStarted = performance.now();
|
|
429
|
+
rmSync(persistedEvidenceDirectory, { recursive: true, force: true });
|
|
430
|
+
if (existsSync(isolatedEvidenceDirectory))
|
|
431
|
+
atomicRenameSync(isolatedEvidenceDirectory, persistedEvidenceDirectory);
|
|
432
|
+
try {
|
|
433
|
+
rmSync(runStagingDirectory, { recursive: true, force: true });
|
|
434
|
+
const evidenceArchivePath = resolve(runStagingDirectory, "evidence.raw.gz");
|
|
435
|
+
const rawEvidence = writeEvidenceArchive([
|
|
436
|
+
{ file: manifestPath, path: "manifest.json" },
|
|
437
|
+
{ directory: persistedEvidenceDirectory },
|
|
438
|
+
{
|
|
439
|
+
directory: resolve(serverEvidenceRoot, runId),
|
|
440
|
+
prefix: "server",
|
|
441
|
+
},
|
|
442
|
+
], evidenceArchivePath);
|
|
443
|
+
printCoverageSummary(analyzeCoverageArchive(evidenceArchivePath, {
|
|
444
|
+
runId,
|
|
445
|
+
testExitCode: testResult.status,
|
|
446
|
+
integrity: runIntegrity,
|
|
447
|
+
generatedAt: startedAt,
|
|
448
|
+
}));
|
|
449
|
+
timings.evidencePublicationMs = performance.now() - phaseStarted;
|
|
450
|
+
atomicWriteFileSync(resolve(runStagingDirectory, "run.json"), `${JSON.stringify({
|
|
451
|
+
id: runId,
|
|
452
|
+
startedAt,
|
|
453
|
+
durationMs: Date.now() - runStartedAt,
|
|
454
|
+
command,
|
|
455
|
+
testExitCode: testResult.status,
|
|
456
|
+
integrity: runIntegrity,
|
|
457
|
+
rawEvidence,
|
|
458
|
+
isolatedBuild: true,
|
|
459
|
+
instrumentedBuildCache: {
|
|
460
|
+
key: buildCacheKey,
|
|
461
|
+
reused: Boolean(reusableBuild),
|
|
462
|
+
},
|
|
463
|
+
timings: roundedTimings(timings),
|
|
464
|
+
}, null, 2)}\n`);
|
|
465
|
+
atomicRenameSync(runStagingDirectory, storedRunDirectory);
|
|
466
|
+
runPublished = true;
|
|
467
|
+
console.log(`[coverage] evidence: ${resolve(storedRunDirectory, "evidence.raw.gz")}`);
|
|
468
|
+
}
|
|
469
|
+
catch (error) {
|
|
470
|
+
timings.evidencePublicationMs = performance.now() - phaseStarted;
|
|
471
|
+
publicationFailed = true;
|
|
472
|
+
console.error("[supercov] failed to publish coverage evidence", error);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
const resultCode = exitCode(buildResult) || exitCode(testResult) || (publicationFailed ? 1 : 0);
|
|
476
|
+
updateRunState(root, runId, { status: resultCode === 0 ? "complete" : "failed" });
|
|
477
|
+
if (runPublished && !finalizePublishedRunStorage(root, runId))
|
|
478
|
+
throw new Error(`Published run ${runId} is missing a durable artifact`);
|
|
479
|
+
console.error(`[supercov] timings ${formatTimings(timings, performance.now() - runStartedMonotonic)}`);
|
|
480
|
+
timingsPrinted = true;
|
|
481
|
+
return resultCode;
|
|
482
|
+
}
|
|
483
|
+
catch (error) {
|
|
484
|
+
const status = receivedSignal ? "interrupted" : "failed";
|
|
485
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
486
|
+
try {
|
|
487
|
+
updateRunState(root, runId, {
|
|
488
|
+
status,
|
|
489
|
+
...(receivedSignal ? { signal: receivedSignal } : {}),
|
|
490
|
+
error: message,
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
catch {
|
|
494
|
+
// The original error remains authoritative.
|
|
495
|
+
}
|
|
496
|
+
if (!receivedSignal)
|
|
497
|
+
console.error(`[supercov] ${message}`);
|
|
498
|
+
return receivedSignal ? signalExitCode(receivedSignal) : 1;
|
|
499
|
+
}
|
|
500
|
+
finally {
|
|
501
|
+
if (!timingsPrinted)
|
|
502
|
+
console.error(`[supercov] timings ${formatTimings(timings, performance.now() - runStartedMonotonic)}`);
|
|
503
|
+
if (signalEscalation) {
|
|
504
|
+
clearTimeout(signalEscalation);
|
|
505
|
+
signalEscalation = undefined;
|
|
506
|
+
}
|
|
507
|
+
for (const [signal, handler] of signalHandlers)
|
|
508
|
+
process.removeListener(signal, handler);
|
|
509
|
+
try {
|
|
510
|
+
rmSync(runStagingDirectory, { recursive: true, force: true });
|
|
511
|
+
rmSync(resolve(serverEvidenceRoot, runId), {
|
|
512
|
+
recursive: true,
|
|
513
|
+
force: true,
|
|
514
|
+
});
|
|
515
|
+
// The stable isolated namespace is a deliberate build/snapshot cache.
|
|
516
|
+
// It never overlaps the user's ordinary build and `supercov clean`
|
|
517
|
+
// removes it deterministically.
|
|
518
|
+
}
|
|
519
|
+
catch (error) {
|
|
520
|
+
console.error(`[supercov] isolated workspace cleanup failed: ${String(error)}`);
|
|
521
|
+
}
|
|
522
|
+
lock.release();
|
|
523
|
+
}
|
|
524
|
+
}
|
|
10
525
|
const commandArgs = process.argv.slice(2);
|
|
11
526
|
const rawArgs = commandArgs[0] === "--help" || commandArgs[0] === "-h"
|
|
12
527
|
? ["help", ...commandArgs.slice(1)]
|
|
13
528
|
: commandArgs;
|
|
14
529
|
const queryCommand = rawArgs[0];
|
|
15
|
-
if (queryCommand
|
|
530
|
+
if (queryCommand === "clean" || queryCommand === "prune") {
|
|
531
|
+
try {
|
|
532
|
+
if (queryCommand === "clean")
|
|
533
|
+
cleanCommand(rawArgs.slice(1));
|
|
534
|
+
else
|
|
535
|
+
pruneCommand(rawArgs.slice(1));
|
|
536
|
+
}
|
|
537
|
+
catch (error) {
|
|
538
|
+
console.error(`[supercov] ${error instanceof Error ? error.message : String(error)}`);
|
|
539
|
+
process.exitCode = 2;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
else if (queryCommand && coverageQueryCommands.has(queryCommand)) {
|
|
16
543
|
try {
|
|
17
544
|
await runQueryCommand(queryCommand, rawArgs.slice(1));
|
|
18
545
|
}
|
|
@@ -30,216 +557,10 @@ else {
|
|
|
30
557
|
const command = separator >= 0 ? process.argv.slice(separator + 1) : [];
|
|
31
558
|
if (command.length === 0) {
|
|
32
559
|
console.error("Usage: supercov -- <test command>");
|
|
33
|
-
process.
|
|
34
|
-
}
|
|
35
|
-
const root = process.cwd();
|
|
36
|
-
const project = discoverCoverageProject(root);
|
|
37
|
-
const runId = new Date().toISOString().replace(/[:.]/g, "-");
|
|
38
|
-
const runStartedAt = Date.now();
|
|
39
|
-
const generatedDirectory = resolve(root, ".supercov");
|
|
40
|
-
const evidenceDirectoryRelative = `.supercov/evidence/${runId}`;
|
|
41
|
-
const evidenceDirectory = resolve(root, evidenceDirectoryRelative);
|
|
42
|
-
const generatedPlaywrightConfig = resolve(generatedDirectory, "playwright.config.mjs");
|
|
43
|
-
const generatedViteConfig = resolve(generatedDirectory, "vite.config.mjs");
|
|
44
|
-
const generatedVitestConfig = resolve(generatedDirectory, "vitest.config.mjs");
|
|
45
|
-
const manifestRelative = `.supercov/work/${runId}/mcdc-manifest.json`;
|
|
46
|
-
const manifestPath = resolve(root, manifestRelative);
|
|
47
|
-
const packageSource = fileURLToPath(new URL(".", import.meta.url));
|
|
48
|
-
const runIntegrity = createRunIntegrity(root, project, packageSource);
|
|
49
|
-
mkdirSync(generatedDirectory, { recursive: true });
|
|
50
|
-
mkdirSync(evidenceDirectory, { recursive: true });
|
|
51
|
-
for (const file of [
|
|
52
|
-
"playwright.js",
|
|
53
|
-
"playwrightReporter.js",
|
|
54
|
-
"provenance.js",
|
|
55
|
-
"register.mjs",
|
|
56
|
-
"resolve-loader.mjs",
|
|
57
|
-
"transport.js",
|
|
58
|
-
"types.js",
|
|
59
|
-
]) {
|
|
60
|
-
copyFileSync(resolve(packageSource, file), resolve(generatedDirectory, file));
|
|
61
|
-
}
|
|
62
|
-
const generatedPlaywrightAdapter = resolve(generatedDirectory, "playwright.js");
|
|
63
|
-
const generatedPlaywrightReporter = resolve(generatedDirectory, "playwrightReporter.js");
|
|
64
|
-
writeFileSync(generatedPlaywrightAdapter, readFileSync(generatedPlaywrightAdapter, "utf8")
|
|
65
|
-
.replace("__SUPERCOV_EVIDENCE_DIRECTORY__", evidenceDirectoryRelative)
|
|
66
|
-
.replace("__SUPERCOV_PLAYWRIGHT_MODULE__", project.playwrightModule)
|
|
67
|
-
.replace("__SUPERCOV_RUN_ID__", runId));
|
|
68
|
-
writeFileSync(generatedPlaywrightReporter, readFileSync(generatedPlaywrightReporter, "utf8").replace("__SUPERCOV_EVIDENCE_DIRECTORY__", evidenceDirectoryRelative));
|
|
69
|
-
const generatedResolveLoader = resolve(generatedDirectory, "resolve-loader.mjs");
|
|
70
|
-
writeFileSync(generatedResolveLoader, readFileSync(generatedResolveLoader, "utf8").replace("__SUPERCOV_PLAYWRIGHT_MODULE__", project.playwrightModule));
|
|
71
|
-
if (project.playwrightConfig) {
|
|
72
|
-
const configImport = project.essentialOffline
|
|
73
|
-
? `../${relative(root, project.playwrightConfig).split(sep).join("/")}`
|
|
74
|
-
: pathToFileURL(project.playwrightConfig).href;
|
|
75
|
-
writeFileSync(generatedPlaywrightConfig, [
|
|
76
|
-
`import './register.mjs';`,
|
|
77
|
-
`import { isAbsolute, resolve } from 'node:path';`,
|
|
78
|
-
`import original from '${configImport}';`,
|
|
79
|
-
`const resolved = typeof original === 'function' ? await original({ command: 'test', mode: 'test' }) : original;`,
|
|
80
|
-
`const originalDirectory = ${JSON.stringify(dirname(project.playwrightConfig))};`,
|
|
81
|
-
`const absoluteFromOriginal = value => value && !isAbsolute(value) ? resolve(originalDirectory, value) : value;`,
|
|
82
|
-
`const normalizeWebServer = server => server ? ({ ...server, cwd: absoluteFromOriginal(server.cwd ?? originalDirectory) }) : server;`,
|
|
83
|
-
`const normalized = { ...resolved,`,
|
|
84
|
-
` testDir: absoluteFromOriginal(resolved?.testDir),`,
|
|
85
|
-
` outputDir: absoluteFromOriginal(resolved?.outputDir),`,
|
|
86
|
-
` snapshotDir: absoluteFromOriginal(resolved?.snapshotDir),`,
|
|
87
|
-
` projects: resolved?.projects?.map(project => ({ ...project, testDir: absoluteFromOriginal(project.testDir), outputDir: absoluteFromOriginal(project.outputDir), snapshotDir: absoluteFromOriginal(project.snapshotDir) })),`,
|
|
88
|
-
` webServer: Array.isArray(resolved?.webServer) ? resolved.webServer.map(normalizeWebServer) : normalizeWebServer(resolved?.webServer),`,
|
|
89
|
-
`};`,
|
|
90
|
-
`const configuredReporters = normalized.reporter;`,
|
|
91
|
-
`const reporters = configuredReporters`,
|
|
92
|
-
` ? (typeof configuredReporters === 'string' ? [[configuredReporters]] : (Array.isArray(configuredReporters[0]) ? configuredReporters : [configuredReporters]))`,
|
|
93
|
-
` : [['list']];`,
|
|
94
|
-
`const coverageReporter = process.env.TEST_IN_CONTAINER === 'true'`,
|
|
95
|
-
` ? '/workspace/.supercov/playwrightReporter.js'`,
|
|
96
|
-
` : ${JSON.stringify(resolve(packageSource, "playwrightReporter.js"))};`,
|
|
97
|
-
`export default { ...normalized, reporter: [...reporters, [coverageReporter]] };`,
|
|
98
|
-
"",
|
|
99
|
-
].join("\n"));
|
|
560
|
+
process.exitCode = 2;
|
|
100
561
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
`import { mcdcVitePlugin } from '${pathToFileURL(resolve(packageSource, "vitePlugin.js")).href}';`,
|
|
104
|
-
`export default async function supercovViteConfig(env) {`,
|
|
105
|
-
` const loaded = await loadConfigFromFile(env, undefined, process.cwd());`,
|
|
106
|
-
` return mergeConfig(loaded?.config ?? {}, { plugins: [mcdcVitePlugin(${JSON.stringify({ root, sourceRoots: project.sourceRoots, manifestPath })})] });`,
|
|
107
|
-
`}`,
|
|
108
|
-
"",
|
|
109
|
-
].join("\n"));
|
|
110
|
-
writeFileSync(generatedVitestConfig, [
|
|
111
|
-
`import { loadConfigFromFile, mergeConfig } from 'vite';`,
|
|
112
|
-
`import { mcdcVitePlugin } from '${pathToFileURL(resolve(packageSource, "vitePlugin.js")).href}';`,
|
|
113
|
-
`import SupercovVitestReporter from '${pathToFileURL(resolve(packageSource, "vitestReporter.js")).href}';`,
|
|
114
|
-
`const discoveredConfig = ${JSON.stringify(project.vitestConfig)};`,
|
|
115
|
-
`export default async function supercovVitestConfig(env) {`,
|
|
116
|
-
` const originalPath = process.env.SUPERCOV_ORIGINAL_VITEST_CONFIG || discoveredConfig;`,
|
|
117
|
-
` const loaded = originalPath ? await loadConfigFromFile(env, originalPath, process.cwd()) : undefined;`,
|
|
118
|
-
` const config = mergeConfig(loaded?.config ?? {}, {`,
|
|
119
|
-
` plugins: [mcdcVitePlugin(${JSON.stringify({ root, sourceRoots: project.sourceRoots, manifestPath })})],`,
|
|
120
|
-
` test: { setupFiles: [${JSON.stringify(resolve(packageSource, "vitest.js"))}], maxConcurrency: 1 },`,
|
|
121
|
-
` });`,
|
|
122
|
-
` const configuredReporters = loaded?.config?.test?.reporters;`,
|
|
123
|
-
` config.test ??= {};`,
|
|
124
|
-
` config.test.reporters = configuredReporters`,
|
|
125
|
-
` ? [...(Array.isArray(configuredReporters) ? configuredReporters : [configuredReporters]), new SupercovVitestReporter()]`,
|
|
126
|
-
` : ['default', new SupercovVitestReporter()];`,
|
|
127
|
-
` return config;`,
|
|
128
|
-
`}`,
|
|
129
|
-
"",
|
|
130
|
-
].join("\n"));
|
|
131
|
-
const coverageEnv = {
|
|
132
|
-
...process.env,
|
|
133
|
-
TEST_MCDC: "true",
|
|
134
|
-
TEST_OFFLINE_RESULTS_RUN_ID: runId,
|
|
135
|
-
SUPERCOV_EVIDENCE_DIR: evidenceDirectoryRelative,
|
|
136
|
-
SUPERCOV_RUN_ID: runId,
|
|
137
|
-
SUPERCOV_MANIFEST: manifestRelative,
|
|
138
|
-
SUPERCOV_PLAYWRIGHT_MODULE: project.playwrightModule,
|
|
139
|
-
SUPERCOV_PROJECT_ROOT: root,
|
|
140
|
-
SUPERCOV_GENERATED_VITEST_CONFIG: generatedVitestConfig,
|
|
141
|
-
SUPERCOV_GENERATED_PLAYWRIGHT_CONFIG: generatedPlaywrightConfig,
|
|
142
|
-
};
|
|
143
|
-
const testNodeOptions = [
|
|
144
|
-
process.env["NODE_OPTIONS"],
|
|
145
|
-
`--import=${pathToFileURL(resolve(generatedDirectory, "register.mjs")).href}`,
|
|
146
|
-
]
|
|
147
|
-
.filter(Boolean)
|
|
148
|
-
.join(" ");
|
|
149
|
-
console.error(`[supercov] instrumenting ${root}`);
|
|
150
|
-
const instrumentedBuild = spawnSync(project.buildCommand[0], [
|
|
151
|
-
...project.buildCommand.slice(1),
|
|
152
|
-
"--",
|
|
153
|
-
"--config",
|
|
154
|
-
".supercov/vite.config.mjs",
|
|
155
|
-
], {
|
|
156
|
-
cwd: root,
|
|
157
|
-
env: {
|
|
158
|
-
...coverageEnv,
|
|
159
|
-
...(project.essentialOffline ? { TEST_OFFLINE: "true" } : {}),
|
|
160
|
-
NODE_ENV: "production",
|
|
161
|
-
},
|
|
162
|
-
stdio: "inherit",
|
|
163
|
-
});
|
|
164
|
-
let testRun;
|
|
165
|
-
let reportFailed = false;
|
|
166
|
-
if (!instrumentedBuild.error && instrumentedBuild.status === 0) {
|
|
167
|
-
// The Essential offline VM pool keys its snapshot on dependency overlays,
|
|
168
|
-
// not the mounted application build. A tiny build-hash overlay invalidates
|
|
169
|
-
// the warm snapshot exactly when the instrumented bundle changes. Other
|
|
170
|
-
// runners simply ignore these environment variables.
|
|
171
|
-
const overlayPackage = "essential-seo/.mcdc-pool";
|
|
172
|
-
if (project.essentialOffline) {
|
|
173
|
-
const overlayDist = resolve(root, ".mcdc-pool/dist");
|
|
174
|
-
const overlayMarker = resolve(overlayDist, "instrumented-build.sha256");
|
|
175
|
-
const bundleHash = createHash("sha256")
|
|
176
|
-
.update(readFileSync(resolve(root, "build/server/index.js")))
|
|
177
|
-
.update(readFileSync(manifestPath))
|
|
178
|
-
.digest("hex");
|
|
179
|
-
mkdirSync(overlayDist, { recursive: true });
|
|
180
|
-
if (!existsSync(overlayMarker) ||
|
|
181
|
-
readFileSync(overlayMarker, "utf8").trim() !== bundleHash) {
|
|
182
|
-
writeFileSync(overlayMarker, `${bundleHash}\n`);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
console.error(`[supercov] running: ${command.join(" ")}`);
|
|
186
|
-
testRun = spawnSync(command[0], command.slice(1), {
|
|
187
|
-
cwd: root,
|
|
188
|
-
stdio: "inherit",
|
|
189
|
-
env: {
|
|
190
|
-
...coverageEnv,
|
|
191
|
-
NODE_OPTIONS: testNodeOptions,
|
|
192
|
-
...(!project.essentialOffline
|
|
193
|
-
? {
|
|
194
|
-
SUPERCOV_CJS_INTERCEPT: "1",
|
|
195
|
-
}
|
|
196
|
-
: {}),
|
|
197
|
-
...(project.essentialOffline
|
|
198
|
-
? {
|
|
199
|
-
TEST_OFFLINE_LOCAL_OVERLAY: "true",
|
|
200
|
-
TEST_OFFLINE_LOCAL_OVERLAY_PKGS: overlayPackage,
|
|
201
|
-
TEST_PLAYWRIGHT_CONFIG: ".supercov/playwright.config.mjs",
|
|
202
|
-
}
|
|
203
|
-
: {}),
|
|
204
|
-
},
|
|
205
|
-
});
|
|
206
|
-
try {
|
|
207
|
-
writeMcdcReport(evidenceDirectory, runId, runStartedAt, manifestPath, testRun.status, runIntegrity);
|
|
208
|
-
const storedRunDirectory = resolve(root, ".supercov/runs", runId);
|
|
209
|
-
writeFileSync(resolve(storedRunDirectory, "run.json"), `${JSON.stringify({
|
|
210
|
-
id: runId,
|
|
211
|
-
startedAt: new Date(runStartedAt).toISOString(),
|
|
212
|
-
durationMs: Date.now() - runStartedAt,
|
|
213
|
-
command,
|
|
214
|
-
testExitCode: testRun.status,
|
|
215
|
-
integrity: runIntegrity,
|
|
216
|
-
}, null, 2)}\n`);
|
|
217
|
-
}
|
|
218
|
-
catch (error) {
|
|
219
|
-
reportFailed = true;
|
|
220
|
-
console.error("[supercov] failed to generate report", error);
|
|
221
|
-
}
|
|
562
|
+
else {
|
|
563
|
+
process.exitCode = await createCoverageRun(command);
|
|
222
564
|
}
|
|
223
|
-
console.error("[supercov] restoring the ordinary build");
|
|
224
|
-
const restore = spawnSync(project.buildCommand[0], project.buildCommand.slice(1), {
|
|
225
|
-
cwd: root,
|
|
226
|
-
env: {
|
|
227
|
-
...process.env,
|
|
228
|
-
...(project.essentialOffline ? { TEST_OFFLINE: "true" } : {}),
|
|
229
|
-
NODE_ENV: "production",
|
|
230
|
-
},
|
|
231
|
-
stdio: "inherit",
|
|
232
|
-
});
|
|
233
|
-
if (instrumentedBuild.error)
|
|
234
|
-
throw instrumentedBuild.error;
|
|
235
|
-
if (testRun?.error)
|
|
236
|
-
throw testRun.error;
|
|
237
|
-
if (restore.error)
|
|
238
|
-
throw restore.error;
|
|
239
|
-
process.exitCode =
|
|
240
|
-
instrumentedBuild.status ||
|
|
241
|
-
testRun?.status ||
|
|
242
|
-
restore.status ||
|
|
243
|
-
(reportFailed ? 1 : 0);
|
|
244
565
|
}
|
|
245
566
|
//# sourceMappingURL=cli.js.map
|