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
+23 -30
View File
@@ -8,10 +8,10 @@ import os
import pathlib
import posixpath
import shutil
import tempfile
import tomllib
from mutate_changes.tool_process import failed, Ran, run_tool
from mutate_changes.changed_lines import by_folder, label
from mutate_changes.tool_process import failed, Ran, remove, run_tool, scratch_folder
from failure import Failure
from mutate_changes.mutmut_mutants import function_keys, mutant_globs, mutated_line, named, statuses
from git_cli.git_repo import git
@@ -19,11 +19,12 @@ from mutate_changes.mutmut_setup import left_out, mutmut_config
CAUGHT = frozenset({"caught by type check", "killed"})
CONFIGS = ("pyproject.toml", "setup.cfg")
# The first run reports every test that fails before any mutant, not only the first, so that the next run can leave them all out.
# Where a test fails before any mutant, a run with these lists every such test, not only the first, so that the next run can leave
# them all out. Each mutant's run would take them too, and run every test where the first that fails is enough.
LIST_FAILURES = ["--maxfail=0", "--continue-on-collection-errors"]
# What mutmut 3.8 prints where no mutant matches the patterns, where no test reaches a mutated function, and where a test fails unmutated.
NO_MUTANT = "Filtered for specific mutants, but nothing matches"
NO_TEST = "Stopping early, because"
NO_TEST = "Stopping early, because we could not find any test case for any mutant"
STATS_FAILED = "failed to collect stats. runner returned"
@@ -38,7 +39,7 @@ class Folder:
@property
def label(self) -> str:
return "the checkout's top" if self.path == "." else self.path
return label(self.path)
def run_mutmut(top: str, changed: dict[str, frozenset[int]], prefix: str | None, jobs: int) -> list[str] | Failure:
@@ -46,7 +47,7 @@ def run_mutmut(top: str, changed: dict[str, frozenset[int]], prefix: str | None,
changed maps each file, by its path from top, to its changed lines."""
report: list[str] = []
for path, files in sorted(by_folder(top, changed).items()):
for path, files in sorted(by_folder(top, changed, CONFIGS).items()):
lines = mutate_folder(top, path, files, prefix, jobs)
if isinstance(lines, Failure):
return lines
@@ -54,17 +55,6 @@ def run_mutmut(top: str, changed: dict[str, frozenset[int]], prefix: str | None,
return report
def by_folder(top: str, changed: dict[str, frozenset[int]]) -> dict[str, dict[str, frozenset[int]]]:
"""The changed files by the folder mutmut runs in for each, "." for top, each file by its path from that folder."""
grouped: dict[str, dict[str, frozenset[int]]] = {}
for path, lines in changed.items():
folder = posixpath.dirname(path)
while folder and not any(os.path.isfile(os.path.join(top, folder, name)) for name in CONFIGS):
folder = posixpath.dirname(folder)
grouped.setdefault(folder or ".", {})[posixpath.relpath(path, folder or ".")] = lines
return grouped
def mutate_folder(top: str, path: str, files: dict[str, frozenset[int]], prefix: str | None, jobs: int) -> list[str] | Failure:
root = os.path.join(top, path)
folder = read_folder(root, path, files)
@@ -76,10 +66,9 @@ def mutate_folder(top: str, path: str, files: dict[str, frozenset[int]], prefix:
listed = git("ls-files", "-z", "--cached", "--others", "--exclude-standard", cwd=root)
if isinstance(listed, Failure):
return listed
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:
copy = os.path.join(scratch, "copy")
copied = copy_files(root, listed.split("\0"), copy)
@@ -87,7 +76,7 @@ def mutate_folder(top: str, path: str, files: dict[str, frozenset[int]], prefix:
except OSError as failure:
return Failure(f"could not set mutmut up in {scratch}, a copy of {root}: {failure}")
finally:
shutil.rmtree(scratch, ignore_errors=True)
remove(scratch)
def read_folder(root: str, path: str, files: dict[str, frozenset[int]]) -> Folder | Failure:
@@ -110,7 +99,7 @@ def project_setup(root: str) -> str | Failure:
pyproject = pathlib.Path(root, "pyproject.toml")
tool = tomllib.loads(pyproject.read_text(encoding="utf-8")).get("tool") if pyproject.is_file() else None
if isinstance(tool, dict) and "mutmut" in tool:
return Failure(f"{pyproject} sets mutmut up under [tool.mutmut]; move that to setup.cfg's [mutmut], the only place mutate-changes sets mutmut up through")
return Failure(f"{pyproject} sets mutmut up under [tool.mutmut]; move that to setup.cfg's [mutmut], since mutate-changes sets mutmut up only there")
setup = pathlib.Path(root, "setup.cfg")
return setup.read_text(encoding="utf-8") if setup.is_file() else ""
except (OSError, UnicodeDecodeError, tomllib.TOMLDecodeError) as failure:
@@ -118,14 +107,14 @@ def project_setup(root: str) -> str | Failure:
def copy_files(root: str, paths: list[str], copy: str) -> list[str]:
"""Copy the files git lists in root, tracked or not ignored, to copy, symlinks as symlinks, and name each file and folder copied to copy's top.
"""Copy each file git lists in root, tracked or untracked but not ignored, into copy, keeping symlinks as symlinks, and return the names of the files and folders at copy's top.
Leaves out mutmut's own mutants/ and the scratch folders of runs that never cleaned up."""
Leaves out mutmut's own mutants/."""
copied = set()
for path in paths:
source, target = os.path.join(root, path), os.path.join(copy, path)
gone_or_submodule = not os.path.lexists(source) or os.path.isdir(source) and not os.path.islink(source)
if not path or path.split("/")[0] == "mutants" or path.startswith(".mutate-changes-") or gone_or_submodule:
if not path or path.split("/")[0] == "mutants" or gone_or_submodule:
continue
os.makedirs(os.path.dirname(target), exist_ok=True)
shutil.copy2(source, target, follow_symlinks=False)
@@ -138,6 +127,8 @@ def mutate_copy(copy: str, copied: list[str], folder: Folder, argv: list[str], p
if isinstance(ran, Failure):
return ran
output, left = ran
if not os.path.isdir(os.path.join(copy, "mutants")):
return Failure(f"mutmut in {folder.label} wrote no mutants/ in {copy}; --prefix must run it in $PWD")
if NO_MUTANT in output:
return [f"mutmut in {folder.label}: none of the functions the changed lines fall in has a mutant."]
results = run_tool(["mutmut", "results", "--all", "true"], copy, prefix)
@@ -146,16 +137,18 @@ def mutate_copy(copy: str, copied: list[str], folder: Folder, argv: list[str], p
shown, counted = not_killed(folder, statuses(results.output), copy)
unreached = " No test reaches them." if NO_TEST in output else ""
left_files = sorted({option.partition("=")[2].partition("::")[0] for option in left})
left_note = f" Left out tests in {', '.join(left_files)} that fail without a mutant." if left else ""
left_note = f" Left out the tests that fail without a mutant, in {', '.join(left_files)}." if left else ""
return [f"mutmut in {folder.label}: {counted} mutants on changed lines, {len(shown)} not killed.{unreached}{left_note}", *shown]
def run_until_stats_pass(copy: str, copied: list[str], folder: Folder, argv: list[str], prefix: str | None) -> tuple[str, list[str]] | Failure:
"""mutmut's output, and the options that left out each test that fails before any mutant: mutmut runs no mutant while one fails."""
ran = configured_run(copy, copied, folder, LIST_FAILURES, argv, prefix)
ran = configured_run(copy, copied, folder, [], argv, prefix)
left: list[str] = []
if isinstance(ran, Ran) and STATS_FAILED in ran.output and (left := left_out(ran.output)):
ran = configured_run(copy, copied, folder, left, argv, prefix)
if isinstance(ran, Ran) and STATS_FAILED in ran.output:
ran = configured_run(copy, copied, folder, LIST_FAILURES, argv, prefix)
if isinstance(ran, Ran) and STATS_FAILED in ran.output and (left := left_out(ran.output)):
ran = configured_run(copy, copied, folder, left, argv, prefix)
if isinstance(ran, Failure):
return ran
if ran.code and NO_MUTANT not in ran.output and NO_TEST not in ran.output: