|
1 | 1 | import importlib.util |
2 | 2 | import os |
3 | 3 | import shutil |
4 | | -from unittest.mock import patch |
| 4 | +from unittest.mock import MagicMock, patch |
5 | 5 |
|
6 | 6 | import pytest |
7 | 7 |
|
|
21 | 21 | _spec.loader.exec_module(_mod) |
22 | 22 |
|
23 | 23 | cleanup_old_versions = _mod.cleanup_old_versions |
| 24 | +download_dependency = _mod.download_dependency |
24 | 25 |
|
25 | 26 |
|
26 | 27 | class TestCleanupOldVersions: |
@@ -169,3 +170,48 @@ def _fail_on_v1(path, *args, **kwargs): |
169 | 170 | assert (tmp_path / "v1").exists() |
170 | 171 | assert (tmp_path / "v2").exists() |
171 | 172 | assert (tmp_path / "v3").exists() |
| 173 | + |
| 174 | + |
| 175 | +class TestDownloadDependency: |
| 176 | + def test_skips_download_when_done_file_exists(self, tmp_path): |
| 177 | + """Already-cached version should not trigger a download.""" |
| 178 | + release = "v1.0.0" |
| 179 | + py_ver = "3.11" |
| 180 | + version_path = tmp_path / release / f"python{py_ver}" |
| 181 | + version_path.mkdir(parents=True) |
| 182 | + (version_path / f"{py_ver}-done").write_text("ok") |
| 183 | + |
| 184 | + with ( |
| 185 | + patch.object(_mod, "BASE_PATH", str(tmp_path)), |
| 186 | + patch.object(_mod.subprocess, "Popen") as mock_popen, |
| 187 | + ): |
| 188 | + download_dependency(release, py_ver, "fake-bucket") |
| 189 | + |
| 190 | + mock_popen.assert_not_called() |
| 191 | + |
| 192 | + def test_downloads_when_done_file_missing(self, tmp_path): |
| 193 | + """Missing done file should trigger the download.""" |
| 194 | + release = "v1.0.0" |
| 195 | + py_ver = "3.11" |
| 196 | + version_path = tmp_path / release / f"python{py_ver}" |
| 197 | + version_path.mkdir(parents=True) |
| 198 | + |
| 199 | + mock_aws = MagicMock() |
| 200 | + mock_aws.stdout = MagicMock() |
| 201 | + mock_aws.stderr = MagicMock(read=MagicMock(return_value=b"")) |
| 202 | + mock_aws.wait.return_value = 0 |
| 203 | + |
| 204 | + mock_tar = MagicMock() |
| 205 | + mock_tar.communicate.return_value = (b"", b"") |
| 206 | + mock_tar.returncode = 0 |
| 207 | + |
| 208 | + with ( |
| 209 | + patch.object(_mod, "BASE_PATH", str(tmp_path)), |
| 210 | + patch.object( |
| 211 | + _mod.subprocess, "Popen", side_effect=[mock_aws, mock_tar] |
| 212 | + ) as mock_popen, |
| 213 | + ): |
| 214 | + download_dependency(release, py_ver, "fake-bucket") |
| 215 | + |
| 216 | + assert mock_popen.call_count == 2 |
| 217 | + assert (version_path / f"{py_ver}-done").exists() |
0 commit comments