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
+58 -2
View File
@@ -62,10 +62,11 @@ else:
print("Traceback (most recent call last):\n File \"/uv/site-packages/mutmut/__main__.py\", line 1050, in _run\n File \"/uv/site-packages/mutmut/workers/isolation.py\", line 889, in wait_for_result\n worker = pop(pid)\nKeyError: 974598")
sys.exit(1)
"""
# cargo-mutants, Infection and Stryker, which log the folder they run in and how they were called, and exit with the code the test
# 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.
FAKE_TOOLS = r"""import os, pathlib, sys
# 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.
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:
written.write(f"{os.getcwd()}\n{' '.join(args)}\n")
@@ -80,6 +81,13 @@ with log.open("a") as written:
(outcomes / "caught.txt").chmod(0)
if os.environ.get("OUTPUT_LOCKED"):
outcomes.chmod(0o555)
if args[0] == "gremlins":
git_config = (name for name in sorted(os.environ) if name.startswith(("GIT_CONFIG_COUNT", "GIT_CONFIG_KEY_", "GIT_CONFIG_VALUE_")))
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)
print(f"{args[0]} reports")
sys.exit(code)
"""
@@ -404,6 +412,49 @@ class MutateChangesRuns(unittest.TestCase):
self.assertRegex(ran.stderr, rf"^could not hand cargo-mutants the diff or read its outcomes in {re.escape(str(self.checkout.resolve()))}/\.mutate-changes-\w+: ")
self.assertEqual(sorted(path.name for path in self.checkout.iterdir()), [".git", "README.md", "package.json", "pkg", "src", "web"])
def change_go(self):
"""Commit a Go file and its test, then change both."""
(self.checkout / "calc.go").write_text("package calc\n\nfunc Add(a, b int) int {\n\treturn a + b\n}\n")
(self.checkout / "calc_test.go").write_text("package calc\n")
self.commit()
(self.checkout / "calc.go").write_text("package calc\n\nfunc Add(a, b int) int {\n\treturn a + b + 0\n}\n")
(self.checkout / "calc_test.go").write_text("package calc\n\nfunc helper() {}\n")
def test_go_goes_to_gremlins_at_the_top_and_prints_each_mutant_that_lived_or_is_not_covered(self):
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"]
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")
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):
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")
def test_a_gremlins_run_that_fails_or_leaves_no_report_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+: ")
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: ")
self.assertEqual(sorted(path.name for path in self.checkout.iterdir()), [".git", "README.md", "calc.go", "calc_test.go", "pkg"])
def test_a_branch_that_adds_no_line_a_tool_mutates_says_so_and_names_what_it_changes(self):
# A file the branch deletes is no longer there to mutate, so the report doesn't name it.
(self.checkout / "pkg" / "old.md").write_text("Gone.\n")
@@ -425,6 +476,11 @@ class MutateChangesRuns(unittest.TestCase):
ran = self.run_tool(flag)
self.assertEqual((ran.returncode, ran.stdout.splitlines()[0]), (0, "Mutation-test the lines a branch adds or changes, and print each mutant its tests don't kill."))
def test_help_shows_the_gremlins_prefix_that_mounts_gits_data_in_a_worktree(self):
text = self.run_tool("--help").stdout
self.assertIn("-v \"$common:$common\"", text)
self.assertIn("--git-common-dir", text)
def test_unknown_arguments_fail_saying_why(self):
usage = "usage: mutate-changes [--base <ref>] [--prefix <shell text>] [--jobs <n>]"
for args, reason in (