Repository navigation
ci: surface the changed-code mutation report as a job summary and a named artifact #2149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
easonLiangWorldedtech
wants to merge
10
commits into
Zoo-Code-Org:main
Choose a base branch
from
easonLiangWorldedtech:feat/mutation-report-discoverability
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+515
−1
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
07ae220
ci(mutation-gate): surface per-file mutation results in the job summary
easonLiangWorldedtech 82625a5
fix(ci): guard the per-file mutation summary instead of a second cont…
easonLiangWorldedtech d0307b0
docs: describe the mutation summary counts and point detail work at t…
easonLiangWorldedtech bd71f74
ci(mutation): extract and harden the per-file summary script
easonLiangWorldedtech 2c64cb7
style: format the mutation summary script and its tests
easonLiangWorldedtech cbe6142
fix(mutation): score Timeout mutants as detected and validate project…
easonLiangWorldedtech 73aedfa
fix(mutation): harden summary rendering and write atomically
easonLiangWorldedtech e04c8bb
docs(mutation): state the score formula as (Killed + Timeout) / Mutants
easonLiangWorldedtech 547b64b
test(mutation): force the stdout branch in CI
easonLiangWorldedtech 1fa318a
fix(mutation): render fixed diagnostics and clean up summary temp files
easonLiangWorldedtech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| import fs from "node:fs" | ||
| import path from "node:path" | ||
| import { pathToFileURL } from "node:url" | ||
|
|
||
| /** | ||
| * Build the per-file mutation summary that the mutation-testing workflow appends to the job | ||
| * summary. | ||
| * | ||
| * The Stryker JSON reports are untrusted input for this script: report.files keys are file | ||
| * paths and they are rendered into a Markdown table. Every path is therefore validated | ||
| * (normalised repo-relative form, safe characters) and Markdown-escaped before insertion, and | ||
| * the report shape is checked rather than assumed. | ||
| */ | ||
|
|
||
| const REPORT_DIRECTORY = "reports/mutation" | ||
| const VALID_STATUSES = new Set(["Killed", "Survived", "NoCoverage", "Timeout"]) | ||
| // Repo-relative paths only: letters, digits and the separators/characters a source path | ||
| // normally uses. Anything else (backticks, pipes, control characters, drive letters, ...) | ||
| // is rejected instead of rendered into the summary. | ||
| const SAFE_RELATIVE_PATH = /^[A-Za-z0-9._@+/-]+$/ | ||
| const BACKSLASH = String.fromCharCode(92) | ||
| const BACKTICK = String.fromCharCode(96) | ||
|
|
||
| /** Escape the Markdown table metacharacters and line breaks a file path could carry. */ | ||
| export function escapeMarkdownCell(value) { | ||
| return String(value) | ||
| .split(BACKSLASH) | ||
| .join(BACKSLASH + BACKSLASH) | ||
| .split("|") | ||
| .join(BACKSLASH + "|") | ||
| .split(BACKTICK) | ||
| .join(BACKSLASH + BACKTICK) | ||
| .split("[") | ||
| .join(BACKSLASH + "[") | ||
| .split("]") | ||
| .join(BACKSLASH + "]") | ||
| .split("(") | ||
| .join(BACKSLASH + "(") | ||
| .split(")") | ||
| .join(BACKSLASH + ")") | ||
| .split("\r") | ||
| .join(BACKSLASH + "r") | ||
| .split("\n") | ||
| .join(BACKSLASH + "n") | ||
| } | ||
|
|
||
| /** | ||
| * Render a report directory name for diagnostics. Package directory names are simple slugs; a | ||
| * name that does not match that shape is not rendered verbatim. | ||
| */ | ||
| function renderReportName(name) { | ||
| return /^[A-Za-z0-9._@+-]+$/.test(name) ? name : "report" | ||
| } | ||
|
|
||
| /** | ||
| * Return the normalised repo-relative path for one report entry, or undefined when the entry | ||
| * cannot be trusted for display. | ||
| */ | ||
| export function normalizeReportPath(filePath, projectRoot, cwd) { | ||
| if (typeof filePath !== "string" || filePath.length === 0) return undefined | ||
| if (projectRoot !== undefined && typeof projectRoot !== "string") return undefined | ||
| if (filePath.includes("\u0000")) return undefined | ||
| const absolute = path.resolve(projectRoot ?? cwd, filePath) | ||
| const relative = path.relative(cwd, absolute).replaceAll("\\", "/") | ||
| if (relative.length === 0 || path.isAbsolute(relative) || relative.startsWith("..")) return undefined | ||
| if (!SAFE_RELATIVE_PATH.test(relative)) return undefined | ||
| return relative | ||
| } | ||
|
|
||
| /** | ||
| * Collect per-file mutation counts from every report under reportRoot. | ||
| * | ||
| * Returns the Markdown lines for the summary (including the header when any row exists) and | ||
| * the list of unreadable reports that were skipped. | ||
| */ | ||
| export function summarizeMutationReports({ reportRoot = REPORT_DIRECTORY, cwd = process.cwd() } = {}) { | ||
| const rows = [] | ||
| const unreadable = [] | ||
| let directories = [] | ||
|
|
||
| try { | ||
| directories = fs.existsSync(reportRoot) ? fs.readdirSync(reportRoot, { withFileTypes: true }) : [] | ||
| } catch { | ||
| // Raw filesystem error text is untrusted input for the summary; keep a fixed diagnostic. | ||
| unreadable.push("reports/mutation: directory could not be read") | ||
| } | ||
|
|
||
| for (const directory of directories) { | ||
| if (!directory.isDirectory()) continue | ||
| const reportFile = path.join(reportRoot, directory.name, "mutation.json") | ||
| if (!fs.existsSync(reportFile)) continue | ||
| let report | ||
| try { | ||
| report = JSON.parse(fs.readFileSync(reportFile, "utf8")) | ||
| } catch { | ||
| // The parser message can quote report content; render a fixed, bounded diagnostic. | ||
| unreadable.push(renderReportName(directory.name) + ": invalid JSON") | ||
| continue | ||
| } | ||
| if ( | ||
| report === null || | ||
| typeof report !== "object" || | ||
| report.files === null || | ||
| typeof report.files !== "object" | ||
| ) { | ||
| unreadable.push(renderReportName(directory.name) + ": report has no files map") | ||
| continue | ||
| } | ||
| for (const [filePath, file] of Object.entries(report.files)) { | ||
| if (file === null || typeof file !== "object") continue | ||
| const counts = { mutants: 0, killed: 0, survived: 0, noCoverage: 0, timeout: 0 } | ||
| const mutants = Array.isArray(file.mutants) ? file.mutants : [] | ||
| for (const mutant of mutants) { | ||
| if (mutant === null || typeof mutant !== "object") continue | ||
| if (!VALID_STATUSES.has(mutant.status)) continue | ||
| counts.mutants += 1 | ||
| if (mutant.status === "Killed") counts.killed += 1 | ||
| if (mutant.status === "Survived") counts.survived += 1 | ||
| if (mutant.status === "NoCoverage") counts.noCoverage += 1 | ||
| if (mutant.status === "Timeout") counts.timeout += 1 | ||
| } | ||
| if (counts.mutants === 0) continue | ||
| const relative = normalizeReportPath(filePath, report.projectRoot, cwd) | ||
| if (relative === undefined) { | ||
| unreadable.push(renderReportName(directory.name) + ": rejected unsafe report path") | ||
| continue | ||
| } | ||
| rows.push({ file: relative, ...counts }) | ||
| } | ||
| } | ||
|
|
||
| const lines = ["", "### Per-file mutation results", ""] | ||
|
|
||
| if (rows.length === 0) { | ||
| lines.push("No Stryker JSON report was produced for this run; see the step log above for the reason.") | ||
| } else { | ||
| rows.sort((left, right) => { | ||
| const blocked = right.survived + right.noCoverage - (left.survived + left.noCoverage) | ||
| return blocked === 0 ? left.file.localeCompare(right.file) : blocked | ||
| }) | ||
| lines.push( | ||
| "Every file below comes from the " + | ||
| BACKTICK + | ||
| "changed-code-mutation-report" + | ||
| BACKTICK + | ||
| " artifact (" + | ||
| BACKTICK + | ||
| "reports/mutation/<package>/mutation.json" + | ||
| BACKTICK + | ||
| "). Rows are sorted by Survived + NoCoverage, worst first; Score is (Killed + Timeout) / Mutants, matching Stryker's mutation score.", | ||
| "", | ||
| "| File | Mutants | Killed | Survived | NoCoverage | Score |", | ||
| "| --- | ---: | ---: | ---: | ---: | ---: |", | ||
| ) | ||
| for (const row of rows) { | ||
| // Stryker counts a Timeout mutant as detected, so the mutation score numerator is | ||
| // Killed + Timeout (the table's Killed column stays Killed-only). | ||
| const score = (((row.killed + row.timeout) / row.mutants) * 100).toFixed(1) | ||
| lines.push( | ||
| "| " + | ||
| BACKTICK + | ||
| escapeMarkdownCell(row.file) + | ||
| BACKTICK + | ||
| " | " + | ||
| row.mutants + | ||
| " | " + | ||
| row.killed + | ||
| " | " + | ||
| row.survived + | ||
| " | " + | ||
| row.noCoverage + | ||
| " | " + | ||
| score + | ||
| "% |", | ||
| ) | ||
| } | ||
| lines.push( | ||
| "", | ||
| "Close a surviving or uncovered mutant with a focused test that fails under the mutation; the artifact does not need to be downloaded to read this table.", | ||
| ) | ||
| } | ||
|
|
||
| if (unreadable.length > 0) lines.push("", "Unreadable report(s): " + unreadable.map(escapeMarkdownCell).join("; ")) | ||
|
|
||
| return { lines, rows, unreadable } | ||
| } | ||
|
|
||
| /** | ||
| * Append through a crash-safe path: read the existing summary, write the combined content to a | ||
| * temporary file in the same directory, then rename it over the target. A partial append would | ||
| * leave the job summary truncated; the rename is atomic within one filesystem. | ||
| */ | ||
| function appendSummaryAtomically(outputFile, summary) { | ||
| const existing = fs.existsSync(outputFile) ? fs.readFileSync(outputFile, "utf8") : "" | ||
| const directory = path.dirname(outputFile) | ||
| const baseName = path.basename(outputFile) | ||
| const temporary = path.join(directory, "." + baseName + "." + process.pid + "." + Date.now() + ".tmp") | ||
| let renamed = false | ||
| try { | ||
| fs.writeFileSync(temporary, existing + summary) | ||
| fs.renameSync(temporary, outputFile) | ||
| renamed = true | ||
| } finally { | ||
| if (!renamed) { | ||
| try { | ||
| fs.unlinkSync(temporary) | ||
| } catch { | ||
| // Best effort: the caller is already failing the write. | ||
| } | ||
| } | ||
| } | ||
| // A cancelled earlier run can leave its temp file beside the summary; clean those up. | ||
| try { | ||
| for (const entry of fs.readdirSync(directory)) { | ||
| if (entry.startsWith("." + baseName + ".") && entry.endsWith(".tmp")) { | ||
| try { | ||
| fs.unlinkSync(path.join(directory, entry)) | ||
| } catch { | ||
| // Another writer may own it; never fail the summary for the sweep. | ||
| } | ||
| } | ||
| } | ||
| } catch { | ||
| // The directory sweep is best effort. | ||
| } | ||
| } | ||
|
|
||
| /** Entry point used by the workflow: append the summary to GITHUB_STEP_SUMMARY (or stdout). */ | ||
| export function writeMutationSummary({ | ||
| reportRoot = REPORT_DIRECTORY, | ||
| cwd = process.cwd(), | ||
| outputFile = process.env.GITHUB_STEP_SUMMARY, | ||
| } = {}) { | ||
| const { lines } = summarizeMutationReports({ reportRoot, cwd }) | ||
| const summary = lines.join("\n") + "\n" | ||
| try { | ||
| if (outputFile) appendSummaryAtomically(outputFile, summary) | ||
| else process.stdout.write(summary) | ||
| } catch (error) { | ||
| console.log( | ||
| "::warning title=Mutation test advisory::Could not write the per-file mutation summary: " + error.message, | ||
| ) | ||
| } | ||
| return summary | ||
| } | ||
|
|
||
| // When executed directly (the workflow step), write the summary for this run. | ||
| if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { | ||
| writeMutationSummary() | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.