@fairfox/polly 0.91.0 → 0.92.1
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/dist/cli/polly.js +3 -3
- package/dist/cli/polly.js.map +3 -3
- package/dist/gallery/gallery.css +2 -2
- package/dist/gallery/gallery.js +248 -241
- package/dist/src/background/index.js +27 -11
- package/dist/src/background/index.js.map +2 -2
- package/dist/src/background/message-router.js +27 -11
- package/dist/src/background/message-router.js.map +2 -2
- package/dist/src/client/index.js +56 -56
- package/dist/src/client/index.js.map +4 -4
- package/dist/src/elysia/index.js +16 -7
- package/dist/src/elysia/index.js.map +4 -4
- package/dist/src/elysia/plugin.d.ts +4 -4
- package/dist/src/index.js +27 -11
- package/dist/src/index.js.map +2 -2
- package/dist/src/mesh.js +2 -2
- package/dist/src/mesh.js.map +3 -3
- package/dist/src/peer.js +6 -6
- package/dist/src/peer.js.map +3 -3
- package/dist/src/polly-ui/index.css +2 -2
- package/dist/src/polly-ui/markdown.js +1844 -1010
- package/dist/src/polly-ui/markdown.js.map +5 -5
- package/dist/src/polly-ui/styles.css +2 -2
- package/dist/src/shared/adapters/index.js +27 -11
- package/dist/src/shared/adapters/index.js.map +2 -2
- package/dist/src/shared/lib/context-helpers.js +27 -11
- package/dist/src/shared/lib/context-helpers.js.map +2 -2
- package/dist/src/shared/lib/message-bus.js +27 -11
- package/dist/src/shared/lib/message-bus.js.map +2 -2
- package/dist/src/shared/lib/resource.js +17 -1
- package/dist/src/shared/lib/resource.js.map +2 -2
- package/dist/src/shared/lib/state.js +17 -1
- package/dist/src/shared/lib/state.js.map +2 -2
- package/dist/src/shared/state/app-state.js +17 -1
- package/dist/src/shared/state/app-state.js.map +2 -2
- package/dist/tools/bdd/src/cli.js +20 -2
- package/dist/tools/bdd/src/cli.js.map +4 -4
- package/dist/tools/bdd/src/index.js +20 -2
- package/dist/tools/bdd/src/index.js.map +3 -3
- package/dist/tools/init/templates/pwa/package.json.template +2 -3
- package/dist/tools/mutate/src/args.d.ts +25 -3
- package/dist/tools/mutate/src/cli.js +3238 -312
- package/dist/tools/mutate/src/cli.js.map +32 -12
- package/dist/tools/mutate/src/config.d.ts +13 -2
- package/dist/tools/mutate/src/define.d.ts +76 -0
- package/dist/tools/mutate/src/engine/ast.d.ts +13 -0
- package/dist/tools/mutate/src/engine/baseline.d.ts +20 -0
- package/dist/tools/mutate/src/engine/cache.d.ts +32 -0
- package/dist/tools/mutate/src/engine/coverage.d.ts +60 -0
- package/dist/tools/mutate/src/engine/generate.d.ts +23 -0
- package/dist/tools/mutate/src/engine/graph.d.ts +11 -0
- package/dist/tools/mutate/src/engine/index.d.ts +16 -0
- package/dist/tools/mutate/src/engine/junit.d.ts +38 -0
- package/dist/tools/mutate/src/engine/operators.d.ts +14 -0
- package/dist/tools/mutate/src/engine/preload.d.ts +10 -0
- package/dist/tools/mutate/src/engine/preload.js +180 -0
- package/dist/tools/mutate/src/engine/preload.js.map +10 -0
- package/dist/tools/mutate/src/engine/probes.d.ts +12 -0
- package/dist/tools/mutate/src/engine/report-writer.d.ts +44 -0
- package/dist/tools/mutate/src/engine/runner.d.ts +31 -0
- package/dist/tools/mutate/src/engine/stray-writes.d.ts +25 -0
- package/dist/tools/mutate/src/engine/typecheck.d.ts +37 -0
- package/dist/tools/mutate/src/engine/types.d.ts +191 -0
- package/dist/tools/mutate/src/engine/verify-callsites.d.ts +9 -0
- package/dist/tools/mutate/src/engine/workdir.d.ts +16 -0
- package/dist/tools/mutate/src/engine-config.d.ts +7 -0
- package/dist/tools/mutate/src/index.d.ts +7 -3
- package/dist/tools/mutate/src/index.js +2821 -193
- package/dist/tools/mutate/src/index.js.map +26 -7
- package/dist/tools/mutate/src/ingest.d.ts +30 -1
- package/dist/tools/mutate/src/init.d.ts +2 -1
- package/dist/tools/mutate/src/migrate.d.ts +32 -0
- package/dist/tools/mutate/src/report.d.ts +3 -0
- package/dist/tools/mutate/src/run.d.ts +17 -8
- package/dist/tools/mutate/src/verify-matrix.d.ts +12 -15
- package/dist/tools/quality/src/cli.js +9 -9
- package/dist/tools/quality/src/cli.js.map +4 -4
- package/dist/tools/quality/src/index.js +9 -9
- package/dist/tools/quality/src/index.js.map +4 -4
- package/dist/tools/quality/src/no-tautology-ensures.d.ts +2 -2
- package/dist/tools/quality/src/plugins/import-checks.d.ts +1 -1
- package/dist/tools/test/src/adapters/index.js +3 -2
- package/dist/tools/test/src/adapters/index.js.map +3 -3
- package/dist/tools/test/src/coverage-policy/cli.d.ts +2 -2
- package/dist/tools/test/src/coverage-policy/cli.js +217 -17
- package/dist/tools/test/src/coverage-policy/cli.js.map +7 -5
- package/dist/tools/test/src/coverage-policy/index.d.ts +1 -1
- package/dist/tools/test/src/coverage-policy/index.js +213 -12
- package/dist/tools/test/src/coverage-policy/index.js.map +7 -5
- package/dist/tools/test/src/coverage-policy/mutate-targets.d.ts +29 -12
- package/dist/tools/test/src/e2e-mesh/index.js +6 -6
- package/dist/tools/test/src/e2e-mesh/index.js.map +4 -4
- package/dist/tools/test/src/e2e-relay/index.js +230 -226
- package/dist/tools/test/src/e2e-relay/index.js.map +7 -7
- package/dist/tools/test/src/index.js +3 -2
- package/dist/tools/test/src/index.js.map +3 -3
- package/dist/tools/test/src/visual/index.js +684 -511
- package/dist/tools/test/src/visual/index.js.map +11 -11
- package/dist/tools/verify/src/cli.js +17 -2
- package/dist/tools/verify/src/cli.js.map +4 -4
- package/dist/tools/verify/src/config.d.ts +1 -1
- package/dist/tools/verify/src/config.js +68 -3
- package/dist/tools/verify/src/config.js.map +4 -4
- package/dist/tools/verify/src/primitives/index.d.ts +34 -0
- package/dist/tools/visualize/src/cli.js +16 -1
- package/dist/tools/visualize/src/cli.js.map +3 -3
- package/package.json +29 -39
- package/dist/tools/verify/src/stryker/index.d.ts +0 -32
- package/dist/tools/verify/src/stryker/index.js +0 -86
- package/dist/tools/verify/src/stryker/index.js.map +0 -10
|
@@ -9,6 +9,10 @@ function buildDb(report, dbPath = ":memory:") {
|
|
|
9
9
|
db.exec(`
|
|
10
10
|
DROP TABLE IF EXISTS kills;
|
|
11
11
|
DROP TABLE IF EXISTS covers;
|
|
12
|
+
DROP TABLE IF EXISTS contract_kills;
|
|
13
|
+
DROP TABLE IF EXISTS contract_sites;
|
|
14
|
+
DROP TABLE IF EXISTS contract_violations;
|
|
15
|
+
DROP TABLE IF EXISTS report_meta;
|
|
12
16
|
DROP TABLE IF EXISTS mutants;
|
|
13
17
|
DROP TABLE IF EXISTS tests;
|
|
14
18
|
CREATE TABLE tests (
|
|
@@ -22,8 +26,30 @@ function buildDb(report, dbPath = ":memory:") {
|
|
|
22
26
|
mutator TEXT NOT NULL,
|
|
23
27
|
file TEXT NOT NULL,
|
|
24
28
|
line INTEGER,
|
|
25
|
-
status TEXT NOT NULL
|
|
29
|
+
status TEXT NOT NULL,
|
|
30
|
+
contract_only INTEGER NOT NULL DEFAULT 0
|
|
26
31
|
);
|
|
32
|
+
-- requires/ensures sites and the mutants each one killed (bun engine, contracts on)
|
|
33
|
+
CREATE TABLE contract_sites (
|
|
34
|
+
id TEXT PRIMARY KEY,
|
|
35
|
+
file TEXT NOT NULL,
|
|
36
|
+
line INTEGER NOT NULL,
|
|
37
|
+
primitive TEXT NOT NULL,
|
|
38
|
+
message TEXT,
|
|
39
|
+
handler_from INTEGER,
|
|
40
|
+
handler_to INTEGER
|
|
41
|
+
);
|
|
42
|
+
CREATE TABLE contract_kills (
|
|
43
|
+
mutant_id TEXT NOT NULL REFERENCES mutants(id),
|
|
44
|
+
contract_id TEXT NOT NULL,
|
|
45
|
+
PRIMARY KEY (mutant_id, contract_id)
|
|
46
|
+
);
|
|
47
|
+
CREATE TABLE contract_violations (
|
|
48
|
+
target TEXT NOT NULL,
|
|
49
|
+
test TEXT NOT NULL,
|
|
50
|
+
contract_id TEXT
|
|
51
|
+
);
|
|
52
|
+
CREATE TABLE report_meta (key TEXT PRIMARY KEY, value TEXT NOT NULL);
|
|
27
53
|
-- the kill matrix: (mutant, test) where test detects mutant
|
|
28
54
|
CREATE TABLE kills (
|
|
29
55
|
mutant_id TEXT NOT NULL REFERENCES mutants(id),
|
|
@@ -40,221 +66,2817 @@ function buildDb(report, dbPath = ":memory:") {
|
|
|
40
66
|
CREATE INDEX idx_kills_mutant ON kills(mutant_id);
|
|
41
67
|
`);
|
|
42
68
|
const insTest = db.prepare("INSERT OR IGNORE INTO tests (id, file, name, line) VALUES (?, ?, ?, ?)");
|
|
43
|
-
const insMut = db.prepare("INSERT INTO mutants (id, mutator, file, line, status) VALUES (?, ?, ?, ?, ?)");
|
|
69
|
+
const insMut = db.prepare("INSERT INTO mutants (id, mutator, file, line, status, contract_only) VALUES (?, ?, ?, ?, ?, ?)");
|
|
44
70
|
const insKill = db.prepare("INSERT OR IGNORE INTO kills (mutant_id, test_id) VALUES (?, ?)");
|
|
45
71
|
const insCover = db.prepare("INSERT OR IGNORE INTO covers (mutant_id, test_id) VALUES (?, ?)");
|
|
72
|
+
const insContractKill = db.prepare("INSERT OR IGNORE INTO contract_kills (mutant_id, contract_id) VALUES (?, ?)");
|
|
73
|
+
const insSite = db.prepare("INSERT OR IGNORE INTO contract_sites (id, file, line, primitive, message, handler_from, handler_to) VALUES (?, ?, ?, ?, ?, ?, ?)");
|
|
74
|
+
const insViolation = db.prepare("INSERT INTO contract_violations (target, test, contract_id) VALUES (?, ?, ?)");
|
|
75
|
+
const insMeta = db.prepare("INSERT INTO report_meta (key, value) VALUES (?, ?)");
|
|
46
76
|
const ingest = db.transaction(() => {
|
|
47
77
|
for (const [file, tf] of Object.entries(report.testFiles ?? {})) {
|
|
48
78
|
for (const t of tf.tests) {
|
|
49
79
|
insTest.run(t.id, file || "(unknown)", t.name, t.location?.start.line ?? null);
|
|
50
80
|
}
|
|
51
81
|
}
|
|
52
|
-
for (const [file, f] of Object.entries(report.files)) {
|
|
53
|
-
for (const m of f.mutants) {
|
|
54
|
-
insMut.run(m.id, m.mutatorName, file, m.location.start.line, m.status);
|
|
55
|
-
for (const tid of m.killedBy ?? [])
|
|
56
|
-
insKill.run(m.id, tid);
|
|
57
|
-
for (const tid of m.coveredBy ?? [])
|
|
58
|
-
insCover.run(m.id, tid);
|
|
82
|
+
for (const [file, f] of Object.entries(report.files)) {
|
|
83
|
+
for (const m of f.mutants) {
|
|
84
|
+
insMut.run(m.id, m.mutatorName, file, m.location.start.line, m.status, m.contractOnly ? 1 : 0);
|
|
85
|
+
for (const tid of m.killedBy ?? [])
|
|
86
|
+
insKill.run(m.id, tid);
|
|
87
|
+
for (const tid of m.coveredBy ?? [])
|
|
88
|
+
insCover.run(m.id, tid);
|
|
89
|
+
for (const cid of m.killedByContract ?? [])
|
|
90
|
+
insContractKill.run(m.id, cid);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const contracts = report.contracts;
|
|
94
|
+
if (contracts) {
|
|
95
|
+
insMeta.run("contracts_mode", contracts.mode);
|
|
96
|
+
insMeta.run("runner", contracts.runner);
|
|
97
|
+
for (const s of contracts.sites) {
|
|
98
|
+
insSite.run(s.id, s.file, s.line, s.primitive, s.message ?? null, s.handler?.from ?? null, s.handler?.to ?? null);
|
|
99
|
+
}
|
|
100
|
+
for (const v of contracts.violations) {
|
|
101
|
+
if (v.contracts.length === 0)
|
|
102
|
+
insViolation.run(v.target, v.test, null);
|
|
103
|
+
for (const cid of v.contracts)
|
|
104
|
+
insViolation.run(v.target, v.test, cid);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
ingest();
|
|
109
|
+
return db;
|
|
110
|
+
}
|
|
111
|
+
function queryAll(db, sql) {
|
|
112
|
+
return db.query(sql).all();
|
|
113
|
+
}
|
|
114
|
+
function queryGet(db, sql) {
|
|
115
|
+
return db.query(sql).get();
|
|
116
|
+
}
|
|
117
|
+
async function loadMutationReport(path) {
|
|
118
|
+
return await Bun.file(path).json();
|
|
119
|
+
}
|
|
120
|
+
if (false) {}
|
|
121
|
+
|
|
122
|
+
// tools/mutate/src/decisions.ts
|
|
123
|
+
var DEFAULT_LOG = ".polly/test-debt/decisions.jsonl";
|
|
124
|
+
var VERDICTS = ["keep", "prune", "rewrite", "investigate"];
|
|
125
|
+
function normalizeSource(src) {
|
|
126
|
+
return src.replace(/\/\*[\s\S]*?\*\//g, "").replace(/(^|[^:])\/\/[^\n]*/g, "$1").replace(/\s+/g, " ").trim();
|
|
127
|
+
}
|
|
128
|
+
async function fileHash(file) {
|
|
129
|
+
const f = Bun.file(file);
|
|
130
|
+
if (!await f.exists())
|
|
131
|
+
return null;
|
|
132
|
+
return createHash("sha256").update(normalizeSource(await f.text())).digest("hex").slice(0, 16);
|
|
133
|
+
}
|
|
134
|
+
function fileSignals(db) {
|
|
135
|
+
const rows = queryAll(db, `
|
|
136
|
+
WITH file_kill AS (SELECT DISTINCT t.file file, k.mutant_id mid FROM kills k JOIN tests t ON t.id=k.test_id),
|
|
137
|
+
owner AS (SELECT mid, count(DISTINCT file) nf FROM file_kill GROUP BY mid)
|
|
138
|
+
SELECT fk.file file, count(DISTINCT fk.mid) kills,
|
|
139
|
+
sum(CASE WHEN o.nf=1 THEN 1 ELSE 0 END) unique_kills
|
|
140
|
+
FROM file_kill fk JOIN owner o ON o.mid=fk.mid GROUP BY fk.file`);
|
|
141
|
+
const m = new Map;
|
|
142
|
+
for (const r of rows)
|
|
143
|
+
m.set(r.file, {
|
|
144
|
+
kills: r.kills,
|
|
145
|
+
uniqueKills: r.unique_kills,
|
|
146
|
+
subsumed: r.kills > 0 && r.unique_kills === 0
|
|
147
|
+
});
|
|
148
|
+
return m;
|
|
149
|
+
}
|
|
150
|
+
async function loadDecisions(logPath) {
|
|
151
|
+
const m = new Map;
|
|
152
|
+
const f = Bun.file(logPath);
|
|
153
|
+
if (!await f.exists())
|
|
154
|
+
return m;
|
|
155
|
+
for (const line of (await f.text()).split(`
|
|
156
|
+
`)) {
|
|
157
|
+
const t = line.trim();
|
|
158
|
+
if (!t)
|
|
159
|
+
continue;
|
|
160
|
+
const d = JSON.parse(t);
|
|
161
|
+
m.set(d.file, d);
|
|
162
|
+
}
|
|
163
|
+
return m;
|
|
164
|
+
}
|
|
165
|
+
function staleReason(d, currentHash, current) {
|
|
166
|
+
if (!current)
|
|
167
|
+
return "file no longer kills any mutant (renamed/deleted/refactored?)";
|
|
168
|
+
if (d.hash !== currentHash)
|
|
169
|
+
return "test file changed since the decision";
|
|
170
|
+
if (d.signal.subsumed !== current.subsumed)
|
|
171
|
+
return `subsumption flipped (${d.signal.subsumed} → ${current.subsumed})`;
|
|
172
|
+
if (d.signal.uniqueKills > 0 !== current.uniqueKills > 0)
|
|
173
|
+
return `unique-kill status changed (${d.signal.uniqueKills} → ${current.uniqueKills})`;
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
async function status(db, logPath = DEFAULT_LOG) {
|
|
177
|
+
const signals = fileSignals(db);
|
|
178
|
+
const decisions = await loadDecisions(logPath);
|
|
179
|
+
const needsReview = [];
|
|
180
|
+
const stale = [];
|
|
181
|
+
const settled = [];
|
|
182
|
+
for (const [file, sig] of [...signals].sort((a, b) => b[1].kills - a[1].kills)) {
|
|
183
|
+
const d = decisions.get(file);
|
|
184
|
+
if (!d) {
|
|
185
|
+
if (sig.subsumed)
|
|
186
|
+
needsReview.push(` ${file} (${sig.kills} kills, 0 unique)`);
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
const reason = staleReason(d, await fileHash(file), sig);
|
|
190
|
+
if (reason)
|
|
191
|
+
stale.push(` ${file} [${d.verdict}] — ${reason}`);
|
|
192
|
+
else
|
|
193
|
+
settled.push(` ${file} [${d.verdict}] ${d.rationale ? `— ${d.rationale}` : ""}`);
|
|
194
|
+
}
|
|
195
|
+
for (const [file, d] of decisions) {
|
|
196
|
+
if (!signals.has(file))
|
|
197
|
+
stale.push(` ${file} [${d.verdict}] — file no longer in matrix`);
|
|
198
|
+
}
|
|
199
|
+
const out = [];
|
|
200
|
+
out.push(`NEEDS REVIEW (subsumed files with no decision — ${needsReview.length})`);
|
|
201
|
+
out.push(needsReview.length ? needsReview.join(`
|
|
202
|
+
`) : " none");
|
|
203
|
+
out.push(`
|
|
204
|
+
STALE (decision's basis drifted — re-review — ${stale.length})`);
|
|
205
|
+
out.push(stale.length ? stale.join(`
|
|
206
|
+
`) : " none");
|
|
207
|
+
out.push(`
|
|
208
|
+
SETTLED (fresh decisions, suppressed from action list — ${settled.length})`);
|
|
209
|
+
out.push(settled.length ? settled.join(`
|
|
210
|
+
`) : " none");
|
|
211
|
+
return out.join(`
|
|
212
|
+
`);
|
|
213
|
+
}
|
|
214
|
+
async function decide(file, verdict, rationale, db, logPath = DEFAULT_LOG) {
|
|
215
|
+
if (!VERDICTS.includes(verdict)) {
|
|
216
|
+
console.log(`✗ verdict must be one of: ${VERDICTS.join(", ")}`);
|
|
217
|
+
process.exit(1);
|
|
218
|
+
}
|
|
219
|
+
const sig = fileSignals(db).get(file);
|
|
220
|
+
if (!sig) {
|
|
221
|
+
console.log(`✗ ${file} is not a test file that kills any mutant in the current matrix`);
|
|
222
|
+
process.exit(1);
|
|
223
|
+
}
|
|
224
|
+
const decided = new Date().toISOString().slice(0, 10);
|
|
225
|
+
const record = {
|
|
226
|
+
file,
|
|
227
|
+
verdict,
|
|
228
|
+
rationale,
|
|
229
|
+
signal: sig,
|
|
230
|
+
hash: await fileHash(file),
|
|
231
|
+
decided
|
|
232
|
+
};
|
|
233
|
+
await Bun.write(logPath, (await Bun.file(logPath).exists() ? await Bun.file(logPath).text() : "") + JSON.stringify(record) + `
|
|
234
|
+
`);
|
|
235
|
+
console.log(`✓ recorded: ${file} [${verdict}]${rationale ? ` — ${rationale}` : ""}`);
|
|
236
|
+
}
|
|
237
|
+
if (false) {}
|
|
238
|
+
// src/shared/lib/guards.ts
|
|
239
|
+
function isRecord(input) {
|
|
240
|
+
return typeof input === "object" && input !== null && !Array.isArray(input);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// tools/mutate/src/define.ts
|
|
244
|
+
var MUTATION_CONFIG_FILE = "polly.mutate.ts";
|
|
245
|
+
function defineMutation(config) {
|
|
246
|
+
return config;
|
|
247
|
+
}
|
|
248
|
+
var MUTATOR_NAMES = new Set([
|
|
249
|
+
"ArithmeticOperator",
|
|
250
|
+
"ArrayDeclaration",
|
|
251
|
+
"ArrowFunction",
|
|
252
|
+
"AssignmentOperator",
|
|
253
|
+
"BlockStatement",
|
|
254
|
+
"BooleanLiteral",
|
|
255
|
+
"ConditionalExpression",
|
|
256
|
+
"EqualityOperator",
|
|
257
|
+
"LogicalOperator",
|
|
258
|
+
"MethodExpression",
|
|
259
|
+
"ObjectLiteral",
|
|
260
|
+
"OptionalChaining",
|
|
261
|
+
"Regex",
|
|
262
|
+
"StringLiteral",
|
|
263
|
+
"UnaryOperator",
|
|
264
|
+
"UpdateOperator"
|
|
265
|
+
]);
|
|
266
|
+
function isMutatorName(value) {
|
|
267
|
+
return typeof value === "string" && MUTATOR_NAMES.has(value);
|
|
268
|
+
}
|
|
269
|
+
function strings(value, where) {
|
|
270
|
+
if (!Array.isArray(value) || !value.every((v) => typeof v === "string")) {
|
|
271
|
+
throw new Error(`${where} must be an array of strings`);
|
|
272
|
+
}
|
|
273
|
+
return value;
|
|
274
|
+
}
|
|
275
|
+
function optionalNumber(value, where) {
|
|
276
|
+
if (value === undefined)
|
|
277
|
+
return;
|
|
278
|
+
if (typeof value !== "number" || !Number.isFinite(value))
|
|
279
|
+
throw new Error(`${where} must be a number`);
|
|
280
|
+
return value;
|
|
281
|
+
}
|
|
282
|
+
function optionalString(value, where) {
|
|
283
|
+
if (value === undefined)
|
|
284
|
+
return;
|
|
285
|
+
if (typeof value !== "string")
|
|
286
|
+
throw new Error(`${where} must be a string`);
|
|
287
|
+
return value;
|
|
288
|
+
}
|
|
289
|
+
function isEngineMode(v) {
|
|
290
|
+
return v === "fast" || v === "matrix";
|
|
291
|
+
}
|
|
292
|
+
function isCoverageMode(v) {
|
|
293
|
+
return v === "dynamic" || v === "static";
|
|
294
|
+
}
|
|
295
|
+
function parseRun(raw, where) {
|
|
296
|
+
const mode = raw["mode"];
|
|
297
|
+
if (mode !== undefined && !isEngineMode(mode)) {
|
|
298
|
+
throw new Error(`${where}: mode must be "fast" or "matrix"`);
|
|
299
|
+
}
|
|
300
|
+
const concurrency = optionalNumber(raw["concurrency"], `${where}: concurrency`);
|
|
301
|
+
if (concurrency !== undefined && !(Number.isInteger(concurrency) && concurrency > 0)) {
|
|
302
|
+
throw new Error(`${where}: concurrency must be a positive integer`);
|
|
303
|
+
}
|
|
304
|
+
const coverage = raw["coverage"];
|
|
305
|
+
if (coverage !== undefined && !isCoverageMode(coverage)) {
|
|
306
|
+
throw new Error(`${where}: coverage must be "dynamic" or "static"`);
|
|
307
|
+
}
|
|
308
|
+
const common = { targets: strings(raw["targets"], `${where}: targets`) };
|
|
309
|
+
if (isEngineMode(mode))
|
|
310
|
+
common.mode = mode;
|
|
311
|
+
if (isCoverageMode(coverage))
|
|
312
|
+
common.coverage = coverage;
|
|
313
|
+
if (concurrency !== undefined)
|
|
314
|
+
common.concurrency = concurrency;
|
|
315
|
+
const timeout = raw["timeout"];
|
|
316
|
+
if (timeout !== undefined) {
|
|
317
|
+
if (!isRecord(timeout))
|
|
318
|
+
throw new Error(`${where}: timeout must be { factor, extraMs }`);
|
|
319
|
+
const factor = optionalNumber(timeout["factor"], `${where}: timeout.factor`) ?? 2;
|
|
320
|
+
const extraMs = optionalNumber(timeout["extraMs"], `${where}: timeout.extraMs`) ?? 3000;
|
|
321
|
+
common.timeout = { factor, extraMs };
|
|
322
|
+
}
|
|
323
|
+
return common;
|
|
324
|
+
}
|
|
325
|
+
function parseMutants(raw, where) {
|
|
326
|
+
const out = {};
|
|
327
|
+
const mutators = raw["mutators"];
|
|
328
|
+
if (mutators !== undefined) {
|
|
329
|
+
if (!isRecord(mutators))
|
|
330
|
+
throw new Error(`${where}: mutators must be { exclude }`);
|
|
331
|
+
const exclude = mutators["exclude"] === undefined ? [] : mutators["exclude"];
|
|
332
|
+
if (!Array.isArray(exclude) || !exclude.every(isMutatorName)) {
|
|
333
|
+
throw new Error(`${where}: mutators.exclude holds an unknown mutator name`);
|
|
334
|
+
}
|
|
335
|
+
out.mutators = { exclude };
|
|
336
|
+
}
|
|
337
|
+
const ignore = raw["ignoreVerifyCallsites"];
|
|
338
|
+
if (ignore !== undefined) {
|
|
339
|
+
if (typeof ignore !== "boolean")
|
|
340
|
+
throw new Error(`${where}: ignoreVerifyCallsites must be boolean`);
|
|
341
|
+
out.ignoreVerifyCallsites = ignore;
|
|
342
|
+
}
|
|
343
|
+
const typecheck = raw["typecheck"];
|
|
344
|
+
if (typecheck !== undefined) {
|
|
345
|
+
if (typeof typecheck !== "boolean")
|
|
346
|
+
throw new Error(`${where}: typecheck must be boolean`);
|
|
347
|
+
out.typecheck = typecheck;
|
|
348
|
+
}
|
|
349
|
+
return { ...out, ...parseContracts(raw, where) };
|
|
350
|
+
}
|
|
351
|
+
function isContractsMode(v) {
|
|
352
|
+
return v === "off" || v === "ensures" || v === "all";
|
|
353
|
+
}
|
|
354
|
+
function parseContracts(raw, where) {
|
|
355
|
+
const contracts = raw["contracts"];
|
|
356
|
+
if (contracts === undefined)
|
|
357
|
+
return {};
|
|
358
|
+
if (!isContractsMode(contracts)) {
|
|
359
|
+
throw new Error(`${where}: contracts must be "off", "ensures" or "all"`);
|
|
360
|
+
}
|
|
361
|
+
return { contracts };
|
|
362
|
+
}
|
|
363
|
+
function parseOutputs(raw, where) {
|
|
364
|
+
const out = {};
|
|
365
|
+
const thresholds = raw["thresholds"];
|
|
366
|
+
if (thresholds !== undefined) {
|
|
367
|
+
if (!isRecord(thresholds))
|
|
368
|
+
throw new Error(`${where}: thresholds must be { break }`);
|
|
369
|
+
const brk = optionalNumber(thresholds["break"], `${where}: thresholds.break`);
|
|
370
|
+
out.thresholds = brk === undefined ? {} : { break: brk };
|
|
371
|
+
}
|
|
372
|
+
const report = optionalString(raw["report"], `${where}: report`);
|
|
373
|
+
if (report !== undefined)
|
|
374
|
+
out.report = report;
|
|
375
|
+
const decisions = optionalString(raw["decisions"], `${where}: decisions`);
|
|
376
|
+
if (decisions !== undefined)
|
|
377
|
+
out.decisions = decisions;
|
|
378
|
+
return out;
|
|
379
|
+
}
|
|
380
|
+
function parseCommon(raw, where) {
|
|
381
|
+
return { ...parseRun(raw, where), ...parseMutants(raw, where), ...parseOutputs(raw, where) };
|
|
382
|
+
}
|
|
383
|
+
function parseMutationConfig(raw, where) {
|
|
384
|
+
if (!isRecord(raw))
|
|
385
|
+
throw new Error(`${where}: the default export must be a config object`);
|
|
386
|
+
const { targets, ...common } = parseCommon(raw, where);
|
|
387
|
+
const runner = raw["runner"];
|
|
388
|
+
if (isRecord(runner) && runner["kind"] === "command") {
|
|
389
|
+
const argv = strings(runner["argv"], `${where}: runner.argv`);
|
|
390
|
+
if (argv.length === 0)
|
|
391
|
+
throw new Error(`${where}: runner.argv is empty`);
|
|
392
|
+
return { targets, runner: { kind: "command", argv }, ...common };
|
|
393
|
+
}
|
|
394
|
+
if (runner !== undefined && !(isRecord(runner) && runner["kind"] === "bun-test")) {
|
|
395
|
+
throw new Error(`${where}: runner.kind must be "bun-test" or "command"`);
|
|
396
|
+
}
|
|
397
|
+
const tests = raw["tests"];
|
|
398
|
+
if (!isRecord(tests))
|
|
399
|
+
throw new Error(`${where}: tests.include is required`);
|
|
400
|
+
const include = strings(tests["include"], `${where}: tests.include`);
|
|
401
|
+
if (include.length === 0)
|
|
402
|
+
throw new Error(`${where}: tests.include is empty`);
|
|
403
|
+
const preload = tests["preload"] === undefined ? undefined : strings(tests["preload"], `${where}: tests.preload`);
|
|
404
|
+
return { targets, tests: preload ? { include, preload } : { include }, ...common };
|
|
405
|
+
}
|
|
406
|
+
async function loadMutationConfig(path) {
|
|
407
|
+
const mod = await import(path);
|
|
408
|
+
const raw = isRecord(mod) ? mod["default"] : undefined;
|
|
409
|
+
return parseMutationConfig(raw, path);
|
|
410
|
+
}
|
|
411
|
+
// tools/mutate/src/engine/index.ts
|
|
412
|
+
import { realpathSync as realpathSync4 } from "node:fs";
|
|
413
|
+
import { join as join6, relative as relative5 } from "node:path";
|
|
414
|
+
|
|
415
|
+
// tools/mutate/src/engine/baseline.ts
|
|
416
|
+
import { relative as relative2 } from "node:path";
|
|
417
|
+
|
|
418
|
+
// tools/mutate/src/engine/junit.ts
|
|
419
|
+
import { realpathSync } from "node:fs";
|
|
420
|
+
import { relative, resolve } from "node:path";
|
|
421
|
+
var ENTITIES = {
|
|
422
|
+
""": '"',
|
|
423
|
+
"'": "'",
|
|
424
|
+
"<": "<",
|
|
425
|
+
">": ">",
|
|
426
|
+
"&": "&"
|
|
427
|
+
};
|
|
428
|
+
function decode(s) {
|
|
429
|
+
return s.replace(/&(?:quot|apos|lt|gt|amp|#(\d+)|#x([0-9a-f]+));/gi, (m, dec, hex) => {
|
|
430
|
+
if (dec)
|
|
431
|
+
return String.fromCodePoint(Number(dec));
|
|
432
|
+
if (hex)
|
|
433
|
+
return String.fromCodePoint(Number.parseInt(hex, 16));
|
|
434
|
+
return ENTITIES[m] ?? m;
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
function attr(attrs, key) {
|
|
438
|
+
const m = new RegExp(`\\b${key}="([^"]*)"`).exec(attrs);
|
|
439
|
+
return m?.[1] === undefined ? "" : decode(m[1]);
|
|
440
|
+
}
|
|
441
|
+
function caseStatus(body) {
|
|
442
|
+
if (/<(?:failure|error)\b/.test(body))
|
|
443
|
+
return "failed";
|
|
444
|
+
return /<skipped\b/.test(body) ? "skipped" : "passed";
|
|
445
|
+
}
|
|
446
|
+
var FAILURE = /<(?:failure|error)\b([^>]*?)(?:\/>|>([\s\S]*?)<\/(?:failure|error)>)/;
|
|
447
|
+
function failureMessage(body) {
|
|
448
|
+
const m = FAILURE.exec(body);
|
|
449
|
+
if (!m)
|
|
450
|
+
return;
|
|
451
|
+
const parts = [attr(m[1] ?? "", "message"), decode(m[2] ?? "")].filter((s) => s.length > 0);
|
|
452
|
+
return parts.join(`
|
|
453
|
+
`);
|
|
454
|
+
}
|
|
455
|
+
var TAG = /<testsuite\b([^>]*?)(\/?)>|<\/testsuite>|<testcase\b([^>]*?)(?:\/>|>([\s\S]*?)<\/testcase>)/g;
|
|
456
|
+
function parseJunit(xml, cwd) {
|
|
457
|
+
const cases = [];
|
|
458
|
+
const suites = [];
|
|
459
|
+
for (const [whole, suiteAttrs, selfClosing, caseAttrs, body] of xml.matchAll(TAG)) {
|
|
460
|
+
if (whole.startsWith("</testsuite"))
|
|
461
|
+
suites.pop();
|
|
462
|
+
else if (suiteAttrs !== undefined && selfClosing !== "/")
|
|
463
|
+
suites.push(attr(suiteAttrs, "name"));
|
|
464
|
+
else if (caseAttrs !== undefined)
|
|
465
|
+
cases.push(caseFrom(suites, caseAttrs, body ?? "", cwd));
|
|
466
|
+
}
|
|
467
|
+
return cases;
|
|
468
|
+
}
|
|
469
|
+
function caseFrom(suites, caseAttrs, body, cwd) {
|
|
470
|
+
const path = [...suites.slice(1), attr(caseAttrs, "name")];
|
|
471
|
+
const status = caseStatus(body);
|
|
472
|
+
const message = status === "failed" ? failureMessage(body) : undefined;
|
|
473
|
+
return {
|
|
474
|
+
file: resolve(cwd, attr(caseAttrs, "file")),
|
|
475
|
+
name: path.join(" > "),
|
|
476
|
+
filterName: path.join(" "),
|
|
477
|
+
line: Number(attr(caseAttrs, "line")) || 0,
|
|
478
|
+
status,
|
|
479
|
+
...message === undefined ? {} : { failureMessage: message }
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
function testId(projectCwd, file, name, line) {
|
|
483
|
+
return `${relative(projectCwd, file)}#${name}@${line}`;
|
|
484
|
+
}
|
|
485
|
+
var CONTRACT_MARKER = /\[polly-contract (requires|ensures) (.+?):(\d+)\]/g;
|
|
486
|
+
function realOr(path) {
|
|
487
|
+
try {
|
|
488
|
+
return realpathSync(path);
|
|
489
|
+
} catch {
|
|
490
|
+
return path;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
function contractsIn(text, projectCwd) {
|
|
494
|
+
const ids = new Set;
|
|
495
|
+
for (const m of text.matchAll(CONTRACT_MARKER)) {
|
|
496
|
+
if (m[2] && m[3])
|
|
497
|
+
ids.add(`${relative(projectCwd, realOr(m[2]))}:${m[3]}`);
|
|
498
|
+
}
|
|
499
|
+
return [...ids].sort();
|
|
500
|
+
}
|
|
501
|
+
function contractKills(cases, projectCwd) {
|
|
502
|
+
const out = [];
|
|
503
|
+
for (const c of cases) {
|
|
504
|
+
if (c.status !== "failed" || c.failureMessage === undefined)
|
|
505
|
+
continue;
|
|
506
|
+
const contracts = contractsIn(c.failureMessage, projectCwd);
|
|
507
|
+
if (contracts.length > 0) {
|
|
508
|
+
out.push({ test: testId(projectCwd, c.file, c.name, c.line), contracts });
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
return out;
|
|
512
|
+
}
|
|
513
|
+
var LOAD_FAILURE_NAME = "(file produced no results)";
|
|
514
|
+
function loadFailureId(projectCwd, file) {
|
|
515
|
+
return testId(projectCwd, file, LOAD_FAILURE_NAME, 0);
|
|
516
|
+
}
|
|
517
|
+
function killers(cases, baselineFiles, projectCwd) {
|
|
518
|
+
const ids = cases.filter((c) => c.status === "failed").map((c) => testId(projectCwd, c.file, c.name, c.line));
|
|
519
|
+
const reported = new Set(cases.map((c) => c.file));
|
|
520
|
+
for (const f of [...baselineFiles].sort()) {
|
|
521
|
+
if (!reported.has(f))
|
|
522
|
+
ids.push(loadFailureId(projectCwd, f));
|
|
523
|
+
}
|
|
524
|
+
return ids;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
// tools/mutate/src/engine/runner.ts
|
|
528
|
+
import { randomUUID } from "node:crypto";
|
|
529
|
+
import { existsSync, rmSync } from "node:fs";
|
|
530
|
+
import { tmpdir } from "node:os";
|
|
531
|
+
import { join } from "node:path";
|
|
532
|
+
function preloadPath() {
|
|
533
|
+
const candidates = [
|
|
534
|
+
`${import.meta.dir}/preload.js`,
|
|
535
|
+
`${import.meta.dir}/engine/preload.js`,
|
|
536
|
+
`${import.meta.dir}/preload.ts`
|
|
537
|
+
];
|
|
538
|
+
const found = candidates.find((c) => existsSync(c));
|
|
539
|
+
if (!found)
|
|
540
|
+
throw new Error(`polly mutate: cannot find the engine preload (looked in ${import.meta.dir})`);
|
|
541
|
+
return found;
|
|
542
|
+
}
|
|
543
|
+
function cleanEnv(extra) {
|
|
544
|
+
const env = {};
|
|
545
|
+
for (const [k, v] of Object.entries(process.env)) {
|
|
546
|
+
if (v !== undefined && !k.startsWith("POLLY_MUTATE_") && k !== "POLLY_CONTRACTS")
|
|
547
|
+
env[k] = v;
|
|
548
|
+
}
|
|
549
|
+
return { ...env, ...extra };
|
|
550
|
+
}
|
|
551
|
+
async function readJunit(path) {
|
|
552
|
+
if (!path)
|
|
553
|
+
return;
|
|
554
|
+
const file = Bun.file(path);
|
|
555
|
+
if (!await file.exists())
|
|
556
|
+
return;
|
|
557
|
+
const xml = await file.text();
|
|
558
|
+
rmSync(path, { force: true });
|
|
559
|
+
return xml;
|
|
560
|
+
}
|
|
561
|
+
function processArgv(runner, run, junitPath) {
|
|
562
|
+
const env = { ...run.env, POLLY_CONTRACTS: run.contracts ?? "off" };
|
|
563
|
+
if (runner.kind === "command") {
|
|
564
|
+
const inherited = process.env["BUN_OPTIONS"];
|
|
565
|
+
const preloads = run.preloads.map((p) => `--preload=${p}`).join(" ");
|
|
566
|
+
return {
|
|
567
|
+
argv: runner.argv,
|
|
568
|
+
env: { ...env, BUN_OPTIONS: inherited ? `${inherited} ${preloads}` : preloads }
|
|
569
|
+
};
|
|
570
|
+
}
|
|
571
|
+
return {
|
|
572
|
+
argv: [
|
|
573
|
+
"bun",
|
|
574
|
+
"test",
|
|
575
|
+
...run.preloads.flatMap((p) => ["--preload", p]),
|
|
576
|
+
...run.bail ? ["--bail"] : [],
|
|
577
|
+
...run.namePattern ? ["-t", run.namePattern] : [],
|
|
578
|
+
...junitPath ? ["--reporter=junit", `--reporter-outfile=${junitPath}`] : [],
|
|
579
|
+
...run.files
|
|
580
|
+
],
|
|
581
|
+
env
|
|
582
|
+
};
|
|
583
|
+
}
|
|
584
|
+
async function runBunTest(run, runner = { kind: "bun-test" }) {
|
|
585
|
+
const started = performance.now();
|
|
586
|
+
const junitPath = run.junit && runner.kind === "bun-test" ? join(tmpdir(), `polly-mutate-junit-${randomUUID()}.xml`) : undefined;
|
|
587
|
+
const { argv, env } = processArgv(runner, run, junitPath);
|
|
588
|
+
const group = runner.kind === "command";
|
|
589
|
+
const proc = Bun.spawn(argv, {
|
|
590
|
+
cwd: run.cwd,
|
|
591
|
+
detached: group,
|
|
592
|
+
env: cleanEnv(env),
|
|
593
|
+
stdin: "ignore",
|
|
594
|
+
stdout: "ignore",
|
|
595
|
+
stderr: run.captureStderr ? "pipe" : "ignore"
|
|
596
|
+
});
|
|
597
|
+
let timedOut = false;
|
|
598
|
+
const timer = setTimeout(() => {
|
|
599
|
+
timedOut = true;
|
|
600
|
+
if (group)
|
|
601
|
+
process.kill(-proc.pid, 9);
|
|
602
|
+
else
|
|
603
|
+
proc.kill(9);
|
|
604
|
+
}, run.timeoutMs);
|
|
605
|
+
const stderrText = run.captureStderr && proc.stderr ? new Response(proc.stderr).text() : undefined;
|
|
606
|
+
const exitCode = await proc.exited;
|
|
607
|
+
clearTimeout(timer);
|
|
608
|
+
const ms = performance.now() - started;
|
|
609
|
+
const junitXml = await readJunit(junitPath);
|
|
610
|
+
const outcome = { exitCode, timedOut, ms, ...junitXml ? { junitXml } : {} };
|
|
611
|
+
if (stderrText === undefined)
|
|
612
|
+
return outcome;
|
|
613
|
+
return { ...outcome, stderrTail: (await stderrText).slice(-2000) };
|
|
614
|
+
}
|
|
615
|
+
async function pool(items, concurrency, fn) {
|
|
616
|
+
const results = new Array(items.length);
|
|
617
|
+
let next = 0;
|
|
618
|
+
const worker = async () => {
|
|
619
|
+
while (next < items.length) {
|
|
620
|
+
const i = next++;
|
|
621
|
+
const item = items[i];
|
|
622
|
+
if (item !== undefined)
|
|
623
|
+
results[i] = await fn(item, i);
|
|
624
|
+
}
|
|
625
|
+
};
|
|
626
|
+
await Promise.all(Array.from({ length: Math.max(1, Math.min(concurrency, items.length)) }, worker));
|
|
627
|
+
return results;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
// tools/mutate/src/engine/workdir.ts
|
|
631
|
+
import { existsSync as existsSync2, mkdirSync, mkdtempSync, readFileSync, rmSync as rmSync2, writeFileSync } from "node:fs";
|
|
632
|
+
import { tmpdir as tmpdir2 } from "node:os";
|
|
633
|
+
import { join as join2 } from "node:path";
|
|
634
|
+
function isRecord2(v) {
|
|
635
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
636
|
+
}
|
|
637
|
+
function absolutePreload(spec, projectCwd) {
|
|
638
|
+
if (typeof spec !== "string")
|
|
639
|
+
return spec;
|
|
640
|
+
try {
|
|
641
|
+
return Bun.resolveSync(spec, projectCwd);
|
|
642
|
+
} catch {
|
|
643
|
+
return spec;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
function absolutiseBunfig(config, projectCwd) {
|
|
647
|
+
const fix = (v) => Array.isArray(v) ? v.map((s) => absolutePreload(s, projectCwd)) : absolutePreload(v, projectCwd);
|
|
648
|
+
const out = { ...config };
|
|
649
|
+
if ("preload" in out)
|
|
650
|
+
out["preload"] = fix(out["preload"]);
|
|
651
|
+
const test = out["test"];
|
|
652
|
+
if (isRecord2(test) && "preload" in test)
|
|
653
|
+
out["test"] = { ...test, preload: fix(test["preload"]) };
|
|
654
|
+
return out;
|
|
655
|
+
}
|
|
656
|
+
var tomlKey = (k) => /^[A-Za-z0-9_-]+$/.test(k) ? k : JSON.stringify(k);
|
|
657
|
+
function tomlValue(v) {
|
|
658
|
+
if (Array.isArray(v))
|
|
659
|
+
return `[${v.map(tomlValue).join(", ")}]`;
|
|
660
|
+
if (isRecord2(v)) {
|
|
661
|
+
return `{ ${Object.entries(v).map(([k, x]) => `${tomlKey(k)} = ${tomlValue(x)}`).join(", ")} }`;
|
|
662
|
+
}
|
|
663
|
+
if (typeof v === "string")
|
|
664
|
+
return JSON.stringify(v);
|
|
665
|
+
return String(v);
|
|
666
|
+
}
|
|
667
|
+
function renderToml(config, prefix = []) {
|
|
668
|
+
const lines = [];
|
|
669
|
+
const tables = [];
|
|
670
|
+
for (const [k, v] of Object.entries(config)) {
|
|
671
|
+
if (isRecord2(v))
|
|
672
|
+
tables.push([k, v]);
|
|
673
|
+
else
|
|
674
|
+
lines.push(`${tomlKey(k)} = ${tomlValue(v)}`);
|
|
675
|
+
}
|
|
676
|
+
const sections = tables.map(([k, v]) => {
|
|
677
|
+
const path = [...prefix, k];
|
|
678
|
+
return `[${path.map(tomlKey).join(".")}]
|
|
679
|
+
${renderToml(v, path)}`;
|
|
680
|
+
});
|
|
681
|
+
return [lines.join(`
|
|
682
|
+
`), ...sections].filter(Boolean).join(`
|
|
683
|
+
|
|
684
|
+
`);
|
|
685
|
+
}
|
|
686
|
+
function createWorkdirs(projectCwd) {
|
|
687
|
+
const root = mkdtempSync(join2(tmpdir2(), "polly-mutate-"));
|
|
688
|
+
const files = {};
|
|
689
|
+
const tsconfig = join2(projectCwd, "tsconfig.json");
|
|
690
|
+
if (existsSync2(tsconfig))
|
|
691
|
+
files["tsconfig.json"] = JSON.stringify({ extends: tsconfig });
|
|
692
|
+
const bunfig = join2(projectCwd, "bunfig.toml");
|
|
693
|
+
if (existsSync2(bunfig)) {
|
|
694
|
+
const parsed = Bun.TOML.parse(readFileSync(bunfig, "utf8"));
|
|
695
|
+
if (isRecord2(parsed))
|
|
696
|
+
files["bunfig.toml"] = renderToml(absolutiseBunfig(parsed, projectCwd));
|
|
697
|
+
}
|
|
698
|
+
return {
|
|
699
|
+
root,
|
|
700
|
+
create() {
|
|
701
|
+
mkdirSync(root, { recursive: true });
|
|
702
|
+
const dir = mkdtempSync(join2(root, "run-"));
|
|
703
|
+
for (const [name, text] of Object.entries(files))
|
|
704
|
+
writeFileSync(join2(dir, name), text);
|
|
705
|
+
return dir;
|
|
706
|
+
},
|
|
707
|
+
release: (dir) => rmSync2(dir, { recursive: true, force: true }),
|
|
708
|
+
remove: () => rmSync2(root, { recursive: true, force: true })
|
|
709
|
+
};
|
|
710
|
+
}
|
|
711
|
+
async function inCwd(workdirs, projectCwd, fn) {
|
|
712
|
+
if (!workdirs)
|
|
713
|
+
return fn(projectCwd);
|
|
714
|
+
const dir = workdirs.create();
|
|
715
|
+
try {
|
|
716
|
+
return await fn(dir);
|
|
717
|
+
} finally {
|
|
718
|
+
workdirs.release(dir);
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
// tools/mutate/src/engine/baseline.ts
|
|
723
|
+
var BASELINE_ATTEMPTS = 3;
|
|
724
|
+
var BASELINE_TIMEOUT_MS = 300000;
|
|
725
|
+
function failed(reason, contractViolations) {
|
|
726
|
+
return {
|
|
727
|
+
ok: false,
|
|
728
|
+
ms: 0,
|
|
729
|
+
reason,
|
|
730
|
+
tests: [],
|
|
731
|
+
reportedFiles: new Set,
|
|
732
|
+
...contractViolations ? { contractViolations } : {}
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
var COMMAND = "(command)";
|
|
736
|
+
async function commandViolatesContract(cfg, cwd, preloads, files) {
|
|
737
|
+
const off = await runBunTest({
|
|
738
|
+
cwd,
|
|
739
|
+
files,
|
|
740
|
+
preloads,
|
|
741
|
+
contracts: "off",
|
|
742
|
+
bail: false,
|
|
743
|
+
timeoutMs: BASELINE_TIMEOUT_MS,
|
|
744
|
+
captureStderr: true
|
|
745
|
+
}, cfg.runner);
|
|
746
|
+
return off.exitCode === 0 && !off.timedOut;
|
|
747
|
+
}
|
|
748
|
+
function green(cfg, cwd, outcome) {
|
|
749
|
+
if (cfg.mode === "fast")
|
|
750
|
+
return { ok: true, ms: outcome.ms, tests: [], reportedFiles: new Set };
|
|
751
|
+
const cases = parseJunit(outcome.junitXml ?? "", cwd);
|
|
752
|
+
if (cases.length === 0) {
|
|
753
|
+
return failed("baseline passed but its junit report holds no test case (reporter broke?)");
|
|
754
|
+
}
|
|
755
|
+
return {
|
|
756
|
+
ok: true,
|
|
757
|
+
ms: outcome.ms,
|
|
758
|
+
tests: cases.map((c) => ({
|
|
759
|
+
id: testId(cfg.cwd, c.file, c.name, c.line),
|
|
760
|
+
file: c.file,
|
|
761
|
+
name: c.name,
|
|
762
|
+
line: c.line
|
|
763
|
+
})),
|
|
764
|
+
reportedFiles: new Set(cases.map((c) => c.file))
|
|
765
|
+
};
|
|
766
|
+
}
|
|
767
|
+
async function measureIn(cfg, cwd, preloads, files) {
|
|
768
|
+
let last = "";
|
|
769
|
+
for (let attempt = 1;attempt <= BASELINE_ATTEMPTS; attempt++) {
|
|
770
|
+
const outcome = await runBunTest({
|
|
771
|
+
cwd,
|
|
772
|
+
files,
|
|
773
|
+
preloads,
|
|
774
|
+
contracts: cfg.contracts,
|
|
775
|
+
bail: false,
|
|
776
|
+
timeoutMs: BASELINE_TIMEOUT_MS,
|
|
777
|
+
captureStderr: true,
|
|
778
|
+
junit: cfg.mode === "matrix" || cfg.contracts !== "off" && cfg.runner.kind === "bun-test"
|
|
779
|
+
}, cfg.runner);
|
|
780
|
+
if (outcome.exitCode === 0 && !outcome.timedOut)
|
|
781
|
+
return green(cfg, cwd, outcome);
|
|
782
|
+
last = outcome.timedOut ? `timed out after ${BASELINE_TIMEOUT_MS} ms` : `exit ${outcome.exitCode}`;
|
|
783
|
+
const failLine = outcome.stderrTail?.match(/\(fail\)[^\n]*/)?.[0];
|
|
784
|
+
if (failLine)
|
|
785
|
+
last += `: ${failLine}`;
|
|
786
|
+
if (cfg.contracts === "off" || outcome.timedOut)
|
|
787
|
+
continue;
|
|
788
|
+
const contract = await contractFailure(cfg, cwd, preloads, files, outcome.junitXml, last);
|
|
789
|
+
if (contract)
|
|
790
|
+
return contract;
|
|
791
|
+
}
|
|
792
|
+
return failed(`baseline red ${BASELINE_ATTEMPTS}× on the selected tests (${last})`);
|
|
793
|
+
}
|
|
794
|
+
async function contractFailure(cfg, cwd, preloads, files, junitXml, last) {
|
|
795
|
+
if (cfg.runner.kind === "command") {
|
|
796
|
+
if (!await commandViolatesContract(cfg, cwd, preloads, files))
|
|
797
|
+
return null;
|
|
798
|
+
return failed(`the command fails only with contracts on (POLLY_CONTRACTS=${cfg.contracts}): a scenario violates a contract`, [{ test: COMMAND, contracts: [] }]);
|
|
799
|
+
}
|
|
800
|
+
const cases = parseJunit(junitXml ?? "", cwd);
|
|
801
|
+
const violations = contractKills(cases, cfg.cwd);
|
|
802
|
+
if (violations.length === 0)
|
|
803
|
+
return null;
|
|
804
|
+
const red = cases.filter((c) => c.status === "failed").length;
|
|
805
|
+
return failed(violations.length === red ? `${violations.length} test(s) violate contracts` : `baseline red (${last}) and ${violations.length} of ${red} failing test(s) violate contracts`, violations);
|
|
806
|
+
}
|
|
807
|
+
async function measureBaseline(cfg, workdirs, preloads, target, log) {
|
|
808
|
+
const scratch = await inCwd(workdirs, cfg.cwd, (cwd) => measureIn(cfg, cwd, preloads, target.covering));
|
|
809
|
+
if (scratch.ok || !workdirs || scratch.contractViolations) {
|
|
810
|
+
return { ...scratch, inTree: !workdirs };
|
|
811
|
+
}
|
|
812
|
+
const inTree = await measureIn(cfg, cfg.cwd, preloads, target.covering);
|
|
813
|
+
if (inTree.ok) {
|
|
814
|
+
log(`${relative2(cfg.cwd, target.file)}: tests fail outside ${cfg.cwd} (${scratch.reason}); its mutants run in-tree`);
|
|
815
|
+
}
|
|
816
|
+
return { ...inTree, inTree: true };
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
// tools/mutate/src/engine/cache.ts
|
|
820
|
+
import { existsSync as existsSync3, mkdirSync as mkdirSync2, writeFileSync as writeFileSync2 } from "node:fs";
|
|
821
|
+
import { join as join3 } from "node:path";
|
|
822
|
+
var CACHE_VERSION = 2;
|
|
823
|
+
function mutantSig(m) {
|
|
824
|
+
return `${m.mutatorName}:${m.start}:${m.end}:${m.replacement}`;
|
|
825
|
+
}
|
|
826
|
+
async function textOrEmpty(path) {
|
|
827
|
+
const file = Bun.file(path);
|
|
828
|
+
return await file.exists() ? file.text() : "";
|
|
829
|
+
}
|
|
830
|
+
async function cacheKey(input) {
|
|
831
|
+
const hash = new Bun.CryptoHasher("sha256");
|
|
832
|
+
hash.update(JSON.stringify({ v: CACHE_VERSION, bun: Bun.version, settings: input.settings }));
|
|
833
|
+
for (const m of input.mutants)
|
|
834
|
+
hash.update(`${mutantSig(m)}|${m.ignoredReason ?? ""}
|
|
835
|
+
`);
|
|
836
|
+
for (const f of [...input.closure, ...input.projectFiles]) {
|
|
837
|
+
hash.update(`\x00${f}\x00`);
|
|
838
|
+
hash.update(await textOrEmpty(f));
|
|
839
|
+
}
|
|
840
|
+
return hash.digest("hex");
|
|
841
|
+
}
|
|
842
|
+
async function readCache(dir, key) {
|
|
843
|
+
const file = Bun.file(join3(dir, `${key}.json`));
|
|
844
|
+
if (!await file.exists())
|
|
845
|
+
return null;
|
|
846
|
+
try {
|
|
847
|
+
const entry = await file.json();
|
|
848
|
+
return Array.isArray(entry.results) ? entry : null;
|
|
849
|
+
} catch {
|
|
850
|
+
return null;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
function writeEntry(dir, name, value) {
|
|
854
|
+
mkdirSync2(dir, { recursive: true });
|
|
855
|
+
const ignore = join3(dir, ".gitignore");
|
|
856
|
+
if (!existsSync3(ignore))
|
|
857
|
+
writeFileSync2(ignore, `*
|
|
858
|
+
`);
|
|
859
|
+
writeFileSync2(join3(dir, name), `${JSON.stringify(value)}
|
|
860
|
+
`);
|
|
861
|
+
}
|
|
862
|
+
function writeCache(dir, key, entry) {
|
|
863
|
+
writeEntry(dir, `${key}.json`, entry);
|
|
864
|
+
}
|
|
865
|
+
function writeCoverageCache(dir, key, coverage) {
|
|
866
|
+
writeEntry(dir, `coverage-${key}.json`, coverage);
|
|
867
|
+
}
|
|
868
|
+
async function readCoverageCache(dir, key) {
|
|
869
|
+
const file = Bun.file(join3(dir, `coverage-${key}.json`));
|
|
870
|
+
if (!await file.exists())
|
|
871
|
+
return null;
|
|
872
|
+
try {
|
|
873
|
+
const coverage = await file.json();
|
|
874
|
+
return Array.isArray(coverage.targets) ? coverage : null;
|
|
875
|
+
} catch {
|
|
876
|
+
return null;
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
function cachedResult(r) {
|
|
880
|
+
return {
|
|
881
|
+
sig: mutantSig(r.mutant),
|
|
882
|
+
status: r.status,
|
|
883
|
+
...r.statusReason ? { statusReason: r.statusReason } : {},
|
|
884
|
+
...r.durationMs === undefined ? {} : { durationMs: r.durationMs },
|
|
885
|
+
...r.killedBy ? { killedBy: r.killedBy } : {},
|
|
886
|
+
...r.coveredBy ? { coveredBy: r.coveredBy } : {},
|
|
887
|
+
...r.killedByContract ? { killedByContract: r.killedByContract } : {},
|
|
888
|
+
...r.contractOnly ? { contractOnly: true } : {}
|
|
889
|
+
};
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
// tools/mutate/src/engine/coverage.ts
|
|
893
|
+
import { mkdtempSync as mkdtempSync2, realpathSync as realpathSync2, rmSync as rmSync3, writeFileSync as writeFileSync3 } from "node:fs";
|
|
894
|
+
import { tmpdir as tmpdir3 } from "node:os";
|
|
895
|
+
import { join as join4 } from "node:path";
|
|
896
|
+
|
|
897
|
+
// tools/mutate/src/engine/probes.ts
|
|
898
|
+
import { parseSync } from "oxc-parser";
|
|
899
|
+
|
|
900
|
+
// tools/mutate/src/engine/ast.ts
|
|
901
|
+
function isAstNode(value) {
|
|
902
|
+
return isRecord(value) && typeof value["type"] === "string" && typeof value["start"] === "number" && typeof value["end"] === "number";
|
|
903
|
+
}
|
|
904
|
+
function child(node, key) {
|
|
905
|
+
const value = node[key];
|
|
906
|
+
return isAstNode(value) ? value : undefined;
|
|
907
|
+
}
|
|
908
|
+
function children(node, key) {
|
|
909
|
+
const value = node[key];
|
|
910
|
+
return Array.isArray(value) ? value.filter(isAstNode) : [];
|
|
911
|
+
}
|
|
912
|
+
function stringField(node, key) {
|
|
913
|
+
const value = node[key];
|
|
914
|
+
return typeof value === "string" ? value : undefined;
|
|
915
|
+
}
|
|
916
|
+
function calleeName(call) {
|
|
917
|
+
const callee = child(call, "callee");
|
|
918
|
+
if (!callee)
|
|
919
|
+
return;
|
|
920
|
+
if (callee.type === "Identifier")
|
|
921
|
+
return stringField(callee, "name");
|
|
922
|
+
if (callee.type === "MemberExpression" && callee["computed"] !== true) {
|
|
923
|
+
const property = child(callee, "property");
|
|
924
|
+
if (property?.type === "Identifier")
|
|
925
|
+
return stringField(property, "name");
|
|
926
|
+
}
|
|
927
|
+
return;
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
// tools/mutate/src/engine/probes.ts
|
|
931
|
+
var HIT = "globalThis.__pollyMutateHit?.";
|
|
932
|
+
var PARSE_OPTIONS = { preserveParens: false };
|
|
933
|
+
var LIST_PARENTS = new Set(["Program", "BlockStatement", "StaticBlock", "TSModuleBlock"]);
|
|
934
|
+
var LONE_PARENTS = {
|
|
935
|
+
IfStatement: ["consequent", "alternate"],
|
|
936
|
+
ForStatement: ["body"],
|
|
937
|
+
ForInStatement: ["body"],
|
|
938
|
+
ForOfStatement: ["body"],
|
|
939
|
+
WhileStatement: ["body"],
|
|
940
|
+
DoWhileStatement: ["body"],
|
|
941
|
+
WithStatement: ["body"]
|
|
942
|
+
};
|
|
943
|
+
var ANONYMOUS = new Set(["FunctionExpression", "ArrowFunctionExpression", "ClassExpression"]);
|
|
944
|
+
function isDirective(node) {
|
|
945
|
+
return node?.type === "ExpressionStatement" && typeof node["directive"] === "string";
|
|
946
|
+
}
|
|
947
|
+
function afterDirectives(statements, from) {
|
|
948
|
+
let at = from;
|
|
949
|
+
for (const s of statements) {
|
|
950
|
+
if (!isDirective(s))
|
|
951
|
+
break;
|
|
952
|
+
at = s.end;
|
|
953
|
+
}
|
|
954
|
+
return at;
|
|
955
|
+
}
|
|
956
|
+
function inList(parent, key) {
|
|
957
|
+
return LIST_PARENTS.has(parent.type) && key === "body" || parent.type === "SwitchCase" && key === "consequent";
|
|
958
|
+
}
|
|
959
|
+
function isLone(node, parent, key) {
|
|
960
|
+
return (LONE_PARENTS[parent.type]?.includes(key) ?? false) && node.type !== "BlockStatement" && !node.type.endsWith("Declaration");
|
|
961
|
+
}
|
|
962
|
+
function isWrappedValue(node, parent, key) {
|
|
963
|
+
const value = parent.type === "ArrowFunctionExpression" && key === "body" && parent["expression"] === true || (parent.type === "PropertyDefinition" || parent.type === "AccessorProperty") && key === "value" || parent.type === "AssignmentPattern" && key === "right";
|
|
964
|
+
const named = parent.type !== "ArrowFunctionExpression" && ANONYMOUS.has(node.type);
|
|
965
|
+
return value && !named;
|
|
966
|
+
}
|
|
967
|
+
function sitesOf(node, parent, key) {
|
|
968
|
+
const sites = [];
|
|
969
|
+
const span = { start: node.start, end: node.end };
|
|
970
|
+
if (node.type === "BlockStatement" || node.type === "StaticBlock") {
|
|
971
|
+
sites.push({
|
|
972
|
+
kind: "block",
|
|
973
|
+
...span,
|
|
974
|
+
at: afterDirectives(children(node, "body"), node.start + 1)
|
|
975
|
+
});
|
|
976
|
+
}
|
|
977
|
+
if (!parent)
|
|
978
|
+
return sites;
|
|
979
|
+
if (inList(parent, key)) {
|
|
980
|
+
sites.push({ kind: "statement", ...span, at: isDirective(node) ? node.end : node.start });
|
|
981
|
+
} else if (isLone(node, parent, key)) {
|
|
982
|
+
sites.push({ kind: "lone", ...span, at: node.start });
|
|
983
|
+
} else if (isWrappedValue(node, parent, key)) {
|
|
984
|
+
sites.push({ kind: "expression", ...span, at: node.start });
|
|
985
|
+
}
|
|
986
|
+
return sites;
|
|
987
|
+
}
|
|
988
|
+
function collectSites(program) {
|
|
989
|
+
const sites = [];
|
|
990
|
+
const visit = (node, parent, key) => {
|
|
991
|
+
sites.push(...sitesOf(node, parent, key));
|
|
992
|
+
for (const [k, value] of Object.entries(node)) {
|
|
993
|
+
const list = Array.isArray(value) ? value : [value];
|
|
994
|
+
for (const c of list)
|
|
995
|
+
if (isAstNode(c))
|
|
996
|
+
visit(c, node, k);
|
|
997
|
+
}
|
|
998
|
+
};
|
|
999
|
+
for (const s of children(program, "body"))
|
|
1000
|
+
visit(s, program, "body");
|
|
1001
|
+
return sites;
|
|
1002
|
+
}
|
|
1003
|
+
function innermost(sites, start, end) {
|
|
1004
|
+
let best = null;
|
|
1005
|
+
for (const s of sites) {
|
|
1006
|
+
if (s.start > start || s.end < end)
|
|
1007
|
+
continue;
|
|
1008
|
+
if (!best || s.end - s.start < best.end - best.start)
|
|
1009
|
+
best = s;
|
|
1010
|
+
else if (s.end - s.start === best.end - best.start && s.kind === "block")
|
|
1011
|
+
best = s;
|
|
1012
|
+
}
|
|
1013
|
+
return best;
|
|
1014
|
+
}
|
|
1015
|
+
function edits(site, probe) {
|
|
1016
|
+
const hit = `${HIT}(${probe})`;
|
|
1017
|
+
const open = (text) => ({ at: site.at, text, order: [1, -site.end] });
|
|
1018
|
+
const close = (text) => ({ at: site.end, text, order: [0, -site.start] });
|
|
1019
|
+
switch (site.kind) {
|
|
1020
|
+
case "statement":
|
|
1021
|
+
case "block":
|
|
1022
|
+
return [open(`;${hit};`)];
|
|
1023
|
+
case "lone":
|
|
1024
|
+
return [open(`{;${hit};`), close("}")];
|
|
1025
|
+
case "expression":
|
|
1026
|
+
return [open(`(${hit},`), close(")")];
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
function applyEdits(src, all) {
|
|
1030
|
+
all.sort((a, b) => a.at - b.at || a.order[0] - b.order[0] || a.order[1] - b.order[1]);
|
|
1031
|
+
let out = "";
|
|
1032
|
+
let from = 0;
|
|
1033
|
+
for (const e of all) {
|
|
1034
|
+
out += src.slice(from, e.at) + e.text;
|
|
1035
|
+
from = e.at;
|
|
1036
|
+
}
|
|
1037
|
+
return out + src.slice(from);
|
|
1038
|
+
}
|
|
1039
|
+
function instrument(file, src, mutants, firstProbe) {
|
|
1040
|
+
const parsed = parseSync(file, src, PARSE_OPTIONS);
|
|
1041
|
+
if (parsed.errors.length > 0 || !isAstNode(parsed.program))
|
|
1042
|
+
return null;
|
|
1043
|
+
const sites = collectSites(parsed.program);
|
|
1044
|
+
const bySite = new Map;
|
|
1045
|
+
for (const m of mutants) {
|
|
1046
|
+
const site = innermost(sites, m.start, m.end);
|
|
1047
|
+
if (!site)
|
|
1048
|
+
return null;
|
|
1049
|
+
bySite.set(site, [...bySite.get(site) ?? [], m.id]);
|
|
1050
|
+
}
|
|
1051
|
+
const probes = [];
|
|
1052
|
+
const all = [];
|
|
1053
|
+
for (const [site, ids] of bySite) {
|
|
1054
|
+
all.push(...edits(site, firstProbe + probes.length));
|
|
1055
|
+
probes.push(ids);
|
|
1056
|
+
}
|
|
1057
|
+
const code = applyEdits(src, all);
|
|
1058
|
+
if (parseSync(file, code, PARSE_OPTIONS).errors.length > 0)
|
|
1059
|
+
return null;
|
|
1060
|
+
return { code, probes };
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
// tools/mutate/src/engine/coverage.ts
|
|
1064
|
+
var COVERAGE_VERSION = 1;
|
|
1065
|
+
var COVERAGE_TIMEOUT_MS = 600000;
|
|
1066
|
+
var MAX_PATTERN = 8000;
|
|
1067
|
+
var numbers = (v) => Array.isArray(v) ? v.filter((x) => typeof x === "number") : [];
|
|
1068
|
+
function parseHits(raw) {
|
|
1069
|
+
if (!isRecord(raw) || !Array.isArray(raw["files"]))
|
|
1070
|
+
return null;
|
|
1071
|
+
return {
|
|
1072
|
+
served: numbers(raw["served"]),
|
|
1073
|
+
outside: numbers(raw["outside"]),
|
|
1074
|
+
files: raw["files"].filter(isRecord).map((f) => ({
|
|
1075
|
+
load: numbers(f["load"]),
|
|
1076
|
+
between: numbers(f["between"]),
|
|
1077
|
+
tests: Array.isArray(f["tests"]) ? f["tests"].map(numbers) : [],
|
|
1078
|
+
overlapped: f["overlapped"] === true
|
|
1079
|
+
}))
|
|
1080
|
+
};
|
|
1081
|
+
}
|
|
1082
|
+
function real(path) {
|
|
1083
|
+
try {
|
|
1084
|
+
return realpathSync2(path);
|
|
1085
|
+
} catch {
|
|
1086
|
+
return path;
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
function chunk(items, n) {
|
|
1090
|
+
const count = Math.max(1, Math.min(n, items.length));
|
|
1091
|
+
const size = Math.ceil(items.length / count);
|
|
1092
|
+
const out = [];
|
|
1093
|
+
for (let i = 0;i < items.length; i += size)
|
|
1094
|
+
out.push(items.slice(i, i + size));
|
|
1095
|
+
return out;
|
|
1096
|
+
}
|
|
1097
|
+
function newProbeHits() {
|
|
1098
|
+
return { tests: new Set, files: new Set, loads: new Set, static: false };
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
class Attribution {
|
|
1102
|
+
cwd;
|
|
1103
|
+
probes = new Map;
|
|
1104
|
+
served = new Set;
|
|
1105
|
+
tests = {};
|
|
1106
|
+
byFile = {};
|
|
1107
|
+
constructor(cwd) {
|
|
1108
|
+
this.cwd = cwd;
|
|
1109
|
+
}
|
|
1110
|
+
at(probe) {
|
|
1111
|
+
let hits = this.probes.get(probe);
|
|
1112
|
+
if (!hits) {
|
|
1113
|
+
hits = newProbeHits();
|
|
1114
|
+
this.probes.set(probe, hits);
|
|
1115
|
+
}
|
|
1116
|
+
return hits;
|
|
1117
|
+
}
|
|
1118
|
+
record(c) {
|
|
1119
|
+
const id = testId(this.cwd, c.file, c.name, c.line);
|
|
1120
|
+
if (!this.tests[id]) {
|
|
1121
|
+
this.tests[id] = { id, file: c.file, name: c.name, line: c.line, filterName: c.filterName };
|
|
1122
|
+
const file = real(c.file);
|
|
1123
|
+
this.byFile[file] = [...this.byFile[file] ?? [], id];
|
|
1124
|
+
}
|
|
1125
|
+
return id;
|
|
1126
|
+
}
|
|
1127
|
+
add(testFiles, hits, cases) {
|
|
1128
|
+
for (const t of hits.served)
|
|
1129
|
+
this.served.add(t);
|
|
1130
|
+
for (const p of hits.outside)
|
|
1131
|
+
this.at(p).static = true;
|
|
1132
|
+
const ran = new Map;
|
|
1133
|
+
for (const c of cases) {
|
|
1134
|
+
if (c.status === "skipped")
|
|
1135
|
+
continue;
|
|
1136
|
+
const file = real(c.file);
|
|
1137
|
+
ran.set(file, [...ran.get(file) ?? [], c]);
|
|
1138
|
+
}
|
|
1139
|
+
hits.files.forEach((f, i) => {
|
|
1140
|
+
const file = testFiles[i];
|
|
1141
|
+
if (!file)
|
|
1142
|
+
return;
|
|
1143
|
+
const fileCases = ran.get(file) ?? [];
|
|
1144
|
+
const ids = fileCases.map((c) => this.record(c));
|
|
1145
|
+
for (const p of f.load) {
|
|
1146
|
+
const h = this.at(p);
|
|
1147
|
+
h.static = true;
|
|
1148
|
+
h.loads.add(file);
|
|
1149
|
+
}
|
|
1150
|
+
for (const p of f.between)
|
|
1151
|
+
this.at(p).files.add(file);
|
|
1152
|
+
const ordered = !f.overlapped && f.tests.length === ids.length;
|
|
1153
|
+
f.tests.forEach((probes, k) => {
|
|
1154
|
+
const id = ids[k];
|
|
1155
|
+
for (const p of probes) {
|
|
1156
|
+
if (ordered && id)
|
|
1157
|
+
this.at(p).tests.add(id);
|
|
1158
|
+
else
|
|
1159
|
+
this.at(p).files.add(file);
|
|
1160
|
+
}
|
|
1161
|
+
});
|
|
1162
|
+
});
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
async function planTargets(run) {
|
|
1166
|
+
const plan = [];
|
|
1167
|
+
const probes = [];
|
|
1168
|
+
const failed = new Map;
|
|
1169
|
+
for (const [target, t] of run.targets.entries()) {
|
|
1170
|
+
const probed = instrument(t.file, await Bun.file(t.file).text(), t.mutants, probes.length);
|
|
1171
|
+
if (!probed) {
|
|
1172
|
+
failed.set(target, "its probed source does not parse");
|
|
1173
|
+
continue;
|
|
1174
|
+
}
|
|
1175
|
+
plan.push({ file: t.file, code: probed.code });
|
|
1176
|
+
for (const ids of probed.probes)
|
|
1177
|
+
probes.push({ target, ids });
|
|
1178
|
+
}
|
|
1179
|
+
return { plan, probes, failed };
|
|
1180
|
+
}
|
|
1181
|
+
function sorted(set) {
|
|
1182
|
+
return [...new Set(set)].sort();
|
|
1183
|
+
}
|
|
1184
|
+
function usersOf(t, attribution, mine) {
|
|
1185
|
+
const users = new Set(t.covering);
|
|
1186
|
+
for (const { probe } of mine) {
|
|
1187
|
+
const h = attribution.probes.get(probe);
|
|
1188
|
+
if (!h)
|
|
1189
|
+
continue;
|
|
1190
|
+
const testFiles = [...h.tests].flatMap((id) => {
|
|
1191
|
+
const file = attribution.tests[id]?.file;
|
|
1192
|
+
return file ? [real(file)] : [];
|
|
1193
|
+
});
|
|
1194
|
+
for (const f of [...testFiles, ...h.files, ...h.loads])
|
|
1195
|
+
users.add(f);
|
|
1196
|
+
}
|
|
1197
|
+
return users;
|
|
1198
|
+
}
|
|
1199
|
+
function targetCoverage(run, target, attribution, probes) {
|
|
1200
|
+
const t = run.targets[target];
|
|
1201
|
+
if (!t)
|
|
1202
|
+
return { kind: "static", reason: "missing target" };
|
|
1203
|
+
const mine = probes.flatMap((p, probe) => p.target === target ? [{ probe, ids: p.ids }] : []);
|
|
1204
|
+
if (!attribution.served.has(target) && t.covering.length > 0) {
|
|
1205
|
+
return {
|
|
1206
|
+
kind: "static",
|
|
1207
|
+
reason: "a test imports it but the coverage pass never loaded it (a project preload may)"
|
|
1208
|
+
};
|
|
1209
|
+
}
|
|
1210
|
+
const users = usersOf(t, attribution, mine);
|
|
1211
|
+
const mutants = {};
|
|
1212
|
+
for (const { probe, ids } of mine) {
|
|
1213
|
+
const h = attribution.probes.get(probe) ?? newProbeHits();
|
|
1214
|
+
const files = h.static ? [...users, ...h.files] : [...h.files];
|
|
1215
|
+
const cov = { tests: sorted(h.tests), files: sorted(files) };
|
|
1216
|
+
for (const id of ids)
|
|
1217
|
+
mutants[id] = cov;
|
|
1218
|
+
}
|
|
1219
|
+
return { kind: "dynamic", mutants };
|
|
1220
|
+
}
|
|
1221
|
+
async function collectCoverage(run) {
|
|
1222
|
+
const { plan, probes, failed } = await planTargets(run);
|
|
1223
|
+
const dir = mkdtempSync2(join4(tmpdir3(), "polly-mutate-coverage-"));
|
|
1224
|
+
try {
|
|
1225
|
+
const planPath = join4(dir, "plan.json");
|
|
1226
|
+
writeFileSync3(planPath, JSON.stringify({ targets: plan, tests: run.testFiles }));
|
|
1227
|
+
const attribution = new Attribution(run.cwd);
|
|
1228
|
+
const chunks = chunk(run.testFiles, run.concurrency);
|
|
1229
|
+
const outcomes = await pool(chunks, run.concurrency, async (files, i) => {
|
|
1230
|
+
const out = join4(dir, `hits-${i}.json`);
|
|
1231
|
+
const attempt = (workdirs) => inCwd(workdirs, run.cwd, async (cwd) => {
|
|
1232
|
+
rmSync3(out, { force: true });
|
|
1233
|
+
const outcome = await runBunTest({
|
|
1234
|
+
cwd,
|
|
1235
|
+
files,
|
|
1236
|
+
preloads: run.preloads,
|
|
1237
|
+
env: { POLLY_MUTATE_COVERAGE: planPath, POLLY_MUTATE_COVERAGE_OUT: out },
|
|
1238
|
+
contracts: run.contracts,
|
|
1239
|
+
bail: false,
|
|
1240
|
+
junit: true,
|
|
1241
|
+
timeoutMs: COVERAGE_TIMEOUT_MS,
|
|
1242
|
+
captureStderr: true
|
|
1243
|
+
});
|
|
1244
|
+
const file = Bun.file(out);
|
|
1245
|
+
const hits = await file.exists() ? parseHits(await file.json()) : null;
|
|
1246
|
+
return { outcome, hits, cwd };
|
|
1247
|
+
});
|
|
1248
|
+
const scratch = await attempt(run.workdirs);
|
|
1249
|
+
if (scratch.outcome.exitCode === 0 || !run.workdirs)
|
|
1250
|
+
return scratch;
|
|
1251
|
+
run.log(`coverage pass: process ${i + 1}/${chunks.length} is red in a scratch cwd; rerun in-tree`);
|
|
1252
|
+
return attempt(null);
|
|
1253
|
+
});
|
|
1254
|
+
for (const [i, { outcome, hits, cwd }] of outcomes.entries()) {
|
|
1255
|
+
if (!hits) {
|
|
1256
|
+
run.log(`coverage pass: process ${i + 1}/${chunks.length} wrote no data (${outcome.timedOut ? "timed out" : `exit ${outcome.exitCode}`}); every target keeps static selection`);
|
|
1257
|
+
return null;
|
|
1258
|
+
}
|
|
1259
|
+
if (outcome.exitCode !== 0) {
|
|
1260
|
+
const failLine = outcome.stderrTail?.match(/\(fail\)[^\n]*/)?.[0] ?? "";
|
|
1261
|
+
run.log(`coverage pass: tests failed in process ${i + 1}/${chunks.length} (exit ${outcome.exitCode}) ${failLine}`.trim());
|
|
1262
|
+
}
|
|
1263
|
+
attribution.add(run.testFiles, hits, parseJunit(outcome.junitXml ?? "", cwd));
|
|
1264
|
+
}
|
|
1265
|
+
const targets = run.targets.map((_, i) => {
|
|
1266
|
+
const reason = failed.get(i);
|
|
1267
|
+
if (reason)
|
|
1268
|
+
return { kind: "static", reason };
|
|
1269
|
+
return targetCoverage(run, i, attribution, probes);
|
|
1270
|
+
});
|
|
1271
|
+
return { targets, tests: attribution.tests, byFile: attribution.byFile };
|
|
1272
|
+
} finally {
|
|
1273
|
+
rmSync3(dir, { recursive: true, force: true });
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1276
|
+
var escapeRegex = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
1277
|
+
function selectionFor(cov, coverage) {
|
|
1278
|
+
const named = cov.tests.flatMap((id) => {
|
|
1279
|
+
const t = coverage.tests[id];
|
|
1280
|
+
return t ? [t] : [];
|
|
1281
|
+
});
|
|
1282
|
+
const files = sorted([...cov.files, ...named.map((t) => real(t.file))]);
|
|
1283
|
+
const coveredBy = sorted([...cov.tests, ...cov.files.flatMap((f) => coverage.byFile[f] ?? [])]);
|
|
1284
|
+
if (cov.files.length > 0 || named.length === 0)
|
|
1285
|
+
return { files, coveredBy };
|
|
1286
|
+
const pattern = `^(?:${sorted(named.map((t) => escapeRegex(t.filterName))).join("|")})$`;
|
|
1287
|
+
return pattern.length > MAX_PATTERN ? { files, coveredBy } : { files, namePattern: pattern, coveredBy };
|
|
1288
|
+
}
|
|
1289
|
+
|
|
1290
|
+
// tools/mutate/src/engine/generate.ts
|
|
1291
|
+
import { parseSync as parseSync2 } from "oxc-parser";
|
|
1292
|
+
|
|
1293
|
+
// tools/mutate/src/engine/operators.ts
|
|
1294
|
+
import { mutate as mutateRegex } from "weapon-regex";
|
|
1295
|
+
var EQUALITY = {
|
|
1296
|
+
"<": ["<=", ">="],
|
|
1297
|
+
"<=": ["<", ">"],
|
|
1298
|
+
">": [">=", "<="],
|
|
1299
|
+
">=": [">", "<"],
|
|
1300
|
+
"==": ["!="],
|
|
1301
|
+
"!=": ["=="],
|
|
1302
|
+
"===": ["!=="],
|
|
1303
|
+
"!==": ["==="]
|
|
1304
|
+
};
|
|
1305
|
+
var ARITHMETIC = { "+": "-", "-": "+", "*": "/", "/": "*", "%": "*" };
|
|
1306
|
+
var LOGICAL = { "&&": "||", "||": "&&", "??": "&&" };
|
|
1307
|
+
var ASSIGNMENT = {
|
|
1308
|
+
"+=": "-=",
|
|
1309
|
+
"-=": "+=",
|
|
1310
|
+
"*=": "/=",
|
|
1311
|
+
"/=": "*=",
|
|
1312
|
+
"%=": "*=",
|
|
1313
|
+
"<<=": ">>=",
|
|
1314
|
+
">>=": "<<=",
|
|
1315
|
+
"&=": "|=",
|
|
1316
|
+
"|=": "&=",
|
|
1317
|
+
"&&=": "||=",
|
|
1318
|
+
"||=": "&&=",
|
|
1319
|
+
"??=": "&&="
|
|
1320
|
+
};
|
|
1321
|
+
var STRING_SAFE_ASSIGNMENTS = new Set(["&&=", "||=", "??="]);
|
|
1322
|
+
var BOOLEAN_OPERATORS = new Set(["!=", "!==", "&&", "<", "<=", "==", "===", ">", ">=", "||"]);
|
|
1323
|
+
var LOOPS = new Set(["ForStatement", "WhileStatement", "DoWhileStatement"]);
|
|
1324
|
+
var EMPTY_STRING_REPLACEMENT = '"Stryker was here!"';
|
|
1325
|
+
var METHODS = (() => {
|
|
1326
|
+
const forward = [
|
|
1327
|
+
["charAt", null],
|
|
1328
|
+
["endsWith", "startsWith"],
|
|
1329
|
+
["every", "some"],
|
|
1330
|
+
["filter", null],
|
|
1331
|
+
["reverse", null],
|
|
1332
|
+
["slice", null],
|
|
1333
|
+
["sort", null],
|
|
1334
|
+
["substr", null],
|
|
1335
|
+
["substring", null],
|
|
1336
|
+
["toLocaleLowerCase", "toLocaleUpperCase"],
|
|
1337
|
+
["toLowerCase", "toUpperCase"],
|
|
1338
|
+
["trim", null],
|
|
1339
|
+
["trimEnd", "trimStart"],
|
|
1340
|
+
["min", "max"],
|
|
1341
|
+
["setDate", "setTime"],
|
|
1342
|
+
["setFullYear", "setMonth"],
|
|
1343
|
+
["setHours", "setMinutes"],
|
|
1344
|
+
["setSeconds", "setMilliseconds"],
|
|
1345
|
+
["setUTCDate", "setTime"],
|
|
1346
|
+
["setUTCFullYear", "setUTCMonth"],
|
|
1347
|
+
["setUTCHours", "setUTCMinutes"],
|
|
1348
|
+
["setUTCSeconds", "setUTCMilliseconds"]
|
|
1349
|
+
];
|
|
1350
|
+
const map = new Map(forward);
|
|
1351
|
+
for (const [name, swap] of forward) {
|
|
1352
|
+
if (swap && name !== "setUTCDate" && name !== "getUTCDate")
|
|
1353
|
+
map.set(swap, name);
|
|
1354
|
+
}
|
|
1355
|
+
return map;
|
|
1356
|
+
})();
|
|
1357
|
+
function operatorOffset(src, op, left, right) {
|
|
1358
|
+
const gap = src.slice(left.end, right.start);
|
|
1359
|
+
const at = gap.replace(/\/\*[\s\S]*?\*\/|\/\/[^\n]*/g, (c) => " ".repeat(c.length)).indexOf(op);
|
|
1360
|
+
return at === -1 ? -1 : left.end + at;
|
|
1361
|
+
}
|
|
1362
|
+
function isStringNode(node) {
|
|
1363
|
+
if (!node)
|
|
1364
|
+
return false;
|
|
1365
|
+
return node.type === "Literal" && typeof node["value"] === "string" || node.type === "TemplateLiteral";
|
|
1366
|
+
}
|
|
1367
|
+
var text = (src, node) => src.slice(node.start, node.end);
|
|
1368
|
+
function binaryParts(node, src) {
|
|
1369
|
+
const op = stringField(node, "operator");
|
|
1370
|
+
const left = child(node, "left");
|
|
1371
|
+
const right = child(node, "right");
|
|
1372
|
+
if (!op || !left || !right)
|
|
1373
|
+
return;
|
|
1374
|
+
const at = operatorOffset(src, op, left, right);
|
|
1375
|
+
return at === -1 ? undefined : { op, left, right, at };
|
|
1376
|
+
}
|
|
1377
|
+
var binary = (node, ctx) => {
|
|
1378
|
+
if (node.type !== "BinaryExpression")
|
|
1379
|
+
return;
|
|
1380
|
+
const parts = binaryParts(node, ctx.src);
|
|
1381
|
+
if (!parts)
|
|
1382
|
+
return;
|
|
1383
|
+
const { op, left, right, at } = parts;
|
|
1384
|
+
const span = (rep, mutator) => ctx.emit(at, at + op.length, rep, mutator);
|
|
1385
|
+
for (const rep of EQUALITY[op] ?? [])
|
|
1386
|
+
span(rep, "EqualityOperator");
|
|
1387
|
+
const arith = ARITHMETIC[op];
|
|
1388
|
+
const leftProbe = left.type === "BinaryExpression" ? child(left, "right") : left;
|
|
1389
|
+
if (arith && !isStringNode(right) && !isStringNode(leftProbe))
|
|
1390
|
+
span(arith, "ArithmeticOperator");
|
|
1391
|
+
};
|
|
1392
|
+
var logical = (node, ctx) => {
|
|
1393
|
+
if (node.type !== "LogicalExpression")
|
|
1394
|
+
return;
|
|
1395
|
+
const parts = binaryParts(node, ctx.src);
|
|
1396
|
+
const rep = parts && LOGICAL[parts.op];
|
|
1397
|
+
if (!parts || !rep)
|
|
1398
|
+
return;
|
|
1399
|
+
const { op, left, right, at } = parts;
|
|
1400
|
+
if (op !== "??") {
|
|
1401
|
+
ctx.emit(at, at + op.length, rep, "LogicalOperator");
|
|
1402
|
+
return;
|
|
1403
|
+
}
|
|
1404
|
+
const operand = (n) => n.type === "LogicalExpression" ? `(${text(ctx.src, n)})` : text(ctx.src, n);
|
|
1405
|
+
const slice = (from, to) => ctx.src.slice(from, to);
|
|
1406
|
+
ctx.emit(node.start, node.end, `(${slice(node.start, left.start)}${operand(left)}${slice(left.end, at)}${rep}${slice(at + op.length, right.start)}${operand(right)}${slice(right.end, node.end)})`, "LogicalOperator");
|
|
1407
|
+
};
|
|
1408
|
+
var assignment = (node, ctx) => {
|
|
1409
|
+
if (node.type !== "AssignmentExpression")
|
|
1410
|
+
return;
|
|
1411
|
+
const parts = binaryParts(node, ctx.src);
|
|
1412
|
+
const rep = parts && ASSIGNMENT[parts.op];
|
|
1413
|
+
if (!parts || !rep)
|
|
1414
|
+
return;
|
|
1415
|
+
if (isStringNode(parts.right) && !STRING_SAFE_ASSIGNMENTS.has(parts.op))
|
|
1416
|
+
return;
|
|
1417
|
+
ctx.emit(parts.at, parts.at + parts.op.length, rep, "AssignmentOperator");
|
|
1418
|
+
};
|
|
1419
|
+
var booleanLiteral = (node, ctx) => {
|
|
1420
|
+
if (node.type === "Literal" && typeof node["value"] === "boolean") {
|
|
1421
|
+
ctx.emit(node.start, node.end, String(!node["value"]), "BooleanLiteral");
|
|
1422
|
+
}
|
|
1423
|
+
if (node.type === "UnaryExpression" && node["operator"] === "!") {
|
|
1424
|
+
ctx.emit(node.start, node.start + 1, "", "BooleanLiteral");
|
|
1425
|
+
}
|
|
1426
|
+
};
|
|
1427
|
+
var UNARY = { "+": "-", "-": "+", "~": "" };
|
|
1428
|
+
var unary = (node, ctx) => {
|
|
1429
|
+
if (node.type !== "UnaryExpression")
|
|
1430
|
+
return;
|
|
1431
|
+
const op = stringField(node, "operator");
|
|
1432
|
+
const rep = op === undefined ? undefined : UNARY[op];
|
|
1433
|
+
if (rep !== undefined)
|
|
1434
|
+
ctx.emit(node.start, node.start + 1, rep, "UnaryOperator");
|
|
1435
|
+
};
|
|
1436
|
+
var update = (node, ctx) => {
|
|
1437
|
+
if (node.type !== "UpdateExpression")
|
|
1438
|
+
return;
|
|
1439
|
+
const op = stringField(node, "operator");
|
|
1440
|
+
const arg = child(node, "argument");
|
|
1441
|
+
if (!op || !arg)
|
|
1442
|
+
return;
|
|
1443
|
+
const at = node["prefix"] === true ? node.start : arg.end;
|
|
1444
|
+
const opAt = ctx.src.indexOf(op, at);
|
|
1445
|
+
if (opAt === -1 || opAt >= node.end)
|
|
1446
|
+
return;
|
|
1447
|
+
ctx.emit(opAt, opAt + 2, op === "++" ? "--" : "++", "UpdateOperator");
|
|
1448
|
+
};
|
|
1449
|
+
function isStructuralString(node, ctx) {
|
|
1450
|
+
const p = ctx.parent;
|
|
1451
|
+
if (!p)
|
|
1452
|
+
return false;
|
|
1453
|
+
if (p.type.startsWith("Export") || p.type === "ImportExpression")
|
|
1454
|
+
return true;
|
|
1455
|
+
if (p.type === "JSXAttribute" || p.type === "ExpressionStatement")
|
|
1456
|
+
return true;
|
|
1457
|
+
if ((p.type === "Property" || p.type === "PropertyDefinition") && child(p, "key") === node) {
|
|
1458
|
+
return true;
|
|
1459
|
+
}
|
|
1460
|
+
if (p.type !== "CallExpression")
|
|
1461
|
+
return false;
|
|
1462
|
+
const callee = child(p, "callee");
|
|
1463
|
+
return callee?.type === "Identifier" && (callee["name"] === "require" || callee["name"] === "Symbol");
|
|
1464
|
+
}
|
|
1465
|
+
var stringLiteral = (node, ctx) => {
|
|
1466
|
+
if (node.type === "TemplateLiteral") {
|
|
1467
|
+
const quasis = children(node, "quasis");
|
|
1468
|
+
const value = quasis[0]?.["value"];
|
|
1469
|
+
const empty = quasis.length === 1 && isRecord(value) && value["raw"] === "";
|
|
1470
|
+
ctx.emit(node.start, node.end, empty ? "`Stryker was here!`" : "``", "StringLiteral");
|
|
1471
|
+
return;
|
|
1472
|
+
}
|
|
1473
|
+
if (node.type !== "Literal" || typeof node["value"] !== "string")
|
|
1474
|
+
return;
|
|
1475
|
+
if (isStructuralString(node, ctx))
|
|
1476
|
+
return;
|
|
1477
|
+
ctx.emit(node.start, node.end, node["value"] === "" ? EMPTY_STRING_REPLACEMENT : '""', "StringLiteral");
|
|
1478
|
+
};
|
|
1479
|
+
function isBooleanExpression(node) {
|
|
1480
|
+
if (node.type !== "BinaryExpression" && node.type !== "LogicalExpression")
|
|
1481
|
+
return false;
|
|
1482
|
+
const op = stringField(node, "operator");
|
|
1483
|
+
return op !== undefined && BOOLEAN_OPERATORS.has(op);
|
|
1484
|
+
}
|
|
1485
|
+
function conditionalTest(node, ctx) {
|
|
1486
|
+
const p = ctx.parent;
|
|
1487
|
+
const emit = (rep) => ctx.emit(node.start, node.end, rep, "ConditionalExpression");
|
|
1488
|
+
if (p && ctx.key === "test" && LOOPS.has(p.type)) {
|
|
1489
|
+
emit("false");
|
|
1490
|
+
return true;
|
|
1491
|
+
}
|
|
1492
|
+
if (p?.type === "IfStatement" && ctx.key === "test") {
|
|
1493
|
+
emit("true");
|
|
1494
|
+
emit("false");
|
|
1495
|
+
return true;
|
|
1496
|
+
}
|
|
1497
|
+
if (!isBooleanExpression(node))
|
|
1498
|
+
return false;
|
|
1499
|
+
const parentOp = p?.type === "LogicalExpression" ? stringField(p, "operator") : undefined;
|
|
1500
|
+
if (parentOp !== "||")
|
|
1501
|
+
emit("true");
|
|
1502
|
+
if (parentOp !== "&&")
|
|
1503
|
+
emit("false");
|
|
1504
|
+
return true;
|
|
1505
|
+
}
|
|
1506
|
+
function forWithoutTest(node, ctx) {
|
|
1507
|
+
const init = child(node, "init");
|
|
1508
|
+
const open = ctx.src.indexOf("(", node.start);
|
|
1509
|
+
const semi = open === -1 ? -1 : ctx.src.indexOf(";", init ? init.end : open);
|
|
1510
|
+
if (semi !== -1)
|
|
1511
|
+
ctx.emit(semi + 1, semi + 1, "false", "ConditionalExpression");
|
|
1512
|
+
}
|
|
1513
|
+
function emptySwitchCase(node, ctx) {
|
|
1514
|
+
const body = children(node, "consequent");
|
|
1515
|
+
const first = body[0];
|
|
1516
|
+
const last = body[body.length - 1];
|
|
1517
|
+
if (first && last)
|
|
1518
|
+
ctx.emit(first.start, last.end, "", "ConditionalExpression");
|
|
1519
|
+
}
|
|
1520
|
+
var conditional = (node, ctx) => {
|
|
1521
|
+
if (conditionalTest(node, ctx))
|
|
1522
|
+
return;
|
|
1523
|
+
if (node.type === "ForStatement" && !child(node, "test"))
|
|
1524
|
+
forWithoutTest(node, ctx);
|
|
1525
|
+
if (node.type === "SwitchCase")
|
|
1526
|
+
emptySwitchCase(node, ctx);
|
|
1527
|
+
};
|
|
1528
|
+
function containsSuperCall(node) {
|
|
1529
|
+
if (node.type === "CallExpression" && child(node, "callee")?.type === "Super")
|
|
1530
|
+
return true;
|
|
1531
|
+
for (const value of Object.values(node)) {
|
|
1532
|
+
const list = Array.isArray(value) ? value : [value];
|
|
1533
|
+
for (const c of list)
|
|
1534
|
+
if (isAstNode(c) && containsSuperCall(c))
|
|
1535
|
+
return true;
|
|
1536
|
+
}
|
|
1537
|
+
return false;
|
|
1538
|
+
}
|
|
1539
|
+
function isInvalidConstructorBody(ctx, block) {
|
|
1540
|
+
const n = ctx.ancestors.length;
|
|
1541
|
+
const fn = ctx.ancestors[n - 1];
|
|
1542
|
+
const method = ctx.ancestors[n - 2];
|
|
1543
|
+
const classBody = ctx.ancestors[n - 3];
|
|
1544
|
+
if (!fn || method?.type !== "MethodDefinition" || method["kind"] !== "constructor")
|
|
1545
|
+
return false;
|
|
1546
|
+
const paramProps = children(fn, "params").some((param) => param.type === "TSParameterProperty");
|
|
1547
|
+
const initialisedFields = classBody?.type === "ClassBody" && children(classBody, "body").some((m) => m.type === "PropertyDefinition" && child(m, "value") !== undefined && child(m, "key")?.type !== "PrivateIdentifier");
|
|
1548
|
+
return (paramProps || initialisedFields) && containsSuperCall(block);
|
|
1549
|
+
}
|
|
1550
|
+
var block = (node, ctx) => {
|
|
1551
|
+
if (node.type !== "BlockStatement" || children(node, "body").length === 0)
|
|
1552
|
+
return;
|
|
1553
|
+
if (isInvalidConstructorBody(ctx, node))
|
|
1554
|
+
return;
|
|
1555
|
+
ctx.emit(node.start, node.end, "{}", "BlockStatement");
|
|
1556
|
+
};
|
|
1557
|
+
var arrayDeclaration = (node, ctx) => {
|
|
1558
|
+
if (node.type === "ArrayExpression") {
|
|
1559
|
+
const empty = children(node, "elements").length === 0;
|
|
1560
|
+
ctx.emit(node.start, node.end, empty ? '["Stryker was here"]' : "[]", "ArrayDeclaration");
|
|
1561
|
+
return;
|
|
1562
|
+
}
|
|
1563
|
+
if (node.type !== "CallExpression" && node.type !== "NewExpression")
|
|
1564
|
+
return;
|
|
1565
|
+
const callee = child(node, "callee");
|
|
1566
|
+
if (callee?.type !== "Identifier" || callee["name"] !== "Array")
|
|
1567
|
+
return;
|
|
1568
|
+
const args = children(node, "arguments").length === 0 ? "[]" : "";
|
|
1569
|
+
const prefix = node.type === "NewExpression" ? "new " : "";
|
|
1570
|
+
ctx.emit(node.start, node.end, `${prefix}Array(${args})`, "ArrayDeclaration");
|
|
1571
|
+
};
|
|
1572
|
+
var objectLiteral = (node, ctx) => {
|
|
1573
|
+
if (node.type === "ObjectExpression" && children(node, "properties").length > 0) {
|
|
1574
|
+
ctx.emit(node.start, node.end, "{}", "ObjectLiteral");
|
|
1575
|
+
}
|
|
1576
|
+
};
|
|
1577
|
+
var arrowFunction = (node, ctx) => {
|
|
1578
|
+
if (node.type !== "ArrowFunctionExpression")
|
|
1579
|
+
return;
|
|
1580
|
+
const body = child(node, "body");
|
|
1581
|
+
if (!body || body.type === "BlockStatement" || node["expression"] === false)
|
|
1582
|
+
return;
|
|
1583
|
+
if (body.type === "Identifier" && body["name"] === "undefined")
|
|
1584
|
+
return;
|
|
1585
|
+
ctx.emit(node.start, node.end, "() => undefined", "ArrowFunction");
|
|
1586
|
+
};
|
|
1587
|
+
var optionalChaining = (node, ctx) => {
|
|
1588
|
+
if (node["optional"] !== true)
|
|
1589
|
+
return;
|
|
1590
|
+
if (node.type === "MemberExpression") {
|
|
1591
|
+
const object = child(node, "object");
|
|
1592
|
+
if (!object)
|
|
1593
|
+
return;
|
|
1594
|
+
const q = ctx.src.indexOf("?.", object.end);
|
|
1595
|
+
if (q === -1 || q >= node.end)
|
|
1596
|
+
return;
|
|
1597
|
+
ctx.emit(q, q + 2, node["computed"] === true ? "" : ".", "OptionalChaining");
|
|
1598
|
+
}
|
|
1599
|
+
if (node.type === "CallExpression") {
|
|
1600
|
+
const callee = child(node, "callee");
|
|
1601
|
+
if (!callee)
|
|
1602
|
+
return;
|
|
1603
|
+
const q = ctx.src.indexOf("?.", callee.end);
|
|
1604
|
+
if (q === -1 || q >= node.end)
|
|
1605
|
+
return;
|
|
1606
|
+
ctx.emit(q, q + 2, "", "OptionalChaining");
|
|
1607
|
+
}
|
|
1608
|
+
};
|
|
1609
|
+
var methodExpression = (node, ctx) => {
|
|
1610
|
+
if (node.type !== "CallExpression")
|
|
1611
|
+
return;
|
|
1612
|
+
const callee = child(node, "callee");
|
|
1613
|
+
if (callee?.type !== "MemberExpression" || callee["computed"] === true)
|
|
1614
|
+
return;
|
|
1615
|
+
const property = child(callee, "property");
|
|
1616
|
+
const object = child(callee, "object");
|
|
1617
|
+
if (property?.type !== "Identifier" || !object)
|
|
1618
|
+
return;
|
|
1619
|
+
const name = stringField(property, "name");
|
|
1620
|
+
const swap = name === undefined ? undefined : METHODS.get(name);
|
|
1621
|
+
if (swap === undefined)
|
|
1622
|
+
return;
|
|
1623
|
+
if (swap === null)
|
|
1624
|
+
ctx.emit(node.start, node.end, text(ctx.src, object), "MethodExpression");
|
|
1625
|
+
else
|
|
1626
|
+
ctx.emit(property.start, property.end, swap, "MethodExpression");
|
|
1627
|
+
};
|
|
1628
|
+
function regexPatterns(pattern, flags) {
|
|
1629
|
+
if (pattern.length === 0)
|
|
1630
|
+
return [];
|
|
1631
|
+
try {
|
|
1632
|
+
return mutateRegex(pattern, flags, { mutationLevels: [1] }).map((m) => m.pattern);
|
|
1633
|
+
} catch {
|
|
1634
|
+
return [];
|
|
1635
|
+
}
|
|
1636
|
+
}
|
|
1637
|
+
var regex = (node, ctx) => {
|
|
1638
|
+
if (node.type !== "Literal")
|
|
1639
|
+
return;
|
|
1640
|
+
const literal = node["regex"];
|
|
1641
|
+
if (isRecord(literal)) {
|
|
1642
|
+
const pattern = literal["pattern"];
|
|
1643
|
+
const flags = typeof literal["flags"] === "string" ? literal["flags"] : "";
|
|
1644
|
+
if (typeof pattern !== "string")
|
|
1645
|
+
return;
|
|
1646
|
+
for (const p of regexPatterns(pattern, flags)) {
|
|
1647
|
+
ctx.emit(node.start, node.end, `/${p}/${flags}`, "Regex");
|
|
1648
|
+
}
|
|
1649
|
+
return;
|
|
1650
|
+
}
|
|
1651
|
+
const p = ctx.parent;
|
|
1652
|
+
if (typeof node["value"] !== "string" || p?.type !== "NewExpression")
|
|
1653
|
+
return;
|
|
1654
|
+
if (calleeName(p) !== "RegExp" || child(p, "callee")?.type !== "Identifier")
|
|
1655
|
+
return;
|
|
1656
|
+
const [first, second] = children(p, "arguments");
|
|
1657
|
+
if (first !== node)
|
|
1658
|
+
return;
|
|
1659
|
+
const flags = second?.type === "Literal" ? stringField(second, "value") : undefined;
|
|
1660
|
+
for (const pattern of regexPatterns(node["value"], flags)) {
|
|
1661
|
+
ctx.emit(node.start, node.end, JSON.stringify(pattern), "Regex");
|
|
1662
|
+
}
|
|
1663
|
+
};
|
|
1664
|
+
var OPERATORS = [
|
|
1665
|
+
binary,
|
|
1666
|
+
logical,
|
|
1667
|
+
assignment,
|
|
1668
|
+
booleanLiteral,
|
|
1669
|
+
unary,
|
|
1670
|
+
update,
|
|
1671
|
+
stringLiteral,
|
|
1672
|
+
regex,
|
|
1673
|
+
conditional,
|
|
1674
|
+
block,
|
|
1675
|
+
arrayDeclaration,
|
|
1676
|
+
objectLiteral,
|
|
1677
|
+
arrowFunction,
|
|
1678
|
+
optionalChaining,
|
|
1679
|
+
methodExpression
|
|
1680
|
+
];
|
|
1681
|
+
|
|
1682
|
+
// tools/mutate/src/engine/verify-callsites.ts
|
|
1683
|
+
var VERIFY_PRIMITIVES = new Set([
|
|
1684
|
+
"requires",
|
|
1685
|
+
"ensures",
|
|
1686
|
+
"invariant",
|
|
1687
|
+
"stateConstraint",
|
|
1688
|
+
"forAllPeers",
|
|
1689
|
+
"somePeer"
|
|
1690
|
+
]);
|
|
1691
|
+
function verifyCallsiteReason(name) {
|
|
1692
|
+
return `Inside polly's ${name}(...) — a runtime no-op (compiled away in production, ` + `translated to a TLA+ assertion in verification). No test can observe or kill ` + `mutations here, so they are excluded from the score (polly#143).`;
|
|
1693
|
+
}
|
|
1694
|
+
|
|
1695
|
+
// tools/mutate/src/engine/generate.ts
|
|
1696
|
+
var TS_RUNTIME_WRAPPERS = new Set([
|
|
1697
|
+
"TSAsExpression",
|
|
1698
|
+
"TSSatisfiesExpression",
|
|
1699
|
+
"TSNonNullExpression",
|
|
1700
|
+
"TSInstantiationExpression",
|
|
1701
|
+
"TSTypeAssertion",
|
|
1702
|
+
"TSParameterProperty",
|
|
1703
|
+
"TSModuleBlock"
|
|
1704
|
+
]);
|
|
1705
|
+
var PARSE_OPTIONS2 = { preserveParens: false };
|
|
1706
|
+
var TYPE_KEYS = new Set(["typeAnnotation", "returnType", "typeParameters", "typeArguments"]);
|
|
1707
|
+
var SKIPPED_STATEMENTS = new Set(["ImportDeclaration", "ExportAllDeclaration", "Decorator"]);
|
|
1708
|
+
function isSkipped(node) {
|
|
1709
|
+
if (SKIPPED_STATEMENTS.has(node.type))
|
|
1710
|
+
return true;
|
|
1711
|
+
if (node.type === "VariableDeclaration")
|
|
1712
|
+
return node["declare"] === true;
|
|
1713
|
+
if (node.type === "TSModuleDeclaration")
|
|
1714
|
+
return node["declare"] === true;
|
|
1715
|
+
return node.type.startsWith("TS") && !TS_RUNTIME_WRAPPERS.has(node.type);
|
|
1716
|
+
}
|
|
1717
|
+
function lineIndex(src) {
|
|
1718
|
+
const starts = [0];
|
|
1719
|
+
for (let i = 0;i < src.length; i++)
|
|
1720
|
+
if (src.charCodeAt(i) === 10)
|
|
1721
|
+
starts.push(i + 1);
|
|
1722
|
+
return (offset) => {
|
|
1723
|
+
let lo = 0;
|
|
1724
|
+
let hi = starts.length - 1;
|
|
1725
|
+
while (lo < hi) {
|
|
1726
|
+
const mid = lo + hi + 1 >> 1;
|
|
1727
|
+
if ((starts[mid] ?? 0) <= offset)
|
|
1728
|
+
lo = mid;
|
|
1729
|
+
else
|
|
1730
|
+
hi = mid - 1;
|
|
1731
|
+
}
|
|
1732
|
+
return { line: lo + 1, column: offset - (starts[lo] ?? 0) + 1 };
|
|
1733
|
+
};
|
|
1734
|
+
}
|
|
1735
|
+
function applyMutant(src, m) {
|
|
1736
|
+
return src.slice(0, m.start) + m.replacement + src.slice(m.end);
|
|
1737
|
+
}
|
|
1738
|
+
function verifyReason(node) {
|
|
1739
|
+
if (node.type !== "CallExpression")
|
|
1740
|
+
return;
|
|
1741
|
+
const name = calleeName(node);
|
|
1742
|
+
return name && VERIFY_PRIMITIVES.has(name) ? verifyCallsiteReason(name) : undefined;
|
|
1743
|
+
}
|
|
1744
|
+
var FUNCTION_NODES = new Set([
|
|
1745
|
+
"FunctionDeclaration",
|
|
1746
|
+
"FunctionExpression",
|
|
1747
|
+
"ArrowFunctionExpression"
|
|
1748
|
+
]);
|
|
1749
|
+
function contractSite(file, node, ancestors, position) {
|
|
1750
|
+
if (node.type !== "CallExpression")
|
|
1751
|
+
return;
|
|
1752
|
+
const name = calleeName(node);
|
|
1753
|
+
if (name !== "requires" && name !== "ensures")
|
|
1754
|
+
return;
|
|
1755
|
+
const second = children(node, "arguments")[1];
|
|
1756
|
+
const message = second?.type === "Literal" && typeof second["value"] === "string" ? second["value"] : undefined;
|
|
1757
|
+
const handlerNode = [...ancestors].reverse().find((a) => a.type === "CallExpression" && calleeName(a) === "on") ?? [...ancestors].reverse().find((a) => FUNCTION_NODES.has(a.type));
|
|
1758
|
+
const handler = handlerNode ? { from: position(handlerNode.start).line, to: position(handlerNode.end).line } : undefined;
|
|
1759
|
+
return {
|
|
1760
|
+
file,
|
|
1761
|
+
line: position(node.start).line,
|
|
1762
|
+
primitive: name,
|
|
1763
|
+
...message === undefined ? {} : { message },
|
|
1764
|
+
...handler ? { handler } : {}
|
|
1765
|
+
};
|
|
1766
|
+
}
|
|
1767
|
+
function runtimeChildren(node) {
|
|
1768
|
+
const out = [];
|
|
1769
|
+
for (const [k, value] of Object.entries(node)) {
|
|
1770
|
+
if (TYPE_KEYS.has(k))
|
|
1771
|
+
continue;
|
|
1772
|
+
const list = Array.isArray(value) ? value : [value];
|
|
1773
|
+
for (const c of list)
|
|
1774
|
+
if (isAstNode(c))
|
|
1775
|
+
out.push([k, c]);
|
|
1776
|
+
}
|
|
1777
|
+
return out;
|
|
1778
|
+
}
|
|
1779
|
+
function generateMutants(file, src, opts = {}) {
|
|
1780
|
+
const parsed = parseSync2(file, src, PARSE_OPTIONS2);
|
|
1781
|
+
const first = parsed.errors[0];
|
|
1782
|
+
if (first)
|
|
1783
|
+
throw new Error(`${file}: cannot parse target: ${first.message}`);
|
|
1784
|
+
const position = lineIndex(src);
|
|
1785
|
+
const ignoreVerify = opts.ignoreVerifyCallsites ?? true;
|
|
1786
|
+
const prefix = opts.idPrefix ?? "";
|
|
1787
|
+
const seen = new Set;
|
|
1788
|
+
const excluded = new Set(opts.exclude ?? []);
|
|
1789
|
+
const all = [];
|
|
1790
|
+
const contracts = [];
|
|
1791
|
+
const ancestors = [];
|
|
1792
|
+
const visit = (node, key, ignored) => {
|
|
1793
|
+
if (isSkipped(node))
|
|
1794
|
+
return;
|
|
1795
|
+
const parent = ancestors[ancestors.length - 1];
|
|
1796
|
+
const site = contractSite(file, node, ancestors, position);
|
|
1797
|
+
if (site)
|
|
1798
|
+
contracts.push(site);
|
|
1799
|
+
const ignoredHere = ignored ?? (ignoreVerify ? verifyReason(node) : undefined);
|
|
1800
|
+
const ctx = {
|
|
1801
|
+
src,
|
|
1802
|
+
parent,
|
|
1803
|
+
key,
|
|
1804
|
+
ancestors,
|
|
1805
|
+
emit(start, end, replacement, mutatorName) {
|
|
1806
|
+
if (excluded.has(mutatorName))
|
|
1807
|
+
return;
|
|
1808
|
+
const dedupe = `${start}:${end}:${replacement}`;
|
|
1809
|
+
if (seen.has(dedupe))
|
|
1810
|
+
return;
|
|
1811
|
+
seen.add(dedupe);
|
|
1812
|
+
all.push({
|
|
1813
|
+
id: "",
|
|
1814
|
+
file,
|
|
1815
|
+
mutatorName,
|
|
1816
|
+
start,
|
|
1817
|
+
end,
|
|
1818
|
+
replacement,
|
|
1819
|
+
location: { start: position(start), end: position(end) },
|
|
1820
|
+
...ignoredHere ? { ignoredReason: ignoredHere } : {}
|
|
1821
|
+
});
|
|
1822
|
+
}
|
|
1823
|
+
};
|
|
1824
|
+
for (const op of OPERATORS)
|
|
1825
|
+
op(node, ctx);
|
|
1826
|
+
ancestors.push(node);
|
|
1827
|
+
for (const [k, c] of runtimeChildren(node))
|
|
1828
|
+
visit(c, k, ignoredHere);
|
|
1829
|
+
ancestors.pop();
|
|
1830
|
+
};
|
|
1831
|
+
for (const statement of parsed.program.body) {
|
|
1832
|
+
if (isAstNode(statement))
|
|
1833
|
+
visit(statement, "body", undefined);
|
|
1834
|
+
}
|
|
1835
|
+
const inRange = opts.lines ? all.filter((m) => m.location.start.line >= (opts.lines?.from ?? 0) && m.location.start.line <= (opts.lines?.to ?? 0)) : all;
|
|
1836
|
+
inRange.sort((a, b) => a.start - b.start || a.end - b.end);
|
|
1837
|
+
inRange.forEach((m, i) => {
|
|
1838
|
+
m.id = `${prefix}${i}`;
|
|
1839
|
+
});
|
|
1840
|
+
const mutants = [];
|
|
1841
|
+
const compileErrors = [];
|
|
1842
|
+
for (const m of inRange) {
|
|
1843
|
+
if (parseSync2(file, applyMutant(src, m), PARSE_OPTIONS2).errors.length > 0)
|
|
1844
|
+
compileErrors.push(m);
|
|
1845
|
+
else
|
|
1846
|
+
mutants.push(m);
|
|
1847
|
+
}
|
|
1848
|
+
return { mutants, compileErrors, contracts };
|
|
1849
|
+
}
|
|
1850
|
+
|
|
1851
|
+
// tools/mutate/src/engine/graph.ts
|
|
1852
|
+
import { realpathSync as realpathSync3 } from "node:fs";
|
|
1853
|
+
import { dirname, extname } from "node:path";
|
|
1854
|
+
import { Glob } from "bun";
|
|
1855
|
+
var SOURCE_EXTENSIONS = new Set([".ts", ".tsx", ".js", ".jsx", ".mts", ".cts", ".mjs", ".cjs"]);
|
|
1856
|
+
function loaderFor(file) {
|
|
1857
|
+
const ext = extname(file);
|
|
1858
|
+
if (ext === ".tsx")
|
|
1859
|
+
return "tsx";
|
|
1860
|
+
if (ext === ".jsx")
|
|
1861
|
+
return "jsx";
|
|
1862
|
+
if (ext === ".ts" || ext === ".mts" || ext === ".cts")
|
|
1863
|
+
return "ts";
|
|
1864
|
+
return "js";
|
|
1865
|
+
}
|
|
1866
|
+
function real2(path) {
|
|
1867
|
+
try {
|
|
1868
|
+
return realpathSync3(path);
|
|
1869
|
+
} catch {
|
|
1870
|
+
return path;
|
|
1871
|
+
}
|
|
1872
|
+
}
|
|
1873
|
+
async function findTestFiles(cwd, include) {
|
|
1874
|
+
const found = new Set;
|
|
1875
|
+
for (const pattern of include) {
|
|
1876
|
+
for await (const f of new Glob(pattern).scan({ cwd, absolute: true, onlyFiles: true })) {
|
|
1877
|
+
if (!f.includes("/node_modules/"))
|
|
1878
|
+
found.add(real2(f));
|
|
1879
|
+
}
|
|
1880
|
+
}
|
|
1881
|
+
return [...found].sort();
|
|
1882
|
+
}
|
|
1883
|
+
var transpilers = new Map;
|
|
1884
|
+
function scan(file, text) {
|
|
1885
|
+
const loader = loaderFor(file);
|
|
1886
|
+
let t = transpilers.get(loader);
|
|
1887
|
+
if (!t) {
|
|
1888
|
+
t = new Bun.Transpiler({ loader });
|
|
1889
|
+
transpilers.set(loader, t);
|
|
1890
|
+
}
|
|
1891
|
+
try {
|
|
1892
|
+
return t.scanImports(text).map((i) => i.path);
|
|
1893
|
+
} catch {
|
|
1894
|
+
return [];
|
|
1895
|
+
}
|
|
1896
|
+
}
|
|
1897
|
+
function resolveImports(file, text) {
|
|
1898
|
+
const out = [];
|
|
1899
|
+
for (const spec of scan(file, text)) {
|
|
1900
|
+
if (spec.startsWith("bun:") || spec.startsWith("node:"))
|
|
1901
|
+
continue;
|
|
1902
|
+
let resolved;
|
|
1903
|
+
try {
|
|
1904
|
+
resolved = real2(Bun.resolveSync(spec, dirname(file)));
|
|
1905
|
+
} catch {
|
|
1906
|
+
continue;
|
|
1907
|
+
}
|
|
1908
|
+
if (!resolved.includes("/node_modules/") && SOURCE_EXTENSIONS.has(extname(resolved))) {
|
|
1909
|
+
out.push(resolved);
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1912
|
+
return out;
|
|
1913
|
+
}
|
|
1914
|
+
async function buildImportGraph(testFiles) {
|
|
1915
|
+
const imports = new Map;
|
|
1916
|
+
const queue = [...testFiles];
|
|
1917
|
+
for (let file = queue.pop();file !== undefined; file = queue.pop()) {
|
|
1918
|
+
if (imports.has(file))
|
|
1919
|
+
continue;
|
|
1920
|
+
const out = resolveImports(file, await Bun.file(file).text());
|
|
1921
|
+
imports.set(file, out);
|
|
1922
|
+
queue.push(...out.filter((f) => !imports.has(f)));
|
|
1923
|
+
}
|
|
1924
|
+
return { testFiles, imports };
|
|
1925
|
+
}
|
|
1926
|
+
function importersOf(graph) {
|
|
1927
|
+
const importers = new Map;
|
|
1928
|
+
for (const [from, tos] of graph.imports) {
|
|
1929
|
+
for (const to of tos)
|
|
1930
|
+
importers.set(to, [...importers.get(to) ?? [], from]);
|
|
1931
|
+
}
|
|
1932
|
+
return importers;
|
|
1933
|
+
}
|
|
1934
|
+
function coveringTests(graph, target) {
|
|
1935
|
+
const importers = importersOf(graph);
|
|
1936
|
+
const wanted = real2(target);
|
|
1937
|
+
const seen = new Set([wanted]);
|
|
1938
|
+
const stack = [wanted];
|
|
1939
|
+
for (let f = stack.pop();f !== undefined; f = stack.pop()) {
|
|
1940
|
+
const fresh = (importers.get(f) ?? []).filter((p) => !seen.has(p));
|
|
1941
|
+
for (const p of fresh)
|
|
1942
|
+
seen.add(p);
|
|
1943
|
+
stack.push(...fresh);
|
|
1944
|
+
}
|
|
1945
|
+
const tests = new Set(graph.testFiles);
|
|
1946
|
+
return [...seen].filter((f) => tests.has(f)).sort();
|
|
1947
|
+
}
|
|
1948
|
+
function closureOf(graph, files) {
|
|
1949
|
+
const seen = new Set(files);
|
|
1950
|
+
const stack = [...files];
|
|
1951
|
+
for (let f = stack.pop();f !== undefined; f = stack.pop()) {
|
|
1952
|
+
for (const next of graph.imports.get(f) ?? []) {
|
|
1953
|
+
if (!seen.has(next)) {
|
|
1954
|
+
seen.add(next);
|
|
1955
|
+
stack.push(next);
|
|
1956
|
+
}
|
|
1957
|
+
}
|
|
1958
|
+
}
|
|
1959
|
+
return [...seen].sort();
|
|
1960
|
+
}
|
|
1961
|
+
|
|
1962
|
+
// tools/mutate/src/engine/report-writer.ts
|
|
1963
|
+
import { mkdirSync as mkdirSync3 } from "node:fs";
|
|
1964
|
+
import { dirname as dirname2, relative as relative3 } from "node:path";
|
|
1965
|
+
var ENGINE_NAME = "polly mutate (bun engine)";
|
|
1966
|
+
async function toMutationReport(result, cwd, meta) {
|
|
1967
|
+
const files = {};
|
|
1968
|
+
const testFiles = {};
|
|
1969
|
+
for (const target of result.targets) {
|
|
1970
|
+
const rel = relative3(cwd, target.file);
|
|
1971
|
+
files[rel] = {
|
|
1972
|
+
language: target.file.endsWith(".js") || target.file.endsWith(".jsx") ? "javascript" : "typescript",
|
|
1973
|
+
source: await Bun.file(target.file).text(),
|
|
1974
|
+
mutants: target.results.map((r) => ({
|
|
1975
|
+
id: r.mutant.id,
|
|
1976
|
+
mutatorName: r.mutant.mutatorName,
|
|
1977
|
+
replacement: r.mutant.replacement,
|
|
1978
|
+
status: r.status,
|
|
1979
|
+
...r.statusReason ? { statusReason: r.statusReason } : {},
|
|
1980
|
+
location: r.mutant.location,
|
|
1981
|
+
killedBy: r.killedBy ?? [],
|
|
1982
|
+
coveredBy: r.coveredBy ?? [],
|
|
1983
|
+
killedByContract: r.killedByContract ?? [],
|
|
1984
|
+
...r.contractOnly ? { contractOnly: true } : {}
|
|
1985
|
+
}))
|
|
1986
|
+
};
|
|
1987
|
+
for (const t of target.coveringTests)
|
|
1988
|
+
testFiles[relative3(cwd, t)] ??= { tests: [] };
|
|
1989
|
+
for (const t of target.tests) {
|
|
1990
|
+
const rel = relative3(cwd, t.file);
|
|
1991
|
+
testFiles[rel] ??= { tests: [] };
|
|
1992
|
+
const entry = testFiles[rel];
|
|
1993
|
+
if (!entry.tests.some((x) => x.id === t.id)) {
|
|
1994
|
+
entry.tests.push({
|
|
1995
|
+
id: t.id,
|
|
1996
|
+
name: t.name,
|
|
1997
|
+
location: { start: { line: t.line, column: 1 } }
|
|
1998
|
+
});
|
|
1999
|
+
}
|
|
2000
|
+
}
|
|
2001
|
+
}
|
|
2002
|
+
const sites = result.targets.flatMap((t) => t.contracts.map((c) => ({
|
|
2003
|
+
id: `${relative3(cwd, c.file)}:${c.line}`,
|
|
2004
|
+
file: relative3(cwd, c.file),
|
|
2005
|
+
line: c.line,
|
|
2006
|
+
primitive: c.primitive,
|
|
2007
|
+
...c.message === undefined ? {} : { message: c.message },
|
|
2008
|
+
...c.handler ? { handler: c.handler } : {}
|
|
2009
|
+
})));
|
|
2010
|
+
return {
|
|
2011
|
+
schemaVersion: "1",
|
|
2012
|
+
thresholds: { high: 80, low: 60 },
|
|
2013
|
+
framework: { name: ENGINE_NAME },
|
|
2014
|
+
files,
|
|
2015
|
+
testFiles,
|
|
2016
|
+
contracts: {
|
|
2017
|
+
mode: meta.contracts,
|
|
2018
|
+
runner: meta.runner,
|
|
2019
|
+
sites,
|
|
2020
|
+
violations: result.contractViolations.map((v) => ({
|
|
2021
|
+
target: relative3(cwd, v.target),
|
|
2022
|
+
test: v.test,
|
|
2023
|
+
contracts: v.contracts
|
|
2024
|
+
}))
|
|
2025
|
+
}
|
|
2026
|
+
};
|
|
2027
|
+
}
|
|
2028
|
+
async function writeReport(report, path) {
|
|
2029
|
+
mkdirSync3(dirname2(path), { recursive: true });
|
|
2030
|
+
await Bun.write(path, `${JSON.stringify(report, null, 2)}
|
|
2031
|
+
`);
|
|
2032
|
+
}
|
|
2033
|
+
|
|
2034
|
+
// tools/mutate/src/engine/stray-writes.ts
|
|
2035
|
+
function git(cwd, args) {
|
|
2036
|
+
const proc = Bun.spawnSync(["git", "-C", cwd, ...args], { stdout: "pipe", stderr: "ignore" });
|
|
2037
|
+
return proc.exitCode === 0 ? proc.stdout.toString() : null;
|
|
2038
|
+
}
|
|
2039
|
+
function snapshotWorktree(cwd) {
|
|
2040
|
+
const root = git(cwd, ["rev-parse", "--show-toplevel"])?.trim();
|
|
2041
|
+
if (!root)
|
|
2042
|
+
return null;
|
|
2043
|
+
const out = git(root, [
|
|
2044
|
+
"status",
|
|
2045
|
+
"--porcelain=v1",
|
|
2046
|
+
"-z",
|
|
2047
|
+
"--untracked-files=all",
|
|
2048
|
+
"--ignored=matching"
|
|
2049
|
+
]);
|
|
2050
|
+
if (out === null)
|
|
2051
|
+
return null;
|
|
2052
|
+
const entries = new Set;
|
|
2053
|
+
const tokens = out.split("\x00");
|
|
2054
|
+
for (let i = 0;i < tokens.length; i++) {
|
|
2055
|
+
const token = tokens[i];
|
|
2056
|
+
if (!token)
|
|
2057
|
+
continue;
|
|
2058
|
+
entries.add(token);
|
|
2059
|
+
if (token[0] === "R" || token[0] === "C")
|
|
2060
|
+
i++;
|
|
2061
|
+
}
|
|
2062
|
+
return { root, entries };
|
|
2063
|
+
}
|
|
2064
|
+
function newPaths(before, after) {
|
|
2065
|
+
if (!before || !after || before.root !== after.root)
|
|
2066
|
+
return [];
|
|
2067
|
+
const paths = new Set;
|
|
2068
|
+
for (const entry of after.entries)
|
|
2069
|
+
if (!before.entries.has(entry))
|
|
2070
|
+
paths.add(entry.slice(3));
|
|
2071
|
+
return [...paths].sort();
|
|
2072
|
+
}
|
|
2073
|
+
|
|
2074
|
+
// tools/mutate/src/engine/typecheck.ts
|
|
2075
|
+
import { existsSync as existsSync4 } from "node:fs";
|
|
2076
|
+
import { dirname as dirname3, join as join5, relative as relative4 } from "node:path";
|
|
2077
|
+
var TYPECHECK_OVERRIDES = {
|
|
2078
|
+
allowUnreachableCode: true,
|
|
2079
|
+
noUnusedLocals: false,
|
|
2080
|
+
noUnusedParameters: false
|
|
2081
|
+
};
|
|
2082
|
+
var TYPECHECK_CONFIG_NAME = "tsconfig.polly-mutate.json";
|
|
2083
|
+
var REASON_LIMIT = 300;
|
|
2084
|
+
async function loadApi() {
|
|
2085
|
+
try {
|
|
2086
|
+
return await import("typescript-7/unstable/async");
|
|
2087
|
+
} catch {
|
|
2088
|
+
return null;
|
|
2089
|
+
}
|
|
2090
|
+
}
|
|
2091
|
+
function tsconfigCandidates(file) {
|
|
2092
|
+
const out = [];
|
|
2093
|
+
let dir = dirname3(file);
|
|
2094
|
+
for (;; ) {
|
|
2095
|
+
const candidate = join5(dir, "tsconfig.json");
|
|
2096
|
+
if (existsSync4(candidate))
|
|
2097
|
+
out.push(candidate);
|
|
2098
|
+
const parent = dirname3(dir);
|
|
2099
|
+
if (parent === dir)
|
|
2100
|
+
return out;
|
|
2101
|
+
dir = parent;
|
|
2102
|
+
}
|
|
2103
|
+
}
|
|
2104
|
+
function virtualConfigFor(tsconfig) {
|
|
2105
|
+
return join5(dirname3(tsconfig), TYPECHECK_CONFIG_NAME);
|
|
2106
|
+
}
|
|
2107
|
+
var VIRTUAL_CONFIG_TEXT = JSON.stringify({
|
|
2108
|
+
extends: "./tsconfig.json",
|
|
2109
|
+
compilerOptions: TYPECHECK_OVERRIDES
|
|
2110
|
+
});
|
|
2111
|
+
function errorKeys(api, diagnostics) {
|
|
2112
|
+
return diagnostics.filter((d) => d.category === api.DiagnosticCategory.Error).map((d) => `${d.code}|${d.text}`);
|
|
2113
|
+
}
|
|
2114
|
+
function reasonOf(key) {
|
|
2115
|
+
const sep = key.indexOf("|");
|
|
2116
|
+
const text = key.slice(sep + 1).replace(/\s+/g, " ");
|
|
2117
|
+
const reason = `TS${key.slice(0, sep)}: ${text}`;
|
|
2118
|
+
return reason.length > REASON_LIMIT ? `${reason.slice(0, REASON_LIMIT - 1)}…` : reason;
|
|
2119
|
+
}
|
|
2120
|
+
|
|
2121
|
+
class Session {
|
|
2122
|
+
mod;
|
|
2123
|
+
snapshot = null;
|
|
2124
|
+
overlay = new Map;
|
|
2125
|
+
opened = new Map;
|
|
2126
|
+
api;
|
|
2127
|
+
constructor(mod, cwd) {
|
|
2128
|
+
this.mod = mod;
|
|
2129
|
+
this.api = new mod.API({
|
|
2130
|
+
cwd,
|
|
2131
|
+
fs: {
|
|
2132
|
+
readFile: (f) => this.overlay.get(f),
|
|
2133
|
+
fileExists: (f) => this.overlay.has(f) ? true : undefined
|
|
2134
|
+
}
|
|
2135
|
+
});
|
|
2136
|
+
}
|
|
2137
|
+
async update(params) {
|
|
2138
|
+
const next = await this.api.updateSnapshot(params);
|
|
2139
|
+
await this.snapshot?.dispose();
|
|
2140
|
+
this.snapshot = next;
|
|
2141
|
+
return next;
|
|
2142
|
+
}
|
|
2143
|
+
program(config) {
|
|
2144
|
+
const project = this.snapshot?.getProject(config);
|
|
2145
|
+
if (!project)
|
|
2146
|
+
throw new Error(`polly mutate: tsgo lost the project ${config}`);
|
|
2147
|
+
return project.program;
|
|
2148
|
+
}
|
|
2149
|
+
async projectFor(file) {
|
|
2150
|
+
for (const tsconfig of tsconfigCandidates(file)) {
|
|
2151
|
+
let config = this.opened.get(tsconfig);
|
|
2152
|
+
if (config === undefined) {
|
|
2153
|
+
config = virtualConfigFor(tsconfig);
|
|
2154
|
+
this.overlay.set(config, VIRTUAL_CONFIG_TEXT);
|
|
2155
|
+
const snap = await this.update({ openProjects: [config] });
|
|
2156
|
+
if (!snap.getProject(config))
|
|
2157
|
+
config = null;
|
|
2158
|
+
this.opened.set(tsconfig, config);
|
|
2159
|
+
}
|
|
2160
|
+
if (config === null)
|
|
2161
|
+
continue;
|
|
2162
|
+
const project = this.snapshot?.getProject(config);
|
|
2163
|
+
if (project?.rootFiles.some((f) => samePath(f, file)))
|
|
2164
|
+
return config;
|
|
2165
|
+
}
|
|
2166
|
+
return null;
|
|
2167
|
+
}
|
|
2168
|
+
async errorsIn(config, file) {
|
|
2169
|
+
const program = this.program(config);
|
|
2170
|
+
const [syntactic, semantic] = await Promise.all([
|
|
2171
|
+
program.getSyntacticDiagnostics(file),
|
|
2172
|
+
program.getSemanticDiagnostics(file)
|
|
2173
|
+
]);
|
|
2174
|
+
return [...errorKeys(this.mod, syntactic), ...errorKeys(this.mod, semantic)];
|
|
2175
|
+
}
|
|
2176
|
+
async check(config, target, src) {
|
|
2177
|
+
const invalid = new Map;
|
|
2178
|
+
const baseline = new Set(await this.errorsIn(config, target.file));
|
|
2179
|
+
try {
|
|
2180
|
+
for (const m of target.mutants) {
|
|
2181
|
+
this.overlay.set(target.file, applyMutant(src, m));
|
|
2182
|
+
await this.update({ fileChanges: { changed: [target.file] } });
|
|
2183
|
+
const fresh = (await this.errorsIn(config, target.file)).find((k) => !baseline.has(k));
|
|
2184
|
+
if (fresh !== undefined)
|
|
2185
|
+
invalid.set(m.id, reasonOf(fresh));
|
|
59
2186
|
}
|
|
2187
|
+
} finally {
|
|
2188
|
+
this.overlay.delete(target.file);
|
|
2189
|
+
await this.update({ fileChanges: { changed: [target.file] } });
|
|
60
2190
|
}
|
|
2191
|
+
return invalid;
|
|
2192
|
+
}
|
|
2193
|
+
async close() {
|
|
2194
|
+
await this.api.close();
|
|
2195
|
+
}
|
|
2196
|
+
}
|
|
2197
|
+
function samePath(a, b) {
|
|
2198
|
+
return a === b || a.toLowerCase() === b.toLowerCase();
|
|
2199
|
+
}
|
|
2200
|
+
async function typecheckMutants(targets, opts) {
|
|
2201
|
+
const log = opts.log ?? (() => {
|
|
2202
|
+
return;
|
|
61
2203
|
});
|
|
62
|
-
|
|
63
|
-
|
|
2204
|
+
const started = performance.now();
|
|
2205
|
+
const outcome = { invalid: new Map, skipped: [], checked: 0, wallMs: 0 };
|
|
2206
|
+
const withConfig = targets.filter((t) => {
|
|
2207
|
+
if (tsconfigCandidates(t.file).length > 0)
|
|
2208
|
+
return true;
|
|
2209
|
+
outcome.skipped.push({ file: t.file, reason: "no tsconfig.json above it" });
|
|
2210
|
+
return false;
|
|
2211
|
+
});
|
|
2212
|
+
if (withConfig.length === 0) {
|
|
2213
|
+
for (const s of outcome.skipped)
|
|
2214
|
+
log(`type filter: ${relative4(opts.cwd, s.file)}: ${s.reason}`);
|
|
2215
|
+
outcome.wallMs = Math.round(performance.now() - started);
|
|
2216
|
+
return outcome;
|
|
2217
|
+
}
|
|
2218
|
+
const mod = await loadApi();
|
|
2219
|
+
if (!mod) {
|
|
2220
|
+
log("type filter off: typescript-7 is not installed (bun add typescript-7@npm:typescript@7.0.2)");
|
|
2221
|
+
return null;
|
|
2222
|
+
}
|
|
2223
|
+
const session = new Session(mod, opts.cwd);
|
|
2224
|
+
try {
|
|
2225
|
+
for (const target of withConfig) {
|
|
2226
|
+
const config = await session.projectFor(target.file);
|
|
2227
|
+
if (!config) {
|
|
2228
|
+
outcome.skipped.push({ file: target.file, reason: "no tsconfig.json includes it" });
|
|
2229
|
+
continue;
|
|
2230
|
+
}
|
|
2231
|
+
const src = await Bun.file(target.file).text();
|
|
2232
|
+
const verdicts = await session.check(config, target, src);
|
|
2233
|
+
for (const [id, reason] of verdicts)
|
|
2234
|
+
outcome.invalid.set(id, reason);
|
|
2235
|
+
outcome.checked += target.mutants.length;
|
|
2236
|
+
}
|
|
2237
|
+
} finally {
|
|
2238
|
+
await session.close();
|
|
2239
|
+
}
|
|
2240
|
+
outcome.wallMs = Math.round(performance.now() - started);
|
|
2241
|
+
for (const s of outcome.skipped)
|
|
2242
|
+
log(`type filter: ${relative4(opts.cwd, s.file)}: ${s.reason}`);
|
|
2243
|
+
log(`type filter: ${outcome.invalid.size} of ${outcome.checked} mutants fail to type-check (${(outcome.wallMs / 1000).toFixed(1)} s)`);
|
|
2244
|
+
return outcome;
|
|
64
2245
|
}
|
|
65
|
-
|
|
66
|
-
|
|
2246
|
+
|
|
2247
|
+
// tools/mutate/src/engine/index.ts
|
|
2248
|
+
function projectFiles(cfg) {
|
|
2249
|
+
return [
|
|
2250
|
+
...cfg.testPreload,
|
|
2251
|
+
...["tsconfig.json", "bunfig.toml", "bun.lock"].map((f) => join6(cfg.cwd, f))
|
|
2252
|
+
];
|
|
67
2253
|
}
|
|
68
|
-
function
|
|
69
|
-
|
|
2254
|
+
async function runEngine(cfg, hooks = {}) {
|
|
2255
|
+
if (cfg.runner.kind === "command" && cfg.mode === "matrix") {
|
|
2256
|
+
throw new Error("matrix mode needs per-test results; the command runner has none");
|
|
2257
|
+
}
|
|
2258
|
+
const workdirs = cfg.scratchCwd ? createWorkdirs(cfg.cwd) : null;
|
|
2259
|
+
try {
|
|
2260
|
+
return await run(cfg, hooks, workdirs);
|
|
2261
|
+
} finally {
|
|
2262
|
+
workdirs?.remove();
|
|
2263
|
+
}
|
|
70
2264
|
}
|
|
71
|
-
async function
|
|
72
|
-
|
|
2265
|
+
async function generateAll(cfg, graph) {
|
|
2266
|
+
const out = [];
|
|
2267
|
+
for (const [index, target] of cfg.targets.entries()) {
|
|
2268
|
+
const file = realpathSync4(target.file);
|
|
2269
|
+
const { mutants, compileErrors, contracts } = generateMutants(file, await Bun.file(file).text(), {
|
|
2270
|
+
ignoreVerifyCallsites: cfg.ignoreVerifyCallsites,
|
|
2271
|
+
lines: target.lines,
|
|
2272
|
+
idPrefix: `${index}.`,
|
|
2273
|
+
exclude: cfg.excludeMutators
|
|
2274
|
+
});
|
|
2275
|
+
out.push({
|
|
2276
|
+
file,
|
|
2277
|
+
mutants,
|
|
2278
|
+
compileErrors,
|
|
2279
|
+
covering: graph ? coveringTests(graph, file) : [],
|
|
2280
|
+
contracts
|
|
2281
|
+
});
|
|
2282
|
+
}
|
|
2283
|
+
return out;
|
|
73
2284
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
2285
|
+
async function dynamicCoverage(cfg, graph, generated, preloads, workdirs, log) {
|
|
2286
|
+
const targets = generated.map((g) => ({
|
|
2287
|
+
file: g.file,
|
|
2288
|
+
mutants: g.mutants.filter((m) => !m.ignoredReason),
|
|
2289
|
+
covering: g.covering
|
|
2290
|
+
}));
|
|
2291
|
+
const key = cfg.cacheDir ? await cacheKey({
|
|
2292
|
+
mutants: targets.flatMap((t) => t.mutants),
|
|
2293
|
+
closure: [...graph.imports.keys()].sort(),
|
|
2294
|
+
projectFiles: projectFiles(cfg),
|
|
2295
|
+
settings: {
|
|
2296
|
+
coverage: COVERAGE_VERSION,
|
|
2297
|
+
targets: targets.map((t) => [t.file, t.mutants.length]),
|
|
2298
|
+
tests: graph.testFiles,
|
|
2299
|
+
contracts: cfg.contracts
|
|
2300
|
+
}
|
|
2301
|
+
}) : null;
|
|
2302
|
+
const cached = cfg.cacheDir && key ? await readCoverageCache(cfg.cacheDir, key) : null;
|
|
2303
|
+
if (cached) {
|
|
2304
|
+
log("coverage pass: from cache");
|
|
2305
|
+
return cached;
|
|
2306
|
+
}
|
|
2307
|
+
const started = performance.now();
|
|
2308
|
+
const coverage = await collectCoverage({
|
|
2309
|
+
cwd: cfg.cwd,
|
|
2310
|
+
testFiles: graph.testFiles,
|
|
2311
|
+
targets,
|
|
2312
|
+
preloads,
|
|
2313
|
+
concurrency: cfg.concurrency,
|
|
2314
|
+
workdirs,
|
|
2315
|
+
contracts: cfg.contracts,
|
|
2316
|
+
log
|
|
2317
|
+
});
|
|
2318
|
+
if (!coverage)
|
|
2319
|
+
return null;
|
|
2320
|
+
log(`coverage pass: ${Object.keys(coverage.tests).length} tests in ` + `${((performance.now() - started) / 1000).toFixed(1)} s`);
|
|
2321
|
+
if (cfg.cacheDir && key)
|
|
2322
|
+
writeCoverageCache(cfg.cacheDir, key, coverage);
|
|
2323
|
+
return coverage;
|
|
81
2324
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
2325
|
+
function sortedUnique(items) {
|
|
2326
|
+
return [...new Set(items)].sort();
|
|
2327
|
+
}
|
|
2328
|
+
function dynamicSelection(g, target, coverage) {
|
|
2329
|
+
const selections = new Map;
|
|
2330
|
+
for (const m of g.mutants) {
|
|
2331
|
+
const cov = target.mutants[m.id];
|
|
2332
|
+
if (cov)
|
|
2333
|
+
selections.set(m.id, selectionFor(cov, coverage));
|
|
2334
|
+
}
|
|
2335
|
+
const covering = sortedUnique([...selections.values()].flatMap((sel) => sel.files));
|
|
2336
|
+
const coverageTests = covering.flatMap((f) => (coverage.byFile[f] ?? []).flatMap((id) => {
|
|
2337
|
+
const t = coverage.tests[id];
|
|
2338
|
+
return t ? [{ id: t.id, file: t.file, name: t.name, line: t.line }] : [];
|
|
2339
|
+
}));
|
|
2340
|
+
return { covering, selections, coverageTests };
|
|
2341
|
+
}
|
|
2342
|
+
function selectionKey(mutants, selections) {
|
|
2343
|
+
if (!selections)
|
|
85
2344
|
return null;
|
|
86
|
-
return
|
|
2345
|
+
return mutants.map((m) => {
|
|
2346
|
+
const sel = selections.get(m.id);
|
|
2347
|
+
return [mutantSig(m), sel?.files ?? [], sel?.namePattern ?? ""];
|
|
2348
|
+
});
|
|
87
2349
|
}
|
|
88
|
-
function
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
2350
|
+
async function withCache(cfg, graph, g, picked) {
|
|
2351
|
+
const { covering, selections } = picked;
|
|
2352
|
+
const settings = {
|
|
2353
|
+
mode: cfg.mode,
|
|
2354
|
+
timeout: cfg.timeout,
|
|
2355
|
+
scratchCwd: cfg.scratchCwd,
|
|
2356
|
+
testPreload: cfg.testPreload,
|
|
2357
|
+
coverage: cfg.coverage,
|
|
2358
|
+
typecheck: cfg.typecheck,
|
|
2359
|
+
contracts: cfg.contracts,
|
|
2360
|
+
selections: selectionKey(g.mutants, selections)
|
|
2361
|
+
};
|
|
2362
|
+
const key = cfg.cacheDir && graph && covering.length > 0 ? await cacheKey({
|
|
2363
|
+
mutants: g.mutants,
|
|
2364
|
+
closure: closureOf(graph, covering),
|
|
2365
|
+
projectFiles: projectFiles(cfg),
|
|
2366
|
+
settings
|
|
2367
|
+
}) : null;
|
|
2368
|
+
return {
|
|
2369
|
+
file: g.file,
|
|
2370
|
+
mutants: g.mutants,
|
|
2371
|
+
compileErrors: g.compileErrors,
|
|
2372
|
+
...picked,
|
|
2373
|
+
covered: cfg.runner.kind === "command" || covering.length > 0,
|
|
2374
|
+
cacheKey: key,
|
|
2375
|
+
cached: cfg.cacheDir && key ? await readCache(cfg.cacheDir, key) : null,
|
|
2376
|
+
contracts: g.contracts
|
|
2377
|
+
};
|
|
103
2378
|
}
|
|
104
|
-
async function
|
|
105
|
-
const
|
|
106
|
-
const
|
|
107
|
-
if (!
|
|
108
|
-
|
|
109
|
-
for (const line of (await f.text()).split(`
|
|
110
|
-
`)) {
|
|
111
|
-
const t = line.trim();
|
|
112
|
-
if (!t)
|
|
113
|
-
continue;
|
|
114
|
-
const d = JSON.parse(t);
|
|
115
|
-
m.set(d.file, d);
|
|
2379
|
+
async function prepare(cfg, preloads, workdirs, log) {
|
|
2380
|
+
const command = cfg.runner.kind === "command";
|
|
2381
|
+
const testFiles = command ? [] : await findTestFiles(cfg.cwd, cfg.testInclude);
|
|
2382
|
+
if (!command && testFiles.length === 0) {
|
|
2383
|
+
throw new Error(`no test files match ${cfg.testInclude.join(", ")} under ${cfg.cwd}`);
|
|
116
2384
|
}
|
|
117
|
-
|
|
2385
|
+
const graph = command ? null : await buildImportGraph(testFiles);
|
|
2386
|
+
const generated = await generateAll(cfg, graph);
|
|
2387
|
+
const coverage = graph && cfg.coverage === "dynamic" ? await dynamicCoverage(cfg, graph, generated, preloads, workdirs, log) : null;
|
|
2388
|
+
const prepared = [];
|
|
2389
|
+
for (const [index, g] of generated.entries()) {
|
|
2390
|
+
const target = coverage?.targets[index];
|
|
2391
|
+
if (target?.kind === "static") {
|
|
2392
|
+
log(`${relative5(cfg.cwd, g.file)}: static test selection (${target.reason})`);
|
|
2393
|
+
}
|
|
2394
|
+
const picked = coverage && target?.kind === "dynamic" ? dynamicSelection(g, target, coverage) : { covering: g.covering, selections: null, coverageTests: [] };
|
|
2395
|
+
prepared.push(await withCache(cfg, graph, g, picked));
|
|
2396
|
+
}
|
|
2397
|
+
return { prepared, testFiles: testFiles.length };
|
|
118
2398
|
}
|
|
119
|
-
function
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
2399
|
+
function collectContractViolations(cfg, baselines, log) {
|
|
2400
|
+
const out = [];
|
|
2401
|
+
for (const [p, baseline] of baselines) {
|
|
2402
|
+
for (const v of baseline?.contractViolations ?? []) {
|
|
2403
|
+
out.push({ target: p.file, test: v.test, contracts: v.contracts });
|
|
2404
|
+
const where = v.contracts.length > 0 ? ` → ${v.contracts.join(", ")}` : "";
|
|
2405
|
+
log(`${relative5(cfg.cwd, p.file)}: test violates contract: ${v.test}${where}`);
|
|
2406
|
+
}
|
|
2407
|
+
}
|
|
2408
|
+
return out;
|
|
2409
|
+
}
|
|
2410
|
+
function selected(p, mutant) {
|
|
2411
|
+
return !p.selections || (p.selections.get(mutant.id)?.files.length ?? 0) > 0;
|
|
2412
|
+
}
|
|
2413
|
+
async function runMutant(cfg, workdirs, preloads, p, baseline, job) {
|
|
2414
|
+
const matrix = cfg.mode === "matrix";
|
|
2415
|
+
const selection = p.selections?.get(job.mutant.id);
|
|
2416
|
+
const files = selection?.files ?? p.covering;
|
|
2417
|
+
const coveredBy = selection ? { coveredBy: selection.coveredBy } : {};
|
|
2418
|
+
let runCwd = cfg.cwd;
|
|
2419
|
+
const attempt = (namePattern) => inCwd(job.inTree ? null : workdirs, cfg.cwd, (cwd) => {
|
|
2420
|
+
runCwd = cwd;
|
|
2421
|
+
return runBunTest({
|
|
2422
|
+
cwd,
|
|
2423
|
+
files,
|
|
2424
|
+
preloads,
|
|
2425
|
+
contracts: cfg.contracts,
|
|
2426
|
+
env: {
|
|
2427
|
+
POLLY_MUTATE_FILE: job.mutant.file,
|
|
2428
|
+
POLLY_MUTATE_SPAN: JSON.stringify({
|
|
2429
|
+
start: job.mutant.start,
|
|
2430
|
+
end: job.mutant.end,
|
|
2431
|
+
replacement: job.mutant.replacement
|
|
2432
|
+
})
|
|
2433
|
+
},
|
|
2434
|
+
bail: !matrix,
|
|
2435
|
+
junit: matrix || namePattern !== undefined,
|
|
2436
|
+
timeoutMs: job.timeoutMs,
|
|
2437
|
+
...namePattern ? { namePattern } : {}
|
|
2438
|
+
}, cfg.runner);
|
|
2439
|
+
});
|
|
2440
|
+
let outcome = await attempt(selection?.namePattern);
|
|
2441
|
+
let cases = outcome.junitXml ? parseJunit(outcome.junitXml, runCwd) : [];
|
|
2442
|
+
if (selection?.namePattern && !outcome.timedOut && outcome.exitCode !== 0 && matchedNothing(cases)) {
|
|
2443
|
+
outcome = await attempt(undefined);
|
|
2444
|
+
cases = outcome.junitXml ? parseJunit(outcome.junitXml, runCwd) : [];
|
|
2445
|
+
}
|
|
2446
|
+
const durationMs = Math.round(outcome.ms);
|
|
2447
|
+
const result = { mutant: job.mutant, durationMs, ...coveredBy };
|
|
2448
|
+
if (outcome.timedOut)
|
|
2449
|
+
return { ...result, status: "Timeout" };
|
|
2450
|
+
if (outcome.exitCode === 0)
|
|
2451
|
+
return { ...result, status: "Survived" };
|
|
2452
|
+
if (!matrix)
|
|
2453
|
+
return { ...result, status: "Killed" };
|
|
2454
|
+
const ran = new Set(files);
|
|
2455
|
+
const reported = new Set([...baseline.reportedFiles].filter((f) => ran.has(f)));
|
|
2456
|
+
const killedBy = killers(cases, reported, cfg.cwd);
|
|
2457
|
+
return { ...result, status: "Killed", killedBy, ...contractAttribution(cfg, cases, killedBy) };
|
|
2458
|
+
}
|
|
2459
|
+
function contractAttribution(cfg, cases, killedBy) {
|
|
2460
|
+
if (cfg.contracts === "off")
|
|
2461
|
+
return {};
|
|
2462
|
+
const kills = contractKills(cases, cfg.cwd);
|
|
2463
|
+
if (kills.length === 0)
|
|
2464
|
+
return {};
|
|
2465
|
+
const killedByContract = sortedUnique(kills.flatMap((k) => k.contracts));
|
|
2466
|
+
const failed = cases.filter((c) => c.status === "failed").length;
|
|
2467
|
+
const contractOnly = kills.length === failed && killedBy.length === failed;
|
|
2468
|
+
return { killedByContract, ...contractOnly ? { contractOnly: true } : {} };
|
|
2469
|
+
}
|
|
2470
|
+
function matchedNothing(cases) {
|
|
2471
|
+
return cases.length > 0 && cases.every((c) => c.status === "skipped");
|
|
2472
|
+
}
|
|
2473
|
+
function fromCache(p, entry) {
|
|
2474
|
+
const bySig = new Map(entry.results.map((r) => [r.sig, r]));
|
|
2475
|
+
const results = [...p.mutants, ...p.compileErrors].map((mutant) => {
|
|
2476
|
+
const hit = bySig.get(mutantSig(mutant));
|
|
2477
|
+
if (!hit)
|
|
2478
|
+
return { mutant, status: "RuntimeError", statusReason: "missing from the cache entry" };
|
|
2479
|
+
const { sig: _sig, ...rest } = hit;
|
|
2480
|
+
return { mutant, ...rest };
|
|
2481
|
+
});
|
|
2482
|
+
results.sort((a, b) => a.mutant.start - b.mutant.start);
|
|
2483
|
+
return {
|
|
2484
|
+
file: p.file,
|
|
2485
|
+
coveringTests: p.covering,
|
|
2486
|
+
baselineMs: entry.baselineMs,
|
|
2487
|
+
inTree: entry.inTree,
|
|
2488
|
+
cached: true,
|
|
2489
|
+
tests: entry.tests,
|
|
2490
|
+
results,
|
|
2491
|
+
contracts: p.contracts
|
|
2492
|
+
};
|
|
2493
|
+
}
|
|
2494
|
+
function testsFor(cfg, p, baseline, results) {
|
|
2495
|
+
const tests = [...baseline.tests];
|
|
2496
|
+
const known = new Set(tests.map((t) => t.id));
|
|
2497
|
+
for (const t of p.coverageTests) {
|
|
2498
|
+
if (!known.has(t.id)) {
|
|
2499
|
+
known.add(t.id);
|
|
2500
|
+
tests.push(t);
|
|
2501
|
+
}
|
|
2502
|
+
}
|
|
2503
|
+
const used = new Set(results.flatMap((r) => r.killedBy ?? []));
|
|
2504
|
+
for (const file of p.covering) {
|
|
2505
|
+
const id = loadFailureId(cfg.cwd, file);
|
|
2506
|
+
if (used.has(id) && !known.has(id))
|
|
2507
|
+
tests.push({ id, file, name: LOAD_FAILURE_NAME, line: 0 });
|
|
2508
|
+
}
|
|
2509
|
+
return tests;
|
|
2510
|
+
}
|
|
2511
|
+
function unrun(mutant, statusReason) {
|
|
2512
|
+
return { mutant, status: "RuntimeError", statusReason };
|
|
2513
|
+
}
|
|
2514
|
+
function notRun(p, baseline, mutant) {
|
|
2515
|
+
if (!p.covered) {
|
|
2516
|
+
const reason = p.selections ? "no test ran this target" : "no test file imports this target";
|
|
2517
|
+
return { mutant, status: "NoCoverage", statusReason: reason };
|
|
2518
|
+
}
|
|
2519
|
+
if (baseline?.contractViolations)
|
|
2520
|
+
return unrun(mutant, baseline.reason);
|
|
2521
|
+
if (!selected(p, mutant)) {
|
|
2522
|
+
return { mutant, status: "NoCoverage", statusReason: "no test ran this code", coveredBy: [] };
|
|
2523
|
+
}
|
|
2524
|
+
if (!baseline?.ok)
|
|
2525
|
+
return unrun(mutant, baseline?.reason);
|
|
128
2526
|
return null;
|
|
129
2527
|
}
|
|
130
|
-
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
2528
|
+
function summarise(cfg, p, baseline, byId, invalid, workdirs) {
|
|
2529
|
+
const results = p.mutants.map((mutant) => {
|
|
2530
|
+
if (mutant.ignoredReason)
|
|
2531
|
+
return { mutant, status: "Ignored", statusReason: mutant.ignoredReason };
|
|
2532
|
+
const typeError = invalid.get(mutant.id);
|
|
2533
|
+
if (typeError)
|
|
2534
|
+
return { mutant, status: "CompileError", statusReason: typeError };
|
|
2535
|
+
return notRun(p, baseline, mutant) ?? byId.get(mutant.id) ?? unrun(mutant, "mutant was not run");
|
|
2536
|
+
});
|
|
2537
|
+
for (const mutant of p.compileErrors) {
|
|
2538
|
+
results.push({ mutant, status: "CompileError", statusReason: "mutated source does not parse" });
|
|
2539
|
+
}
|
|
2540
|
+
results.sort((a, b) => a.mutant.start - b.mutant.start);
|
|
2541
|
+
return {
|
|
2542
|
+
file: p.file,
|
|
2543
|
+
coveringTests: p.covering,
|
|
2544
|
+
baselineMs: baseline?.ok ? Math.round(baseline.ms) : null,
|
|
2545
|
+
inTree: baseline?.inTree ?? !workdirs,
|
|
2546
|
+
cached: false,
|
|
2547
|
+
tests: baseline?.ok ? testsFor(cfg, p, baseline, results) : p.coverageTests,
|
|
2548
|
+
results,
|
|
2549
|
+
contracts: p.contracts
|
|
2550
|
+
};
|
|
2551
|
+
}
|
|
2552
|
+
async function run(cfg, hooks, workdirs) {
|
|
2553
|
+
const log = hooks.log ?? (() => {
|
|
2554
|
+
return;
|
|
2555
|
+
});
|
|
2556
|
+
const started = performance.now();
|
|
2557
|
+
const preloads = [...cfg.testPreload, preloadPath()];
|
|
2558
|
+
const beforeRun = snapshotWorktree(cfg.cwd);
|
|
2559
|
+
const { prepared, testFiles } = await prepare(cfg, preloads, workdirs, log);
|
|
2560
|
+
const totalMutants = prepared.reduce((n, p) => n + p.mutants.length, 0);
|
|
2561
|
+
const hits = prepared.filter((p) => p.cached).length;
|
|
2562
|
+
log(`${prepared.length} target(s), ${totalMutants} mutants, ` + (cfg.runner.kind === "command" ? `command \`${cfg.runner.argv.join(" ")}\`` : `${testFiles} test files in scope`) + `, ${cfg.mode} mode` + (cfg.runner.kind === "bun-test" ? `, ${cfg.coverage} coverage` : "") + (cfg.cacheDir ? `, ${hits} target(s) from cache` : ""));
|
|
2563
|
+
const fresh = prepared.filter((p) => !p.cached && p.covered);
|
|
2564
|
+
const [measured, typed] = await Promise.all([
|
|
2565
|
+
pool(fresh, cfg.concurrency, (p) => measureBaseline(cfg, workdirs, preloads, p, log)),
|
|
2566
|
+
cfg.typecheck ? typecheckMutants(prepared.filter((p) => !p.cached).map((p) => ({ file: p.file, mutants: p.mutants.filter((m) => !m.ignoredReason) })), { cwd: cfg.cwd, log }) : null
|
|
2567
|
+
]);
|
|
2568
|
+
const invalid = typed?.invalid ?? new Map;
|
|
2569
|
+
const baselines = new Map(fresh.map((p, i) => [p, measured[i] ?? null]));
|
|
2570
|
+
const afterBaselines = snapshotWorktree(cfg.cwd);
|
|
2571
|
+
const contractViolations = collectContractViolations(cfg, baselines, log);
|
|
2572
|
+
const jobs = [];
|
|
2573
|
+
prepared.forEach((p, target) => {
|
|
2574
|
+
const baseline = baselines.get(p);
|
|
2575
|
+
if (!baseline?.ok)
|
|
2576
|
+
return;
|
|
2577
|
+
const timeoutMs = Math.round(baseline.ms * cfg.timeout.factor + cfg.timeout.extraMs);
|
|
2578
|
+
for (const mutant of p.mutants) {
|
|
2579
|
+
if (!mutant.ignoredReason && !invalid.has(mutant.id) && selected(p, mutant))
|
|
2580
|
+
jobs.push({ target, mutant, timeoutMs, inTree: baseline.inTree });
|
|
142
2581
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
2582
|
+
});
|
|
2583
|
+
jobs.sort((a, b) => b.timeoutMs - a.timeoutMs);
|
|
2584
|
+
let done = 0;
|
|
2585
|
+
const runResults = await pool(jobs, cfg.concurrency, async (job) => {
|
|
2586
|
+
const p = prepared[job.target];
|
|
2587
|
+
const baseline = p && baselines.get(p);
|
|
2588
|
+
if (!p || !baseline)
|
|
2589
|
+
throw new Error(`job for target ${job.target} has no baseline`);
|
|
2590
|
+
const result = await runMutant(cfg, workdirs, preloads, p, baseline, job);
|
|
2591
|
+
done++;
|
|
2592
|
+
hooks.progress?.(done, jobs.length);
|
|
2593
|
+
return result;
|
|
2594
|
+
});
|
|
2595
|
+
const byId = new Map(runResults.map((r) => [r.mutant.id, r]));
|
|
2596
|
+
const targets = prepared.map((p) => p.cached ? fromCache(p, p.cached) : summarise(cfg, p, baselines.get(p) ?? null, byId, invalid, workdirs));
|
|
2597
|
+
const afterMutants = snapshotWorktree(cfg.cwd);
|
|
2598
|
+
const strayWrites = beforeRun ? {
|
|
2599
|
+
root: beforeRun.root,
|
|
2600
|
+
baseline: newPaths(beforeRun, afterBaselines),
|
|
2601
|
+
mutants: newPaths(afterBaselines, afterMutants)
|
|
2602
|
+
} : null;
|
|
2603
|
+
for (const path of strayWrites?.mutants ?? [])
|
|
2604
|
+
log(`warning: a mutant run wrote ${path}`);
|
|
2605
|
+
if (cfg.cacheDir) {
|
|
2606
|
+
targets.forEach((t, i) => {
|
|
2607
|
+
const p = prepared[i];
|
|
2608
|
+
if (!cfg.cacheDir || !p?.cacheKey || t.cached || t.baselineMs === null)
|
|
2609
|
+
return;
|
|
2610
|
+
writeCache(cfg.cacheDir, p.cacheKey, {
|
|
2611
|
+
baselineMs: t.baselineMs,
|
|
2612
|
+
inTree: t.inTree,
|
|
2613
|
+
tests: t.tests,
|
|
2614
|
+
results: t.results.map(cachedResult)
|
|
2615
|
+
});
|
|
2616
|
+
});
|
|
148
2617
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
2618
|
+
const result = {
|
|
2619
|
+
targets,
|
|
2620
|
+
wallMs: Math.round(performance.now() - started),
|
|
2621
|
+
strayWrites,
|
|
2622
|
+
contractViolations
|
|
2623
|
+
};
|
|
2624
|
+
await writeReport(await toMutationReport(result, cfg.cwd, { contracts: cfg.contracts, runner: cfg.runner.kind }), cfg.reportPath);
|
|
2625
|
+
return result;
|
|
2626
|
+
}
|
|
2627
|
+
function mutationScore(results) {
|
|
2628
|
+
let detected = 0;
|
|
2629
|
+
let valid = 0;
|
|
2630
|
+
for (const r of results) {
|
|
2631
|
+
if (r.status === "Killed" || r.status === "Timeout")
|
|
2632
|
+
detected++;
|
|
2633
|
+
if (r.status === "Killed" || r.status === "Timeout" || r.status === "Survived" || r.status === "NoCoverage")
|
|
2634
|
+
valid++;
|
|
152
2635
|
}
|
|
2636
|
+
return valid === 0 ? null : 100 * detected / valid;
|
|
2637
|
+
}
|
|
2638
|
+
// tools/mutate/src/init.ts
|
|
2639
|
+
import { existsSync as existsSync6 } from "node:fs";
|
|
2640
|
+
import { join as join8 } from "node:path";
|
|
2641
|
+
|
|
2642
|
+
// tools/mutate/src/migrate.ts
|
|
2643
|
+
import { existsSync as existsSync5 } from "node:fs";
|
|
2644
|
+
import { dirname as dirname4, join as join7, relative as relative6 } from "node:path";
|
|
2645
|
+
var MAPPED = new Set([
|
|
2646
|
+
"mutate",
|
|
2647
|
+
"testRunner",
|
|
2648
|
+
"bun",
|
|
2649
|
+
"commandRunner",
|
|
2650
|
+
"concurrency",
|
|
2651
|
+
"timeoutFactor",
|
|
2652
|
+
"timeoutMS",
|
|
2653
|
+
"disableBail",
|
|
2654
|
+
"mutator",
|
|
2655
|
+
"thresholds",
|
|
2656
|
+
"ignorers"
|
|
2657
|
+
]);
|
|
2658
|
+
var SILENT = new Set(["$schema", "_comment"]);
|
|
2659
|
+
var SHELL_SYNTAX = /[|&;<>()$`"'\\*?{}[\]~]/;
|
|
2660
|
+
function preloadArgs(nodeArgs) {
|
|
153
2661
|
const out = [];
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
out
|
|
160
|
-
`) : " none");
|
|
161
|
-
out.push(`
|
|
162
|
-
SETTLED (fresh decisions, suppressed from action list — ${settled.length})`);
|
|
163
|
-
out.push(settled.length ? settled.join(`
|
|
164
|
-
`) : " none");
|
|
165
|
-
return out.join(`
|
|
166
|
-
`);
|
|
2662
|
+
nodeArgs.forEach((arg, i) => {
|
|
2663
|
+
const value = arg === "--preload" ? nodeArgs[i + 1] : arg.startsWith("--preload=") ? arg.slice(10) : undefined;
|
|
2664
|
+
if (value)
|
|
2665
|
+
out.push(value);
|
|
2666
|
+
});
|
|
2667
|
+
return out;
|
|
167
2668
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
2669
|
+
function commandArgv(command) {
|
|
2670
|
+
const trimmed = command.trim();
|
|
2671
|
+
return SHELL_SYNTAX.test(trimmed) ? ["sh", "-c", trimmed] : trimmed.split(/\s+/);
|
|
2672
|
+
}
|
|
2673
|
+
function stringList(value) {
|
|
2674
|
+
return Array.isArray(value) ? value.filter((v) => typeof v === "string") : [];
|
|
2675
|
+
}
|
|
2676
|
+
function timeoutOf(raw) {
|
|
2677
|
+
const factor = raw["timeoutFactor"];
|
|
2678
|
+
const ms = raw["timeoutMS"];
|
|
2679
|
+
if (typeof factor !== "number" && typeof ms !== "number")
|
|
2680
|
+
return;
|
|
2681
|
+
return {
|
|
2682
|
+
factor: typeof factor === "number" ? factor : 1.5,
|
|
2683
|
+
extraMs: typeof ms === "number" ? ms : 5000
|
|
2684
|
+
};
|
|
2685
|
+
}
|
|
2686
|
+
function testsOf(raw, where) {
|
|
2687
|
+
const bun = isRecord(raw["bun"]) ? raw["bun"] : {};
|
|
2688
|
+
const include = stringList(bun["testFiles"]);
|
|
2689
|
+
if (include.length === 0)
|
|
2690
|
+
throw new Error(`${where} has no bun.testFiles`);
|
|
2691
|
+
const preload = preloadArgs(stringList(bun["nodeArgs"]));
|
|
2692
|
+
return preload.length > 0 ? { include, preload } : { include };
|
|
2693
|
+
}
|
|
2694
|
+
function runnerOf(raw, where) {
|
|
2695
|
+
const runnerName = raw["testRunner"];
|
|
2696
|
+
if (runnerName === "command") {
|
|
2697
|
+
const cmd = isRecord(raw["commandRunner"]) ? raw["commandRunner"]["command"] : undefined;
|
|
2698
|
+
const argv = commandArgv(typeof cmd === "string" ? cmd : "npm test");
|
|
2699
|
+
return { runner: { kind: "command", argv } };
|
|
172
2700
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
2701
|
+
if (runnerName === undefined || runnerName === "bun") {
|
|
2702
|
+
return {
|
|
2703
|
+
tests: testsOf(raw, where),
|
|
2704
|
+
...raw["disableBail"] === true ? { mode: "matrix" } : {}
|
|
2705
|
+
};
|
|
177
2706
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
2707
|
+
throw new Error(`${where}: testRunner "${String(runnerName)}" has no polly counterpart (bun or command)`);
|
|
2708
|
+
}
|
|
2709
|
+
function tuningOf(raw) {
|
|
2710
|
+
const out = {};
|
|
2711
|
+
if (typeof raw["concurrency"] === "number")
|
|
2712
|
+
out["concurrency"] = raw["concurrency"];
|
|
2713
|
+
const timeout = timeoutOf(raw);
|
|
2714
|
+
if (timeout)
|
|
2715
|
+
out["timeout"] = timeout;
|
|
2716
|
+
const excluded = isRecord(raw["mutator"]) ? stringList(raw["mutator"]["excludedMutations"]) : [];
|
|
2717
|
+
if (excluded.length > 0)
|
|
2718
|
+
out["mutators"] = { exclude: excluded };
|
|
2719
|
+
const brk = isRecord(raw["thresholds"]) ? raw["thresholds"]["break"] : undefined;
|
|
2720
|
+
if (typeof brk === "number")
|
|
2721
|
+
out["thresholds"] = { break: brk };
|
|
2722
|
+
return out;
|
|
2723
|
+
}
|
|
2724
|
+
function strykerToMutation(raw, where) {
|
|
2725
|
+
if (!isRecord(raw))
|
|
2726
|
+
throw new Error(`${where} is not a JSON object`);
|
|
2727
|
+
const draft = { targets: stringList(raw["mutate"]), ...runnerOf(raw, where), ...tuningOf(raw) };
|
|
2728
|
+
const notes = [];
|
|
2729
|
+
if (!stringList(raw["ignorers"]).includes("polly-verify")) {
|
|
2730
|
+
notes.push("ignoreVerifyCallsites defaults to true: requires/ensures mutants are reported Ignored (polly#143)");
|
|
2731
|
+
}
|
|
2732
|
+
const dropped = Object.keys(raw).filter((k) => !MAPPED.has(k) && !SILENT.has(k));
|
|
2733
|
+
if (dropped.length > 0)
|
|
2734
|
+
notes.push(`dropped Stryker-only keys: ${dropped.join(", ")}`);
|
|
2735
|
+
return { config: parseMutationConfig(draft, where), notes };
|
|
2736
|
+
}
|
|
2737
|
+
async function readStrykerConfig(path) {
|
|
2738
|
+
return strykerToMutation(await Bun.file(path).json(), path);
|
|
2739
|
+
}
|
|
2740
|
+
function defineImportSpecifier(dir) {
|
|
2741
|
+
try {
|
|
2742
|
+
Bun.resolveSync("@fairfox/polly/mutate", dir);
|
|
2743
|
+
return "@fairfox/polly/mutate";
|
|
2744
|
+
} catch {
|
|
2745
|
+
const local = join7(import.meta.dir, "define.ts");
|
|
2746
|
+
if (!existsSync5(local))
|
|
2747
|
+
return "@fairfox/polly/mutate";
|
|
2748
|
+
const rel = relative6(dir, local);
|
|
2749
|
+
return rel.startsWith(".") ? rel : `./${rel}`;
|
|
2750
|
+
}
|
|
2751
|
+
}
|
|
2752
|
+
var LINE_WIDTH = 100;
|
|
2753
|
+
function literal(value) {
|
|
2754
|
+
return JSON.stringify(value, null, 2).replace(/^(\s*)"([A-Za-z_$][\w$]*)":/gm, "$1$2:").replace(/([^\s[{,])(?=\n\s*[}\]])/g, "$1,").replace(/^([ ]*)([\w$]+: )?\[\n((?:[ ]*(?:"[^"\n]*"|-?\d+(?:\.\d+)?|true|false),\n)+)([ ]*)\](,?)$/gm, (m, indent, key, items, _close, comma) => {
|
|
2755
|
+
const inline = `${indent}${key ?? ""}[${items.trim().replace(/,$/, "").split(/,\n\s*/).join(", ")}]${comma}`;
|
|
2756
|
+
return inline.length <= LINE_WIDTH ? inline : m;
|
|
2757
|
+
});
|
|
2758
|
+
}
|
|
2759
|
+
function renderMutationConfig(config, specifier) {
|
|
2760
|
+
return [
|
|
2761
|
+
`import { defineMutation } from "${specifier}";`,
|
|
2762
|
+
"",
|
|
2763
|
+
`export default defineMutation(${literal(config)});`,
|
|
2764
|
+
""
|
|
2765
|
+
].join(`
|
|
188
2766
|
`);
|
|
189
|
-
console.log(`✓ recorded: ${file} [${verdict}]${rationale ? ` — ${rationale}` : ""}`);
|
|
190
2767
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
const
|
|
197
|
-
const
|
|
198
|
-
|
|
2768
|
+
async function migrateConfig(strykerPath, opts = {}) {
|
|
2769
|
+
const { config, notes } = await readStrykerConfig(strykerPath);
|
|
2770
|
+
const dir = dirname4(strykerPath);
|
|
2771
|
+
const path = join7(dir, MUTATION_CONFIG_FILE);
|
|
2772
|
+
const source = renderMutationConfig(config, defineImportSpecifier(dir));
|
|
2773
|
+
const existed = existsSync5(path);
|
|
2774
|
+
const written = Boolean(opts.write) && (!existed || Boolean(opts.force));
|
|
2775
|
+
if (written)
|
|
2776
|
+
await Bun.write(path, source);
|
|
2777
|
+
return { path, source, notes, written, existed };
|
|
199
2778
|
}
|
|
2779
|
+
|
|
2780
|
+
// tools/mutate/src/init.ts
|
|
200
2781
|
function detectMutate(projectDir) {
|
|
201
|
-
if (
|
|
2782
|
+
if (existsSync6(join8(projectDir, "src"))) {
|
|
202
2783
|
return ["src/**/*.ts", "!src/**/*.test.ts", "!src/**/*.d.ts"];
|
|
203
2784
|
}
|
|
204
|
-
return ["**/*.ts", "!**/*.test.ts", "!**/*.d.ts", "!
|
|
2785
|
+
return ["**/*.ts", "!**/*.test.ts", "!**/*.d.ts", "!dist/**", `!${MUTATION_CONFIG_FILE}`];
|
|
205
2786
|
}
|
|
206
2787
|
function detectTestGlob(projectDir) {
|
|
207
2788
|
for (const d of ["tests", "test"]) {
|
|
208
|
-
if (
|
|
2789
|
+
if (existsSync6(join8(projectDir, d)))
|
|
209
2790
|
return `${d}/**/*.test.ts`;
|
|
210
2791
|
}
|
|
211
2792
|
return "**/*.test.ts";
|
|
212
2793
|
}
|
|
213
2794
|
async function initConfig(projectDir, opts = {}) {
|
|
214
|
-
const configPath =
|
|
2795
|
+
const configPath = join8(projectDir, MUTATION_CONFIG_FILE);
|
|
215
2796
|
const warnings = [];
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
2797
|
+
const specifier = defineImportSpecifier(projectDir);
|
|
2798
|
+
if (specifier === "@fairfox/polly/mutate") {
|
|
2799
|
+
try {
|
|
2800
|
+
Bun.resolveSync(specifier, projectDir);
|
|
2801
|
+
} catch {
|
|
2802
|
+
warnings.push("@fairfox/polly does not resolve here — install it, or the config cannot load");
|
|
2803
|
+
}
|
|
221
2804
|
}
|
|
222
2805
|
const mutate = detectMutate(projectDir);
|
|
223
2806
|
const testGlob = detectTestGlob(projectDir);
|
|
224
|
-
if (!
|
|
225
|
-
warnings.push("no src/ directory — adjust
|
|
226
|
-
}
|
|
227
|
-
if (!
|
|
228
|
-
warnings.push(`no tests/ directory — using "${testGlob}"; adjust
|
|
229
|
-
}
|
|
230
|
-
if (
|
|
231
|
-
return { configPath, created: false,
|
|
232
|
-
}
|
|
233
|
-
const config = {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
plugins: ["stryker-mutator-bun-runner", pluginRef],
|
|
237
|
-
ignorers: ["polly-verify"],
|
|
238
|
-
coverageAnalysis: "all",
|
|
239
|
-
disableBail: true,
|
|
240
|
-
reporters: ["json", "clear-text"],
|
|
241
|
-
mutate,
|
|
242
|
-
thresholds: { high: 90, low: 70, break: null },
|
|
243
|
-
ignorePatterns: [
|
|
244
|
-
"**/node_modules/**",
|
|
245
|
-
"**/dist/**",
|
|
246
|
-
"**/.git/**",
|
|
247
|
-
"**/.stryker-tmp/**",
|
|
248
|
-
"**/reports/**"
|
|
249
|
-
],
|
|
250
|
-
timeoutMS: 60000,
|
|
251
|
-
concurrency: 4
|
|
252
|
-
};
|
|
253
|
-
await Bun.write(configPath, `${JSON.stringify(config, null, 2)}
|
|
254
|
-
`);
|
|
255
|
-
return { configPath, created: true, pluginRef, mutate, testGlob, warnings };
|
|
2807
|
+
if (!existsSync6(join8(projectDir, "src"))) {
|
|
2808
|
+
warnings.push("no src/ directory — adjust `targets` to point at your source");
|
|
2809
|
+
}
|
|
2810
|
+
if (!existsSync6(join8(projectDir, "tests")) && !existsSync6(join8(projectDir, "test"))) {
|
|
2811
|
+
warnings.push(`no tests/ directory — using "${testGlob}"; adjust tests.include if needed`);
|
|
2812
|
+
}
|
|
2813
|
+
if (existsSync6(configPath) && !opts.force) {
|
|
2814
|
+
return { configPath, created: false, specifier, mutate, testGlob, warnings };
|
|
2815
|
+
}
|
|
2816
|
+
const config = { targets: mutate, tests: { include: [testGlob] } };
|
|
2817
|
+
await Bun.write(configPath, renderMutationConfig(config, specifier));
|
|
2818
|
+
return { configPath, created: true, specifier, mutate, testGlob, warnings };
|
|
256
2819
|
}
|
|
257
2820
|
// tools/mutate/src/report.ts
|
|
2821
|
+
function contractsSection(db, p) {
|
|
2822
|
+
const meta = new Map(queryAll(db, "SELECT key, value FROM report_meta").map((r) => [
|
|
2823
|
+
r.key,
|
|
2824
|
+
r.value
|
|
2825
|
+
]));
|
|
2826
|
+
const mode = meta.get("contracts_mode");
|
|
2827
|
+
if (mode === undefined)
|
|
2828
|
+
return;
|
|
2829
|
+
const nSites = queryGet(db, "SELECT count(*) n FROM contract_sites")?.n ?? 0;
|
|
2830
|
+
p(`
|
|
2831
|
+
CONTRACTS (requires/ensures as kill oracles — mode ${mode}, ${nSites} site(s))`);
|
|
2832
|
+
if (mode === "off") {
|
|
2833
|
+
p(` off — the sites are inventoried only; 'polly mutate run --contracts all' makes each one fail the test that falsifies it`);
|
|
2834
|
+
return;
|
|
2835
|
+
}
|
|
2836
|
+
const violations = queryAll(db, "SELECT target, test, contract_id FROM contract_violations ORDER BY target, test, contract_id");
|
|
2837
|
+
if (violations.length > 0) {
|
|
2838
|
+
p(` TEST VIOLATES CONTRACT (the unmutated code fails a contract under a test: ${violations.length})`);
|
|
2839
|
+
for (const v of violations) {
|
|
2840
|
+
p(` ${v.target}: ${v.test}${v.contract_id ? ` → ${v.contract_id}` : ""}`);
|
|
2841
|
+
}
|
|
2842
|
+
}
|
|
2843
|
+
if (meta.get("runner") === "command") {
|
|
2844
|
+
p(` command runner: no per-test results, so kills are not attributed to contracts; compare the killed count against a --contracts off run`);
|
|
2845
|
+
return;
|
|
2846
|
+
}
|
|
2847
|
+
if (mode === "ensures")
|
|
2848
|
+
p(` requires sites are inventoried, not enforced (mode ensures)`);
|
|
2849
|
+
const only = queryAll(db, "SELECT file, line, mutator, count(*) n FROM mutants WHERE contract_only=1 GROUP BY file, line, mutator ORDER BY file, line");
|
|
2850
|
+
const onlyTotal = only.reduce((a, r) => a + r.n, 0);
|
|
2851
|
+
p(` CONTRACT-ONLY KILLS (no test assertion caught these; a contract did: ${onlyTotal})`);
|
|
2852
|
+
for (const r of only)
|
|
2853
|
+
p(` ${r.file}:${r.line} ${r.mutator}${r.n > 1 ? ` ×${r.n}` : ""}`);
|
|
2854
|
+
if (nSites === 0)
|
|
2855
|
+
return;
|
|
2856
|
+
const sites = queryAll(db, `SELECT s.id, s.primitive, s.message,
|
|
2857
|
+
(SELECT count(*) FROM contract_kills k WHERE k.contract_id = s.id) n,
|
|
2858
|
+
(SELECT count(*) FROM mutants m
|
|
2859
|
+
WHERE m.file = s.file AND s.handler_from IS NOT NULL
|
|
2860
|
+
AND m.line BETWEEN s.handler_from AND s.handler_to
|
|
2861
|
+
AND m.status IN ('Killed', 'Survived', 'Timeout')) exercised
|
|
2862
|
+
FROM contract_sites s ORDER BY n DESC, s.id`);
|
|
2863
|
+
p(` KILLS PER CONTRACT`);
|
|
2864
|
+
for (const s of sites) {
|
|
2865
|
+
p(` ${s.id} ${s.primitive} ${s.n} kill(s)${s.message ? ` “${s.message}”` : ""}`);
|
|
2866
|
+
}
|
|
2867
|
+
const toothless = sites.filter((s) => s.n === 0 && s.exercised > 0);
|
|
2868
|
+
const idle = sites.filter((s) => s.n === 0 && s.exercised === 0);
|
|
2869
|
+
p(` CONTRACTS WITHOUT TEETH (the handler's mutants ran, the contract never fired: ${toothless.length})`);
|
|
2870
|
+
if (toothless.length === 0)
|
|
2871
|
+
p(` none`);
|
|
2872
|
+
for (const s of toothless)
|
|
2873
|
+
p(` ${s.id} ${s.primitive}${s.message ? ` “${s.message}”` : ""}`);
|
|
2874
|
+
if (idle.length > 0) {
|
|
2875
|
+
p(` HANDLER NOT EXERCISED (no mutant in the contract's handler ran: ${idle.length})`);
|
|
2876
|
+
for (const s of idle)
|
|
2877
|
+
p(` ${s.id} ${s.primitive}`);
|
|
2878
|
+
}
|
|
2879
|
+
}
|
|
258
2880
|
function greedyCover(units, universe) {
|
|
259
2881
|
const remaining = new Set(universe);
|
|
260
2882
|
const chosen = [];
|
|
@@ -285,11 +2907,16 @@ function report(db, opts = {}) {
|
|
|
285
2907
|
const matrixComplete = opts.matrixComplete ?? true;
|
|
286
2908
|
const out = [];
|
|
287
2909
|
const p = (s = "") => out.push(s);
|
|
288
|
-
const tot = queryGet(db, "SELECT count(*) n, sum(status='Killed') killed, sum(status='Survived') survived, sum(status='Timeout') timeout, sum(status='NoCoverage') nocov FROM mutants") ?? { n: 0, killed: 0, survived: 0, timeout: 0, nocov: 0 };
|
|
2910
|
+
const tot = queryGet(db, "SELECT count(*) n, sum(status='Killed') killed, sum(status='Survived') survived, sum(status='Timeout') timeout, sum(status='NoCoverage') nocov, sum(status='CompileError') invalid, sum(status='Ignored') ignored FROM mutants") ?? { n: 0, killed: 0, survived: 0, timeout: 0, nocov: 0, invalid: 0, ignored: 0 };
|
|
289
2911
|
const nTests = queryGet(db, "SELECT count(*) n FROM tests")?.n ?? 0;
|
|
290
2912
|
const detected = tot.killed + tot.timeout;
|
|
291
|
-
const
|
|
292
|
-
|
|
2913
|
+
const valid = detected + tot.survived + tot.nocov;
|
|
2914
|
+
const score = valid === 0 ? "n/a" : `${(detected / valid * 100).toFixed(1)}%`;
|
|
2915
|
+
const excluded = [
|
|
2916
|
+
tot.invalid > 0 ? `${tot.invalid} invalid` : "",
|
|
2917
|
+
tot.ignored > 0 ? `${tot.ignored} ignored` : ""
|
|
2918
|
+
].filter(Boolean).map((s) => ` · ${s}`).join("");
|
|
2919
|
+
p(`MUTATION score ${score} ${tot.killed} killed · ${tot.timeout} timeout · ${tot.survived} survived · ${tot.nocov} no-coverage${excluded} (${tot.n} mutants, ${nTests} tests)`);
|
|
293
2920
|
if (matrixComplete) {
|
|
294
2921
|
const mult = queryAll(db, `
|
|
295
2922
|
SELECT CASE WHEN c=1 THEN '1' WHEN c=2 THEN '2' WHEN c BETWEEN 3 AND 5 THEN '3-5' ELSE '6+' END bucket, count(*) n
|
|
@@ -354,26 +2981,28 @@ SUBSUMED CASES (${subsumedCases.length} individual tests whose every kill is ca
|
|
|
354
2981
|
p(`
|
|
355
2982
|
⚠ KILL MATRIX INCOMPLETE — redundancy/subsumption analysis suppressed.`);
|
|
356
2983
|
p(` Every mutant has at most one recorded killer, so "which tests are redundant"`);
|
|
357
|
-
p(` cannot be derived.
|
|
358
|
-
p(`
|
|
2984
|
+
p(` cannot be derived. Run 'polly mutate run --mode matrix' (every`);
|
|
2985
|
+
p(` killer recorded), or 'polly mutate verify' to diagnose. Score/gaps/theatre below still hold.`);
|
|
359
2986
|
}
|
|
360
|
-
const
|
|
361
|
-
const
|
|
2987
|
+
const byLine = (status) => queryAll(db, `SELECT file, line, mutator, count(*) n FROM mutants WHERE status='${status}' GROUP BY file, line, mutator ORDER BY file, line`);
|
|
2988
|
+
const lineOf = (r) => ` ${r.file}:${r.line} ${r.mutator}${r.n > 1 ? ` ×${r.n}` : ""}`;
|
|
2989
|
+
const gaps = byLine("NoCoverage");
|
|
362
2990
|
const gapTotal = gaps.reduce((a, r) => a + r.n, 0);
|
|
363
2991
|
p(`
|
|
364
2992
|
GAPS (NoCoverage — no test executes this code: ${gapTotal})`);
|
|
365
2993
|
if (gapTotal === 0)
|
|
366
2994
|
p(` none — every mutable location is reached by some test`);
|
|
367
2995
|
for (const r of gaps)
|
|
368
|
-
p(
|
|
369
|
-
const theatre =
|
|
2996
|
+
p(lineOf(r));
|
|
2997
|
+
const theatre = byLine("Survived");
|
|
370
2998
|
const theatreTotal = theatre.reduce((a, r) => a + r.n, 0);
|
|
371
2999
|
p(`
|
|
372
3000
|
THEATRE (Survived — code is covered but no test detects the mutation: ${theatreTotal})`);
|
|
373
3001
|
if (theatreTotal === 0)
|
|
374
3002
|
p(` none`);
|
|
375
3003
|
for (const r of theatre)
|
|
376
|
-
p(
|
|
3004
|
+
p(lineOf(r));
|
|
3005
|
+
contractsSection(db, p);
|
|
377
3006
|
return out.join(`
|
|
378
3007
|
`);
|
|
379
3008
|
}
|
|
@@ -392,20 +3021,6 @@ function isMatrixComplete(report) {
|
|
|
392
3021
|
}
|
|
393
3022
|
async function verifyMatrix(opts = {}) {
|
|
394
3023
|
const reportPath = opts.reportPath ?? DEFAULT_REPORT;
|
|
395
|
-
if (opts.run) {
|
|
396
|
-
console.log("Running mutation pass (this can take many minutes for a large mutate set)...");
|
|
397
|
-
const cmd = [
|
|
398
|
-
"bunx",
|
|
399
|
-
"stryker",
|
|
400
|
-
"run",
|
|
401
|
-
...opts.strykerConfig ? ["--configFile", opts.strykerConfig] : []
|
|
402
|
-
];
|
|
403
|
-
const proc = Bun.spawn(cmd, { stdout: "inherit", stderr: "inherit" });
|
|
404
|
-
const code = await proc.exited;
|
|
405
|
-
if (code !== 0) {
|
|
406
|
-
return { ok: false, checks: [{ name: "stryker run", ok: false, detail: `exited ${code}` }] };
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
3024
|
if (!await Bun.file(reportPath).exists()) {
|
|
410
3025
|
return {
|
|
411
3026
|
ok: false,
|
|
@@ -413,7 +3028,7 @@ async function verifyMatrix(opts = {}) {
|
|
|
413
3028
|
{
|
|
414
3029
|
name: "report exists",
|
|
415
3030
|
ok: false,
|
|
416
|
-
detail: `no report at ${reportPath}. Run
|
|
3031
|
+
detail: `no report at ${reportPath}. Run 'polly mutate verify --run' first.`
|
|
417
3032
|
}
|
|
418
3033
|
]
|
|
419
3034
|
};
|
|
@@ -438,16 +3053,29 @@ async function verifyMatrix(opts = {}) {
|
|
|
438
3053
|
add("coverage collected (not all NoCoverage)", noCov < mutants.length, `${noCov}/${mutants.length} NoCoverage — all-NoCoverage means the coverage dump broke`);
|
|
439
3054
|
return { ok: checks.every((c) => c.ok), checks };
|
|
440
3055
|
}
|
|
441
|
-
if (false) {}
|
|
442
3056
|
export {
|
|
3057
|
+
MUTATION_CONFIG_FILE,
|
|
3058
|
+
VERIFY_PRIMITIVES,
|
|
3059
|
+
applyMutant,
|
|
443
3060
|
buildDb,
|
|
3061
|
+
buildImportGraph,
|
|
3062
|
+
coveringTests,
|
|
444
3063
|
decide,
|
|
3064
|
+
defineMutation,
|
|
445
3065
|
fileSignals,
|
|
3066
|
+
findTestFiles,
|
|
3067
|
+
generateMutants,
|
|
446
3068
|
initConfig,
|
|
447
3069
|
isMatrixComplete,
|
|
3070
|
+
loadMutationConfig,
|
|
3071
|
+
migrateConfig,
|
|
3072
|
+
mutationScore,
|
|
3073
|
+
parseMutationConfig,
|
|
448
3074
|
report,
|
|
3075
|
+
runEngine,
|
|
449
3076
|
status,
|
|
3077
|
+
strykerToMutation,
|
|
450
3078
|
verifyMatrix
|
|
451
3079
|
};
|
|
452
3080
|
|
|
453
|
-
//# debugId=
|
|
3081
|
+
//# debugId=7E2AC280526BD57E64756E2164756E21
|