"""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 tomllib from mutate_changes.changed_lines import by_folder, label from mutate_changes.tool_process import failed, Ran, remove, Run, 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 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) 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(by_folder(top, changed, CONFIGS).items()): lines = mutate_folder(top, path, files, run, git, jobs) if isinstance(lines, Failure): return lines report.extend(lines) return report def mutate_folder(top: str, path: str, files: dict[str, frozenset[int]], run: Run, git: Git, 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 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) 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) -> list[str] | Failure: ran = run_until_stats_pass(copy, copied, folder, argv, run) 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(["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) 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 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], run: Run) -> 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, [], 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: return failed(f"mutmut run in {folder.label}", ran) return ran.output, 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 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 # Sorted by name, a file's mutants come one after another, so mutated_line parses its copy once. for name, status in sorted(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