Files
claude-config/bin/mutate_changes/mutmut_tool.py
T

257 lines
13 KiB
Python

"""Mutate the changed lines of a branch's Python files with mutmut 3, or every line of the files given, in a scratch copy of the folder each file's tests run from.
mutmut reads its settings from the folder it runs in, writes beside them, and names each module by its path from there. So each file is
mutated from the folder its tests import it from: the nearest folder above it, up to the checkout's top, that holds a setup.cfg or a
pyproject.toml, or the file's own folder where a test beside it imports it by its file name (bare_imports.py)."""
import dataclasses
import os
import pathlib
import posixpath
import shutil
import tomllib
import typing
from mutate_changes.changed_lines import by_folder, label
from mutate_changes.mutmut_mutants import Change, crash_in_mutmut, function_keys, mutant_globs, mutated_lines, named, statuses
from mutate_changes.tool_process import failed, Ran, remove, Run, scratch_folder
from failure import Failure
from git_cli.git_repo import Git
from mutate_changes.bare_imports import imported_by_file_name
from mutate_changes.mutmut_setup import left_out, mutmut_config
CAUGHT = frozenset({"caught by type check", "killed"})
CONFIGS = ("pyproject.toml", "setup.cfg")
# 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 we could not find any test case for any mutant"
STATS_FAILED = "failed to collect stats. runner returned"
@dataclasses.dataclass(frozen=True)
class Folder:
"""The changed files mutmut mutates from one folder."""
path: str # from the checkout's top; "." for the top itself
files: dict[str, frozenset[int]] # each file by its path from the folder, with its changed lines
sources: dict[str, str]
keys: dict[str, list[str]] # mutmut's key of each function or method a changed line falls in
setup: str # the folder's setup.cfg, empty where it has none
@property
def label(self) -> str:
return label(self.path)
@dataclasses.dataclass(frozen=True)
class Ended:
"""How mutmut's run in a folder ended. `found` is empty in two cases: the folder has no mutant, and `none` says why; or mutmut crashed, and `crash` says how."""
folder: Folder
found: dict[str, str] = dataclasses.field(default_factory=dict) # each mutant's status by mutmut's name of it
copies: dict[str, str] = dataclasses.field(default_factory=dict) # mutmut's mutants/ copy of each of the folder's files it wrote one for
none: str = ""
crash: str = ""
notes: str = "" # the end of the report's line: that no test reaches the mutants, and the tests left out
class Located(typing.NamedTuple):
"""A mutant in a function or method that a folder's lines fall in. change is None where the lines it changes cannot be found."""
file: str # from the folder
name: str
status: str
change: Change | None
class Killed(typing.NamedTuple):
"""A mutant mutmut reports as caught, by the tests or its type check, by mutmut's name of it, with its file's path from the top and the lines it changes."""
path: str
name: str
change: Change
def run_mutmut(top: str, changed: dict[str, frozenset[int]], run: Run, git: Git, jobs: int) -> list[str] | Failure:
"""For each folder mutmut runs in, a line on what it mutated and left out, then a line per mutant on a changed line that its tests don't kill.
changed maps each file, by its path from top, to its changed lines."""
report: list[str] = []
for path, files in sorted(imported_by_file_name(top, by_folder(top, changed, CONFIGS), CONFIGS).items()):
listed = git("ls-files", "-z", "--cached", "--others", "--exclude-standard", cwd=os.path.join(top, path))
if isinstance(listed, Failure):
return listed
ended = mutate_folder(top, path, files, listed.split("\0"), run, jobs)
if isinstance(ended, Failure):
return ended
report.extend(reported(ended))
return report
def killed_by_mutmut(top: str, paths: list[str], run: Run, jobs: int) -> list[Killed] | Failure:
"""Each mutant in the files at paths, from top, that the tests kill, in the order of mutmut's names within each folder it runs in.
top holds the project's files and no others, as an unpacked `git archive` does: mutmut's copy of a folder takes every file under it."""
try:
lines = {path: frozenset(range(1, len(pathlib.Path(top, path).read_bytes().splitlines()) + 1)) for path in paths}
except OSError as failure:
return Failure(f"could not read the files to mutate in {top}: {failure}")
killed: list[Killed] = []
for path, files in sorted(imported_by_file_name(top, by_folder(top, lines, CONFIGS), CONFIGS).items()):
root = os.path.join(top, path)
# os.walk lists a link to a folder among the folders and doesn't enter it; the copy keeps it as a link, as it keeps one git lists.
listed = [os.path.relpath(os.path.join(folder, name), root) for folder, folders, names in os.walk(root) for name in [*names, *(name for name in folders if os.path.islink(os.path.join(folder, name)))]]
ended = mutate_folder(top, path, files, listed, run, jobs)
if isinstance(ended, Failure) or ended.crash:
return ended if isinstance(ended, Failure) else Failure(f"mutmut in {ended.folder.label} crashed with {ended.crash}")
killed += [Killed(posixpath.normpath(posixpath.join(path, mutant.file)), mutant.name, mutant.change) for mutant in located(ended) if mutant.status in CAUGHT and mutant.change]
return killed
def mutate_folder(top: str, path: str, files: dict[str, frozenset[int]], listed: list[str], run: Run, jobs: int) -> Ended | Failure:
"""Runs mutmut over the functions and methods that the lines in `files` fall in. It runs in a scratch copy of the folder at `path`, and the copy holds the files in `listed`."""
root = os.path.join(top, path)
folder = read_folder(root, path, files)
if isinstance(folder, Failure):
return folder
globs = [glob for file in sorted(files) for glob in mutant_globs(file, folder.keys[file])]
if not globs:
return Ended(folder, none="no changed line falls in a function or method, the only code mutmut mutates.")
scratch = scratch_folder(top)
if isinstance(scratch, Failure):
return scratch
try:
copy = os.path.join(scratch, "copy")
copied = copy_files(root, listed, copy)
return mutate_copy(copy, copied, folder, ["mutmut", "run", "--max-children", str(jobs), *globs], run)
except OSError as failure:
return Failure(f"could not set mutmut up in {scratch}, a copy of {root}: {failure}")
finally:
remove(scratch)
def read_folder(root: str, path: str, files: dict[str, frozenset[int]]) -> Folder | Failure:
setup = project_setup(root)
if isinstance(setup, Failure):
return setup
sources, keys = {}, {}
for file, lines in files.items():
try:
sources[file] = pathlib.Path(root, file).read_text(encoding="utf-8")
keys[file] = function_keys(sources[file], lines)
except (OSError, SyntaxError, UnicodeDecodeError) as failure:
return Failure(f"could not read {os.path.join(root, file)}: {failure}")
return Folder(path, files, sources, keys, setup)
def project_setup(root: str) -> str | Failure:
"""The folder's setup.cfg, empty where it has none. A Failure where its pyproject.toml sets mutmut up, since mutmut then reads no setup.cfg."""
try:
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], 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:
return Failure(f"could not read the mutmut settings in {root}: {failure}")
def copy_files(root: str, paths: list[str], copy: str) -> list[str]:
"""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/."""
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 gone_or_submodule:
continue
os.makedirs(os.path.dirname(target), exist_ok=True)
shutil.copy2(source, target, follow_symlinks=False)
copied.add(path.split("/")[0])
return sorted(copied)
def mutate_copy(copy: str, copied: list[str], folder: Folder, argv: list[str], run: Run) -> Ended | Failure:
ran = run_until_stats_pass(copy, copied, folder, argv, run)
if isinstance(ran, Failure):
return ran
(output, code), left = ran
crash = crash_in_mutmut(output)
if code and crash:
return Ended(folder, crash=crash)
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 Ended(folder, none="none of the functions the changed lines fall in has a mutant.")
results = run(["mutmut", "results", "--all", "true"], copy)
if isinstance(results, Failure) or results.code:
return results if isinstance(results, Failure) else failed(f"mutmut results in {folder.label}", results)
copies = {file: mutated.read_text(encoding="utf-8", errors="replace") for file in folder.files if (mutated := pathlib.Path(copy, "mutants", file)).is_file()}
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 the tests that fail without a mutant, in {', '.join(left_files)}." if left else ""
return Ended(folder, statuses(results.output), copies, notes=unreached + left_note)
def reported(ended: Ended) -> list[str]:
"""A line on what mutmut mutated in the folder and left out, then a line per mutant on a changed line that its tests don't kill."""
label = ended.folder.label
if ended.crash:
return [f"mutmut in {label} crashed with {ended.crash}: mutate its changed lines by hand."]
if ended.none:
return [f"mutmut in {label}: {ended.none}"]
shown, counted = not_killed(ended)
return [f"mutmut in {label}: {counted} mutants on changed lines, {len(shown)} not killed.{ended.notes}", *shown]
def run_until_stats_pass(copy: str, copied: list[str], folder: Folder, argv: list[str], run: Run) -> tuple[tuple[str, int], list[str]] | Failure:
"""mutmut's output and exit code, 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, [], argv, run)
left: list[str] = []
if isinstance(ran, Ran) and STATS_FAILED in ran.output:
ran = configured_run(copy, copied, folder, LIST_FAILURES, argv, run)
if isinstance(ran, Ran) and STATS_FAILED in ran.output and (left := left_out(ran.output)):
ran = configured_run(copy, copied, folder, left, argv, run)
if isinstance(ran, Failure):
return ran
if ran.code and NO_MUTANT not in ran.output and NO_TEST not in ran.output and not crash_in_mutmut(ran.output):
return failed(f"mutmut run in {folder.label}", ran)
return (ran.output, ran.code), left
def configured_run(copy: str, copied: list[str], folder: Folder, options: list[str], argv: list[str], run: Run) -> Ran | Failure:
config = mutmut_config(folder.setup, sorted(folder.files), copied, options)
if isinstance(config, Failure):
return Failure(f"{folder.label}: {config.reason}")
pathlib.Path(copy, "setup.cfg").write_text(config, encoding="utf-8")
return run(argv, copy)
def located(ended: Ended) -> typing.Iterator[Located]:
"""Each mutant of the run that is in a function or method the folder's lines fall in, in the order of mutmut's names."""
folder = ended.folder
# Sorted by name, a file's mutants come one after another, so mutated_lines parses its copy once.
for name, status in sorted(ended.found.items()):
hit = named(name, list(folder.files))
if hit is None or hit[1] not in folder.keys[hit[0]]:
continue
file, key, number = hit
yield Located(file, name, status, mutated_lines(folder.sources[file], ended.copies.get(file, ""), key, number))
def not_killed(ended: Ended) -> tuple[list[str], int]:
"""A line per mutant on a changed line that its tests don't kill, in order of path and line, and how many mutants the changed lines hold."""
folder = ended.folder
shown: list[tuple[str, int, str]] = []
counted = 0
for mutant in located(ended):
change = mutant.change
if change and change.line not in folder.files[mutant.file]:
continue
counted += 1
if mutant.status not in CAUGHT:
# The first line the mutant has in place of the source's; where it only removes lines, it has none.
line, text = (change.line, "".join(change.now[:1]).strip() or "lines removed") if change else (0, f"{mutant.name}, on a line mutate-changes cannot find")
shown.append((posixpath.normpath(posixpath.join(folder.path, mutant.file)), line, f"{mutant.status}: {text}"))
return [f"{file}:{line}: {text}" if line else f"{file}: {text}" for file, line, text in sorted(shown)], counted