56 lines
3.3 KiB
Python
56 lines
3.3 KiB
Python
"""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."""
|
|
import os
|
|
import pathlib
|
|
import shutil
|
|
import tempfile
|
|
|
|
from mutate_changes.tool_process import failed, run_tool
|
|
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})
|
|
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", "--no-renames", 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}")
|
|
try:
|
|
pathlib.Path(scratch, "branch.diff").write_text(diff, encoding="utf-8")
|
|
relative = os.path.relpath(scratch, top)
|
|
ran = run_tool(["cargo", "mutants", "--in-diff", os.path.join(relative, "branch.diff"), "--output", relative, "--jobs", str(jobs)], top, prefix)
|
|
if isinstance(ran, Failure) or ran.code not in CARGO_FINISHED:
|
|
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}")
|
|
finally:
|
|
shutil.rmtree(scratch, ignore_errors=True)
|
|
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]
|
|
|
|
|
|
def outcome_lines(path: pathlib.Path) -> list[str]:
|
|
return path.read_text(encoding="utf-8", errors="replace").splitlines() if path.is_file() else []
|
|
|
|
|
|
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()]
|
|
|
|
|
|
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."""
|
|
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()]
|