Skip to content

Remove deleted test files from mutants/ - #636

Open
nafiurahman00 wants to merge 1 commit into
boxed:mainfrom
nafiurahman00:fix/remove-deleted-files-from-mutants
Open

nafiurahman00 wants to merge 1 commit into
boxed:mainfrom
nafiurahman00:fix/remove-deleted-files-from-mutants

Conversation

@nafiurahman00

Copy link
Copy Markdown

copy_also_copy_files() copies tests/ into mutants/ with shutil.copytree(..., dirs_exist_ok=True), which only adds and overwrites files. If you delete a test file, its copy stays in mutants/tests/, pytest keeps collecting it, and the mutants it used to kill are still reported as killed until you rm -rf mutants.

To reproduce:

mkdir -p src/calc tests; : > src/calc/__init__.py
printf 'def double(x):\n    return x * 2\n' > src/calc/ops.py
printf 'from calc.ops import double\n\ndef test_runs():\n    double(2)\n' > tests/test_weak.py
printf 'from calc.ops import double\n\ndef test_value():\n    assert double(3) == 6\n' > tests/test_strong.py
printf '[project]\nname = "calc"\nversion = "0"\n[tool.mutmut]\nsource_paths = ["src/"]\npytest_add_cli_args_test_selection = ["tests/"]\n[tool.pytest.ini_options]\npythonpath = ["src"]\n' > pyproject.toml
mutmut run
rm tests/test_strong.py                        # the only test that kills the mutants
rm -rf mutants/mutmut-stats.json mutants/src   # force fresh stats/results but keep the copied tests
mutmut run
mutmut results --all true

On main both mutants are still killed and mutants/tests/test_strong.py is still there. With this change they're survived and the file is gone.

The fix removes files (and directories) from the mutants/ copy that no longer exist in the project, right after the copy. __pycache__ is left alone since Python ignores a cached .pyc without its source, and clearing it every run would just make pytest recompile. If an also_copy directory overlaps a source path it's skipped, because mutants/ has mutmut's generated files there.

Added tests/utils/test_file_utils.py. The two deletion tests fail on main and pass with the fix. uv run pytest passes (482 passed, 4 skipped), and so does pre-commit run --all-files.

This is related to #626: once a test change triggers a re-run, the deleted test also needs to actually stop running.

copy_also_copy_files() uses copytree(dirs_exist_ok=True), which never
deletes anything. If you delete a test file, its copy stays in
mutants/tests/ and pytest keeps running it, so the mutants it killed
still show up as killed.

After copying a directory, remove the files in the mutants/ copy that
no longer exist in the project. __pycache__ is left alone, and
directories that overlap the source paths are skipped.
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.

1 participant