-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathtest_builder.py
More file actions
506 lines (412 loc) · 16 KB
/
Copy pathtest_builder.py
File metadata and controls
506 lines (412 loc) · 16 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
import shutil
import subprocess
import tarfile
import time
from pathlib import Path
from typing import Self
import pydantic
import pytest
from pyodide_build import common
from pyodide_build.build_env import BuildArgs, get_build_flag
from pyodide_build.recipe import builder as _builder
from pyodide_build.recipe.builder import (
RecipeBuilder,
RecipeBuilderPackage,
RecipeBuilderSharedLibrary,
RecipeBuilderStaticLibrary,
_load_recipe,
)
from pyodide_build.recipe.spec import _SourceSpec
RECIPE_DIR = Path(__file__).parent / "_test_recipes"
WHEEL_DIR = Path(__file__).parent.parent / "_test_wheels"
@pytest.fixture
def tmp_builder(tmp_path):
builder = RecipeBuilder.get_builder(
recipe=RECIPE_DIR / "pkg_1",
build_args=BuildArgs(),
build_dir=tmp_path,
force_rebuild=False,
continue_=False,
)
yield builder
def test_constructor(tmp_path):
builder = RecipeBuilder.get_builder(
recipe=RECIPE_DIR / "beautifulsoup4",
build_args=BuildArgs(),
build_dir=tmp_path / "beautifulsoup4" / "build",
force_rebuild=False,
continue_=False,
)
assert builder.name == "beautifulsoup4"
assert builder.version == "4.13.3"
assert builder.fullname == "beautifulsoup4-4.13.3"
assert builder.pkg_root == RECIPE_DIR / "beautifulsoup4"
assert builder.build_dir == tmp_path / "beautifulsoup4" / "build"
assert (
builder.src_extract_dir
== tmp_path / "beautifulsoup4" / "build" / "beautifulsoup4-4.13.3"
)
assert (
builder.src_dist_dir
== tmp_path / "beautifulsoup4" / "build" / "beautifulsoup4-4.13.3" / "dist"
)
assert builder.dist_dir == RECIPE_DIR / "beautifulsoup4" / "dist"
assert builder.library_install_prefix == tmp_path / ".libs"
def test_get_builder(tmp_path):
builder = RecipeBuilder.get_builder(
recipe=RECIPE_DIR / "pkg_1",
build_args=BuildArgs(),
build_dir=tmp_path,
force_rebuild=False,
continue_=False,
)
assert isinstance(builder, RecipeBuilder)
assert isinstance(builder, RecipeBuilderPackage)
builder = RecipeBuilder.get_builder(
recipe=RECIPE_DIR / "libtest",
build_args=BuildArgs(),
build_dir=tmp_path,
force_rebuild=False,
continue_=False,
)
assert isinstance(builder, RecipeBuilder)
assert isinstance(builder, RecipeBuilderStaticLibrary)
builder = RecipeBuilder.get_builder(
recipe=RECIPE_DIR / "libtest_shared",
build_args=BuildArgs(),
build_dir=tmp_path,
force_rebuild=False,
continue_=False,
)
assert isinstance(builder, RecipeBuilder)
assert isinstance(builder, RecipeBuilderSharedLibrary)
def test_load_recipe():
root, recipe = _load_recipe(RECIPE_DIR / "pkg_1")
assert root == RECIPE_DIR / "pkg_1"
assert recipe.package.name == "pkg_1"
root, recipe = _load_recipe(RECIPE_DIR / "pkg_1" / "meta.yaml")
assert root == RECIPE_DIR / "pkg_1"
assert recipe.package.name == "pkg_1"
def test_prepare_source(monkeypatch, tmp_path, dummy_xbuildenv):
class subprocess_result:
returncode = 0
stdout = ""
monkeypatch.setattr(subprocess, "run", lambda *args, **kwargs: subprocess_result)
monkeypatch.setattr(_builder, "check_checksum", lambda *args, **kwargs: True)
monkeypatch.setattr(shutil, "unpack_archive", lambda *args, **kwargs: True)
monkeypatch.setattr(shutil, "move", lambda *args, **kwargs: True)
test_pkgs = [
RECIPE_DIR / "packaging/meta.yaml",
# RECIPE_DIR / "micropip/meta.yaml",
]
for pkg in test_pkgs:
builder = RecipeBuilder.get_builder(
recipe=pkg,
build_args=BuildArgs(),
build_dir=tmp_path / "build",
)
builder._prepare_source()
assert builder.src_extract_dir.is_dir()
def test_check_executables(tmp_path, monkeypatch):
builder = RecipeBuilder.get_builder(
recipe=RECIPE_DIR / "pkg_test_executable",
build_args=BuildArgs(),
build_dir=tmp_path,
)
monkeypatch.setattr(
common, "find_missing_executables", lambda executables: ["echo"]
)
with pytest.raises(
RuntimeError, match="The following executables are required to build"
):
builder._check_executables()
def test_get_helper_vars(tmp_path):
builder = RecipeBuilder.get_builder(
recipe=RECIPE_DIR / "pkg_1",
build_args=BuildArgs(),
build_dir=tmp_path / "pkg_1" / "build",
)
helper_vars = builder._get_helper_vars()
assert helper_vars["PKGDIR"] == str(RECIPE_DIR / "pkg_1")
assert helper_vars["PKG_VERSION"] == "1.0.0"
assert helper_vars["PKG_BUILD_DIR"] == str(
tmp_path / "pkg_1" / "build" / "pkg_1-1.0.0"
)
assert helper_vars["DISTDIR"] == str(
tmp_path / "pkg_1" / "build" / "pkg_1-1.0.0" / "dist"
)
assert helper_vars["WASM_LIBRARY_DIR"] == str(tmp_path / ".libs")
assert helper_vars["EM_PKG_CONFIG_PATH"] == str(
tmp_path / ".libs" / "lib" / "pkgconfig"
)
assert helper_vars["PKG_CONFIG_LIBDIR"] == str(
tmp_path / ".libs" / "lib" / "pkgconfig"
)
def test_create_constraints_file_no_override(tmp_path, dummy_xbuildenv):
builder = RecipeBuilder.get_builder(
recipe=RECIPE_DIR
/ "pkg_test_executable", # constraints not set, so no override
build_args=BuildArgs(),
build_dir=tmp_path,
)
path = builder._create_constraints_file()
assert path == get_build_flag("PIP_CONSTRAINT")
def test_create_constraints_file_override(tmp_path, dummy_xbuildenv):
builder = RecipeBuilder.get_builder(
recipe=RECIPE_DIR / "pkg_test_constraint",
build_args=BuildArgs(),
build_dir=tmp_path,
)
paths = builder._create_constraints_file()
assert paths == get_build_flag("PIP_CONSTRAINT") + " " + str(
tmp_path / "constraints.txt"
)
data = Path(paths.split()[-1]).read_text().strip().split("\n")
assert data[-3:] == ["numpy < 2.0", "pytest == 7.0", "setuptools < 75"], data
class MockSourceSpec(_SourceSpec):
@pydantic.model_validator(mode="after")
def _check_patches_extra(self) -> Self:
return self
@pytest.mark.parametrize("is_wheel", [False, True])
def test_needs_rebuild(tmpdir, is_wheel):
pkg_root = Path(tmpdir)
buildpath = pkg_root / "build"
meta_yaml = pkg_root / "meta.yaml"
version = "12"
if is_wheel:
dist_dir = pkg_root / "dist"
dist_dir.mkdir()
# Build of current version with wrong abi
(dist_dir / "regex-12-cp311-cp311-pyemscripten_2024_0_wasm32.whl").touch()
# Build of old version with current abi
(dist_dir / "regex-11-cp312-cp312-pyemscripten_2024_0_wasm32.whl").touch()
# the version we're trying to build
packaged = dist_dir / "regex-12-cp312-cp312-pyemscripten_2024_0_wasm32.whl"
else:
packaged = buildpath / ".packaged"
patch_file = pkg_root / "patch"
extra_file = pkg_root / "extra"
src_path = pkg_root / "src"
src_path_file = src_path / "file"
source_metadata = MockSourceSpec(
patches=[
str(patch_file),
],
extras=[
(str(extra_file), ""),
],
path=str(src_path),
)
buildpath.mkdir()
meta_yaml.touch()
patch_file.touch()
extra_file.touch()
src_path.mkdir()
src_path_file.touch()
# No .packaged file, rebuild
assert _builder.needs_rebuild(
pkg_root, buildpath, source_metadata, is_wheel, version
)
# .packaged file exists, no rebuild
packaged.touch()
assert not _builder.needs_rebuild(
pkg_root, buildpath, source_metadata, is_wheel, version
)
# newer meta.yaml file, rebuild
packaged.touch()
time.sleep(0.01)
meta_yaml.touch()
assert _builder.needs_rebuild(
pkg_root, buildpath, source_metadata, is_wheel, version
)
# newer patch file, rebuild
packaged.touch()
time.sleep(0.01)
patch_file.touch()
assert _builder.needs_rebuild(
pkg_root, buildpath, source_metadata, is_wheel, version
)
# newer extra file, rebuild
packaged.touch()
time.sleep(0.01)
extra_file.touch()
assert _builder.needs_rebuild(
pkg_root, buildpath, source_metadata, is_wheel, version
)
# newer source path, rebuild
packaged.touch()
time.sleep(0.01)
src_path_file.touch()
assert _builder.needs_rebuild(
pkg_root, buildpath, source_metadata, is_wheel, version
)
# newer .packaged file, no rebuild
packaged.touch()
assert not _builder.needs_rebuild(
pkg_root, buildpath, source_metadata, is_wheel, version
)
@pytest.mark.parametrize("modify_rpath", [False, True])
def test_copy_sharedlib(tmp_path, modify_rpath):
wheel_file_name = "sharedlib_test_py-1.0-cp310-cp310-emscripten_3_1_21_wasm32.whl"
wheel = WHEEL_DIR / "wheel" / wheel_file_name
libdir = WHEEL_DIR / "lib"
wheel_copy = tmp_path / wheel_file_name
shutil.copy(wheel, wheel_copy)
common.unpack_wheel(wheel_copy)
name, ver, _ = wheel.name.split("-", 2)
wheel_dir_name = f"{name}-{ver}"
wheel_dir = tmp_path / wheel_dir_name
dep_map = _builder.copy_sharedlibs(wheel_copy, wheel_dir, libdir, modify_rpath)
deps = ("sharedlib-test.so", "sharedlib-test-dep.so", "sharedlib-test-dep2.so")
for dep in deps:
assert dep in dep_map
def test_extract_tarballname():
url = "https://www.test.com/ball.tar.gz"
headers = [
{},
{"Content-Disposition": "inline"},
{"Content-Disposition": "attachment"},
{"Content-Disposition": 'attachment; filename="ball 2.tar.gz"'},
{"Content-Disposition": "attachment; filename*=UTF-8''ball%203.tar.gz"},
]
tarballnames = [
"ball.tar.gz",
"ball.tar.gz",
"ball.tar.gz",
"ball 2.tar.gz",
"ball 3.tar.gz",
]
for header, tarballname in zip(headers, tarballnames, strict=True):
assert _builder._extract_tarballname(url, header) == tarballname
class TestInstallToLibraryDir:
"""Tests for _install_to_library_dir method."""
def test_copies_distdir_to_library_install_prefix(self, tmp_path):
builder = RecipeBuilder.get_builder(
recipe=RECIPE_DIR / "libtest",
build_args=BuildArgs(),
build_dir=tmp_path / "libtest" / "build",
)
# Create fake artifacts in src_dist_dir
builder.src_dist_dir.mkdir(parents=True, exist_ok=True)
lib_dir = builder.src_dist_dir / "lib"
include_dir = builder.src_dist_dir / "include"
lib_dir.mkdir()
include_dir.mkdir()
(lib_dir / "libtest.a").write_text("fake static lib")
(include_dir / "test.h").write_text("fake header")
builder._install_to_library_dir()
# Verify artifacts were copied to library_install_prefix
assert (builder.library_install_prefix / "lib" / "libtest.a").exists()
assert (
builder.library_install_prefix / "lib" / "libtest.a"
).read_text() == "fake static lib"
assert (builder.library_install_prefix / "include" / "test.h").exists()
assert (
builder.library_install_prefix / "include" / "test.h"
).read_text() == "fake header"
def test_noop_when_distdir_missing(self, tmp_path):
builder = RecipeBuilder.get_builder(
recipe=RECIPE_DIR / "libtest",
build_args=BuildArgs(),
build_dir=tmp_path / "libtest" / "build",
)
# src_dist_dir does not exist, should not raise
assert not builder.src_dist_dir.exists()
builder._install_to_library_dir()
assert not builder.library_install_prefix.exists()
def test_merges_with_existing_library_dir(self, tmp_path):
builder = RecipeBuilder.get_builder(
recipe=RECIPE_DIR / "libtest",
build_args=BuildArgs(),
build_dir=tmp_path / "libtest" / "build",
)
# Pre-populate library_install_prefix with existing artifacts
lib_dir = builder.library_install_prefix / "lib"
lib_dir.mkdir(parents=True)
(lib_dir / "libexisting.a").write_text("existing lib")
# Create new artifacts in src_dist_dir
builder.src_dist_dir.mkdir(parents=True, exist_ok=True)
dist_lib_dir = builder.src_dist_dir / "lib"
dist_lib_dir.mkdir()
(dist_lib_dir / "libtest.a").write_text("new lib")
builder._install_to_library_dir()
# Both old and new artifacts should exist
assert (
builder.library_install_prefix / "lib" / "libexisting.a"
).read_text() == "existing lib"
assert (
builder.library_install_prefix / "lib" / "libtest.a"
).read_text() == "new lib"
class TestCreateLibraryArchive:
"""Tests for _create_library_archive method."""
@pytest.fixture(autouse=True)
def _cleanup_dist_dir(self):
dist_dir = RECIPE_DIR / "libtest" / "dist"
yield
shutil.rmtree(dist_dir, ignore_errors=True)
def _populate_dist_dir(self, builder):
"""Set up a dist_dir with typical FHS-structured library artifacts."""
builder.dist_dir.mkdir(parents=True, exist_ok=True)
lib_dir = builder.dist_dir / "lib"
include_dir = builder.dist_dir / "include"
pkgconfig_dir = lib_dir / "pkgconfig"
lib_dir.mkdir()
include_dir.mkdir()
pkgconfig_dir.mkdir(parents=True)
(lib_dir / "libtest.a").write_text("fake static lib")
(include_dir / "test.h").write_text("fake header")
(pkgconfig_dir / "test.pc").write_text("Name: test\nVersion: 1.0.0\n")
def test_creates_tar_gz_archive(self, tmp_path):
builder = RecipeBuilder.get_builder(
recipe=RECIPE_DIR / "libtest",
build_args=BuildArgs(),
build_dir=tmp_path / "libtest" / "build",
)
self._populate_dist_dir(builder)
archive_path = builder._create_library_archive()
assert archive_path is not None
assert archive_path.exists()
assert archive_path.name == "libtest-1.0.0-wasm32.tar.gz"
assert archive_path.parent == builder.dist_dir
def test_archive_contains_all_artifacts(self, tmp_path):
builder = RecipeBuilder.get_builder(
recipe=RECIPE_DIR / "libtest",
build_args=BuildArgs(),
build_dir=tmp_path / "libtest" / "build",
)
self._populate_dist_dir(builder)
archive_path = builder._create_library_archive()
with tarfile.open(archive_path, "r:gz") as tf:
names = {m.name for m in tf.getmembers() if m.isfile()}
assert "./lib/libtest.a" in names
assert "./include/test.h" in names
assert "./lib/pkgconfig/test.pc" in names
def test_returns_none_when_dist_dir_missing(self, tmp_path):
builder = RecipeBuilder.get_builder(
recipe=RECIPE_DIR / "libtest",
build_args=BuildArgs(),
build_dir=tmp_path / "libtest" / "build",
)
assert not builder.dist_dir.exists()
assert builder._create_library_archive() is None
def test_returns_none_when_dist_dir_empty(self, tmp_path):
builder = RecipeBuilder.get_builder(
recipe=RECIPE_DIR / "libtest",
build_args=BuildArgs(),
build_dir=tmp_path / "libtest" / "build",
)
builder.dist_dir.mkdir(parents=True)
assert builder._create_library_archive() is None
def test_archive_is_extractable_with_correct_layout(self, tmp_path):
builder = RecipeBuilder.get_builder(
recipe=RECIPE_DIR / "libtest",
build_args=BuildArgs(),
build_dir=tmp_path / "libtest" / "build",
)
self._populate_dist_dir(builder)
archive_path = builder._create_library_archive()
extract_dir = tmp_path / "extracted"
shutil.unpack_archive(archive_path, extract_dir)
assert (extract_dir / "lib" / "libtest.a").read_text() == "fake static lib"
assert (extract_dir / "include" / "test.h").read_text() == "fake header"
assert (extract_dir / "lib" / "pkgconfig" / "test.pc").exists()