Answer stability round 1: 1 🔴, 2 🟡 (0 misread); pin the git diff settings Gremlins parses, show the mutant total, and a container prefix that runs as root
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
"""Mutate a branch's changed lines with cargo-mutants, Gremlins, Stryker and Infection, each of which takes the changed lines itself."""
|
||||
"""Mutate a branch's changed lines with cargo-mutants, Gremlins, Infection and Stryker, each of which takes the changed lines itself."""
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
@@ -12,8 +12,10 @@ 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"})
|
||||
# What Gremlins v0.6.0 needs of the diff it reads from git: one run of lines per hunk is all it mutates, so a hunk holds the changed
|
||||
# lines and none around them; it takes each file's path after a b/; and it reads no colour codes. A user's git settings change each.
|
||||
GREMLINS_GIT_SETTINGS = {"diff.context": "0", "diff.noprefix": "false", "diff.mnemonicPrefix": "false", "diff.srcPrefix": "a/", "diff.dstPrefix": "b/", "color.diff": "never"}
|
||||
|
||||
|
||||
def run_cargo_mutants(top: str, base: str, paths: list[str], run: Run, git: Git, jobs: int) -> list[str] | Failure:
|
||||
@@ -42,37 +44,50 @@ 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>`.
|
||||
class Mutant(typing.NamedTuple):
|
||||
"""A mutant in Gremlins' report, in the order the report prints them."""
|
||||
|
||||
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."""
|
||||
path: str
|
||||
line: int
|
||||
column: int
|
||||
status: str
|
||||
mutator: str
|
||||
|
||||
|
||||
def run_gremlins(top: str, base: str, run: Run, jobs: int) -> list[str] | Failure:
|
||||
"""A line with the number of mutants on the changed lines and how many of them Gremlins reports as lived or not covered, then a
|
||||
line per such mutant, as `<path>:<line>:<column>: <status>: <mutator>`.
|
||||
|
||||
Gremlins runs in top and mutates the Go lines the branch changes since base. It skips `*_test.go` itself. It reads the diff from
|
||||
git, so `run` must hold the environment that `with_plain_git_diff` makes. Gremlins exits nonzero on a mutation score below a
|
||||
threshold the project sets, which its exit code doesn't tell from a run that broke, so any nonzero exit fails the run."""
|
||||
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)
|
||||
report = pathlib.Path(scratch, "report.json")
|
||||
ran = run(["gremlins", "unleash", "--diff", base, "--output", os.path.join(os.path.relpath(scratch, top), report.name), "--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)
|
||||
return ran if isinstance(ran, Failure) else whole_output("Gremlins", ran)
|
||||
# Gremlins writes no report where it finds no mutant, and `null` for a list with no entry.
|
||||
files = (json.loads(report.read_text(encoding="utf-8"))["files"] or []) if report.is_file() else []
|
||||
mutants = [Mutant(file["file_name"], mutant["line"], mutant["column"], mutant["status"], mutant["type"]) for file in files for mutant in file["mutations"] or []]
|
||||
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)]
|
||||
# Gremlins lists the mutants on the unchanged lines of a hunk too, as SKIPPED. It lists the files in no fixed order.
|
||||
unkilled = sorted(mutant for mutant in mutants if mutant.status in GREMLINS_UNKILLED)
|
||||
on_changed_lines = sum(mutant.status != "SKIPPED" for mutant in mutants)
|
||||
return [f"gremlins: {on_changed_lines} mutants on changed lines, {len(unkilled)} not killed.", *(f"{m.path}:{m.line}:{m.column}: {m.status}: {m.mutator}" for m 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 with_plain_git_diff(env: typing.Mapping[str, str]) -> dict[str, str]:
|
||||
"""env with the settings of GREMLINS_GIT_SETTINGS added to the ones it holds, as git reads them from its GIT_CONFIG_* variables."""
|
||||
held = env.get("GIT_CONFIG_COUNT", "")
|
||||
first = int(held) if held.isdecimal() else 0
|
||||
added = {f"GIT_CONFIG_{part}_{first + index}": text for index, (key, value) in enumerate(GREMLINS_GIT_SETTINGS.items()) for part, text in (("KEY", key), ("VALUE", value))}
|
||||
return {**env, **added, "GIT_CONFIG_COUNT": str(first + len(GREMLINS_GIT_SETTINGS))}
|
||||
|
||||
|
||||
def outcome_lines(path: pathlib.Path) -> list[str]:
|
||||
|
||||
Reference in New Issue
Block a user