(fixtures)
| 323 | |
| 324 | @pytest.fixture() |
| 325 | def mock_pypi_rest_api(fixtures): |
| 326 | mirror_pth = Path(fixtures.path("mirror/")) |
| 327 | |
| 328 | mock_data = {} |
| 329 | |
| 330 | for x in mirror_pth.glob("*.json"): |
| 331 | mock_data[f"https://pypi.org/pypi/{x.name.split('.')[0]}/json"] = json.loads(x.read_text()) |
| 332 | |
| 333 | |
| 334 | def _callback_download(request): |
| 335 | filename = request.url.split("/")[-1] |
| 336 | file_pth = fixtures.path(f"mirror/{filename}") |
| 337 | if os.path.exists(file_pth): |
| 338 | with open(file_pth, "rb") as fd: |
| 339 | return (200, {'Content-length': str(os.stat(file_pth).st_size)}, fd.read()) |
| 340 | else: |
| 341 | return (404, {"Content-length": 0}, "") |
| 342 | |
| 343 | |
| 344 | def _callback(request): |
| 345 | resp = json.dumps(mock_data[request.url]) |
| 346 | return (200, {}, resp) |
| 347 | |
| 348 | |
| 349 | def _activate_mock(rsps): |
| 350 | for url in mock_data.keys(): |
| 351 | rsps.add_callback( |
| 352 | responses.GET, |
| 353 | url=url, |
| 354 | callback=_callback |
| 355 | ) |
| 356 | |
| 357 | rsps.add_callback( |
| 358 | responses.GET, |
| 359 | url=re.compile(r"https://files.pythonhosted.org/packages/.+"), |
| 360 | callback=_callback_download |
| 361 | ) |
| 362 | |
| 363 | |
| 364 | return _activate_mock |
| 365 | |
| 366 | |
| 367 | @pytest.fixture() |
no test coverage detected