Skip to content

Re-test cached mutants whose tests also run changed functions - #657

Open
ZacKienzle2 wants to merge 1 commit into
boxed:mainfrom
ZacKienzle2:fix/reset-tests-reaching-changes
Open

ZacKienzle2 wants to merge 1 commit into
boxed:mainfrom
ZacKienzle2:fix/reset-tests-reaching-changes

Conversation

@ZacKienzle2

Copy link
Copy Markdown
Contributor

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_edges only 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_changes takes the changed functions from the stored and current function hashes. It collects the tests that tests_by_mangled_function_name records 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 run add. It also covers a case no call edge can see. 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 used to leave g's mutants marked killed.

It runs in collect_or_load_stats before _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.

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 boxed left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. mutmut print-time-estimates wipes every cached verdict. print_time_estimates calls collect_or_load_stats(runner) without calling create_mutants() first, so current_function_hashes is empty. Every function in old_function_hashes then counts as deleted, and _reset_results_for_function_changes sets every mutant that shares a test with one of them to None in the .meta files. After a full mutmut run, running mutmut print-time-estimates makes mutmut results show everything as not checked, and the next run re-tests the whole project. Suggestion: call the reset only on the run path, or skip it when current_function_hashes is empty.

  2. 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

  1. The reset uses coverage data from before the change. It runs before _forget_tests_in_changed_files and the incremental stats collection, so new links aren't in tests_by_mangled_function_name yet. If the edited f now also calls k, and test t covers f, then t isn't recorded against k, and k'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

  1. 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_mutate files or outside paths_to_mutate, to functions with no mutations, and (with mutate_only_covered_lines) to uncovered functions are not detected. Suggestion: say "changed or deleted mutated function", or hash all functions.

  2. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cross-call invalidation: changing a callee does not re-test mutants in its callers (3.8.0)

2 participants