forked from jupyterlab/jupyter-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_core_path.py
More file actions
397 lines (304 loc) · 13.7 KB
/
Copy pathtest_core_path.py
File metadata and controls
397 lines (304 loc) · 13.7 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
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import io
import json
import tarfile
import urllib.error
from pathlib import Path
import pytest
from jupyter_builder import core_path, federated_extensions
from jupyter_builder.federated_extensions import (
_check_node_version,
_ensure_builder,
_read_rspack_node_range,
)
def _make_core_package_tarball(content: bytes) -> bytes:
buf = io.BytesIO()
with tarfile.open(fileobj=buf, mode="w:gz") as tar:
info = tarfile.TarInfo(name="package/core.package.json")
info.size = len(content)
tar.addfile(info, io.BytesIO(content))
return buf.getvalue()
def test_get_core_meta_prefers_installed_core_meta(tmp_path):
ext_path = tmp_path / "ext"
core_meta_dir = ext_path / "node_modules" / "@jupyterlab" / "core-meta"
core_meta_dir.mkdir(parents=True)
(core_meta_dir / "core.package.json").write_text("{}")
location = core_path.get_core_meta(ext_path=str(ext_path))
assert location == str(core_meta_dir / "core.package.json")
def test_get_core_meta_installs_dependencies_before_searching(tmp_path, monkeypatch):
ext_path = tmp_path / "ext"
ext_path.mkdir()
calls = []
def fake_check_call(args, cwd):
calls.append((args, Path(cwd)))
core_meta_dir = Path(cwd) / "node_modules" / "@jupyterlab" / "core-meta"
core_meta_dir.mkdir(parents=True)
(core_meta_dir / "core.package.json").write_text("{}")
monkeypatch.setattr(core_path.subprocess, "check_call", fake_check_call)
location = core_path.get_core_meta(ext_path=ext_path)
assert calls == [(["jlpm"], ext_path)]
assert location == str(
ext_path / "node_modules" / "@jupyterlab" / "core-meta" / "core.package.json",
)
def test_get_core_meta_uses_cache_for_explicit_version_before_installed(tmp_path, monkeypatch):
ext_path = tmp_path / "ext"
core_meta_dir = ext_path / "node_modules" / "@jupyterlab" / "core-meta"
core_meta_dir.mkdir(parents=True)
(core_meta_dir / "core.package.json").write_text("{}")
monkeypatch.setenv("HOME", str(tmp_path))
cached_file = (
tmp_path / ".cache" / "jupyterlab_builder" / "core" / "v-test" / "core.package.json"
)
cached_file.parent.mkdir(parents=True)
cached_file.write_text('{"from": "cache"}')
location = core_path.get_core_meta(version="v-test", ext_path=ext_path)
assert location == str(cached_file)
def test_get_core_meta_fetches_npm_core_meta_before_github(tmp_path, monkeypatch):
ext_path = tmp_path / "ext"
ext_path.mkdir()
monkeypatch.setenv("HOME", str(tmp_path))
def fake_check_call(_args, cwd):
(Path(cwd) / "node_modules").mkdir(parents=True)
tarball_content = b'{"devDependencies": {}}'
tarball_bytes = _make_core_package_tarball(tarball_content)
tarball_url = f"{core_path.JPBLD_NPM_URL}/@jupyterlab/core-meta/-/core-meta-4.6.0-alpha.4.tgz"
registry_meta = json.dumps({"dist": {"tarball": tarball_url}}).encode()
calls = []
def fake_urlopen(req_or_url, **_kwargs):
url = getattr(req_or_url, "full_url", req_or_url)
calls.append(url)
if url == f"{core_path.JPBLD_NPM_URL}/@jupyterlab/core-meta/4.6.0-alpha.4":
return io.BytesIO(registry_meta)
if url == tarball_url:
return io.BytesIO(tarball_bytes)
msg = f"Unexpected URL {url}"
raise AssertionError(msg)
monkeypatch.setattr(core_path.subprocess, "check_call", fake_check_call)
monkeypatch.setattr(core_path.urllib.request, "urlopen", fake_urlopen)
location = core_path.get_core_meta(version="4.6.0-alpha.4", ext_path=ext_path)
assert calls == [
f"{core_path.JPBLD_NPM_URL}/@jupyterlab/core-meta/4.6.0-alpha.4",
tarball_url,
]
assert location == str(
tmp_path / ".cache" / "jupyterlab_builder" / "core" / "4.6.0-alpha.4" / "core.package.json",
)
assert json.loads(
(
tmp_path
/ ".cache"
/ "jupyterlab_builder"
/ "core"
/ "4.6.0-alpha.4"
/ "core.package.json"
).read_text(),
) == {"devDependencies": {}}
def test_get_core_meta_falls_back_to_github_when_npm_fails(tmp_path, monkeypatch):
ext_path = tmp_path / "ext"
ext_path.mkdir()
monkeypatch.setenv("HOME", str(tmp_path))
github_url = (
"https://raw.githubusercontent.com/"
"jupyterlab/jupyterlab/4.6.0-alpha.4/"
"jupyterlab/staging/package.json"
)
calls = []
def fake_urlopen(req_or_url, **_kwargs):
url = getattr(req_or_url, "full_url", req_or_url)
calls.append(url)
if url == f"{core_path.JPBLD_NPM_URL}/@jupyterlab/core-meta/4.6.0-alpha.4":
msg = "Not Found"
raise urllib.error.URLError(msg)
if url == github_url:
return io.BytesIO(b'{"dependencies": {}}')
msg = f"Unexpected URL {url}"
raise AssertionError(msg)
monkeypatch.setattr(core_path.urllib.request, "urlopen", fake_urlopen)
location = core_path.get_core_meta(version="4.6.0-alpha.4", ext_path=ext_path)
assert calls == [
f"{core_path.JPBLD_NPM_URL}/@jupyterlab/core-meta/4.6.0-alpha.4",
github_url,
]
assert location == str(
tmp_path / ".cache" / "jupyterlab_builder" / "core" / "4.6.0-alpha.4" / "core.package.json",
)
def test_get_core_meta_wildcard_version_resolves_from_npm_and_downloads_core_meta(
tmp_path,
monkeypatch,
):
ext_path = tmp_path / "ext"
ext_path.mkdir()
monkeypatch.setenv("HOME", str(tmp_path))
tarball_content = b'{"devDependencies": {}}'
tarball_bytes = _make_core_package_tarball(tarball_content)
tarball_url = f"{core_path.JPBLD_NPM_URL}/@jupyterlab/core-meta/-/core-meta-4.5.3.tgz"
registry_meta = json.dumps({"dist": {"tarball": tarball_url}}).encode()
calls = []
def fake_urlopen(req_or_url, **_kwargs):
url = getattr(req_or_url, "full_url", req_or_url)
calls.append(url)
if url == f"{core_path.JPBLD_NPM_URL}/@jupyterlab/core-meta":
return io.BytesIO(
json.dumps({"versions": {"4.5.0": {}, "4.5.3": {}, "4.6.0": {}}}).encode(),
)
if url == f"{core_path.JPBLD_NPM_URL}/@jupyterlab/core-meta/4.5.3":
return io.BytesIO(registry_meta)
if url == tarball_url:
return io.BytesIO(tarball_bytes)
msg = f"Unexpected URL {url}"
raise AssertionError(msg)
monkeypatch.setattr(core_path.urllib.request, "urlopen", fake_urlopen)
location = core_path.get_core_meta(version="4.5.x", ext_path=ext_path)
assert calls == [
f"{core_path.JPBLD_NPM_URL}/@jupyterlab/core-meta",
f"{core_path.JPBLD_NPM_URL}/@jupyterlab/core-meta/4.5.3",
tarball_url,
]
assert location == str(
tmp_path / ".cache" / "jupyterlab_builder" / "core" / "4.5.3" / "core.package.json",
)
def test_resolve_wildcard_npm_version_returns_highest_match(monkeypatch):
def fake_urlopen(req_or_url, **_kwargs):
url = getattr(req_or_url, "full_url", req_or_url)
assert url == f"{core_path.JPBLD_NPM_URL}/@jupyterlab/core-meta"
return io.BytesIO(
json.dumps(
{
"versions": {
"4.4.9": {},
"4.5.1": {},
"4.5.12": {},
"4.6.0-alpha.4": {},
},
},
).encode(),
)
monkeypatch.setattr(core_path.urllib.request, "urlopen", fake_urlopen)
resolved = core_path._resolve_wildcard_npm_version("4.5.x")
assert resolved == "4.5.12"
def test_resolve_wildcard_npm_version_matches_prerelease_versions(monkeypatch):
def fake_urlopen(req_or_url, **_kwargs):
url = getattr(req_or_url, "full_url", req_or_url)
assert url == f"{core_path.JPBLD_NPM_URL}/@jupyterlab/core-meta"
return io.BytesIO(
json.dumps(
{
"versions": {
"4.6.0-alpha.3": {},
"4.6.0-alpha.4": {},
"4.6.0-alpha.5": {},
},
},
).encode(),
)
monkeypatch.setattr(core_path.urllib.request, "urlopen", fake_urlopen)
resolved = core_path._resolve_wildcard_npm_version("4.6.x")
assert resolved == "4.6.0-alpha.5"
def test_resolve_wildcard_npm_version_raises_when_no_matches(monkeypatch):
monkeypatch.setattr(
core_path.urllib.request,
"urlopen",
lambda *_args, **_kwargs: io.BytesIO(
json.dumps({"versions": {"4.6.0": {}, "5.0.0": {}}}).encode(),
),
)
with pytest.raises(urllib.error.URLError, match="No published @jupyterlab/core-meta"):
core_path._resolve_wildcard_npm_version("4.5.x")
def test_get_core_meta_latest_uses_npm_latest_and_caches_by_resolved_version(tmp_path, monkeypatch):
ext_path = tmp_path / "ext"
ext_path.mkdir()
monkeypatch.setenv("HOME", str(tmp_path))
tarball_content = b'{"devDependencies": {}}'
tarball_bytes = _make_core_package_tarball(tarball_content)
tarball_url = f"{core_path.JPBLD_NPM_URL}/@jupyterlab/core-meta/-/core-meta-4.6.0-alpha.4.tgz"
registry_meta = json.dumps({"dist": {"tarball": tarball_url}}).encode()
calls = []
def fake_urlopen(req_or_url, **_kwargs):
url = getattr(req_or_url, "full_url", req_or_url)
calls.append(url)
if url == f"{core_path.JPBLD_NPM_URL}/@jupyterlab/core-meta/latest":
return io.BytesIO(json.dumps({"version": "4.6.0-alpha.4"}).encode())
if url == f"{core_path.JPBLD_NPM_URL}/@jupyterlab/core-meta/4.6.0-alpha.4":
return io.BytesIO(registry_meta)
if url == tarball_url:
return io.BytesIO(tarball_bytes)
msg = f"Unexpected URL {url}"
raise AssertionError(msg)
monkeypatch.setattr(core_path.urllib.request, "urlopen", fake_urlopen)
location = core_path.get_core_meta(version="latest", ext_path=ext_path)
assert calls == [
f"{core_path.JPBLD_NPM_URL}/@jupyterlab/core-meta/latest",
f"{core_path.JPBLD_NPM_URL}/@jupyterlab/core-meta/4.6.0-alpha.4",
tarball_url,
]
assert location == str(
tmp_path / ".cache" / "jupyterlab_builder" / "core" / "4.6.0-alpha.4" / "core.package.json",
)
def test_ensure_builder_with_jupyter_builder(tmp_path):
ext_path = tmp_path / "ext"
core_path_dir = tmp_path / "core-meta"
core_package_file = core_path_dir / "core.package.json"
builder_dir = ext_path / "node_modules" / "@jupyter" / "builder"
builder_dir.mkdir(parents=True)
core_path_dir.mkdir()
core_package_file.write_text(json.dumps({"devDependencies": {"@jupyter/builder": "^5.0.0"}}))
(ext_path / "package.json").write_text(
json.dumps({"devDependencies": {"@jupyter/builder": "^5.0.0"}}),
)
(builder_dir / "package.json").write_text(json.dumps({"version": "5.0.0"}))
builder_path, marker_pkg = _ensure_builder(str(ext_path), str(core_package_file))
assert builder_path == str(builder_dir / "lib" / "build-labextension.js")
assert marker_pkg == "@jupyter/builder"
def test_ensure_builder_with_jupyterlab_builder(tmp_path):
ext_path = tmp_path / "ext"
core_path_dir = tmp_path / "core-meta"
core_package_file = core_path_dir / "core.package.json"
builder_dir = ext_path / "node_modules" / "@jupyterlab" / "builder"
builder_dir.mkdir(parents=True)
core_path_dir.mkdir()
core_package_file.write_text(json.dumps({"devDependencies": {"@jupyterlab/builder": "^5.0.0"}}))
(ext_path / "package.json").write_text(
json.dumps({"devDependencies": {"@jupyterlab/builder": "^5.0.0"}}),
)
(builder_dir / "package.json").write_text(json.dumps({"version": "5.0.0"}))
builder_path, marker_pkg = _ensure_builder(str(ext_path), str(core_package_file))
assert builder_path == str(builder_dir / "lib" / "build-labextension.js")
assert marker_pkg == "@jupyterlab/builder"
def _write_rspack(ext_path: Path, node_range: str) -> None:
rspack_dir = ext_path / "node_modules" / "@rspack" / "core"
rspack_dir.mkdir(parents=True)
(rspack_dir / "package.json").write_text(json.dumps({"engines": {"node": node_range}}))
def test_read_rspack_node_range_reads_engines(tmp_path):
ext_path = tmp_path / "ext"
ext_path.mkdir()
_write_rspack(ext_path, "^20.19.0 || >=22.12.0")
builder = str(ext_path / "node_modules" / "@jupyter" / "builder" / "lib" / "x.js")
assert _read_rspack_node_range(builder, str(ext_path)) == "^20.19.0 || >=22.12.0"
def test_read_rspack_node_range_falls_back_when_missing(tmp_path):
ext_path = tmp_path / "ext"
ext_path.mkdir()
assert _read_rspack_node_range(str(ext_path), str(ext_path)) == "^20.19.0 || >=22.12.0"
def test_check_node_version_raises_on_old_node(tmp_path, monkeypatch):
ext_path = tmp_path / "ext"
ext_path.mkdir()
_write_rspack(ext_path, "^20.19.0 || >=22.12.0")
monkeypatch.setattr(federated_extensions, "_which_node_js", lambda: "node")
monkeypatch.setattr(
federated_extensions.subprocess,
"check_output",
lambda *_args, **_kwargs: b"v18.20.8\n",
)
with pytest.raises(RuntimeError, match=r"requires Node\.js .* \(found v18\.20\.8\)"):
_check_node_version(str(ext_path), str(ext_path))
def test_check_node_version_passes_on_supported_node(tmp_path, monkeypatch):
ext_path = tmp_path / "ext"
ext_path.mkdir()
_write_rspack(ext_path, "^20.19.0 || >=22.12.0")
monkeypatch.setattr(federated_extensions, "_which_node_js", lambda: "node")
monkeypatch.setattr(
federated_extensions.subprocess,
"check_output",
lambda *_args, **_kwargs: b"v22.12.0\n",
)
_check_node_version(str(ext_path), str(ext_path))