Skip to content

Commit 5e40fae

Browse files
committed
fix(ci): deduplicate mutation annotations
1 parent 745656a commit 5e40fae

2 files changed

Lines changed: 36 additions & 12 deletions

File tree

‎scripts/stryker-diff.mjs‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -460,26 +460,36 @@ export function formatAdvisoryCommand(advisory) {
460460

461461
export function formatAnnotations(blockingMutants, packageRoot, state = { total: 0, perFile: new Map() }) {
462462
const annotations = []
463+
const mutantsByLocation = new Map()
463464

464-
for (const mutant of blockingMutants.sort((left, right) => {
465+
for (const mutant of [...blockingMutants].sort((left, right) => {
465466
const pathOrder = left.filePath.localeCompare(right.filePath)
466467
return pathOrder || left.location.start.line - right.location.start.line
467468
})) {
468469
const repositoryPath = path.posix.join(packageRoot, mutant.filePath.replaceAll("\\", "/"))
469470
const key = `${repositoryPath}:${mutant.location.start.line}`
471+
const group = mutantsByLocation.get(key) ?? { repositoryPath, mutants: [] }
472+
group.mutants.push(mutant)
473+
mutantsByLocation.set(key, group)
474+
}
475+
476+
for (const [key, { repositoryPath, mutants }] of mutantsByLocation) {
470477
const fileCount = state.perFile.get(repositoryPath) ?? 0
471-
if (annotations.some((annotation) => annotation.key === key) || fileCount >= 7 || state.total >= 20) continue
478+
if (fileCount >= 7 || state.total >= 20) continue
472479

480+
const mutant = mutants[0]
473481
const replacement = String(mutant.replacement ?? "")
474482
.replace(/\s+/g, " ")
475483
.trim()
476484
.slice(0, 160)
485+
const location = `${repositoryPath}:${mutant.location.start.line}`
486+
const detail = `${mutant.status} ${mutant.mutatorName} mutant${replacement ? ` (replacement: ${replacement})` : ""}`
477487
annotations.push({
478488
key,
479489
file: repositoryPath,
480490
line: mutant.location.start.line,
481491
message:
482-
`${mutant.status} ${mutant.mutatorName} mutant${replacement ? ` (replacement: ${replacement})` : ""}. ` +
492+
`${location}: ${mutants.length === 1 ? detail : `${mutants.length} mutation test gaps; example: ${detail}`}. ` +
483493
"See the job summary for the complete list and resolution guidance.",
484494
})
485495
state.perFile.set(repositoryPath, fileCount + 1)
@@ -652,12 +662,6 @@ export function evaluateReport(report, packageEntry) {
652662
"The result is inconclusive; consider fixing flaky or slow tests, or reducing the changed scope.",
653663
)
654664
}
655-
if (counts.blocking.length > 0) {
656-
advisories.push(
657-
`${packageEntry.id} has ${counts.survived} surviving and ${counts.noCoverage} uncovered changed-code mutants. ` +
658-
"Consider adding or strengthening focused tests.",
659-
)
660-
}
661665
return { ...counts, advisories }
662666
}
663667

@@ -729,7 +733,7 @@ export function runManifest(repoRoot, manifest, reportRoot) {
729733
reportPath,
730734
changedLines: packageEntry.changedExecutableLines,
731735
...counts,
732-
result: counts.advisories.length > 0 ? "Advisory findings" : "Passed",
736+
result: counts.blocking.length > 0 || counts.advisories.length > 0 ? "Advisory findings" : "Passed",
733737
})
734738
} catch (error) {
735739
advisories.push(error.message)

‎scripts/stryker-diff.test.mjs‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,26 @@ describe("failure output", () => {
529529
for (const mutant of manyMutants) assert.ok(grouped.includes(mutant.mutatorName))
530530
})
531531

532+
it("emits one distinguishable annotation per source location", () => {
533+
const annotations = formatAnnotations(
534+
[
535+
...blocking,
536+
{
537+
filePath: "utils/other.ts",
538+
status: "NoCoverage",
539+
mutatorName: "ConditionalExpression",
540+
replacement: "true",
541+
location: { start: { line: 9 } },
542+
},
543+
],
544+
"src",
545+
)
546+
547+
assert.equal(annotations.length, 2)
548+
assert.match(annotations[0].message, /^src\/core\/value\.ts:4: 2 mutation test gaps; example:/)
549+
assert.match(annotations[1].message, /^src\/utils\/other\.ts:9: 2 mutation test gaps; example:/)
550+
})
551+
532552
it("shares annotation limits across packages", () => {
533553
const state = { total: 0, perFile: new Map() }
534554
const first = formatAnnotations(
@@ -681,7 +701,7 @@ describe("failure output", () => {
681701
describe("report evaluation", () => {
682702
const packageEntry = { id: "core", root: "packages/core" }
683703

684-
it("reports surviving and uncovered changed-code mutants as advisory", () => {
704+
it("reports surviving and uncovered mutants through detailed annotations without a redundant aggregate", () => {
685705
const report = {
686706
files: {
687707
"src/value.ts": {
@@ -704,7 +724,7 @@ describe("report evaluation", () => {
704724
}
705725

706726
const result = evaluateReport(report, packageEntry)
707-
assert.match(result.advisories.join("\n"), /1 surviving and 1 uncovered/)
727+
assert.deepEqual(result.advisories, [])
708728
assert.equal(formatAnnotations(mutantCounts(report).blocking, packageEntry.root).length, 2)
709729
})
710730

0 commit comments

Comments
 (0)