diff --git a/tests/unit/python/utils/cache/test_files.py b/tests/unit/python/utils/cache/test_files.py index 14b554017e..780c949467 100644 --- a/tests/unit/python/utils/cache/test_files.py +++ b/tests/unit/python/utils/cache/test_files.py @@ -82,6 +82,8 @@ class TestIterProjectFiles(_ProjectRootFixture, unittest.TestCase): _touch(self.root / "node_modules/pkg/index.js", "module.exports = {};") _touch(self.root / "vendor/nette/utils/readme.md", "# vendored") _touch(self.root / "__pycache__/dead.pyc", "") + _touch(self.root / "build/ruff-cache-0/0.16.7/entry.json", "{}") + _touch(self.root / "cli/build/docs/readme/overview.py", "x = 1") def test_skip_dirs_pruned(self) -> None: all_paths = set(iter_project_files()) @@ -93,6 +95,17 @@ class TestIterProjectFiles(_ProjectRootFixture, unittest.TestCase): self.assertFalse(any("/node_modules/" in p for p in all_paths)) self.assertFalse(any("/vendor/" in p for p in all_paths)) + def test_build_is_pruned_at_the_root_only(self) -> None: + all_paths = set(iter_project_files()) + self.assertFalse( + any(p.startswith(f"{self.root}/build/") for p in all_paths), + f"root build/ artefacts reached the walk: {all_paths}", + ) + self.assertTrue( + any(p.endswith("cli/build/docs/readme/overview.py") for p in all_paths), + f"cli/build is source and must survive the prune: {all_paths}", + ) + def test_extensions_filter(self) -> None: ymls = list(iter_project_files(extensions=(".yml",))) self.assertEqual(len(ymls), 1) @@ -104,12 +117,13 @@ class TestIterProjectFiles(_ProjectRootFixture, unittest.TestCase): def test_exclude_tests_skips_tests_subtree(self) -> None: with_tests = list(iter_project_files(extensions=(".py",))) - self.assertEqual(len(with_tests), 1) + self.assertEqual(len(with_tests), 2) without_tests = list( iter_project_files(extensions=(".py",), exclude_tests=True) ) - self.assertEqual(without_tests, []) + self.assertEqual(len(without_tests), 1) + self.assertTrue(without_tests[0].endswith("cli/build/docs/readme/overview.py")) def test_exclude_dirs_path_segment_match(self) -> None: kept = list(iter_project_files(exclude_dirs=("docs",))) diff --git a/utils/cache/files.py b/utils/cache/files.py index 6efd2d95c4..599e654238 100644 --- a/utils/cache/files.py +++ b/utils/cache/files.py @@ -53,6 +53,8 @@ _DEFAULT_SKIP_DIRS: frozenset[str] = frozenset( } ) +_ROOT_SKIP_DIRS: frozenset[str] = frozenset({"build"}) + @lru_cache(maxsize=4) def _all_project_files(root_str: str) -> tuple[str, ...]: @@ -64,7 +66,10 @@ def _all_project_files(root_str: str) -> tuple[str, ...]: """ paths: list[str] = [] for dirpath, dirnames, filenames in os.walk(root_str, topdown=True): - dirnames[:] = [d for d in dirnames if d not in _DEFAULT_SKIP_DIRS] + skip = _DEFAULT_SKIP_DIRS + if dirpath == root_str: + skip = skip | _ROOT_SKIP_DIRS + dirnames[:] = [d for d in dirnames if d not in skip] paths.extend(str(Path(dirpath) / fn) for fn in filenames) return tuple(paths)