From ff0127f2e610ab4d1a6c31b9e1604086be17d2b3 Mon Sep 17 00:00:00 2001 From: mccalpink <81862107+mccalpink@users.noreply.github.com> Date: Fri, 20 Nov 2026 07:41:31 +0000 Subject: [PATCH] Abort when the forced fail test hits a pytest internal error or collects no tests The forced fail control only rejected exit code 0, so a pytest INTERNALERROR (3) or a run without collected tests (5) was accepted as a successful control and mutation testing continued. Both now abort the run. Exit codes 1 (tests failed) and 2 (collection interrupted by the forced fail) are still accepted. --- src/mutmut/__main__.py | 14 +++++++++++++- tests/mutation/test_mutation.py | 34 +++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/mutmut/__main__.py b/src/mutmut/__main__.py index 0254aef6..819aeb81 100644 --- a/src/mutmut/__main__.py +++ b/src/mutmut/__main__.py @@ -238,14 +238,26 @@ def write_all_mutants_to_file(*, out: TextIOBase, source: str, filename: Path) - return mutated_file +PYTEST_EXIT_INTERNAL_ERROR = 3 +PYTEST_EXIT_NO_TESTS_COLLECTED = 5 + + def run_forced_fail_test(runner: MutantRunner) -> None: set_mutant_under_test("fail") with CatchOutput(spinner_title="Running forced fail test") as catcher: try: - if runner.run_forced_fail() == 0: + exit_code = runner.run_forced_fail() + if exit_code == 0: catcher.dump_output() print("FAILED: Unable to force test failures") raise SystemExit(1) + if exit_code in (PYTEST_EXIT_INTERNAL_ERROR, PYTEST_EXIT_NO_TESTS_COLLECTED): + # No test could run, so the control proves nothing. Exit codes 1 (tests failed) and + # 2 (interrupted) are accepted: with the forced fail enabled, a test module that calls + # mutated code while it is imported fails collection, which pytest reports as code 2. + catcher.dump_output() + print(f"FAILED: Forced fail test could not run the tests (exit code {exit_code})") + raise SystemExit(1) except MutmutProgrammaticFailException: pass set_mutant_under_test("") diff --git a/tests/mutation/test_mutation.py b/tests/mutation/test_mutation.py index b7284ca1..be8f39dc 100644 --- a/tests/mutation/test_mutation.py +++ b/tests/mutation/test_mutation.py @@ -1239,6 +1239,40 @@ def test_run_forced_fail_test_with_all_tests_passing(_start, _stop, _dump_output assert "FAILED: Unable to force test failures" in out +# Negate the effects of CatchOutput because it does not play nicely with capfd in GitHub Actions +@pytest.mark.parametrize("exit_code", [3, 5]) +@patch.object(CatchOutput, "dump_output") +@patch.object(CatchOutput, "stop") +@patch.object(CatchOutput, "start") +def test_run_forced_fail_test_aborts_when_no_tests_could_run(_start, _stop, _dump_output, exit_code, capfd): + # pytest exit codes 3 (internal error) and 5 (no tests collected) mean that no test ran, + # so the forced fail test must not be accepted. + runner = _mocked_runner_run_forced_failed(return_value=exit_code) + + with pytest.raises(SystemExit) as error: + run_forced_fail_test(runner) + + assert error.value.code == 1 + out, _ = capfd.readouterr() + assert f"FAILED: Forced fail test could not run the tests (exit code {exit_code})" in out + + +# Negate the effects of CatchOutput because it does not play nicely with capfd in GitHub Actions +@patch.object(CatchOutput, "dump_output") +@patch.object(CatchOutput, "stop") +@patch.object(CatchOutput, "start") +def test_run_forced_fail_test_accepts_interrupted_collection(_start, _stop, _dump_output, capfd): + # With the forced fail enabled, a test module that calls mutated code at import time + # (e.g. in `parametrize`) fails collection, which pytest reports as exit code 2. + runner = _mocked_runner_run_forced_failed(return_value=2) + + run_forced_fail_test(runner) + + out, _ = capfd.readouterr() + assert "done" in out + assert not os.environ["MUTANT_UNDER_TEST"] + + def _mocked_runner_run_forced_failed(return_value=None, side_effect=None): runner = Mock() runner.run_forced_fail = Mock(return_value=return_value, side_effect=side_effect)