Have mutmut copy each entry of the folder rather than '.', which copied mutants/ into itself and collected the tests twice

This commit is contained in:
2026-11-11 19:14:49 +01:00
parent e99eef43b9
commit 79e8ce1368
3 changed files with 42 additions and 46 deletions
+14 -17
View File
@@ -12,7 +12,7 @@ from mutate_changes.changed_lines import added_lines, runs, tool_for
from failure import Failure
from mutate_changes.mutmut_mutants import function_keys, mutant_globs, mutated_line, named, statuses
from test_env import isolated_env
from mutate_changes.mutmut_setup import leaving_out, left_out, mutmut_config
from mutate_changes.mutmut_setup import left_out, mutmut_config
TOOL = pathlib.Path(__file__).resolve().parents[1] / "mutate-changes"
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"
@@ -73,14 +73,14 @@ class ChangedLines(unittest.TestCase):
class MutmutSetup(unittest.TestCase):
def test_the_config_mutates_the_sources_and_copies_the_whole_folder(self):
text = mutmut_config("", ["x/a.py", "b.py"], [])
def test_the_config_mutates_the_sources_and_copies_each_entry_of_the_folder(self):
text = mutmut_config("", ["x/a.py", "b.py"], ["b.py", "x"], [])
self.assertEqual(read_as_mutmut(text, "source_paths"), ["x/a.py", "b.py"])
self.assertEqual(read_as_mutmut(text, "also_copy"), ["."])
self.assertEqual(read_as_mutmut(text, "also_copy"), ["b.py", "x"])
def test_the_config_keeps_the_projects_settings_and_appends_the_tests_left_out(self):
existing = "[tool:pytest]\naddopts = -q\n\n[mutmut]\npytest_add_cli_args = -k\n not Slow\ndebug = true\n"
text = mutmut_config(existing, ["a.py"], ["--deselect=t/test_a.py::T::test_b[50%]"])
text = mutmut_config(existing, ["a.py"], ["a.py", "t"], ["--deselect=t/test_a.py::T::test_b[50%]"])
self.assertEqual(read_as_mutmut(text, "pytest_add_cli_args"), ["-k", "not Slow", "--deselect=t/test_a.py::T::test_b[50%]"])
self.assertEqual(read_as_mutmut(text, "debug"), ["true"])
config = configparser.ConfigParser()
@@ -88,24 +88,20 @@ class MutmutSetup(unittest.TestCase):
self.assertEqual(config.get("tool:pytest", "addopts"), "-q")
def test_a_setup_cfg_that_cannot_be_read_fails(self):
self.assertIsInstance(mutmut_config("[mutmut]\na = 1\na = 2\n", ["a.py"], []), Failure)
self.assertIsInstance(mutmut_config("[mutmut]\na = 1\na = 2\n", ["a.py"], ["a.py"], []), Failure)
def test_left_out_names_each_test_the_summary_reports_failing(self):
output = textwrap.dedent("""\
FAILED fake.py::t - this line is a test's own output, above the summary
ERROR root:x.py:3 a log line
=========================== short test summary info ============================
FAILED mutants/repo_checks/test_repo_checks.py::Repo::test_a - AssertionError: x - y
ERROR mutants/larv_review/test_larv_review.py - FileNotFoundError: SKILL.md
ERROR test_io.py
FAILED repo_checks/test_repo_checks.py::Repo::test_a - AssertionError: x - y
ERROR larv_review/test_larv_review.py - FileNotFoundError: SKILL.md
FAILED a/test_b.py::T::test_c
2 failed, 2 errors in 3.10s
2 failed, 1 error in 3.10s
failed to collect stats. runner returned 1
""")
self.assertEqual(left_out(output), ["mutants/repo_checks/test_repo_checks.py::Repo::test_a", "mutants/larv_review/test_larv_review.py", "test_io.py", "a/test_b.py::T::test_c"])
def test_a_test_is_deselected_by_its_nodeid_and_a_file_ignored_by_its_path_in_mutants(self):
self.assertEqual(leaving_out(["mutants/t/test_a.py::T::test_b", "mutants/t/test_c.py", "t/test_d.py"], "/copy"), ["--deselect=mutants/t/test_a.py::T::test_b", "--ignore=/copy/mutants/t/test_c.py", "--ignore=/copy/mutants/t/test_d.py"])
self.assertEqual(left_out(output), ["--deselect=repo_checks/test_repo_checks.py::Repo::test_a", "--ignore=larv_review/test_larv_review.py", "--deselect=a/test_b.py::T::test_c"])
def test_left_out_is_empty_without_a_summary(self):
self.assertEqual(left_out("failed to collect stats. runner returned 1\n"), [])
@@ -145,15 +141,15 @@ class MutmutMutants(unittest.TestCase):
# Stands in for mutmut, run through --prefix as `python3 <this> <log> <mode> mutmut ...` in the scratch copy. It logs each call with
# the setup.cfg it finds there, then writes a mutants/ copy and prints results, both shaped like mutmut 3's.
FAKE_MUTMUT = r"""import os, pathlib, sys
FAKE_MUTMUT = r"""import pathlib, sys
log, mode, args = pathlib.Path(sys.argv[1]), sys.argv[2], sys.argv[3:]
config = pathlib.Path("setup.cfg").read_text()
with log.open("a") as written:
written.write(" ".join(args) + "\n" + config + "---\n")
if args[:2] == ["mutmut", "results"]:
print(" calc.x_triple__mutmut_1: survived\n calc.x_triple__mutmut_2: killed\n calc.x_double__mutmut_1: survived")
elif mode == "fails-once" and not ("--deselect=mutants/test_calc.py::Calc::test_slow" in config and f"--ignore={os.getcwd()}/mutants/test_io.py" in config):
print("==== short test summary info ====\nFAILED mutants/test_calc.py::Calc::test_slow - boom\nERROR mutants/test_io.py - FileNotFoundError\nfailed to collect stats. runner returned 1")
elif mode == "fails-once" and not ("--deselect=test_calc.py::Calc::test_slow" in config and "--ignore=test_io.py" in config):
print("==== short test summary info ====\nFAILED test_calc.py::Calc::test_slow - boom\nERROR test_io.py - FileNotFoundError\nfailed to collect stats. runner returned 1")
sys.exit(1)
else:
pathlib.Path("mutants").mkdir(exist_ok=True)
@@ -201,6 +197,7 @@ class MutateChangesTool(unittest.TestCase):
calls = self.log.read_text()
self.assertIn("mutmut run --max-children 2 calc.x_triple*\n", calls)
self.assertIn("source_paths = calc.py\n", calls)
self.assertIn("also_copy = calc.py\n\tsetup.cfg\n\ttest_calc.py\n", calls)
self.assertEqual((self.checkout / "pkg" / "setup.cfg").read_text(), "[mutmut]\n")
self.assertEqual(sorted(path.name for path in self.checkout.iterdir()), [".git", "pkg"])