190 lines
9.3 KiB
Python
190 lines
9.3 KiB
Python
"""Mutate the changed lines of a branch's Python files with mutmut 3, 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 nearest folder above it, up to the checkout's top, that holds a setup.cfg or a pyproject.toml: the folder its tests
|
|
import it from."""
|
|
import dataclasses
|
|
import os
|
|
import pathlib
|
|
import posixpath
|
|
import shutil
|
|
import tempfile
|
|
import tomllib
|
|
|
|
from mutate_changes.tool_process import failed, Ran, run_tool
|
|
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
|
|
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.
|
|
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"
|
|
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 "the checkout's top" if self.path == "." else self.path
|
|
|
|
|
|
def run_mutmut(top: str, changed: dict[str, frozenset[int]], prefix: str | None, 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(by_folder(top, changed).items()):
|
|
lines = mutate_folder(top, path, files, prefix, jobs)
|
|
if isinstance(lines, Failure):
|
|
return lines
|
|
report.extend(lines)
|
|
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)
|
|
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 [f"mutmut in {folder.label}: no changed line falls in a function or method, the only code mutmut mutates."]
|
|
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}")
|
|
try:
|
|
copy = os.path.join(scratch, "copy")
|
|
copy_files(root, listed.split("\0"), copy)
|
|
return mutate_copy(copy, folder, ["mutmut", "run", "--max-children", str(jobs), *globs], 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)
|
|
|
|
|
|
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], the only place mutate-changes sets mutmut up through")
|
|
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) -> None:
|
|
"""Copy the files git lists in root, tracked or not ignored, to copy, symlinks as symlinks. Leaves out mutmut's own mutants/ and the scratch folders of runs that never cleaned up."""
|
|
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:
|
|
continue
|
|
os.makedirs(os.path.dirname(target), exist_ok=True)
|
|
shutil.copy2(source, target, follow_symlinks=False)
|
|
|
|
|
|
def mutate_copy(copy: str, folder: Folder, argv: list[str], prefix: str | None) -> list[str] | Failure:
|
|
ran = run_until_stats_pass(copy, folder, argv, prefix)
|
|
if isinstance(ran, Failure):
|
|
return ran
|
|
output, left = ran
|
|
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)
|
|
if isinstance(results, Failure) or results.code:
|
|
return results if isinstance(results, Failure) else failed(f"mutmut results in {folder.label}", results)
|
|
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 ""
|
|
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, 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, folder, LIST_FAILURES, 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, 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:
|
|
return failed(f"mutmut run in {folder.label}", ran)
|
|
return ran.output, left
|
|
|
|
|
|
def configured_run(copy: str, folder: Folder, options: list[str], argv: list[str], prefix: str | None) -> Ran | Failure:
|
|
config = mutmut_config(folder.setup, sorted(folder.files), 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_tool(argv, copy, prefix)
|
|
|
|
|
|
def not_killed(folder: Folder, found: dict[str, str], copy: str) -> 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."""
|
|
copies: dict[str, str] = {}
|
|
shown: list[tuple[str, int, str]] = []
|
|
counted = 0
|
|
for name, status in 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
|
|
if file not in copies:
|
|
mutated = pathlib.Path(copy, "mutants", file)
|
|
copies[file] = mutated.read_text(encoding="utf-8", errors="replace") if mutated.is_file() else ""
|
|
where = mutated_line(folder.sources[file], copies[file], key, number)
|
|
if where and where[0] not in folder.files[file]:
|
|
continue
|
|
counted += 1
|
|
if status not in CAUGHT:
|
|
line, text = where or (0, f"{name}, on a line mutate-changes cannot find")
|
|
shown.append((posixpath.normpath(posixpath.join(folder.path, file)), line, f"{status}: {text}"))
|
|
return [f"{file}:{line}: {text}" if line else f"{file}: {text}" for file, line, text in sorted(shown)], counted
|