|
| 1 | +import functools |
| 2 | +import gzip |
1 | 3 | import io |
2 | 4 | import sys |
3 | 5 | import zipfile |
|
14 | 16 | PLATFORM = f"emscripten_{EMSCRIPTEN_VER.replace('.', '_')}_wasm32" |
15 | 17 | CPVER = f"cp{sys.version_info.major}{sys.version_info.minor}" |
16 | 18 |
|
| 19 | +TEST_PYPI_RESPONSE_DIR = Path(__file__).parent / "test_data" / "pypi_response" |
| 20 | + |
| 21 | + |
| 22 | +def _read_pypi_response(file: Path) -> bytes: |
| 23 | + return gzip.decompress(file.read_bytes()) |
| 24 | + |
17 | 25 |
|
18 | 26 | def _build(build_dir, dist_dir): |
19 | 27 | import build |
@@ -186,7 +194,7 @@ def add_pkg_version( |
186 | 194 | self.metadata_map[filename] = metadata |
187 | 195 | self.top_level_map[filename] = top_level |
188 | 196 |
|
189 | | - async def _get_pypi_json(self, pkgname, kwargs): |
| 197 | + async def query_package(self, pkgname, kwargs, index_urls=None): |
190 | 198 | from micropip.package_index import ProjectInfo |
191 | 199 |
|
192 | 200 | try: |
@@ -229,9 +237,43 @@ def write_file(filename, contents): |
229 | 237 | @pytest.fixture |
230 | 238 | def mock_fetch(monkeypatch, mock_importlib): |
231 | 239 | pytest.importorskip("packaging") |
232 | | - from micropip import transaction |
| 240 | + from micropip import package_index, transaction |
233 | 241 |
|
234 | 242 | result = mock_fetch_cls() |
235 | | - monkeypatch.setattr(transaction, "_get_pypi_json", result._get_pypi_json) |
| 243 | + monkeypatch.setattr(package_index, "query_package", result.query_package) |
236 | 244 | monkeypatch.setattr(transaction, "fetch_bytes", result._fetch_bytes) |
237 | 245 | return result |
| 246 | + |
| 247 | + |
| 248 | +def _mock_package_index_gen( |
| 249 | + httpserver, |
| 250 | + pkgs=("black", "pytest", "numpy", "pytz", "snowballstemmer"), |
| 251 | + content_type="application/json", |
| 252 | + suffix="_json.json.gz", |
| 253 | +): |
| 254 | + # Run a mock server that serves as a package index |
| 255 | + import secrets |
| 256 | + |
| 257 | + base = secrets.token_hex(16) |
| 258 | + |
| 259 | + for pkg in pkgs: |
| 260 | + data = _read_pypi_response(TEST_PYPI_RESPONSE_DIR / f"{pkg}{suffix}") |
| 261 | + httpserver.expect_request(f"/{base}/{pkg}/").respond_with_data( |
| 262 | + data, |
| 263 | + content_type=content_type, |
| 264 | + headers={"Access-Control-Allow-Origin": "*"}, |
| 265 | + ) |
| 266 | + |
| 267 | + index_url = httpserver.url_for(base) |
| 268 | + |
| 269 | + return index_url |
| 270 | + |
| 271 | + |
| 272 | +@pytest.fixture |
| 273 | +def mock_package_index_json_api(httpserver): |
| 274 | + return functools.partial( |
| 275 | + _mock_package_index_gen, |
| 276 | + httpserver=httpserver, |
| 277 | + suffix="_json.json.gz", |
| 278 | + content_type="application/json", |
| 279 | + ) |
0 commit comments