@t09tanaka/test-gates 0.0.0-stage → 0.1.0

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/index.js ADDED
@@ -0,0 +1,659 @@
1
+ import {
2
+ DEFAULT_FORBIDDEN_SOURCE,
3
+ DEFAULT_GATE_EXTENSIONS,
4
+ DEFAULT_IMPURE_IMPORTS,
5
+ DEFAULT_IMPURE_NAMED_IMPORTS,
6
+ DEFAULT_IMPURE_PATHS,
7
+ DEFAULT_REPORT_FILE,
8
+ EXCLUDE_ENV,
9
+ MANIFEST_FILE,
10
+ ManifestError,
11
+ existsExact,
12
+ gateExtensionOf,
13
+ gateSpecs,
14
+ isUsableEquivalentMutant,
15
+ judgeSelfcheck,
16
+ loadGates,
17
+ looksLikeSpec,
18
+ parseManifest,
19
+ readManifest,
20
+ readText,
21
+ resolveSettings,
22
+ validateEquivalentMutants
23
+ } from "./chunk-LMD3ANJ2.js";
24
+
25
+ // src/commands/check.ts
26
+ import fs from "fs";
27
+ import path from "path";
28
+
29
+ // src/core/imports.ts
30
+ var FROM_STATEMENT = /(?:^|[\n;])[ \t]*(import|export)\s+(type\s+)?([\w$\s,{}*]*?)\s*\bfrom\s*(['"])([^'"\n]+)\4/g;
31
+ var SIDE_EFFECT_IMPORT = /(?:^|[\n;])[ \t]*import\s*(['"])([^'"\n]+)\1/g;
32
+ var CALL_IMPORT = /\b(?:require|import)\s*\(\s*(['"`])([^'"`\n]+)\1\s*\)/g;
33
+ function lineAt(source, index) {
34
+ let line = 1;
35
+ for (let i = 0; i < index; i += 1) {
36
+ if (source[i] === "\n") {
37
+ line += 1;
38
+ }
39
+ }
40
+ return line;
41
+ }
42
+ function parseImports(source) {
43
+ const found = [];
44
+ for (const match of source.matchAll(FROM_STATEMENT)) {
45
+ const clause = match[3];
46
+ const module = match[5];
47
+ const braces = /\{([^}]*)\}/.exec(clause);
48
+ const names = braces ? braces[1].split(",").map((part) => part.trim()).filter((part) => part !== "" && !/^type\s/.test(part)).map((part) => part.split(/\s+as\s+/)[0].trim()) : [];
49
+ const outsideBraces = clause.replace(/\{[^}]*\}/, "").replace(/,/g, "").trim();
50
+ found.push({
51
+ module,
52
+ line: lineAt(source, match.index + match[0].lastIndexOf(module)),
53
+ typeOnly: match[2] !== void 0,
54
+ names,
55
+ whole: outsideBraces !== ""
56
+ });
57
+ }
58
+ for (const match of source.matchAll(SIDE_EFFECT_IMPORT)) {
59
+ const module = match[2];
60
+ found.push({
61
+ module,
62
+ line: lineAt(source, match.index + match[0].lastIndexOf(module)),
63
+ typeOnly: false,
64
+ names: [],
65
+ whole: false
66
+ });
67
+ }
68
+ for (const match of source.matchAll(CALL_IMPORT)) {
69
+ const module = match[2];
70
+ found.push({
71
+ module,
72
+ line: lineAt(source, match.index + match[0].lastIndexOf(module)),
73
+ typeOnly: false,
74
+ names: [],
75
+ whole: true
76
+ });
77
+ }
78
+ return found.sort((a, b) => a.line - b.line);
79
+ }
80
+
81
+ // src/core/scan.ts
82
+ var IGNORE_DIRECTIVE = /\b(istanbul|c8|v8)\s+ignore\b/;
83
+ var DECORATOR = /^\s*@[A-Za-z_$][\w$.]*\s*\(/;
84
+ var STRYKER_COMMENT = /Stryker\s+(disable|restore)\b/;
85
+ var DISABLED_TESTS = [
86
+ { regex: /\.\s*(skip|only|todo)\b/, label: ".skip / .only / .todo" },
87
+ {
88
+ regex: /\b(?:describe|it|test|suite)(?:\s*\.\s*\w+)*\s*\.\s*(?:skipIf|runIf|fails)\b/,
89
+ label: ".skipIf / .runIf / .fails"
90
+ },
91
+ {
92
+ regex: /\b(xit|xtest|xdescribe|fit|fdescribe)\s*[.(]/,
93
+ label: "xit / xtest / xdescribe / fit / fdescribe"
94
+ }
95
+ ];
96
+ var MODULE_MOCK = /\b(?:jest|vi)\s*\.\s*(?:mock|doMock|unstable_mockModule|setMock)\s*\(\s*(?:import\s*\(\s*)?(['"`])([^'"`\n]+)\1/g;
97
+ var SCRIPT_EXTENSION = /\.[cm]?[jt]sx?$/;
98
+ function eachLine(source, visit) {
99
+ source.split("\n").forEach((line, index) => visit(line, index + 1));
100
+ }
101
+ function escapeRegExp(text) {
102
+ return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
103
+ }
104
+ function scanGateSource(gatePath, source, settings) {
105
+ const violations = [];
106
+ eachLine(source, (line, lineNumber) => {
107
+ if (DECORATOR.test(line)) {
108
+ violations.push({
109
+ file: gatePath,
110
+ line: lineNumber,
111
+ message: "decorator found (not a pure module). Move the file to candidates"
112
+ });
113
+ }
114
+ if (IGNORE_DIRECTIVE.test(line)) {
115
+ violations.push({ file: gatePath, line: lineNumber, message: "coverage ignore directive" });
116
+ }
117
+ if (STRYKER_COMMENT.test(line)) {
118
+ violations.push({
119
+ file: gatePath,
120
+ line: lineNumber,
121
+ message: "Stryker comment (disable / restore). Allow an equivalent mutant in test-gates.json (equivalentMutants) instead"
122
+ });
123
+ }
124
+ for (const rule of settings.forbiddenSource) {
125
+ if (rule.regex.test(line)) {
126
+ violations.push({ file: gatePath, line: lineNumber, message: rule.reason });
127
+ }
128
+ }
129
+ });
130
+ for (const imported of parseImports(source)) {
131
+ if (imported.typeOnly) {
132
+ continue;
133
+ }
134
+ if (!settings.allowedImports.some((regex) => regex.test(imported.module))) {
135
+ for (const rule of settings.impureImports) {
136
+ if (rule.regex.test(imported.module)) {
137
+ violations.push({
138
+ file: gatePath,
139
+ line: imported.line,
140
+ message: `runtime import of "${imported.module}" (${rule.reason})`
141
+ });
142
+ }
143
+ }
144
+ }
145
+ for (const rule of settings.impureNamedImports) {
146
+ if (imported.module !== rule.module) {
147
+ continue;
148
+ }
149
+ const hit = rule.names.filter(
150
+ (name) => imported.names.includes(name) || imported.whole && new RegExp(`\\b${escapeRegExp(name)}\\b`).test(source)
151
+ );
152
+ if (hit.length > 0) {
153
+ violations.push({
154
+ file: gatePath,
155
+ line: imported.line,
156
+ message: `runtime use of ${hit.join(", ")} from "${rule.module}" (${rule.reason})`
157
+ });
158
+ }
159
+ }
160
+ }
161
+ return violations.sort((a, b) => a.line - b.line);
162
+ }
163
+ function resolveSpecifier(specifier, fromFile, aliases) {
164
+ let joined = null;
165
+ if (specifier.startsWith(".")) {
166
+ const directory = fromFile.includes("/") ? fromFile.slice(0, fromFile.lastIndexOf("/")) : "";
167
+ joined = directory === "" ? specifier : `${directory}/${specifier}`;
168
+ } else {
169
+ for (const alias of aliases) {
170
+ if (specifier.startsWith(alias.prefix)) {
171
+ joined = alias.target + specifier.slice(alias.prefix.length);
172
+ break;
173
+ }
174
+ }
175
+ }
176
+ if (joined === null) {
177
+ return null;
178
+ }
179
+ const segments = [];
180
+ for (const segment of joined.split("/")) {
181
+ if (segment === "..") {
182
+ segments.pop();
183
+ } else if (segment !== "." && segment !== "") {
184
+ segments.push(segment);
185
+ }
186
+ }
187
+ return segments.join("/").replace(SCRIPT_EXTENSION, "");
188
+ }
189
+ function scanSpecSource(gatePath, specPath, source, settings) {
190
+ const violations = [];
191
+ eachLine(source, (line, lineNumber) => {
192
+ if (IGNORE_DIRECTIVE.test(line)) {
193
+ violations.push({ file: specPath, line: lineNumber, message: "coverage ignore directive" });
194
+ }
195
+ for (const { regex, label } of DISABLED_TESTS) {
196
+ if (regex.test(line)) {
197
+ violations.push({
198
+ file: specPath,
199
+ line: lineNumber,
200
+ message: `${label} (a skipped or focused test)`
201
+ });
202
+ }
203
+ }
204
+ });
205
+ const gateModule = gatePath.replace(SCRIPT_EXTENSION, "");
206
+ const gateDirectory = gateModule.replace(/\/index$/, "");
207
+ for (const match of source.matchAll(MODULE_MOCK)) {
208
+ const specifier = match[2];
209
+ const resolved = resolveSpecifier(specifier, specPath, settings.importAliases);
210
+ if (resolved === gateModule || resolved === gateDirectory) {
211
+ let line = 1;
212
+ for (let i = 0; i < match.index; i += 1) {
213
+ if (source[i] === "\n") {
214
+ line += 1;
215
+ }
216
+ }
217
+ violations.push({
218
+ file: specPath,
219
+ line,
220
+ message: `mocks the module under test ("${specifier}")`
221
+ });
222
+ }
223
+ }
224
+ return violations.sort((a, b) => a.line - b.line);
225
+ }
226
+ var STRYKER_WEAKENING = /\b(excludedMutations|ignoreStatic|ignorers)\b/;
227
+ function scanStrykerConfig(configPath, source) {
228
+ const violations = [];
229
+ eachLine(source, (line, lineNumber) => {
230
+ const match = STRYKER_WEAKENING.exec(line);
231
+ if (match) {
232
+ violations.push({
233
+ file: configPath,
234
+ line: lineNumber,
235
+ message: `${match[1]} takes mutants out of the evaluation. Allow equivalent mutants one by one in test-gates.json instead`
236
+ });
237
+ }
238
+ });
239
+ return violations;
240
+ }
241
+
242
+ // src/commands/check.ts
243
+ var STRYKER_CONFIG_NAMES = [
244
+ "stryker.gates.config.mjs",
245
+ "stryker.gates.config.js",
246
+ "stryker.gates.config.cjs",
247
+ "stryker.gates.config.json"
248
+ ];
249
+ function findStrykerConfig(manifest) {
250
+ if (manifest.settings.strykerConfigFile !== null) {
251
+ return manifest.settings.strykerConfigFile;
252
+ }
253
+ return STRYKER_CONFIG_NAMES.find((name) => fs.existsSync(path.join(manifest.dir, name))) ?? null;
254
+ }
255
+ function isPlainRelativePath(file) {
256
+ return !path.isAbsolute(file) && !/^[A-Za-z]:/.test(file) && !file.includes("\\") && !file.split("/").some((segment) => segment === ".." || segment === "." || segment === "");
257
+ }
258
+ function checkGates(manifest) {
259
+ const { dir, settings } = manifest;
260
+ const violations = [...manifest.violations];
261
+ for (const gate of manifest.gates) {
262
+ if (!isPlainRelativePath(gate.path)) {
263
+ violations.push({
264
+ file: MANIFEST_FILE,
265
+ message: `${gate.path}: write the path relative to the subproject, with "/" and without ".."`
266
+ });
267
+ continue;
268
+ }
269
+ if (gateExtensionOf(gate.path, settings.gateExtensions) === null) {
270
+ violations.push({
271
+ file: gate.path,
272
+ message: `a gate must be one of ${settings.gateExtensions.join(" / ")} (settings.gateExtensions)`
273
+ });
274
+ continue;
275
+ }
276
+ if (looksLikeSpec(gate.path, settings)) {
277
+ violations.push({ file: gate.path, message: "a spec cannot be a gate" });
278
+ continue;
279
+ }
280
+ for (const rule of settings.impurePaths) {
281
+ if (rule.regex.test(gate.path)) {
282
+ violations.push({
283
+ file: gate.path,
284
+ message: `cannot be a gate (${rule.reason}). Move it to candidates`
285
+ });
286
+ }
287
+ }
288
+ if (!existsExact(dir, gate.path)) {
289
+ violations.push({
290
+ file: gate.path,
291
+ message: "file not found (the check is case-sensitive)"
292
+ });
293
+ continue;
294
+ }
295
+ violations.push(...scanGateSource(gate.path, readText(dir, gate.path), settings));
296
+ if (gate.specCandidates.length === 0) {
297
+ continue;
298
+ }
299
+ if (gate.existingSpecs.length === 0) {
300
+ violations.push({
301
+ file: gate.path,
302
+ message: `spec not found (looked for ${gate.specCandidates.join(", ")}; the check is case-sensitive)`
303
+ });
304
+ continue;
305
+ }
306
+ if (gate.existingSpecs.length > 1) {
307
+ violations.push({
308
+ file: gate.path,
309
+ message: `more than one spec (${gate.existingSpecs.join(", ")}). Keep one, or narrow settings.spec.suffixes`
310
+ });
311
+ continue;
312
+ }
313
+ const specPath = gate.existingSpecs[0];
314
+ violations.push(...scanSpecSource(gate.path, specPath, readText(dir, specPath), settings));
315
+ }
316
+ for (const candidate of manifest.candidates) {
317
+ if (!isPlainRelativePath(candidate.path) || !existsExact(dir, candidate.path, "any")) {
318
+ violations.push({
319
+ file: MANIFEST_FILE,
320
+ message: `${candidate.path}: candidate not found. Remove it, or fix the path`
321
+ });
322
+ }
323
+ }
324
+ const strykerConfig = findStrykerConfig(manifest);
325
+ if (strykerConfig !== null) {
326
+ if (!fs.existsSync(path.join(dir, strykerConfig))) {
327
+ violations.push({ file: strykerConfig, message: "Stryker config not found" });
328
+ } else {
329
+ violations.push(...scanStrykerConfig(strykerConfig, readText(dir, strykerConfig)));
330
+ }
331
+ }
332
+ return violations;
333
+ }
334
+
335
+ // src/core/mutation-result.ts
336
+ var DETECTED = /* @__PURE__ */ new Set(["Killed", "Timeout"]);
337
+ var SURVIVED = /* @__PURE__ */ new Set(["Survived", "NoCoverage"]);
338
+ var NOT_EVALUABLE = /* @__PURE__ */ new Set(["RuntimeError", "CompileError"]);
339
+ function normalizeCode(text) {
340
+ return text.replace(/\s+/g, " ").trim();
341
+ }
342
+ function normalizePath(file) {
343
+ return file.replace(/\\/g, "/").replace(/^\.\//, "");
344
+ }
345
+ function normalizeNewlines(text) {
346
+ return text.replace(/\r\n/g, "\n");
347
+ }
348
+ function sliceByLocation(source, location) {
349
+ const lines = source.split("\n");
350
+ const { start, end } = location;
351
+ const first = lines[start.line - 1];
352
+ const last = lines[end.line - 1];
353
+ if (first === void 0 || last === void 0 || start.column < 1 || end.column < 1 || end.line < start.line || end.line === start.line && end.column < start.column) {
354
+ return null;
355
+ }
356
+ if (start.line === end.line) {
357
+ return first.slice(start.column - 1, end.column - 1);
358
+ }
359
+ return [
360
+ first.slice(start.column - 1),
361
+ ...lines.slice(start.line, end.line - 1),
362
+ last.slice(0, end.column - 1)
363
+ ].join("\n");
364
+ }
365
+ function keyOf(mutator, original, replacement) {
366
+ return JSON.stringify([mutator, normalizeCode(original), normalizeCode(replacement)]);
367
+ }
368
+ function describeAllowance(entry) {
369
+ const occurrence = entry.occurrence === void 0 ? "" : ` / occurrence ${entry.occurrence}`;
370
+ return `${entry.mutator} / ${normalizeCode(entry.original)} \u2192 ${normalizeCode(entry.replacement)}${occurrence}`;
371
+ }
372
+ function judgeMutationReport(input) {
373
+ const violations = [];
374
+ const survivors = [];
375
+ const summary = {
376
+ total: 0,
377
+ detected: 0,
378
+ timeout: 0,
379
+ allowed: 0,
380
+ notEvaluable: 0,
381
+ unallowed: 0
382
+ };
383
+ const files = /* @__PURE__ */ new Map();
384
+ for (const [file, result] of Object.entries(input.report.files ?? {})) {
385
+ files.set(normalizePath(file), result);
386
+ }
387
+ for (const gate of input.gates) {
388
+ const gatePath = normalizePath(gate.path);
389
+ const shapeViolations = validateEquivalentMutants(gate.path, gate.equivalentMutants);
390
+ violations.push(...shapeViolations);
391
+ const allowances = Array.isArray(gate.equivalentMutants) ? gate.equivalentMutants : [];
392
+ const result = files.get(gatePath);
393
+ if (!result || !Array.isArray(result.mutants) || result.mutants.length === 0) {
394
+ violations.push({
395
+ file: gate.path,
396
+ message: "no mutants in the report (the gate was not mutation tested)"
397
+ });
398
+ continue;
399
+ }
400
+ const diskSource = input.readSource(gate.path);
401
+ const source = result.source ?? diskSource;
402
+ if (source === void 0) {
403
+ violations.push({
404
+ file: gate.path,
405
+ message: "source is neither in the report nor readable on disk"
406
+ });
407
+ continue;
408
+ }
409
+ if (result.source !== void 0 && diskSource !== void 0 && normalizeNewlines(result.source) !== normalizeNewlines(diskSource)) {
410
+ violations.push({
411
+ file: gate.path,
412
+ message: "the report was made from a different version of this file. Run Stryker again"
413
+ });
414
+ continue;
415
+ }
416
+ const located = [];
417
+ const byKey = /* @__PURE__ */ new Map();
418
+ const sorted = [...result.mutants].sort(
419
+ (a, b) => a.location.start.line - b.location.start.line || a.location.start.column - b.location.start.column
420
+ );
421
+ let locationsValid = true;
422
+ for (const mutant of sorted) {
423
+ const original = sliceByLocation(normalizeNewlines(source), mutant.location);
424
+ if (original === null) {
425
+ violations.push({
426
+ file: gate.path,
427
+ line: mutant.location.start.line,
428
+ message: `the report location of a ${mutant.mutatorName} mutant is outside the source`
429
+ });
430
+ locationsValid = false;
431
+ continue;
432
+ }
433
+ const replacement = mutant.replacement ?? "";
434
+ const key = keyOf(mutant.mutatorName, original, replacement);
435
+ const group = byKey.get(key) ?? [];
436
+ const entry = {
437
+ mutant,
438
+ original,
439
+ replacement,
440
+ occurrence: group.length + 1,
441
+ sameKeyCount: 0
442
+ };
443
+ group.push(entry);
444
+ byKey.set(key, group);
445
+ located.push(entry);
446
+ }
447
+ for (const group of byKey.values()) {
448
+ for (const entry of group) {
449
+ entry.sameKeyCount = group.length;
450
+ }
451
+ }
452
+ if (!locationsValid) {
453
+ continue;
454
+ }
455
+ const allowed = /* @__PURE__ */ new Set();
456
+ allowances.forEach((allowance, index) => {
457
+ if (!isUsableEquivalentMutant(allowance)) {
458
+ return;
459
+ }
460
+ const at = `${gate.path}: equivalentMutants[${index}] (${describeAllowance(allowance)})`;
461
+ const group = byKey.get(keyOf(allowance.mutator, allowance.original, allowance.replacement)) ?? [];
462
+ if (allowance.occurrence === void 0 && group.length > 1) {
463
+ violations.push({
464
+ file: MANIFEST_FILE,
465
+ message: `${at}: matches ${group.length} mutants in the file. Add "occurrence" (1-based) to pick one`
466
+ });
467
+ return;
468
+ }
469
+ const target = group[(allowance.occurrence ?? 1) - 1];
470
+ if (!target || !SURVIVED.has(target.mutant.status)) {
471
+ const why = target ? `the mutant is now ${target.mutant.status}` : "no such mutant in this run";
472
+ violations.push({
473
+ file: MANIFEST_FILE,
474
+ message: `${at}: stale allowance (${why}). Remove it`
475
+ });
476
+ return;
477
+ }
478
+ if (allowed.has(target)) {
479
+ violations.push({
480
+ file: MANIFEST_FILE,
481
+ message: `${at}: another allowance already covers this mutant`
482
+ });
483
+ return;
484
+ }
485
+ allowed.add(target);
486
+ });
487
+ for (const entry of located) {
488
+ const { mutant } = entry;
489
+ summary.total += 1;
490
+ if (DETECTED.has(mutant.status)) {
491
+ summary.detected += 1;
492
+ if (mutant.status === "Timeout") {
493
+ summary.timeout += 1;
494
+ }
495
+ } else if (NOT_EVALUABLE.has(mutant.status)) {
496
+ summary.notEvaluable += 1;
497
+ } else if (SURVIVED.has(mutant.status)) {
498
+ if (allowed.has(entry)) {
499
+ summary.allowed += 1;
500
+ continue;
501
+ }
502
+ summary.unallowed += 1;
503
+ survivors.push({
504
+ file: gate.path,
505
+ line: mutant.location.start.line,
506
+ column: mutant.location.start.column,
507
+ status: mutant.status,
508
+ mutator: mutant.mutatorName,
509
+ original: normalizeCode(entry.original),
510
+ replacement: normalizeCode(entry.replacement),
511
+ occurrence: entry.occurrence,
512
+ sameKeyCount: entry.sameKeyCount,
513
+ allowance: {
514
+ mutator: mutant.mutatorName,
515
+ original: normalizeCode(entry.original),
516
+ replacement: normalizeCode(entry.replacement),
517
+ ...entry.sameKeyCount > 1 ? { occurrence: entry.occurrence } : {},
518
+ reason: ""
519
+ }
520
+ });
521
+ } else {
522
+ violations.push({
523
+ file: gate.path,
524
+ line: mutant.location.start.line,
525
+ message: mutant.status === "Ignored" ? `a ${mutant.mutatorName} mutant was ignored. Taking mutants out with Stryker comments or mutator exclusions is not allowed` : `a ${mutant.mutatorName} mutant was not evaluated (status: ${mutant.status})`
526
+ });
527
+ }
528
+ }
529
+ }
530
+ if (summary.total === 0) {
531
+ violations.push({
532
+ file: MANIFEST_FILE,
533
+ message: "no mutants were evaluated (the mutation test ran on nothing)"
534
+ });
535
+ }
536
+ return { violations, survivors, summary };
537
+ }
538
+ function formatSurvivor(survivor) {
539
+ const occurrence = survivor.sameKeyCount > 1 ? ` / occurrence ${survivor.occurrence} of ${survivor.sameKeyCount}` : "";
540
+ return `${survivor.file}:${survivor.line}:${survivor.column} / ${survivor.mutator} / ${survivor.original} \u2192 ${survivor.replacement}${occurrence} (${survivor.status})`;
541
+ }
542
+ function formatSummary(summary) {
543
+ return `mutants ${summary.total} / detected ${summary.detected} (timeout ${summary.timeout}) / allowed equivalent ${summary.allowed} / not evaluable ${summary.notEvaluable} / unallowed survivors ${summary.unallowed}`;
544
+ }
545
+
546
+ // src/core/lcov.ts
547
+ function normalizePath2(file, rootDir) {
548
+ let normalized = file.replace(/\\/g, "/");
549
+ if (rootDir !== void 0) {
550
+ const root = rootDir.replace(/\\/g, "/").replace(/\/+$/, "");
551
+ if (normalized.startsWith(`${root}/`)) {
552
+ normalized = normalized.slice(root.length + 1);
553
+ }
554
+ }
555
+ return normalized.replace(/^\.\//, "");
556
+ }
557
+ function parseLcov(text, rootDir) {
558
+ const files = /* @__PURE__ */ new Map();
559
+ let record = null;
560
+ const closeRecord = () => {
561
+ if (record && !record.hasDa) {
562
+ record.file.found += record.found;
563
+ record.file.hit += record.hit;
564
+ }
565
+ record = null;
566
+ };
567
+ for (const rawLine of text.split("\n")) {
568
+ const line = rawLine.trim();
569
+ if (line.startsWith("SF:")) {
570
+ closeRecord();
571
+ const path2 = normalizePath2(line.slice(3), rootDir);
572
+ const file = files.get(path2) ?? { lines: /* @__PURE__ */ new Map(), found: 0, hit: 0 };
573
+ files.set(path2, file);
574
+ record = { file, hasDa: false, found: 0, hit: 0 };
575
+ } else if (line === "end_of_record") {
576
+ closeRecord();
577
+ } else if (record) {
578
+ const open = record;
579
+ if (line.startsWith("DA:")) {
580
+ const [lineNumber, hits] = line.slice(3).split(",");
581
+ const number = Number(lineNumber);
582
+ const count = Number(hits);
583
+ if (Number.isInteger(number) && Number.isFinite(count)) {
584
+ open.hasDa = true;
585
+ open.file.lines.set(number, Math.max(open.file.lines.get(number) ?? 0, count));
586
+ }
587
+ } else if (line.startsWith("LF:")) {
588
+ open.found = Number(line.slice(3)) || 0;
589
+ } else if (line.startsWith("LH:")) {
590
+ open.hit = Number(line.slice(3)) || 0;
591
+ }
592
+ }
593
+ }
594
+ closeRecord();
595
+ const result = /* @__PURE__ */ new Map();
596
+ for (const [file, accumulator] of files) {
597
+ const uncovered = [...accumulator.lines.entries()].filter(([, hits]) => hits <= 0).map(([lineNumber]) => lineNumber).sort((a, b) => a - b);
598
+ result.set(file, {
599
+ found: accumulator.lines.size + accumulator.found,
600
+ hit: accumulator.lines.size - uncovered.length + accumulator.hit,
601
+ uncovered
602
+ });
603
+ }
604
+ return result;
605
+ }
606
+ function judgeLcov(gatePaths, coverage) {
607
+ const violations = [];
608
+ const results = [];
609
+ for (const gatePath of gatePaths) {
610
+ const file = coverage.get(normalizePath2(gatePath));
611
+ if (!file) {
612
+ violations.push({ file: gatePath, message: "no record in the lcov file" });
613
+ results.push({ path: gatePath, found: 0, hit: 0, ok: false });
614
+ continue;
615
+ }
616
+ if (file.found === 0) {
617
+ violations.push({ file: gatePath, message: "no instrumented lines in the lcov record" });
618
+ results.push({ path: gatePath, found: 0, hit: 0, ok: false });
619
+ continue;
620
+ }
621
+ const ok = file.hit === file.found;
622
+ if (!ok) {
623
+ const shown = file.uncovered.slice(0, 20).join(", ");
624
+ const more = file.uncovered.length > 20 ? ", \u2026" : "";
625
+ violations.push({
626
+ file: gatePath,
627
+ ...file.uncovered[0] === void 0 ? {} : { line: file.uncovered[0] },
628
+ message: `${file.hit}/${file.found} lines covered` + (shown === "" ? "" : ` (uncovered: ${shown}${more})`)
629
+ });
630
+ }
631
+ results.push({ path: gatePath, found: file.found, hit: file.hit, ok });
632
+ }
633
+ return { violations, results };
634
+ }
635
+ export {
636
+ DEFAULT_FORBIDDEN_SOURCE,
637
+ DEFAULT_GATE_EXTENSIONS,
638
+ DEFAULT_IMPURE_IMPORTS,
639
+ DEFAULT_IMPURE_NAMED_IMPORTS,
640
+ DEFAULT_IMPURE_PATHS,
641
+ DEFAULT_REPORT_FILE,
642
+ EXCLUDE_ENV,
643
+ MANIFEST_FILE,
644
+ ManifestError,
645
+ checkGates,
646
+ formatSummary,
647
+ formatSurvivor,
648
+ gateSpecs,
649
+ judgeLcov,
650
+ judgeMutationReport,
651
+ judgeSelfcheck,
652
+ loadGates,
653
+ normalizeCode,
654
+ parseLcov,
655
+ parseManifest,
656
+ readManifest,
657
+ resolveSettings,
658
+ sliceByLocation
659
+ };