forked from pyodide/micropip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
411 lines (323 loc) · 12.1 KB
/
Copy pathconftest.py
File metadata and controls
411 lines (323 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import functools
import gzip
import io
import sys
import zipfile
from dataclasses import dataclass
from importlib.metadata import Distribution, PackageNotFoundError
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Any
import pytest
from pytest_httpserver import HTTPServer
from pytest_pyodide import spawn_web_server
from micropip._vendored.packaging.utils import parse_wheel_filename
def pytest_addoption(parser):
parser.addoption(
"--run-remote-index-tests",
action="store_true",
default=None,
help="Run tests that query remote package indexes.",
)
parser.addoption(
"--integration",
action="store_true",
default=None,
help="Run integration tests.",
)
EMSCRIPTEN_VER = "3.1.14"
PLATFORM = f"emscripten_{EMSCRIPTEN_VER.replace('.', '_')}_wasm32"
CPVER = f"cp{sys.version_info.major}{sys.version_info.minor}"
TEST_PYPI_RESPONSE_DIR = Path(__file__).parent / "test_data" / "pypi_response"
TEST_WHEEL_DIR = Path(__file__).parent / "test_data" / "wheel"
SNOWBALL_WHEEL = "snowballstemmer-2.0.0-py2.py3-none-any.whl"
PYTEST_WHEEL = "pytest-7.2.2-py3-none-any.whl"
def _read_gzipped_testfile(file: Path) -> bytes:
return gzip.decompress(file.read_bytes())
def _build(build_dir, dist_dir):
import build
from build.env import IsolatedEnvBuilder
with IsolatedEnvBuilder() as env:
builder = build.ProjectBuilder(build_dir)
builder.python_executable = env.executable
builder.scripts_dir = env.scripts_dir
env.install(builder.build_system_requires)
builder.build("wheel", output_directory=dist_dir)
@pytest.fixture(scope="session")
def wheel_path(tmp_path_factory):
# Build a micropip wheel for testing
output_dir = tmp_path_factory.mktemp("wheel")
_build(Path(__file__).parent.parent, output_dir)
yield output_dir
@pytest.fixture
def selenium_standalone_micropip(selenium_standalone, wheel_path):
"""Import micropip before entering test so that global initialization of
micropip doesn't count towards hiwire refcount.
"""
wheel_dir = Path(wheel_path)
wheel_files = list(wheel_dir.glob("*.whl"))
if not wheel_files:
pytest.exit("No wheel files found in wheel/ directory")
wheel_file = wheel_files[0]
with spawn_web_server(wheel_dir) as server:
server_hostname, server_port, _ = server
base_url = f"http://{server_hostname}:{server_port}/"
selenium_standalone.run_js(
f"""
await pyodide.loadPackage("{base_url + wheel_file.name}");
await pyodide.loadPackage(["packaging"]);
pyodide.runPython("import micropip");
"""
)
yield selenium_standalone
class WheelCatalog:
"""
A catalog of wheels for testing.
"""
@dataclass
class Wheel:
_path: Path
name: str
version: str
filename: str
top_level: str
url: str
@property
def content(self) -> bytes:
return self._path.read_bytes()
def __init__(self):
self._wheels = {}
self._httpserver = HTTPServer()
self._httpserver.no_handler_status_code = 404
def __enter__(self):
self._httpserver.__enter__()
return self
def __exit__(self, *args: Any):
self._httpserver.__exit__(*args)
def _register_handler(self, endpoint: str, data: bytes) -> str:
self._httpserver.expect_request(f"/{endpoint}").respond_with_data(
data,
content_type="application/zip",
headers={"Access-Control-Allow-Origin": "*"},
)
return self._httpserver.url_for(f"/{endpoint}")
def add_wheel(self, path: Path, replace: bool = True):
name, version = parse_wheel_filename(path.name)[0:2]
url = self._register_handler(path.name, path.read_bytes())
metadata_file_endpoint = path.with_suffix(".whl.metadata")
if metadata_file_endpoint.exists():
self._register_handler(
metadata_file_endpoint.name, metadata_file_endpoint.read_bytes()
)
if name in self._wheels and not replace:
return
self._wheels[name] = self.Wheel(
path, name, str(version), path.name, name.replace("-", "_"), url
)
def get(self, name: str) -> Wheel:
return self._wheels[name]
@pytest.fixture(scope="session")
def wheel_catalog(pytestconfig):
"""Run a mock server that serves pre-built wheels"""
with WheelCatalog() as catalog:
for wheel in TEST_WHEEL_DIR.glob("*.whl"):
catalog.add_wheel(wheel)
# Add wheels in the pyodide distribution so we can use it in the test.
# This is a workaround to get a wheel build with a same emscripten version that the Pyodide is build with,
# But probably we should find a better way that does not depend on Pyodide distribution.
dist_dir = Path(pytestconfig.getoption("dist_dir"))
for wheel in dist_dir.glob("*.whl"):
catalog.add_wheel(wheel, replace=False)
yield catalog
@pytest.fixture
def mock_platform(monkeypatch):
monkeypatch.setenv("_PYTHON_HOST_PLATFORM", PLATFORM)
from micropip import _utils
_utils.sys_tags.cache_clear()
monkeypatch.setattr(_utils, "get_platform", lambda: PLATFORM)
@pytest.fixture
def wheel_base(monkeypatch):
with TemporaryDirectory() as tmpdirname:
WHEEL_BASE = Path(tmpdirname).absolute()
import site
monkeypatch.setattr(
site, "getsitepackages", lambda: [WHEEL_BASE], raising=False
)
yield WHEEL_BASE
@pytest.fixture
def mock_importlib(monkeypatch, wheel_base):
import importlib.metadata
def _mock_importlib_from_name(name: str) -> Distribution:
dists = _mock_importlib_distributions()
for dist in dists:
if dist.name == name:
return dist
raise PackageNotFoundError(name)
def _mock_importlib_version(name: str) -> str:
dists = _mock_importlib_distributions()
for dist in dists:
if dist.name == name:
return dist.version
raise PackageNotFoundError(name)
def _mock_importlib_distributions():
return (Distribution.at(p) for p in wheel_base.glob("*.dist-info")) # type: ignore[union-attr]
monkeypatch.setattr(importlib.metadata, "version", _mock_importlib_version)
monkeypatch.setattr(
importlib.metadata, "distributions", _mock_importlib_distributions
)
monkeypatch.setattr(
importlib.metadata.Distribution, "from_name", _mock_importlib_from_name
)
class Wildcard:
def __eq__(self, other):
return True
class mock_fetch_cls:
def __init__(self):
self.releases_map = {}
self.metadata_map = {}
self.top_level_map = {}
def _make_wheel_filename(
self, name: str, version: str, platform: str = "generic"
) -> str:
if platform == "generic":
platform_str = "py3-none-any"
elif platform == "emscripten":
platform_str = f"{CPVER}-{CPVER}-{PLATFORM}"
elif platform == "linux":
platform_str = f"{CPVER}-{CPVER}-manylinux_2_31_x86_64"
elif platform == "windows":
platform_str = f"{CPVER}-{CPVER}-win_amd64"
elif platform == "invalid":
platform_str = f"{CPVER}-{CPVER}-invalid"
else:
platform_str = platform
return f"{name.replace('-', '_').lower()}-{version}-{platform_str}.whl"
def __eq__(self, other):
return True
def add_pkg_version(
self,
name: str,
version: str = "1.0.0",
*,
requirements: list[str] | None = None,
extras: dict[str, list[str]] | None = None,
platform: str = "generic",
top_level: list[str] | None = None,
) -> None:
if requirements is None:
requirements = []
if extras is None:
extras = {}
if top_level is None:
top_level = []
if name not in self.releases_map:
self.releases_map[name] = {
"info": {
"name": name,
},
"releases": {},
}
releases = self.releases_map[name]["releases"]
filename = self._make_wheel_filename(name, version, platform)
releases[version] = [
{
"filename": filename,
"url": f"http://fake.domain/f/{filename}",
"digests": {
"sha256": Wildcard(),
},
}
]
metadata = [("Name", name), ("Version", version)] + [
("Requires-Dist", req) for req in requirements
]
for extra, reqs in extras.items():
metadata += [("Provides-Extra", extra)] + [
("Requires-Dist", f"{req}; extra == {extra!r}") for req in reqs
]
self.metadata_map[filename] = metadata
self.top_level_map[filename] = top_level
async def query_package(self, pkgname, index_urls, kwargs):
from micropip.package_index import ProjectInfo
try:
return ProjectInfo.from_json_api(self.releases_map[pkgname])
except KeyError as e:
raise ValueError(
f"Can't fetch metadata for '{pkgname}' from PyPI. "
"Please make sure you have entered a correct package name."
) from e
async def _fetch_bytes(self, url, kwargs):
from micropip.transaction import WheelInfo
wheel_info = WheelInfo.from_url(url)
version = wheel_info.version
name = wheel_info.name
filename = wheel_info.filename
metadata = self.metadata_map[filename]
metadata_str = "\n".join(": ".join(x) for x in metadata)
toplevel = self.top_level_map[filename]
toplevel_str = "\n".join(toplevel)
metadata_dir = f"{name}-{version}.dist-info"
tmp = io.BytesIO()
with zipfile.ZipFile(tmp, "w", zipfile.ZIP_DEFLATED) as archive:
def write_file(filename, contents):
archive.writestr(f"{metadata_dir}/{filename}", contents)
write_file("METADATA", metadata_str)
write_file("WHEEL", "Wheel-Version: 1.0")
write_file("top_level.txt", toplevel_str)
tmp.seek(0)
return tmp.read()
@pytest.fixture
def mock_fetch(monkeypatch, mock_importlib):
pytest.importorskip("packaging")
from micropip import package_index, wheelinfo
result = mock_fetch_cls()
monkeypatch.setattr(package_index, "query_package", result.query_package)
monkeypatch.setattr(wheelinfo, "fetch_bytes", result._fetch_bytes)
return result
def _mock_package_index_gen(
httpserver,
pkgs=("black", "pytest", "numpy", "pytz", "snowballstemmer"),
pkgs_not_found=(),
content_type="application/json",
suffix="_json.json",
):
# Run a mock server that serves as a package index
import secrets
base = secrets.token_hex(16)
for pkg in pkgs:
data = (TEST_PYPI_RESPONSE_DIR / f"{pkg}{suffix}").read_bytes()
httpserver.expect_request(f"/{base}/{pkg}/").respond_with_data(
data,
content_type=content_type,
headers={"Access-Control-Allow-Origin": "*"},
)
for pkg in pkgs_not_found:
httpserver.expect_request(f"/{base}/{pkg}/").respond_with_data(
"Not found", status=404, content_type="text/plain"
)
index_url = httpserver.url_for(base)
return index_url
@pytest.fixture
def mock_package_index_json_api(httpserver):
return functools.partial(
_mock_package_index_gen,
httpserver=httpserver,
suffix="_json.json",
content_type="application/json",
)
@pytest.fixture
def mock_package_index_simple_json_api(httpserver):
return functools.partial(
_mock_package_index_gen,
httpserver=httpserver,
suffix="_simple.json",
content_type="application/vnd.pypi.simple.v1+json",
)
@pytest.fixture
def mock_package_index_simple_html_api(httpserver):
return functools.partial(
_mock_package_index_gen,
httpserver=httpserver,
suffix="_simple.html",
content_type="text/html",
)