Test the planters' and mutmut's report on the lines the mutation run left alive
CI / tests (pull_request) Successful in 3m53s

This commit is contained in:
2026-11-23 21:58:35 +01:00
parent 8bab7b3a6f
commit fc9749aa0a
3 changed files with 64 additions and 8 deletions
+33 -2
View File
@@ -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):