Repository navigation
Re-test cached mutants whose tests also run changed functions - #657
ZacKienzle2 wants to merge 1 commit into
Conversation
A cached verdict is decided by the tests that cover the mutant's function. When another function those tests also run changes or is deleted, the tests can now pass or fail differently, but only the changed function's own mutants were reset. A test such as y = g(x); assert f(y) == expected kills g's mutants through f, which g never calls, so a change that weakens f left g's mutants killed. Before the stale stats are cleaned up, every mutant of a function sharing a test with a changed or deleted function is now reset, from the loaded test associations.
boxed
left a comment
There was a problem hiding this comment.
from Claude:
I went through this for correctness. Re-testing mutants whose verdict can change through a shared test fixes a real stale-verdict problem, but there are a couple of issues to sort out first.
Should be fixed before merge
-
mutmut print-time-estimateswipes every cached verdict.print_time_estimatescallscollect_or_load_stats(runner)without callingcreate_mutants()first, socurrent_function_hashesis empty. Every function inold_function_hashesthen counts as deleted, and_reset_results_for_function_changessets every mutant that shares a test with one of them to None in the.metafiles. After a fullmutmut run, runningmutmut print-time-estimatesmakesmutmut resultsshow everything as not checked, and the next run re-tests the whole project. Suggestion: call the reset only on therunpath, or skip it whencurrent_function_hashesis empty. -
One broad test makes incremental runs close to full runs. A changed function's tests are expanded to every function those tests reach. A few integration or Django test-client tests that touch hundreds of functions turn a one-line edit into a reset of most mutants. No config option turns this off, and it ignores
track_dependencies. Suggestion: put it behind a config option, or narrow the reset, and document the cost.
Gaps in the approach
- The reset uses coverage data from before the change. It runs before
_forget_tests_in_changed_filesand the incremental stats collection, so new links aren't intests_by_mangled_function_nameyet. If the editedfnow also callsk, and testtcoversf, thentisn't recorded againstk, andk's mutants keep their old verdicts. This limitation already exists, but the new reset inherits it. Suggestion: run the reset (or a second pass of it) after the incremental stats are collected.
Minor
-
The docs promise more than the code does. The README and HISTORY say a change to any function re-tests the mutants that share a test with it, but only functions that have mutants get a hash. Changes to functions in
do_not_mutatefiles or outsidepaths_to_mutate, to functions with no mutations, and (withmutate_only_covered_lines) to uncovered functions are not detected. Suggestion: say "changed or deleted mutated function", or hash all functions. -
Under
mutate_only_covered_lines, changes in coverage look like changed functions. Hashes are only recorded for functions that got at least one mutation. If a test edit makes a function gain or lose its only covered mutation, it looks added or deleted, and every mutant sharing its tests is reset although no source changed.
Fixes #597.
The 3.7.0 changelog says changing a called function invalidates the mutants of its callers. The dependency edges are recorded, but
_invalidate_stale_dependency_edgesonly removes the changed functions from the caller sets. No cached verdict of a caller is reset, which is what #597 reproduces.This resets at the level of tests instead of call edges. A mutant's verdict is decided by the tests that cover its function. If any of those tests also runs a function that changed or was deleted since the last run, the test can now pass or fail differently, so the mutant is re-tested.
_reset_results_for_function_changestakes the changed functions from the stored and current function hashes. It collects the tests thattests_by_mangled_function_namerecords for them, and resets, through_reset_mutant_results, every mutant of a function covered by one of those tests.That covers the callers in #597:
total's tests runadd. It also covers a case no call edge can see. A test such asy = g(x); assert f(y) == expectedkillsg's mutants throughf, whichgnever calls, so a change that weakensfused to leaveg's mutants marked killed.It runs in
collect_or_load_statsbefore_cleanup_stale_stats, which forgets the tests of functions in deleted modules, so a deleted function's tests still count. On a first run (no stored hashes) it does nothing.Tests: the shared-test case above, a deleted function, and the first run. The README paragraph in "Detecting dependency and config changes" and HISTORY describe the new reset.