Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/mutmut/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
Expand Down
34 changes: 34 additions & 0 deletions tests/mutation/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down