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:
2026-11-21 00:02:27 +01:00
parent 89f5fcbc16
commit 846d6c6e36
4 changed files with 87 additions and 59 deletions
+23 -17
View File
@@ -65,7 +65,8 @@ else:
# cargo-mutants, Gremlins, Infection and Stryker, which log the folder they run in and how they were called, and exit with the code the test
# gives. cargo-mutants also logs the diff it is given and writes its outcome files, all but unviable.txt, as a run without an
# unviable mutant does; with CAUGHT_UNREADABLE set, nobody may read caught.txt, and with OUTPUT_LOCKED, nobody may delete from mutants.out.
# Gremlins also logs the git settings it finds in its environment and writes a JSON report in Gremlins' shape, with BAD_REPORT set not JSON.
# Gremlins also logs the git settings it finds in its environment and writes a JSON report in Gremlins' shape. With REPORT set, it
# writes that text in its place, or with REPORT=none, no report, as Gremlins does where it finds no mutant.
FAKE_TOOLS = r"""import json, os, pathlib, sys
log, code, args = pathlib.Path(sys.argv[1]), int(sys.argv[2]), sys.argv[3:]
with log.open("a") as written:
@@ -86,8 +87,9 @@ with log.open("a") as written:
written.write("".join(f"{name}={os.environ[name]}\n" for name in git_config))
killed, skipped, uncovered, lived = (dict(type=kind, status=status, line=line, column=column) for kind, status, line, column in (("ARITHMETIC_BASE", "KILLED", 4, 11), ("CONDITIONALS_BOUNDARY", "SKIPPED", 8, 11), ("CONDITIONALS_BOUNDARY", "NOT COVERED", 12, 11), ("ARITHMETIC_BASE", "LIVED", 4, 15)))
files = [dict(file_name="calc.go", mutations=[killed, skipped, uncovered, lived]), dict(file_name="sub/other.go", mutations=[dict(type="INVERT_NEGATIVES", status="LIVED", line=2, column=9)])]
report = "not json" if os.environ.get("BAD_REPORT") else json.dumps(dict(go_module="x", files=files))
pathlib.Path(args[args.index("--output") + 1]).write_text(report)
report = os.environ.get("REPORT") or json.dumps(dict(go_module="x", files=files))
if report != "none":
pathlib.Path(args[args.index("--output") + 1]).write_text(report)
print(f"{args[0]} reports")
sys.exit(code)
"""
@@ -424,32 +426,36 @@ class MutateChangesRuns(unittest.TestCase):
self.change_go()
fake = f"python3 {self.temp / 'fake_tools.py'} {self.log} 0"
ran = self.run_tool("--base", "HEAD", "--jobs=3", "--prefix", fake, cwd=self.checkout / "pkg")
report = ["gremlins: 3 mutants not killed.", "calc.go:4:15: LIVED: ARITHMETIC_BASE", "calc.go:12:11: NOT COVERED: CONDITIONALS_BOUNDARY", "sub/other.go:2:9: LIVED: INVERT_NEGATIVES"]
report = ["gremlins: 4 mutants on changed lines, 3 not killed.", "calc.go:4:15: LIVED: ARITHMETIC_BASE", "calc.go:12:11: NOT COVERED: CONDITIONALS_BOUNDARY", "sub/other.go:2:9: LIVED: INVERT_NEGATIVES"]
self.assertEqual((ran.returncode, ran.stdout.splitlines()[:-2], ran.stderr), (0, report, ""))
self.assertEqual(ran.stdout.splitlines()[-2], "Not mutated, as test files: calc_test.go.")
folder, command, config = self.log.read_text().split("\n", 2)
self.assertEqual(folder, str(self.checkout.resolve()))
base = self.git("rev-parse", "HEAD").strip()
self.assertRegex(command, rf"^gremlins unleash --diff {base} --output (\.mutate-changes-\w+)/report\.json --workers 3$")
# Gremlins mutates one run of lines per hunk, so the hunks hold the changed lines and no lines around them.
self.assertEqual(config, "GIT_CONFIG_COUNT=1\nGIT_CONFIG_KEY_0=diff.context\nGIT_CONFIG_VALUE_0=0\n")
keys = ["diff.context", "diff.noprefix", "diff.mnemonicPrefix", "diff.srcPrefix", "diff.dstPrefix", "color.diff"]
values = ["0", "false", "false", "a/", "b/", "never"]
self.assertEqual(config, "GIT_CONFIG_COUNT=6\n" + "".join(f"GIT_CONFIG_KEY_{n}={key}\n" for n, key in enumerate(keys)) + "".join(f"GIT_CONFIG_VALUE_{n}={value}\n" for n, value in enumerate(values)))
self.assertEqual(sorted(path.name for path in self.checkout.iterdir()), [".git", "README.md", "calc.go", "calc_test.go", "pkg"])
def test_gremlins_git_settings_come_after_those_the_environment_holds(self):
def test_a_gremlins_run_that_finds_no_mutant_reports_none(self):
self.change_go()
env = {"GIT_CONFIG_COUNT": "1", "GIT_CONFIG_KEY_0": "safe.directory", "GIT_CONFIG_VALUE_0": "*"}
self.run_tool("--base", "HEAD", "--prefix", f"python3 {self.temp / 'fake_tools.py'} {self.log} 0", env=env)
config = self.log.read_text().split("\n", 2)[2]
self.assertEqual(config, "GIT_CONFIG_COUNT=2\nGIT_CONFIG_KEY_0=safe.directory\nGIT_CONFIG_KEY_1=diff.context\nGIT_CONFIG_VALUE_0=*\nGIT_CONFIG_VALUE_1=0\n")
for report in ("none", '{"files": null}', '{"files": [{"file_name": "calc.go", "mutations": null}]}'):
with self.subTest(report):
ran = self.run_tool("--base", "HEAD", "--prefix", f"python3 {self.temp / 'fake_tools.py'} {self.log} 0", env={"REPORT": report})
self.assertEqual((ran.returncode, ran.stdout.splitlines()[0], ran.stderr), (0, "gremlins: 0 mutants on changed lines, 0 not killed.", ""))
def test_a_gremlins_run_that_fails_or_leaves_no_report_fails_the_run(self):
def test_a_gremlins_run_that_fails_or_leaves_a_report_it_cannot_read_fails_the_run(self):
self.change_go()
fake = f"python3 {self.temp / 'fake_tools.py'} {self.log}"
ran = self.run_tool("--base", "HEAD", "--prefix", f"{fake} 1")
self.assertEqual((ran.returncode, ran.stderr), (1, "gremlins exited 1:\ngremlins reports\n"))
ran = self.run_tool("--base", "HEAD", "--prefix", f"{fake} 0", env={"BAD_REPORT": "1"})
self.assertEqual(ran.returncode, 1)
self.assertRegex(ran.stderr, rf"^could not read Gremlins' report in {re.escape(str(self.checkout.resolve()))}/\.mutate-changes-\w+: ")
# Gremlins also exits nonzero on a mutation score below the project's threshold, where its output lists what survived.
ran = self.run_tool("--base", "HEAD", "--prefix", f"{fake} 10")
self.assertEqual((ran.returncode, ran.stderr), (1, "Gremlins exited 10:\ngremlins reports\n"))
for report in ("not json", "[]", '{"files": 3}'):
with self.subTest(report):
ran = self.run_tool("--base", "HEAD", "--prefix", f"{fake} 0", env={"REPORT": report})
self.assertEqual(ran.returncode, 1)
self.assertRegex(ran.stderr, rf"^could not read Gremlins' report in {re.escape(str(self.checkout.resolve()))}/\.mutate-changes-\w+: ")
ran = self.run_tool("--base", "HEAD", env={"PATH": str(self.path_with_git())})
self.assertEqual(ran.returncode, 1)
self.assertRegex(ran.stderr, "^gremlins unleash .* could not start: ")