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
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changelog
Unreleased
~~~~~~~~~~

* Re-test the cached mutants of every function that shares a test with a changed or deleted function. A test such as ``y = g(x); assert f(y) == expected`` kills the mutants of ``g`` through ``f``, which ``g`` never calls, so a change that weakens ``f`` could leave a stale "killed" verdict on ``g``

* Add a ``pytest_add_cli_args_clean_run`` config for pytest arguments of the clean run alone. The stats run and the run of each mutant leave them out, so the clean run of a large suite can spread over pytest-xdist workers with ``["-n", "auto"]``

* Add a ``pytest_add_cli_args_stats_run`` config for pytest arguments of the stats run alone. Under pytest-xdist each worker records which of its tests reach which function and returns them to mutmut, so the stats come out the same as a serial run's. Workers of both runs import the mutated code first, whatever ``sys.path`` pytest-xdist froze for them
Expand Down
4 changes: 3 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,9 @@ You can add and override pytest arguments:
Detecting dependency and config changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Between runs, mutmut only re-tests mutants in functions whose source changed.
Between runs, mutmut only re-tests mutants in functions whose source changed,
and the mutants of every function that shares a test with a changed or
deleted function, since that test can now pass or fail differently.
Changes outside your Python source — a dependency upgrade, a data file, a
config file — cannot be tied to a function, so they would otherwise be missed
and you would get cached results that no longer reflect reality.
Expand Down
27 changes: 27 additions & 0 deletions src/mutmut/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,31 @@ def should_reset(key: str, exit_code: int) -> bool:
print(f" Re-testing {count} mutant(s) affected by the test changes")


def _reset_results_for_function_changes() -> None:
"""Reset verdicts that changed functions may have changed through the tests they share.

A mutant's verdict is decided by the tests that cover its function. A test that also
reaches a changed or deleted function can now behave differently, whether that
function is a callee of the mutated one or only feeds the test's assertion, so every
mutant covered by such a test is re-tested. The changed functions' own mutants are
already reset when their file is mutated again.
"""
old_hashes = state().old_function_hashes
if not old_hashes:
return
new_hashes = state().current_function_hashes
changed = {f for f in old_hashes.keys() | new_hashes.keys() if old_hashes.get(f) != new_hashes.get(f)}
tests_by_function = state().tests_by_mangled_function_name
affected_tests = set().union(*(tests_by_function.get(f, set()) for f in changed))
if not affected_tests:
return
affected_functions = {f for f, tests in tests_by_function.items() if tests & affected_tests}

count = _reset_mutant_results(lambda key, exit_code: mangled_name_from_mutant_name(key) in affected_functions)
if count:
print(f" Re-testing {count} mutant(s) whose tests reach changed functions")


def _refresh_test_file_baseline(*, full_run: bool) -> None:
"""Save test file hashes for the next run.

Expand Down Expand Up @@ -883,6 +908,8 @@ def collect_or_load_stats(
# Run full stats
run_stats_collection(runner)
else:
# before the cleanup, which forgets the tests of deleted modules' functions
_reset_results_for_function_changes()
_cleanup_stale_stats()
if config().track_dependencies and invalidate_stale_callers:
_invalidate_stale_dependency_edges()
Expand Down
49 changes: 49 additions & 0 deletions tests/mutation/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from mutmut.__main__ import _register_mutant_result
from mutmut.__main__ import _report_watched_file_changes
from mutmut.__main__ import _reset_mutant_results
from mutmut.__main__ import _reset_results_for_function_changes
from mutmut.__main__ import _reset_results_for_test_changes
from mutmut.__main__ import compute_test_file_hashes
from mutmut.__main__ import compute_watched_file_hashes
Expand Down Expand Up @@ -2284,6 +2285,54 @@ def test_test_changes_reset_only_affected_verdicts(tmp_path, monkeypatch):
reset_state()


def test_function_changes_reset_mutants_sharing_a_test(tmp_path, monkeypatch):
"""A test like ``y = g(x); assert f(y) == ...`` kills g's mutants through f, which g never calls."""
reset_state()
monkeypatch.chdir(tmp_path)
monkeypatch.setattr(mutmut.__main__, "config", lambda: _config_for_invalidation())
state().tests_by_mangled_function_name["mymod.x_f"] = {"tests/test_a.py::t"}
state().tests_by_mangled_function_name["mymod.x_g"] = {"tests/test_a.py::t"}
state().tests_by_mangled_function_name["mymod.x_h"] = {"tests/test_b.py::t"}
state().old_function_hashes.update({"mymod.x_f": "old", "mymod.x_g": "same", "mymod.x_h": "same"})
state().current_function_hashes.update({"mymod.x_f": "new", "mymod.x_g": "same", "mymod.x_h": "same"})
_write_meta({"mymod.x_g__mutmut_1": 1, "mymod.x_g__mutmut_2": 0, "mymod.x_h__mutmut_1": 1})

_reset_results_for_function_changes()

assert _load_results() == {"mymod.x_g__mutmut_1": None, "mymod.x_g__mutmut_2": None, "mymod.x_h__mutmut_1": 1}
reset_state()


def test_function_changes_count_deleted_functions(tmp_path, monkeypatch):
reset_state()
monkeypatch.chdir(tmp_path)
monkeypatch.setattr(mutmut.__main__, "config", lambda: _config_for_invalidation())
state().tests_by_mangled_function_name["mymod.x_gone"] = {"tests/test_a.py::t"}
state().tests_by_mangled_function_name["mymod.x_g"] = {"tests/test_a.py::t"}
state().old_function_hashes.update({"mymod.x_gone": "old", "mymod.x_g": "same"})
state().current_function_hashes.update({"mymod.x_g": "same"})
_write_meta({"mymod.x_g__mutmut_1": 1})

_reset_results_for_function_changes()

assert _load_results() == {"mymod.x_g__mutmut_1": None}
reset_state()


def test_function_changes_reset_nothing_on_first_run(tmp_path, monkeypatch):
reset_state()
monkeypatch.chdir(tmp_path)
monkeypatch.setattr(mutmut.__main__, "config", lambda: _config_for_invalidation())
state().tests_by_mangled_function_name["mymod.x_g"] = {"tests/test_a.py::t"}
state().current_function_hashes.update({"mymod.x_g": "new"})
_write_meta({"mymod.x_g__mutmut_1": 1})

_reset_results_for_function_changes()

assert _load_results() == {"mymod.x_g__mutmut_1": 1}
reset_state()


def test_test_file_baseline_keeps_changed_conftest_until_full_run(tmp_path, monkeypatch):
reset_state()
monkeypatch.chdir(tmp_path)
Expand Down
Loading