Answer stability round 1: 3 🔴, 5 🟡 (0 misread); run only a Stryker the project installed, tell mutmut's setup fault from no test, pin cargo-mutants' diff prefixes, keep -x, end the tool and report its scratch folder on SIGTERM, and test option-like names; with product-owner's and prose's code fixes

This commit is contained in:
2026-11-11 21:12:08 +01:00
parent 63369d5944
commit 581c8721a0
10 changed files with 357 additions and 134 deletions
+31 -19
View File
@@ -1,13 +1,11 @@
"""Mutate a branch's changed lines with the tools that limit a run to them themselves: cargo-mutants, Stryker and Infection. Each runs at the checkout's top."""
"""Mutate a branch's changed lines with cargo-mutants, Stryker and Infection, each of which takes the changed lines itself."""
import os
import pathlib
import shutil
import tempfile
from mutate_changes.tool_process import failed, run_tool
from mutate_changes.changed_lines import by_folder, label, runs
from mutate_changes.tool_process import failed, remove, run_tool, scratch_folder
from failure import Failure
from git_cli.git_repo import git
from mutate_changes.changed_lines import runs
# cargo-mutants' exit codes for a run that finished: every mutant caught, some missed, and some timed out.
CARGO_FINISHED = frozenset({0, 2, 3})
@@ -15,14 +13,16 @@ CARGO_OUTCOMES = ("caught", "missed", "timeout", "unviable")
def run_cargo_mutants(top: str, base: str, paths: list[str], prefix: str | None, jobs: int) -> list[str] | Failure:
"""A line with cargo-mutants' mutant count, then a line per mutant it missed or that timed out, as `<path>:<line>:<column>: <outcome>: <mutation>`."""
diff = git("diff", "--no-color", "--no-ext-diff", "--no-textconv", base, "--", *paths, cwd=top)
"""A line with cargo-mutants' mutant count, then a line per mutant it missed or that timed out, as `<path>:<line>:<column>: <outcome>: <mutation>`.
cargo-mutants runs in top."""
# cargo-mutants finds each file's path after a b/, a prefix that settings such as diff.mnemonicPrefix change.
diff = git("diff", "--no-color", "--no-ext-diff", "--no-textconv", "--src-prefix=a/", "--dst-prefix=b/", base, "--", *paths, cwd=top)
if isinstance(diff, Failure):
return diff
try:
scratch = tempfile.mkdtemp(prefix=".mutate-changes-", dir=top)
except OSError as failure:
return Failure(f"could not make a scratch folder in {top}: {failure}")
scratch = scratch_folder(top)
if isinstance(scratch, Failure):
return scratch
try:
pathlib.Path(scratch, "branch.diff").write_text(diff, encoding="utf-8")
relative = os.path.relpath(scratch, top)
@@ -31,9 +31,9 @@ def run_cargo_mutants(top: str, base: str, paths: list[str], prefix: str | None,
return ran if isinstance(ran, Failure) else failed("cargo mutants", ran)
outcomes = {outcome: outcome_lines(pathlib.Path(scratch, "mutants.out", f"{outcome}.txt")) for outcome in CARGO_OUTCOMES}
except OSError as failure:
return Failure(f"could not read cargo-mutants' outcomes in {scratch}: {failure}")
return Failure(f"could not hand cargo-mutants the diff or read its outcomes in {scratch}: {failure}")
finally:
shutil.rmtree(scratch, ignore_errors=True)
remove(scratch)
uncaught = [f"{location}: {outcome}: {mutation}" for outcome in ("missed", "timeout") for location, _, mutation in (line.partition(": ") for line in outcomes[outcome])]
return [f"cargo-mutants: {sum(map(len, outcomes.values()))} mutants on changed lines, {len(uncaught)} not caught.", *uncaught]
@@ -42,14 +42,26 @@ def outcome_lines(path: pathlib.Path) -> list[str]:
return path.read_text(encoding="utf-8", errors="replace").splitlines() if path.is_file() else []
# Stryker and Infection also exit nonzero on a mutation score below a threshold the project sets, which their exit code doesn't
# tell from a run that broke, so either fails the run.
def run_stryker(top: str, changed: dict[str, frozenset[int]], prefix: str | None, jobs: int) -> list[str] | Failure:
"""Stryker's exit code, then its own report of the mutants on the changed lines."""
mutate = ",".join(f"{path}:{first}-{last}" for path, lines in sorted(changed.items()) for first, last in runs(lines))
ran = run_tool(["npx", "stryker", "run", "--mutate", mutate, "--concurrency", str(jobs), "--reporters", "clear-text"], top, prefix)
return ran if isinstance(ran, Failure) else [f"Stryker exited {ran.code}; its report:", *ran.output.splitlines()]
"""For each folder Stryker runs in, a line naming it, then Stryker's own report of the mutants on the changed lines.
Stryker runs in the nearest folder above each file that holds a package.json, else in top. Through `npx --no`, it runs only where
the project installed it."""
report: list[str] = []
for folder, files in sorted(by_folder(top, changed, ("package.json",)).items()):
mutate = ",".join(f"{path}:{first}-{last}" for path, lines in sorted(files.items()) for first, last in runs(lines))
ran = run_tool(["npx", "--no", "stryker", "run", "--mutate", mutate, "--concurrency", str(jobs), "--reporters", "clear-text"], os.path.join(top, folder), prefix)
if isinstance(ran, Failure) or ran.code:
return ran if isinstance(ran, Failure) else failed(f"Stryker in {label(folder)}", ran)
report.extend([f"Stryker in {label(folder)}:", *ran.output.splitlines()])
return report
def run_infection(top: str, base: str, prefix: str | None, jobs: int) -> list[str] | Failure:
"""Infection's exit code, then its own report of the mutants on the lines the branch changes since base."""
"""A line naming where Infection ran, top, then its own report of the mutants on the lines the branch changes since base."""
ran = run_tool(["vendor/bin/infection", "--git-diff-lines", f"--git-diff-base={base}", f"--threads={jobs}", "--show-mutations=max", "--no-progress"], top, prefix)
return ran if isinstance(ran, Failure) else [f"Infection exited {ran.code}; its report:", *ran.output.splitlines()]
if isinstance(ran, Failure) or ran.code:
return ran if isinstance(ran, Failure) else failed("Infection", ran)
return ["Infection in the checkout's top:", *ran.output.splitlines()]