Kill the surviving mutants of the file-name import move and the mutmut crash line

This commit is contained in:
Lilleman
2026-11-20 20:04:03 +01:00
parent 91cf86e0cd
commit 9bd385d9cd
2 changed files with 36 additions and 24 deletions
+10 -11
View File
@@ -11,24 +11,23 @@ from mutate_changes.changed_lines import is_test
def imported_by_file_name(top: str, grouped: dict[str, dict[str, frozenset[int]]], configs: tuple[str, ...]) -> dict[str, dict[str, frozenset[int]]]:
"""grouped, with each file that a test beside it imports by its file name moved to its own folder, named from the checkout's top.
"""grouped, with each file of the checkout's top group that a test beside it imports by its file name moved to its own folder.
A folder that holds one of configs keeps its files: the settings there decide how its tests import them."""
regrouped: dict[str, dict[str, frozenset[int]]] = {}
for folder, files in grouped.items():
configured = any(os.path.isfile(os.path.join(top, folder, name)) for name in configs)
for path, lines in files.items():
own = posixpath.join(folder, posixpath.dirname(path))
moved = not configured and posixpath.basename(path)[:-3] in top_level_imports(os.path.join(top, own)) and not os.path.isfile(os.path.join(top, own, "__init__.py"))
target, name = (posixpath.normpath(own), posixpath.basename(path)) if moved else (folder, path)
regrouped.setdefault(target, {})[name] = lines
A file in a folder that holds one of configs stays: the settings there decide how its tests import it. So do all files where the top holds one."""
if "." not in grouped or any(os.path.isfile(os.path.join(top, name)) for name in configs):
return grouped
regrouped: dict[str, dict[str, frozenset[int]]] = {name: files for name, files in grouped.items() if name != "."}
for path, lines in grouped["."].items():
own = posixpath.dirname(path) or "."
moved = posixpath.basename(path)[:-3] in top_level_imports(os.path.join(top, own)) and not os.path.isfile(os.path.join(top, own, "__init__.py"))
regrouped.setdefault(own if moved else ".", {})[posixpath.basename(path) if moved else path] = lines
return regrouped
def top_level_imports(folder: str) -> set[str]:
"""The modules the test files in folder import by their first name. A test file that doesn't parse names none."""
names: set[str] = set()
for test in pathlib.Path(folder).glob("*.py"):
for test in sorted(pathlib.Path(folder).glob("*.py")):
if not is_test(test.name):
continue
try:
+26 -13
View File
@@ -92,32 +92,45 @@ class ChangedLines(unittest.TestCase):
def test_a_file_its_sibling_test_imports_by_file_name_moves_to_its_own_folder(self):
with tempfile.TemporaryDirectory() as top:
tests = {
"docker/scripts/test_run.py": "import unittest\nimport run_harness as rh\n",
"docker/scripts/test_other.py": "from other import thing\n",
"pkg/test_pkg.py": "from pkg.mod import x\n",
"docker/scripts/a_helper.py": "import not_a_test\n",
"docker/scripts/test_a_bad.py": "def (:\n",
"docker/scripts/test_b_run.py": "import unittest\nimport run_harness as rh\nimport dotted.sub\nfrom other import thing\nfrom dotted2.sub import z\nfrom .rel import y\n",
"docker/scripts/test_c_odd.py": "import \u00e9\n",
"pkg/test_pkg.py": "from pkg.mod import x\nimport mod\n",
"pkg/__init__.py": "",
"lib/test_lib.py": "import lib_tool\n",
"lib/__init__.py": "",
"broken/test_broken.py": "def (:\n",
"test_top.py": "import top_tool\n",
}
for path, text in tests.items():
pathlib.Path(top, path).parent.mkdir(parents=True, exist_ok=True)
pathlib.Path(top, path).write_text(text)
pathlib.Path(top, path).write_text(text, encoding="utf-8")
lines = frozenset({1})
grouped = {".": {name: lines for name in ("docker/scripts/run_harness.py", "docker/scripts/solo.py", "pkg/mod.py", "lib/lib_tool.py", "broken/broken.py")}}
names = ("docker/scripts/run_harness.py", "docker/scripts/dotted.py", "docker/scripts/other.py", "docker/scripts/dotted2.py", "docker/scripts/rel.py",
"docker/scripts/solo.py", "docker/scripts/not_a_test.py", "pkg/mod.py", "lib/lib_tool.py", "top_tool.py", "solo_top.py")
grouped = {".": {name: lines for name in names}, "web": {"app.py": lines}}
moved = ("run_harness.py", "dotted.py", "other.py", "dotted2.py")
self.assertEqual(imported_by_file_name(top, grouped, ("setup.cfg",)), {
".": {"docker/scripts/solo.py": lines, "pkg/mod.py": lines, "lib/lib_tool.py": lines, "broken/broken.py": lines},
"docker/scripts": {"run_harness.py": lines},
".": {"docker/scripts/rel.py": lines, "docker/scripts/solo.py": lines, "docker/scripts/not_a_test.py": lines, "pkg/mod.py": lines, "lib/lib_tool.py": lines, "top_tool.py": lines, "solo_top.py": lines},
"docker/scripts": {name: lines for name in moved},
"web": {"app.py": lines},
})
self.assertEqual(imported_by_file_name(top, {"web": {"app.py": lines}}, ("setup.cfg",)), {"web": {"app.py": lines}})
pathlib.Path(top, "setup.cfg").write_text("")
self.assertEqual(imported_by_file_name(top, grouped, ("setup.cfg",)), grouped)
def test_a_traceback_that_ends_inside_mutmut_is_a_crash_of_mutmut(self):
inside = 'Traceback (most recent call last):\n File "/a/mutmut/__main__.py", line 9, in run\n File "/a/mutmut/workers/isolation.py", line 889, in wait\nKeyError: 974598\n'
outside = 'Traceback (most recent call last):\n File "/repo/calc.py", line 3, in add\nKeyError: 1\n'
self.assertEqual(crash_in_mutmut(inside), "KeyError: 974598, in mutmut/workers/isolation.py line 889")
self.assertEqual(crash_in_mutmut("Running stats\n" + inside + "\n"), "KeyError: 974598, in mutmut/workers/isolation.py line 889")
self.assertEqual((crash_in_mutmut(outside), crash_in_mutmut("all fine"), crash_in_mutmut("")), (None, None, None))
def trace(*frames, error="KeyError: 974598"):
return "Traceback (most recent call last):\n" + "".join(f' File "{path}", line {number}, in f\n' for path, number in frames) + error + "\n"
inside = trace(("/a/mutmut/__main__.py", 9), ("/a/mutmut/workers/isolation.py", 889), ("/a/mutmut/workers/isolation.py", 12))
outside = trace(("/a/mutmut/__main__.py", 9), ("/repo/calc.py", 3), error="KeyError: 1")
place = "KeyError: 974598, in mutmut/workers/isolation.py line 12"
self.assertEqual((crash_in_mutmut(inside), crash_in_mutmut("Running stats\n" + inside + "\n")), (place, place))
self.assertEqual(crash_in_mutmut(outside + trace(("/a/mutmut/x.py", 1), error="E: 2")), "E: 2, in mutmut/x.py line 1")
self.assertEqual(crash_in_mutmut(trace(("/a/mutmut/x.py", 1), error="E: 2") + outside), None)
self.assertEqual(crash_in_mutmut(trace(("C:\\py\\mutmut\\workers\\x.py", 5))), "KeyError: 974598, in mutmut/workers/x.py line 5")
self.assertEqual(crash_in_mutmut(trace(("/a/mutmut/x.py", 1), error="").replace("Traceback (most recent call last):", "")), None)
self.assertEqual((crash_in_mutmut(outside), crash_in_mutmut("all fine"), crash_in_mutmut(""), crash_in_mutmut("Traceback (most recent call last):\nKeyError: 1")), (None,) * 4)
def test_not_mutated_names_the_files_no_tool_covers_then_the_test_files(self):
self.assertEqual(not_mutated(["README.md", "a.py", "b.go", "test_a.py", "web/a.test.ts"]), ["Not mutated, as no tool covers their language: README.md, b.go.", "Not mutated, as test files: test_a.py, web/a.test.ts."])