Answer principles round 1: 1 🔴, 7 🟡 (0 misread); every effect is handed in as an argument, no test patches a global, and the traces principle scopes tools that run on their user's machine
CI / tests (pull_request) Successful in 3m43s

This commit is contained in:
2026-11-19 12:42:03 +01:00
parent 8ca612b7a4
commit cb3135bc8c
124 changed files with 1437 additions and 793 deletions
+21 -13
View File
@@ -42,22 +42,28 @@ Exit 1 too when git fails, a file cannot be read or copied, a tool cannot start,
Infection also exit nonzero on a mutation score below a threshold the project sets, which fails the run as well and prints their
whole report.
"""
import functools
import os
import sys
import time
import typing
from mutate_changes.changed_lines import added_lines, not_mutated, tool_for
from machine_load.slots import CPUS_PER_SLOT
from git_cli.git_repo import default_ref, git, top
from git_cli.git_repo import default_ref, Git, top
from failure import Failure
from git_cli import git_repo
from mutate_changes.in_diff_tools import run_cargo_mutants, run_infection, run_stryker
from mutate_changes.mutmut_tool import run_mutmut
from mutate_changes.tool_process import run_tool
USAGE = "usage: mutate-changes [--base <ref>] [--prefix <shell text>] [--jobs <n>]"
# A panel of three readers, seated as the README's audience, picked the command's name and these options on 2026-11-11.
OPTIONS = ("--base", "--jobs", "--prefix")
def main(argv: list[str]) -> int:
def main(argv: list[str], env: typing.Mapping[str, str]) -> int:
"""Runs mutate-changes with the arguments argv; git and each tool run in the environment env."""
if argv in (["-h"], ["--help"]):
print(__doc__.strip())
return 0
@@ -66,7 +72,7 @@ def main(argv: list[str]) -> int:
print(f"{given.reason}; {USAGE}", file=sys.stderr)
return 1
started = time.monotonic()
report = mutate(given)
report = mutate(given, env)
if isinstance(report, Failure):
print(report.reason, file=sys.stderr)
return 1
@@ -89,12 +95,13 @@ def options(argv: list[str]) -> dict[str, str] | Failure:
return given
def mutate(given: dict[str, str]) -> list[str] | Failure:
def mutate(given: dict[str, str], env: typing.Mapping[str, str]) -> list[str] | Failure:
"""Each tool's report on the lines the branch adds, then the lines naming the changed files no tool mutates."""
checkout = top()
git = functools.partial(git_repo.git, env=env)
checkout = top(git)
if isinstance(checkout, Failure):
return checkout
base = given.get("--base") or default_ref(cwd=checkout)
base = given.get("--base") or default_ref(cwd=checkout, run=git)
if isinstance(base, Failure):
return base
found = git("merge-base", base, "HEAD", cwd=checkout)
@@ -105,27 +112,28 @@ def mutate(given: dict[str, str]) -> list[str] | Failure:
if isinstance(names, Failure):
return names
paths = list(filter(None, names.split("\0")))
changed = changed_by_tool(checkout, merge_base, paths)
changed = changed_by_tool(checkout, merge_base, paths, git)
if isinstance(changed, Failure):
return changed
prefix, jobs = given.get("--prefix"), int(given.get("--jobs", CPUS_PER_SLOT))
jobs = int(given.get("--jobs", CPUS_PER_SLOT))
run = functools.partial(run_tool, prefix=given.get("--prefix"), env=env, killpg=os.killpg)
report = [] if changed else ["No tool ran: the branch adds no line to a file that a tool mutates."]
for tool, files in sorted(changed.items()):
if tool == "cargo-mutants":
lines = run_cargo_mutants(checkout, merge_base, sorted(files), prefix, jobs)
lines = run_cargo_mutants(checkout, merge_base, sorted(files), run, git, jobs)
elif tool == "infection":
lines = run_infection(checkout, merge_base, prefix, jobs)
lines = run_infection(checkout, merge_base, run, jobs)
elif tool == "stryker":
lines = run_stryker(checkout, files, prefix, jobs)
lines = run_stryker(checkout, files, run, jobs)
else:
lines = run_mutmut(checkout, files, prefix, jobs)
lines = run_mutmut(checkout, files, run, git, jobs)
if isinstance(lines, Failure):
return lines
report.extend(lines)
return [*report, *not_mutated(paths)]
def changed_by_tool(checkout: str, merge_base: str, paths: list[str]) -> dict[str, dict[str, frozenset[int]]] | Failure:
def changed_by_tool(checkout: str, merge_base: str, paths: list[str], git: Git) -> dict[str, dict[str, frozenset[int]]] | Failure:
"""Each tool's files among paths, by path from the checkout's top, with the lines the working tree adds since merge_base."""
changed: dict[str, dict[str, frozenset[int]]] = {}
for path in paths: