|
7 | 7 | from cpp_linter_hooks.util import ( |
8 | 8 | get_version_from_dependency, |
9 | 9 | _resolve_version, |
| 10 | + _is_version_installed, |
10 | 11 | _install_tool, |
11 | 12 | resolve_install, |
12 | 13 | DEFAULT_CLANG_FORMAT_VERSION, |
@@ -117,6 +118,51 @@ def test_resolve_version_clang_tidy(user_input, expected): |
117 | 118 | assert result == expected |
118 | 119 |
|
119 | 120 |
|
| 121 | +# Tests for _is_version_installed |
| 122 | +@pytest.mark.benchmark |
| 123 | +def test_is_version_installed_not_in_path(): |
| 124 | + """Test _is_version_installed when tool is not in PATH.""" |
| 125 | + with patch("shutil.which", return_value=None): |
| 126 | + result = _is_version_installed("clang-format", "20.1.7") |
| 127 | + assert result is None |
| 128 | + |
| 129 | + |
| 130 | +@pytest.mark.benchmark |
| 131 | +def test_is_version_installed_version_matches(): |
| 132 | + """Test _is_version_installed when installed version matches.""" |
| 133 | + mock_path = "/usr/bin/clang-format" |
| 134 | + |
| 135 | + def patched_run(*args, **kwargs): |
| 136 | + return subprocess.CompletedProcess( |
| 137 | + args, returncode=0, stdout="clang-format version 20.1.7" |
| 138 | + ) |
| 139 | + |
| 140 | + with ( |
| 141 | + patch("shutil.which", return_value=mock_path), |
| 142 | + patch("subprocess.run", side_effect=patched_run), |
| 143 | + ): |
| 144 | + result = _is_version_installed("clang-format", "20.1.7") |
| 145 | + assert result == Path(mock_path) |
| 146 | + |
| 147 | + |
| 148 | +@pytest.mark.benchmark |
| 149 | +def test_is_version_installed_version_mismatch(): |
| 150 | + """Test _is_version_installed when installed version doesn't match.""" |
| 151 | + mock_path = "/usr/bin/clang-format" |
| 152 | + |
| 153 | + def patched_run(*args, **kwargs): |
| 154 | + return subprocess.CompletedProcess( |
| 155 | + args, returncode=0, stdout="clang-format version 22.1.0" |
| 156 | + ) |
| 157 | + |
| 158 | + with ( |
| 159 | + patch("shutil.which", return_value=mock_path), |
| 160 | + patch("subprocess.run", side_effect=patched_run), |
| 161 | + ): |
| 162 | + result = _is_version_installed("clang-format", "20.1.7") |
| 163 | + assert result is None |
| 164 | + |
| 165 | + |
120 | 166 | # Tests for _install_tool |
121 | 167 | @pytest.mark.benchmark |
122 | 168 | def test_install_tool_success(): |
@@ -178,20 +224,32 @@ def test_resolve_install_tool_already_installed_correct_version(): |
178 | 224 | """Test resolve_install when tool is already installed with correct version.""" |
179 | 225 | mock_path = "/usr/bin/clang-format" |
180 | 226 |
|
| 227 | + def patched_run(*args, **kwargs): |
| 228 | + return subprocess.CompletedProcess( |
| 229 | + args, returncode=0, stdout="clang-format version 20.1.7" |
| 230 | + ) |
| 231 | + |
181 | 232 | with ( |
182 | 233 | patch("shutil.which", return_value=mock_path), |
| 234 | + patch("subprocess.run", side_effect=patched_run), |
183 | 235 | ): |
184 | 236 | result = resolve_install("clang-format", "20.1.7") |
185 | 237 | assert Path(result) == Path(mock_path) |
186 | 238 |
|
187 | 239 |
|
188 | 240 | @pytest.mark.benchmark |
189 | 241 | def test_resolve_install_tool_version_mismatch(): |
190 | | - """Test resolve_install when tool has wrong version.""" |
| 242 | + """Test resolve_install when tool has wrong version, triggering reinstall.""" |
191 | 243 | mock_path = "/usr/bin/clang-format" |
192 | 244 |
|
| 245 | + def patched_run(*args, **kwargs): |
| 246 | + return subprocess.CompletedProcess( |
| 247 | + args, returncode=0, stdout="clang-format version 22.1.0" |
| 248 | + ) |
| 249 | + |
193 | 250 | with ( |
194 | 251 | patch("shutil.which", return_value=mock_path), |
| 252 | + patch("subprocess.run", side_effect=patched_run), |
195 | 253 | patch( |
196 | 254 | "cpp_linter_hooks.util._install_tool", return_value=Path(mock_path) |
197 | 255 | ) as mock_install, |
|
0 commit comments