From 63369d5944ce5e9921ef9e204e02d996e3400b2d Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Wed, 11 Nov 2026 20:31:28 +0100 Subject: [PATCH] Drop mutate-changes' --no-renames, which a diff limited to the changed paths makes moot, and test the reason each of its failures gives --- bin/mutate_changes/in_diff_tools.py | 2 +- bin/mutate_changes/main.py | 4 +- .../test_mutate_changes_runs.py | 65 ++++++++++++++++--- 3 files changed, 60 insertions(+), 11 deletions(-) diff --git a/bin/mutate_changes/in_diff_tools.py b/bin/mutate_changes/in_diff_tools.py index 013e9b0a..c7166ed4 100644 --- a/bin/mutate_changes/in_diff_tools.py +++ b/bin/mutate_changes/in_diff_tools.py @@ -16,7 +16,7 @@ CARGO_OUTCOMES = ("caught", "missed", "timeout", "unviable") def run_cargo_mutants(top: str, base: str, paths: list[str], prefix: str | None, jobs: int) -> list[str] | Failure: """A line with cargo-mutants' mutant count, then a line per mutant it missed or that timed out, as `::: : `.""" - diff = git("diff", "--no-color", "--no-ext-diff", "--no-textconv", "--no-renames", base, "--", *paths, cwd=top) + diff = git("diff", "--no-color", "--no-ext-diff", "--no-textconv", base, "--", *paths, cwd=top) if isinstance(diff, Failure): return diff try: diff --git a/bin/mutate_changes/main.py b/bin/mutate_changes/main.py index aee424b3..da9f6481 100644 --- a/bin/mutate_changes/main.py +++ b/bin/mutate_changes/main.py @@ -111,14 +111,14 @@ def mutate(given: dict[str, str]) -> list[str] | Failure: def changed_by_tool(checkout: str, merge_base: str) -> dict[str, dict[str, frozenset[int]]] | Failure: """Each tool's files that the working tree changes since merge_base, by path from the checkout's top, with their changed lines.""" - names = git("diff", "--name-only", "-z", "--diff-filter=d", "--no-renames", merge_base, cwd=checkout) + names = git("diff", "--name-only", "-z", "--diff-filter=d", merge_base, cwd=checkout) if isinstance(names, Failure): return names changed: dict[str, dict[str, frozenset[int]]] = {} for path in filter(None, names.split("\0")): if (tool := tool_for(path)) is None: continue - diff = git("diff", "-U0", "--no-color", "--no-ext-diff", "--no-textconv", "--no-renames", merge_base, "--", path, cwd=checkout, errors="replace") + diff = git("diff", "-U0", "--no-color", "--no-ext-diff", "--no-textconv", merge_base, "--", path, cwd=checkout, errors="replace") if isinstance(diff, Failure): return diff if lines := added_lines(diff): diff --git a/bin/mutate_changes/test_mutate_changes_runs.py b/bin/mutate_changes/test_mutate_changes_runs.py index 3c93392a..b3ad13e1 100644 --- a/bin/mutate_changes/test_mutate_changes_runs.py +++ b/bin/mutate_changes/test_mutate_changes_runs.py @@ -30,7 +30,8 @@ if args[:2] == ["mutmut", "results"]: if mode == "results-fail" or args[2:] != ["--all", "true"]: sys.exit("mutmut results broke") double, triple_1, triple_2 = ("no tests",) * 3 if mode == "no-test" else ("survived", "survived", "killed") - print(f" other.x_f__mutmut_1: survived\n calc.x_double__mutmut_1: {double}\n calc.x_triple__mutmut_1: {triple_1}\n calc.x_triple__mutmut_2: {triple_2}") + # The branch doesn't change aside.py, so mutate-changes skips its mutant, which comes first in name order. + print(f" aside.x_f__mutmut_1: survived\n calc.x_double__mutmut_1: {double}\n calc.x_triple__mutmut_1: {triple_1}\n calc.x_triple__mutmut_2: {triple_2}") elif mode == "fails-once" and not ("--deselect=test_calc.py::test_slow[n=1]" in config and "--ignore=test_io.py" in config): print("==== short test summary info ====\nFAILED test_calc.py::test_slow[n=1] - boom\nERROR test_io.py - FileNotFoundError\nfailed to collect stats. runner returned 1") sys.exit(1) @@ -45,7 +46,7 @@ else: """ # cargo-mutants, 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. +# unviable mutant does; with CAUGHT_UNREADABLE set, nobody may read caught.txt. FAKE_TOOLS = r"""import os, pathlib, sys log, code, args = pathlib.Path(sys.argv[1]), int(sys.argv[2]), sys.argv[3:] with log.open("a") as written: @@ -55,8 +56,10 @@ with log.open("a") as written: outcomes = pathlib.Path(args[args.index("--output") + 1], "mutants.out") outcomes.mkdir() (outcomes / "caught.txt").write_bytes(b"src/lib.rs:2:7: replace + with - in add, \xff not being UTF-8\n") - (outcomes / "missed.txt").write_text("src/lib.rs:2:5: replace add -> i32 with 0\n") + (outcomes / "missed.txt").write_text("src/lib.rs:2:5: replace add(a: i32, b: i32) -> i32 with 0\n") (outcomes / "timeout.txt").write_text("src/lib.rs:2:7: replace + with * in add\n") + if os.environ.get("CAUGHT_UNREADABLE"): + (outcomes / "caught.txt").chmod(0) print(f"{args[0]} reports") sys.exit(code) """ @@ -192,6 +195,39 @@ class MutateChangesRuns(unittest.TestCase): ran = self.run_tool(*self.faked("passes")) self.assertEqual((ran.returncode, ran.stdout.splitlines()[:2]), (0, PASSED), ran.stderr) + def test_a_changed_file_or_setting_it_cannot_read_fails_the_run(self): + pkg = self.checkout.resolve() / "pkg" + for name, text, reason in ( + ("calc.py", "def double(n:\n", f"could not read {pkg}/calc.py: "), + ("pyproject.toml", "[tool\n", f"could not read the mutmut settings in {pkg}: "), + ("setup.cfg", "[mutmut\n", "pkg: could not read setup.cfg: "), + ): + with self.subTest(name): + (pkg / name).write_text(text) + ran = self.run_tool(*self.faked("passes")) + self.assertEqual(ran.returncode, 1) + self.assertRegex(ran.stderr, f"^{re.escape(reason)}") + self.git("checkout", "HEAD", "--", "pkg") + (pkg / "pyproject.toml").unlink(missing_ok=True) + + def test_a_scratch_folder_it_cannot_make_fails_the_run(self): + top = self.checkout.resolve() + self.addCleanup(top.chmod, 0o755) + failed = f"^could not make a scratch folder in {re.escape(str(top))}: " + top.chmod(0o555) + self.assertRegex(self.run_tool(*self.faked("passes")).stderr, failed) + top.chmod(0o755) + self.change_rust_php_and_typescript() + top.chmod(0o555) + self.assertRegex(self.run_tool("--base", "HEAD").stderr, failed) + + def test_a_file_it_cannot_copy_fails_the_run(self): + (self.checkout / "pkg" / "fixture.txt").chmod(0) + ran = self.run_tool(*self.faked("passes")) + top = re.escape(str(self.checkout.resolve())) + self.assertRegex(ran.stderr, rf"^could not set mutmut up in {top}/\.mutate-changes-\w+, a copy of {top}/pkg: ") + self.assertEqual(sorted(path.name for path in self.checkout.iterdir()), [".git", "README.md", "pkg"]) + def test_a_tool_that_fails_fails_the_run_with_the_end_of_its_output(self): ran = self.run_tool("--prefix", "seq 45; echo broken >&2; printf '\\377'; false") self.assertEqual((ran.returncode, ran.stderr), (1, "\n".join(["mutmut run in pkg exited 1:", *map(str, range(8, 46)), "broken", "�"]) + "\n")) @@ -219,7 +255,7 @@ class MutateChangesRuns(unittest.TestCase): def test_rust_php_and_typescript_go_to_cargo_mutants_infection_and_stryker_at_the_checkouts_top(self): self.change_rust_php_and_typescript() ran = self.run_tool("--base", "HEAD", "--jobs=3", "--prefix", f"python3 {self.temp / 'fake_tools.py'} {self.log} 2", cwd=self.checkout / "src") - report = ["cargo-mutants: 3 mutants on changed lines, 2 not caught.", "src/lib.rs:2:5: missed: replace add -> i32 with 0", "src/lib.rs:2:7: timeout: replace + with * in add"] + report = ["cargo-mutants: 3 mutants on changed lines, 2 not caught.", "src/lib.rs:2:5: missed: replace add(a: i32, b: i32) -> i32 with 0", "src/lib.rs:2:7: timeout: replace + with * in add"] self.assertEqual((ran.returncode, ran.stdout.splitlines()[:-1]), (0, [*report, "Infection exited 2; its report:", "vendor/bin/infection reports", "Stryker exited 2; its report:", "npx reports"]), ran.stderr) cargo, infection, stryker = self.log.read_text().split(f"{self.checkout.resolve()}\n")[1:] command, diff = cargo.split("\n", 1) @@ -247,6 +283,13 @@ class MutateChangesRuns(unittest.TestCase): ran = self.run_tool("--base", "HEAD", "--prefix", f"python3 {self.temp / 'fake_tools.py'} {self.log} {code}") self.assertEqual((ran.returncode, ran.stderr), (1, f"cargo mutants exited {code}:\ncargo reports\n") if failed else (0, "")) + def test_cargo_mutants_outcomes_it_cannot_read_fail_the_run(self): + self.change_rust_php_and_typescript() + ran = self.run_tool("--base", "HEAD", "--prefix", f"python3 {self.temp / 'fake_tools.py'} {self.log} 0", env={"CAUGHT_UNREADABLE": "1"}) + self.assertEqual(ran.returncode, 1) + self.assertRegex(ran.stderr, rf"^could not read cargo-mutants' 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", "pkg", "src"]) + def test_a_branch_that_changes_no_mutated_language_says_so(self): ran = self.run_tool(*self.faked("passes"), "--base", "HEAD") self.assertEqual((ran.returncode, ran.stdout), (0, "No line the branch changes is in a language mutate-changes mutates.\n")) @@ -258,12 +301,18 @@ 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_unknown_arguments_fail(self): - for args in (["--jobs", "0"], ["--jobs", "x"], ["--jobs"], ["--colour"], ["--base", "a", "--base", "b"]): + def test_unknown_arguments_fail_saying_why(self): + usage = "usage: mutate-changes [--base ] [--prefix ] [--jobs ]" + for args, reason in ( + (["--jobs", "0"], "--jobs takes a whole number from 1, not 0"), + (["--jobs", "x"], "--jobs takes a whole number from 1, not x"), + (["--jobs"], "--jobs needs a value"), + (["--colour"], "unknown or repeated argument --colour"), + (["--base", "a", "--base", "b"], "unknown or repeated argument --base"), + ): with self.subTest(args): ran = self.run_tool(*args) - self.assertEqual(ran.returncode, 1) - self.assertIn("usage: mutate-changes", ran.stderr) + self.assertEqual((ran.returncode, ran.stderr), (1, f"{reason}; {usage}\n")) def test_the_launcher_runs_the_tool(self): # Run from the launcher's folder: there, under mutmut, the child finds the setup.cfg that mutmut's mutated code reads.