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
10 changes: 9 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,15 @@ To catch this, mutmut detects non-Python files that changed since the last full
run and warns you about them. If your project is a git repository and git is
installed, mutmut uses git (a soft dependency no extra package is required) to
find every changed non-Python file, respecting your `.gitignore`. Python files
are excluded because their changes are already tracked per function.
are excluded because their changes are already tracked: source files per
function, and test files as described below.

Test files are tracked by their content. If you edit or delete a test file,
mutmut re-runs the mutants of the functions its tests covered, so removing an
assertion won't leave old ``killed`` results around. New tests also re-run the
``survived`` and ``no tests`` mutants of the functions they cover. Changes to a
``conftest.py`` are handled like a dependency file change (see
``on_dependency_change`` below), since they can affect any test.

On a full run with git available, mutmut also records the content hashes of the
tracked non-Python files. This means a later run in an environment without git
Expand Down
110 changes: 105 additions & 5 deletions src/mutmut/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,95 @@ def compute_watched_file_hashes() -> dict[str, str]:
return _hash_files(paths)


def _test_file_of(test_name: str) -> str:
# "tests/test_x.py::TestA::test_b[1]" -> "tests/test_x.py"
return test_name.split("::", 1)[0]


def _is_conftest(path: str) -> bool:
return Path(path).name == "conftest.py"


def compute_test_file_hashes(test_names: Iterable[str]) -> dict[str, str]:
"""Hash the files these tests live in, plus any conftest.py above them."""
files = {_test_file_of(name) for name in test_names}
conftests = {
str(parent / "conftest.py") for f in files for parent in Path(f).parents if (parent / "conftest.py").is_file()
}
return _hash_files(sorted(files | conftests))


def _changed_test_files() -> set[str]:
"""Test files (and conftest.py files) that were changed, added or deleted since the last run."""
old = state().old_test_file_hashes
if not old:
return set()
new = _hash_files(old.keys())
new.update(compute_test_file_hashes(collected_test_names()))
return {p for p in old.keys() | new.keys() if old.get(p) != new.get(p)}


def _forget_tests_in_changed_files() -> set[str]:
"""Forget the stats of tests in changed files so they get collected again as new tests.

Returns the functions those tests used to cover. conftest.py changes are left to
on_dependency_change since they can affect any test.
"""
changed_files = {p for p in _changed_test_files() if not _is_conftest(p)}
if not changed_files:
return set()

tests_by_function = state().tests_by_mangled_function_name
known_tests = collected_test_names().union(*tests_by_function.values())
stale_tests = {t for t in known_tests if _test_file_of(t) in changed_files}
covered = {f for f, tests in tests_by_function.items() if tests & stale_tests}
for f in covered:
tests_by_function[f] -= stale_tests
for t in stale_tests:
state().duration_by_test.pop(t, None)

print(f" {len(changed_files)} test file(s) changed since the last run: {', '.join(sorted(changed_files))}")
return covered


def _reset_results_for_test_changes(edited_functions: set[str], recollected_tests: set[str]) -> None:
"""Reset verdicts that new or edited tests may have changed.

Functions an edited test used to cover get all their verdicts reset (a weaker test
can turn a kill into a survivor). Functions that new/edited tests now cover only get
survived and "no tests" reset, since extra coverage can't un-kill a mutant.
"""
newly_covered = {
f for f, tests in state().tests_by_mangled_function_name.items() if tests & recollected_tests
} - edited_functions
if not edited_functions and not newly_covered:
return

def should_reset(key: str, exit_code: int) -> bool:
function = mangled_name_from_mutant_name(key)
if function in edited_functions:
return True
return function in newly_covered and status_by_exit_code[exit_code] in ("survived", "no tests")

count = _reset_mutant_results(should_reset)
if count:
print(f" Re-testing {count} mutant(s) affected by the test changes")


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

conftest.py hashes are only updated on a full run, same as the watched files, so the
warning keeps showing until the cache is rebuilt.
"""
new = compute_test_file_hashes(collected_test_names())
old = state().old_test_file_hashes
if not full_run and old:
new = {p: h for p, h in new.items() if not _is_conftest(p)}
new.update({p: h for p, h in old.items() if _is_conftest(p)})
state().test_file_hashes = new


def _run_git(args: list[str]) -> str | None:
"""Run a git command at the project root. Returns stdout, or None on any failure
(git not installed, not a repo, unknown ref, ...). Git is a soft dependency: this
Expand Down Expand Up @@ -547,6 +636,8 @@ def _changed_dependency_files() -> set[str]:
changed = _changed_hashed_files()
else:
changed = _changed_hashed_files()
# conftest.py can affect every test, so treat it like a dependency file
changed |= {p for p in _changed_test_files() if _is_conftest(p)}
return {p for p in changed if not _is_excluded(p, cfg)}


Expand Down Expand Up @@ -598,7 +689,7 @@ def _reset_mutant_results(should_reset: Callable[[str, int], bool]) -> int:


def _report_watched_file_changes() -> bool:
"""Surface non-Python files that changed since the last full run.
"""Surface non-Python files (and conftest.py) that changed since the last full run.

Returns True only when the configured policy is ``rerun`` and something changed,
asking the caller to reset all results. Silent when there is no baseline yet.
Expand All @@ -612,10 +703,10 @@ def _report_watched_file_changes() -> bool:
return False
listed = sorted(changed)
if policy == "rerun":
print(f" {len(listed)} non-Python file(s) changed; rerunning all mutants: {', '.join(listed)}")
print(f" {len(listed)} dependency file(s) changed; rerunning all mutants: {', '.join(listed)}")
return True
# default: warn but keep the cache
print(f" Warning: {len(listed)} non-Python file(s) changed since the last full run: {', '.join(listed)}")
print(f" Warning: {len(listed)} dependency file(s) changed since the last full run: {', '.join(listed)}")
print(" These cannot be tracked for behavioral changes, so cached results were kept.")
print(' If the changes affect your tests, delete the mutants/ directory or set on_dependency_change = "rerun".')
return False
Expand Down Expand Up @@ -668,7 +759,8 @@ def collect_or_load_stats(
if did_load and apply_config_invalidation:
force_full = _apply_config_change_invalidation(mutants_caught_by_type_checker or {})

if not did_load or force_full:
full_run = not did_load or force_full
if full_run:
# A full run rebuilds the cache, so reset the change-detection baseline to "now".
_refresh_change_detection_baseline()
# Run full stats
Expand All @@ -679,6 +771,9 @@ def collect_or_load_stats(
_invalidate_stale_dependency_edges()
save_stats()

# tests in edited files are forgotten here so they get picked up as new tests below
edited_functions = _forget_tests_in_changed_files()

# Run incremental stats
with CatchOutput(spinner_title="Listing all tests") as output_catcher:
set_mutant_under_test("list_all_tests")
Expand All @@ -694,9 +789,14 @@ def collect_or_load_stats(
new_tests = all_tests_result.new_tests()

if new_tests:
print(f"Found {len(new_tests)} new tests, rerunning stats collection")
print(f"Found {len(new_tests)} new or changed tests, rerunning stats collection")
run_stats_collection(runner, tests=new_tests)

_reset_results_for_test_changes(edited_functions, new_tests)

_refresh_test_file_baseline(full_run=full_run)
save_stats()


def save_cicd_stats(source_file_mutation_data_by_path: dict[str, SourceFileMutationData]) -> None:
s = calculate_summary_stats(source_file_mutation_data_by_path)
Expand Down
3 changes: 3 additions & 0 deletions src/mutmut/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class MutmutState:
watched_file_hashes: dict[str, str] = field(default_factory=dict)
old_git_commit: str | None = None
git_commit: str | None = None
# Hashes of test files (and conftest.py) from the last run, updated every run.
old_test_file_hashes: dict[str, str] = field(default_factory=dict)
test_file_hashes: dict[str, str] = field(default_factory=dict)
# Migrated from module-level globals in mutmut/__init__.py.
stats_time: float | None = None
duration_by_test: defaultdict[str, float] = field(default_factory=lambda: defaultdict(float))
Expand Down
3 changes: 3 additions & 0 deletions src/mutmut/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ def load_stats() -> bool:
state().old_config_fingerprint = data.pop("config_fingerprint", {})
state().old_watched_file_hashes = data.pop("watched_file_hashes", {})
state().old_git_commit = data.pop("git_commit", None)
state().old_test_file_hashes = data.pop("test_file_hashes", {})
state().test_file_hashes = state().old_test_file_hashes
# Preserve the loaded baseline; only a full run refreshes it.
state().watched_file_hashes = state().old_watched_file_hashes
state().git_commit = state().old_git_commit
Expand All @@ -141,6 +143,7 @@ def save_stats() -> None:
config_fingerprint=config().config_fingerprint(),
watched_file_hashes=state().watched_file_hashes,
git_commit=state().git_commit,
test_file_hashes=state().test_file_hashes,
),
f,
indent=4,
Expand Down
Loading