Leave out a failing test file by its absolute path, since pytest 9.2 under mutmut 3.8.1 reports nodeids as mutants/<path>

This commit is contained in:
2026-11-11 19:04:24 +01:00
parent 25967807ff
commit e99eef43b9
3 changed files with 28 additions and 17 deletions
+11 -4
View File
@@ -1,6 +1,7 @@
"""The setup.cfg that has mutmut mutate a branch's changed files, and the tests left out because they fail before any mutant."""
import configparser
import io
import os
import re
from failure import Failure
@@ -44,8 +45,14 @@ def listed(items: list[str]) -> str:
def left_out(output: str) -> list[str]:
"""The pytest options that leave out each test a failed run's short summary reports: --deselect for a test, --ignore for a file that failed to collect."""
"""The nodeid of each test, or test file that failed to collect, that a failed run's short summary reports."""
summary = SUMMARY.split(output)
if len(summary) < 2:
return []
return [f"--deselect={nodeid}" if "::" in nodeid else f"--ignore={nodeid}" for nodeid in REPORTED.findall(summary[-1])]
return REPORTED.findall(summary[-1]) if len(summary) > 1 else []
def leaving_out(nodeids: list[str], copy: str) -> list[str]:
"""The pytest options that leave out each nodeid: --deselect for a test, --ignore for a file.
mutmut 3.8.1 runs pytest in copy's mutants/ folder, yet pytest 9.2 reports each nodeid from copy, as mutants/<path>. --deselect takes the
nodeid as reported, and --ignore an absolute path, built the same way whether or not the nodeid starts with mutants/."""
return [f"--deselect={nodeid}" if "::" in nodeid else f"--ignore={os.path.join(copy, 'mutants', nodeid.removeprefix('mutants/'))}" for nodeid in nodeids]
+4 -4
View File
@@ -15,7 +15,7 @@ from mutate_changes.tool_process import failed, Ran, run_tool
from failure import Failure
from mutate_changes.mutmut_mutants import function_keys, mutant_globs, mutated_line, named, statuses
from git_cli.git_repo import git
from mutate_changes.mutmut_setup import left_out, mutmut_config
from mutate_changes.mutmut_setup import leaving_out, left_out, mutmut_config
CAUGHT = frozenset({"caught by type check", "killed"})
CONFIGS = ("pyproject.toml", "setup.cfg")
@@ -140,17 +140,17 @@ def mutate_copy(copy: str, folder: Folder, argv: list[str], prefix: str | None)
return results if isinstance(results, Failure) else failed(f"mutmut results in {folder.label}", results)
shown, counted = not_killed(folder, statuses(results.output), copy)
unreached = " No test reaches them." if NO_TEST in output else ""
left_files = sorted({option.partition("=")[2].partition("::")[0] for option in left})
left_files = sorted({nodeid.partition("::")[0].removeprefix("mutants/") for nodeid in left})
left_note = f" Left out tests in {', '.join(left_files)} that fail without a mutant." if left else ""
return [f"mutmut in {folder.label}: {counted} mutants on changed lines, {len(shown)} not killed.{unreached}{left_note}", *shown]
def run_until_stats_pass(copy: str, folder: Folder, argv: list[str], prefix: str | None) -> tuple[str, list[str]] | Failure:
"""mutmut's output, and the options that left out each test that fails before any mutant: mutmut runs no mutant while one fails."""
"""mutmut's output, and the nodeid of each test it left out because the test fails before any mutant: mutmut runs no mutant while one fails."""
ran = configured_run(copy, folder, LIST_FAILURES, argv, prefix)
left: list[str] = []
if isinstance(ran, Ran) and STATS_FAILED in ran.output and (left := left_out(ran.output)):
ran = configured_run(copy, folder, left, argv, prefix)
ran = configured_run(copy, folder, leaving_out(left, copy), argv, prefix)
if isinstance(ran, Failure):
return ran
if ran.code and NO_MUTANT not in ran.output and NO_TEST not in ran.output:
+13 -9
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 left_out, mutmut_config
from mutate_changes.mutmut_setup import leaving_out, 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"
@@ -95,13 +95,17 @@ class MutmutSetup(unittest.TestCase):
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 repo_checks/test_repo_checks.py::Repo::test_a - AssertionError: x - y
ERROR larv_review/test_larv_review.py - FileNotFoundError: SKILL.md
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 a/test_b.py::T::test_c
2 failed, 1 error in 3.10s
2 failed, 2 errors in 3.10s
failed to collect stats. runner returned 1
""")
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"])
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"])
def test_left_out_is_empty_without_a_summary(self):
self.assertEqual(left_out("failed to collect stats. runner returned 1\n"), [])
@@ -141,15 +145,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 pathlib, sys
FAKE_MUTMUT = r"""import os, 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 "--deselect=test_calc.py::Calc::test_slow" not in config:
print("==== short test summary info ====\nFAILED test_calc.py::Calc::test_slow - boom\nfailed to collect stats. runner returned 1")
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")
sys.exit(1)
else:
pathlib.Path("mutants").mkdir(exist_ok=True)
@@ -203,7 +207,7 @@ class MutateChangesTool(unittest.TestCase):
def test_leaves_out_the_tests_that_fail_before_any_mutant_and_names_them(self):
ran = self.run_tool(*self.faked("fails-once"))
self.assertEqual(ran.returncode, 0, ran.stderr)
self.assertEqual(ran.stdout.splitlines()[:2], ["mutmut in pkg: 2 mutants on changed lines, 1 not killed. Left out tests in test_calc.py that fail without a mutant.", "pkg/calc.py:6: survived: return n / 3"])
self.assertEqual(ran.stdout.splitlines()[:2], ["mutmut in pkg: 2 mutants on changed lines, 1 not killed. Left out tests in test_calc.py, test_io.py that fail without a mutant.", "pkg/calc.py:6: survived: return n / 3"])
first, second = self.log.read_text().split("---\n")[:2]
self.assertIn("--maxfail=0", first)
self.assertNotIn("--maxfail=0", second)