diff --git a/bin/mutate_changes/test_mutate_changes.py b/bin/mutate_changes/test_mutate_changes.py index 86b8b116..f214a5df 100644 --- a/bin/mutate_changes/test_mutate_changes.py +++ b/bin/mutate_changes/test_mutate_changes.py @@ -14,14 +14,14 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from mutate_changes.changed_lines import added_lines, by_folder, not_mutated, runs, tool_for -from mutate_changes.mutmut_tool import copy_files +from mutate_changes.mutmut_tool import copy_files, Ended, Folder, mutate_folder, NO_MUTANT, reported from mutate_changes.mutmut_mutants import crash_in_mutmut, function_keys, mutant_globs, mutated_lines, named, statuses from failure import Failure from mutate_changes.bare_imports import imported_by_file_name from test_env import isolated_env from mutate_changes.mutmut_setup import left_out, mutmut_config from mutate_changes import main -from mutate_changes.tool_process import run_tool, scratch_folder +from mutate_changes.tool_process import Ran, run_tool, scratch_folder from mutate_changes.in_diff_tools import with_plain_git_diff SOURCE = "def double(n):\n\treturn n * 2\n\n\nclass Pt:\n\tdef moved(self, by=1):\n\t\treturn self.x + by\n" @@ -279,6 +279,37 @@ class MutmutTool(unittest.TestCase): self.assertEqual(walked, ["a.py", "d", "d/b.py", "dirlink", "link"]) self.assertEqual((os.readlink(copy / "link"), os.readlink(copy / "dirlink")), ("a.py", "d")) + def folder_report(self, source: str, said: str) -> tuple[list[str], list[list[str]]]: + """What mutmut's run reports over every line of pkg/calc.py with that source, where mutmut prints `said`, and the commands that ran.""" + ran: list[list[str]] = [] + + def run(argv: list[str], cwd: str) -> Ran: + ran.append(argv) + pathlib.Path(cwd, "mutants").mkdir(exist_ok=True) + return Ran(0, said) + + with tempfile.TemporaryDirectory() as top: + pathlib.Path(top, "pkg").mkdir() + pathlib.Path(top, "pkg/calc.py").write_text(source) + ended = mutate_folder(top, "pkg", {"calc.py": frozenset(range(1, 9))}, ["calc.py"], run, 2) + self.assertIsInstance(ended, Ended) + return reported(ended), ran + + def test_a_folder_whose_lines_fall_in_no_function_says_so_and_runs_no_mutmut(self): + self.assertEqual(self.folder_report("LIMIT = 3\n", ""), (["mutmut in pkg: no changed line falls in a function or method, the only code mutmut mutates."], [])) + + def test_a_folder_whose_functions_have_no_mutant_says_so(self): + report, ran = self.folder_report("def double(n):\n\treturn n\n", NO_MUTANT) + self.assertEqual(report, ["mutmut in pkg: none of the functions the changed lines fall in has a mutant."]) + self.assertEqual([argv[:2] for argv in ran], [["mutmut", "run"]]) + + def test_a_mutant_not_killed_shows_the_first_line_it_puts_in_place_or_that_it_removes_lines(self): + source = "def both(n):\n\tn = n + 1\n\treturn n\n\n\ndef triple(n):\n\treturn (\n\t\tn * 3\n\t)\n" + copy = "def x_both__mutmut_orig(n):\n\tn = n + 1\n\treturn n\ndef x_both__mutmut_1(n):\n\treturn n\ndef x_triple__mutmut_orig(n):\n\treturn (\n\t\tn * 3\n\t)\ndef x_triple__mutmut_1(n):\n\treturn [\n\t\tn * 3\n\t]\n" + folder = Folder("pkg", {"calc.py": frozenset(range(1, 10))}, {"calc.py": source}, {"calc.py": ["x_both", "x_triple"]}, "") + ended = Ended(folder, {"calc.x_both__mutmut_1": "survived", "calc.x_triple__mutmut_1": "survived"}, {"calc.py": copy}) + self.assertEqual(reported(ended), ["mutmut in pkg: 2 mutants on changed lines, 2 not killed.", "pkg/calc.py:2: survived: lines removed", "pkg/calc.py:7: survived: return ["]) + class ToolProcess(unittest.TestCase): def test_git_lists_nothing_in_a_scratch_folder(self): diff --git a/bin/task_bench/test_go_bugs.py b/bin/task_bench/test_go_bugs.py index 8143b457..1ee49fa9 100644 --- a/bin/task_bench/test_go_bugs.py +++ b/bin/task_bench/test_go_bugs.py @@ -58,6 +58,9 @@ class ReappliedTest(unittest.TestCase): found = reapplied(in_diff_tools.Mutant("calc.go", 5, 12, "KILLED", "ARITHMETIC_BASE"), self.SOURCE.split(b"\n")) self.assertEqual(found, Mutant("calc.go", 5, "calc.go:5:12 ARITHMETIC_BASE", (b"\t\treturn a - b",), (b"\t\treturn a + b",))) + def test_the_first_column_is_1(self): + self.assertEqual(self.planted(1, 1, "INVERT_NEGATIVES", b"-x"), "+x") + def test_counts_the_column_in_bytes_as_go_does(self): self.assertEqual(self.planted(1, 15, "CONDITIONALS_NEGATION", 'x := "åäö" == y\n'.encode()), 'x := "åäö" != y') @@ -156,8 +159,8 @@ class KilledMutantsTest(unittest.TestCase): def test_keeps_the_killed_mutants_of_the_files_asked_for_whose_place_holds_the_mutators_token_as_the_line_each_changes(self): report = {"files": [ - {"file_name": "render/pick.go", "mutations": [{"line": 5, "column": 11, "status": "KILLED", "type": "ARITHMETIC_BASE"}, {"line": 4, "column": 1, "status": "KILLED", "type": "ARITHMETIC_BASE"}, {"line": 5, "column": 2, "status": "LIVED", "type": "INVERT_LOGICAL"}, {"line": 6, "column": 2, "status": "NOT COVERED", "type": "INVERT_LOGICAL"}]}, - {"file_name": "calc.go", "mutations": [{"line": 1, "column": 1, "status": "KILLED", "type": "ARITHMETIC_BASE"}]}, + {"file_name": "render/pick.go", "mutations": [{"line": 5, "column": 11, "status": "KILLED", "type": "ARITHMETIC_BASE"}, {"line": 4, "column": 1, "status": "KILLED", "type": "ARITHMETIC_BASE"}, {"line": 5, "column": 11, "status": "LIVED", "type": "ARITHMETIC_BASE"}, {"line": 6, "column": 2, "status": "NOT COVERED", "type": "INVERT_LOGICAL"}]}, + {"file_name": "calc.go", "mutations": [{"line": 5, "column": 11, "status": "KILLED", "type": "ARITHMETIC_BASE"}]}, ]} self.assertEqual(killed_mutants(str(self.top), ["render/pick.go"], self.gremlins(report), 3), [Mutant("render/pick.go", 5, "render/pick.go:5:11 ARITHMETIC_BASE", (b"\treturn a + b",), (b"\treturn a - b",))]) @@ -166,6 +169,12 @@ class KilledMutantsTest(unittest.TestCase): self.assertIsInstance(found, Failure) self.assertIn("gremlins said so", found.reason) + def test_a_mutated_file_that_cannot_be_read_is_a_failure_that_names_the_tree(self): + report = {"files": [{"file_name": "gone.go", "mutations": [{"line": 5, "column": 11, "status": "KILLED", "type": "ARITHMETIC_BASE"}]}]} + found = killed_mutants(str(self.top), ["gone.go"], self.gremlins(report), 3) + self.assertIsInstance(found, Failure) + self.assertIn(f"could not read the files Gremlins mutated in {self.top}", found.reason) + if __name__ == "__main__": unittest.main() diff --git a/bin/task_bench/test_python_bugs.py b/bin/task_bench/test_python_bugs.py index d23223c5..46d684d7 100644 --- a/bin/task_bench/test_python_bugs.py +++ b/bin/task_bench/test_python_bugs.py @@ -99,14 +99,16 @@ class KilledMutantsTest(unittest.TestCase): (self.top / path).parent.mkdir(parents=True, exist_ok=True) (self.top / path).write_text(text) self.ran: list[tuple[list[str], str, list[str]]] = [] + # Where the fake mutmut writes its copy of calc.py, from the folder it runs in. + self.copy = "pkg/calc.py" def mutmut(self, results: str = RESULTS, code: int = 0, said: str = ""): def run(argv: list[str], cwd: str) -> Ran: self.ran.append((argv, cwd, sorted(str(path.relative_to(cwd)) for path in pathlib.Path(cwd).rglob("*") if path.is_file()))) if argv[:2] == ["mutmut", "results"]: return Ran(0, results) - pathlib.Path(cwd, "mutants/pkg").mkdir(parents=True) - pathlib.Path(cwd, "mutants/pkg/calc.py").write_text(COPY) + pathlib.Path(cwd, "mutants", self.copy).parent.mkdir(parents=True) + pathlib.Path(cwd, "mutants", self.copy).write_text(COPY) return Ran(code, said) return run @@ -138,8 +140,22 @@ class KilledMutantsTest(unittest.TestCase): self.assertIsInstance(found, Failure) self.assertIn(reason, found.reason) - def test_a_file_that_is_gone_is_a_failure(self): - self.assertIsInstance(killed_mutants(str(self.top), ["bin/pkg/gone.py"], self.mutmut(), 3), Failure) + def test_a_file_that_is_gone_is_a_failure_that_names_the_tree(self): + found = killed_mutants(str(self.top), ["bin/pkg/gone.py"], self.mutmut(), 3) + self.assertIsInstance(found, Failure) + self.assertIn(f"could not read the files to mutate in {self.top}", found.reason) + + def test_mutates_a_function_on_the_files_first_line_and_one_on_its_last(self): + (self.top / "bin/pkg/ends.py").write_text("def first(n): return n + 1\n\n\ndef last(n): return n * 2") + killed_mutants(str(self.top), ["bin/pkg/ends.py"], self.mutmut(), 3) + self.assertEqual(self.ran[0][0][4:], ["pkg.ends.x_first*", "pkg.ends.x_last*"]) + + def test_a_file_in_the_folder_mutmut_runs_in_at_the_top_keeps_its_path(self): + (self.top / "setup.cfg").write_text("[tool:pytest]\n") + (self.top / "calc.py").write_text(CALC) + self.copy = "calc.py" + found = killed_mutants(str(self.top), ["calc.py"], self.mutmut(), 3) + self.assertEqual(found[0], Mutant("calc.py", 2, "calc.py:2 x_double__mutmut_1", (b"\treturn n * 2",), (b"\treturn n / 2",))) class TaskTextTest(unittest.TestCase):