Mutate the Go lines a branch changes with Gremlins in mutate-changes
CI / tests (pull_request) Successful in 3m54s

This commit is contained in:
2026-11-20 23:24:21 +01:00
parent 1de88b9b20
commit a430bca9f9
6 changed files with 118 additions and 9 deletions
+38 -1
View File
@@ -1,6 +1,8 @@
"""Mutate a branch's changed lines with cargo-mutants, Stryker and Infection, each of which takes the changed lines itself."""
"""Mutate a branch's changed lines with cargo-mutants, Gremlins, Stryker and Infection, each of which takes the changed lines itself."""
import json
import os
import pathlib
import typing
from mutate_changes.changed_lines import by_folder, label, runs
from mutate_changes.tool_process import failed, Ran, remove, Run, scratch_folder
@@ -10,6 +12,8 @@ from git_cli.git_repo import Git
# cargo-mutants' exit codes for a run that finished: every mutant caught, some missed, and some timed out.
CARGO_FINISHED = frozenset({0, 2, 3})
CARGO_OUTCOMES = ("caught", "missed", "timeout", "unviable")
# The statuses in Gremlins' report of a mutant that its tests don't kill.
GREMLINS_UNKILLED = frozenset({"LIVED", "NOT COVERED"})
def run_cargo_mutants(top: str, base: str, paths: list[str], run: Run, git: Git, jobs: int) -> list[str] | Failure:
@@ -38,6 +42,39 @@ def run_cargo_mutants(top: str, base: str, paths: list[str], run: Run, git: Git,
return [f"cargo-mutants: {sum(map(len, outcomes.values()))} mutants on changed lines, {len(uncaught)} not caught.", *uncaught]
def run_gremlins(top: str, base: str, run: Run, jobs: int) -> list[str] | Failure:
"""A line with the number of mutants Gremlins reports as lived or not covered, then a line per mutant, as
`<path>:<line>:<column>: <status>: <mutator>`.
Gremlins runs in top and mutates the Go lines the branch changes since base. It skips test files itself. Its git needs `run` to
hold the settings that `with_zero_diff_context` adds to an environment."""
scratch = scratch_folder(top)
if isinstance(scratch, Failure):
return scratch
try:
report = os.path.join(os.path.relpath(scratch, top), "report.json")
ran = run(["gremlins", "unleash", "--diff", base, "--output", report, "--workers", str(jobs)], top)
if isinstance(ran, Failure) or ran.code:
return ran if isinstance(ran, Failure) else failed("gremlins", ran)
# Gremlins writes `null` for a list with no entry.
files = json.loads(pathlib.Path(scratch, "report.json").read_text(encoding="utf-8"))["files"] or []
unkilled = sorted((file["file_name"], mutant["line"], mutant["column"], mutant["status"], mutant["type"]) for file in files for mutant in file["mutations"] or [] if mutant["status"] in GREMLINS_UNKILLED)
except (OSError, ValueError, KeyError, TypeError) as failure:
return Failure(f"could not read Gremlins' report in {scratch}: {failure}")
finally:
remove(scratch)
return [f"gremlins: {len(unkilled)} mutants not killed.", *(f"{path}:{line}:{column}: {status}: {mutator}" for path, line, column, status, mutator in unkilled)]
def with_zero_diff_context(env: typing.Mapping[str, str]) -> dict[str, str]:
"""env with git set, through its GIT_CONFIG_* variables, to print no context lines around a change, after any setting env holds.
Gremlins v0.6.0 mutates one run of lines per hunk, so a hunk must hold only the lines the branch changes."""
held = env.get("GIT_CONFIG_COUNT", "0")
count = int(held) if held.isdecimal() else 0
return {**env, "GIT_CONFIG_COUNT": str(count + 1), f"GIT_CONFIG_KEY_{count}": "diff.context", f"GIT_CONFIG_VALUE_{count}": "0"}
def outcome_lines(path: pathlib.Path) -> list[str]:
return path.read_text(encoding="utf-8", errors="replace").splitlines() if path.is_file() else []