|
| 1 | +"""Unit tests for CLI glob caching functions.""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from yamlfix.entrypoints.cli import ( |
| 8 | + _GLOB_CACHE, |
| 9 | + _clear_glob_cache, |
| 10 | + _glob_cache, |
| 11 | + _rglob_cache, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture(autouse=True) |
| 16 | +def clear_cache(): |
| 17 | + """Clear the glob cache before and after each test.""" |
| 18 | + _clear_glob_cache() |
| 19 | + yield |
| 20 | + _clear_glob_cache() |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def test_directory(tmp_path: Path) -> Path: |
| 25 | + """Create a test directory structure with YAML files.""" |
| 26 | + # Create files in root |
| 27 | + (tmp_path / "test.yaml").write_text("test") |
| 28 | + (tmp_path / "test.yml").write_text("test") |
| 29 | + (tmp_path / "other.txt").write_text("test") |
| 30 | + |
| 31 | + # Create subdirectory with nested files |
| 32 | + subdir = tmp_path / "subdir" |
| 33 | + subdir.mkdir() |
| 34 | + (subdir / "nested.yaml").write_text("test") |
| 35 | + (subdir / "nested.yml").write_text("test") |
| 36 | + |
| 37 | + return tmp_path |
| 38 | + |
| 39 | + |
| 40 | +class TestGlobCache: |
| 41 | + """Test the _glob_cache function.""" |
| 42 | + |
| 43 | + def test_glob_cache_basic_functionality(self, test_directory: Path): |
| 44 | + """Test that _glob_cache returns correct files and caches results.""" |
| 45 | + result = _glob_cache(test_directory, "*.yaml") |
| 46 | + |
| 47 | + # Should find files in current directory only (not recursive) |
| 48 | + assert test_directory / "test.yaml" in result |
| 49 | + assert test_directory / "subdir" / "nested.yaml" not in result |
| 50 | + |
| 51 | + # Cache should be populated |
| 52 | + assert len(_GLOB_CACHE) == 1 |
| 53 | + cache_key = (str(test_directory), "*.yaml", "g") |
| 54 | + assert cache_key in _GLOB_CACHE |
| 55 | + |
| 56 | + def test_glob_cache_uses_cache_on_repeat_calls(self, test_directory: Path): |
| 57 | + """Test that subsequent calls use cached results.""" |
| 58 | + # First call |
| 59 | + result1 = _glob_cache(test_directory, "*.yaml") |
| 60 | + cache_size_after_first = len(_GLOB_CACHE) |
| 61 | + |
| 62 | + # Second call should use cache |
| 63 | + result2 = _glob_cache(test_directory, "*.yaml") |
| 64 | + cache_size_after_second = len(_GLOB_CACHE) |
| 65 | + |
| 66 | + assert result1 == result2 |
| 67 | + assert cache_size_after_first == cache_size_after_second == 1 |
| 68 | + |
| 69 | + def test_glob_cache_different_patterns_create_separate_entries( |
| 70 | + self, test_directory: Path |
| 71 | + ): |
| 72 | + """Test that different glob patterns create separate cache entries.""" |
| 73 | + result_yaml = _glob_cache(test_directory, "*.yaml") |
| 74 | + result_yml = _glob_cache(test_directory, "*.yml") |
| 75 | + |
| 76 | + assert len(_GLOB_CACHE) == 2 |
| 77 | + assert result_yaml != result_yml |
| 78 | + assert test_directory / "test.yaml" in result_yaml |
| 79 | + assert test_directory / "test.yml" in result_yml |
| 80 | + |
| 81 | + def test_glob_cache_different_directories_create_separate_entries( |
| 82 | + self, test_directory: Path |
| 83 | + ): |
| 84 | + """Test that different directories create separate cache entries.""" |
| 85 | + # Create another directory |
| 86 | + other_dir = test_directory / "other" |
| 87 | + other_dir.mkdir() |
| 88 | + (other_dir / "other.yaml").write_text("test") |
| 89 | + |
| 90 | + result1 = _glob_cache(test_directory, "*.yaml") |
| 91 | + result2 = _glob_cache(other_dir, "*.yaml") |
| 92 | + |
| 93 | + assert len(_GLOB_CACHE) == 2 |
| 94 | + assert result1 != result2 |
| 95 | + assert test_directory / "test.yaml" in result1 |
| 96 | + assert other_dir / "other.yaml" in result2 |
| 97 | + |
| 98 | + |
| 99 | +class TestRglobCache: |
| 100 | + """Test the _rglob_cache function.""" |
| 101 | + |
| 102 | + def test_rglob_cache_basic_functionality(self, test_directory: Path): |
| 103 | + """Test that _rglob_cache returns correct files recursively and caches results.""" |
| 104 | + result = _rglob_cache(test_directory, "*.yaml") |
| 105 | + |
| 106 | + # Should find files recursively |
| 107 | + assert test_directory / "test.yaml" in result |
| 108 | + assert test_directory / "subdir" / "nested.yaml" in result |
| 109 | + |
| 110 | + # Cache should be populated with rglob key |
| 111 | + assert len(_GLOB_CACHE) == 1 |
| 112 | + cache_key = (str(test_directory), "*.yaml", "r") |
| 113 | + assert cache_key in _GLOB_CACHE |
| 114 | + |
| 115 | + def test_rglob_cache_vs_glob_cache_different_keys(self, test_directory: Path): |
| 116 | + """Test that rglob and glob use different cache keys.""" |
| 117 | + glob_result = _glob_cache(test_directory, "*.yaml") |
| 118 | + rglob_result = _rglob_cache(test_directory, "*.yaml") |
| 119 | + |
| 120 | + # Should have separate cache entries |
| 121 | + assert len(_GLOB_CACHE) == 2 |
| 122 | + |
| 123 | + # Results should be different (rglob includes nested files) |
| 124 | + assert len(rglob_result) > len(glob_result) |
| 125 | + assert test_directory / "subdir" / "nested.yaml" in rglob_result |
| 126 | + assert test_directory / "subdir" / "nested.yaml" not in glob_result |
| 127 | + |
| 128 | + def test_rglob_cache_uses_cache_on_repeat_calls(self, test_directory: Path): |
| 129 | + """Test that subsequent rglob calls use cached results.""" |
| 130 | + result1 = _rglob_cache(test_directory, "*.yaml") |
| 131 | + cache_size_after_first = len(_GLOB_CACHE) |
| 132 | + |
| 133 | + result2 = _rglob_cache(test_directory, "*.yaml") |
| 134 | + cache_size_after_second = len(_GLOB_CACHE) |
| 135 | + |
| 136 | + assert result1 == result2 |
| 137 | + assert cache_size_after_first == cache_size_after_second == 1 |
| 138 | + |
| 139 | + |
| 140 | +class TestCacheClear: |
| 141 | + """Test the _clear_glob_cache function.""" |
| 142 | + |
| 143 | + def test_clear_glob_cache_empties_cache(self, test_directory: Path): |
| 144 | + """Test that cache clearing removes all entries.""" |
| 145 | + # Populate cache with several entries |
| 146 | + _glob_cache(test_directory, "*.yaml") |
| 147 | + _glob_cache(test_directory, "*.yml") |
| 148 | + _rglob_cache(test_directory, "*.yaml") |
| 149 | + |
| 150 | + assert len(_GLOB_CACHE) == 3 |
| 151 | + |
| 152 | + # Clear cache |
| 153 | + _clear_glob_cache() |
| 154 | + |
| 155 | + assert len(_GLOB_CACHE) == 0 |
| 156 | + |
| 157 | + def test_clear_glob_cache_allows_fresh_caching(self, test_directory: Path): |
| 158 | + """Test that after clearing, caching works normally again.""" |
| 159 | + # First round of caching |
| 160 | + _glob_cache(test_directory, "*.yaml") |
| 161 | + assert len(_GLOB_CACHE) == 1 |
| 162 | + |
| 163 | + # Clear and verify empty |
| 164 | + _clear_glob_cache() |
| 165 | + assert len(_GLOB_CACHE) == 0 |
| 166 | + |
| 167 | + # Second round should work normally |
| 168 | + _glob_cache(test_directory, "*.yaml") |
| 169 | + assert len(_GLOB_CACHE) == 1 |
| 170 | + |
| 171 | + |
| 172 | +class TestCacheKeyConstruction: |
| 173 | + """Test that cache keys are constructed correctly.""" |
| 174 | + |
| 175 | + def test_cache_key_includes_directory_path(self, test_directory: Path): |
| 176 | + """Test that cache keys include the directory path.""" |
| 177 | + _glob_cache(test_directory, "*.yaml") |
| 178 | + |
| 179 | + expected_key = (str(test_directory), "*.yaml", "g") |
| 180 | + assert expected_key in _GLOB_CACHE |
| 181 | + |
| 182 | + def test_cache_key_distinguishes_glob_vs_rglob(self, test_directory: Path): |
| 183 | + """Test that glob and rglob operations have different key suffixes.""" |
| 184 | + _glob_cache(test_directory, "*.yaml") |
| 185 | + _rglob_cache(test_directory, "*.yaml") |
| 186 | + |
| 187 | + glob_key = (str(test_directory), "*.yaml", "g") |
| 188 | + rglob_key = (str(test_directory), "*.yaml", "r") |
| 189 | + |
| 190 | + assert glob_key in _GLOB_CACHE |
| 191 | + assert rglob_key in _GLOB_CACHE |
| 192 | + assert len(_GLOB_CACHE) == 2 |
0 commit comments