Files
cibuildwheel/test/test_pyodide.py
T
d2c0e59833 feat: pyemscripten platform tag (PEP 783), 314.0a1, pyodide-eol flag, and maintenance updates (#2812)
* Use `pyodide config get emscripten_dir`

* Update constraints

* Updates for `pyemscripten` platform tag

* Weird constraints stuff...

* Revert "Weird constraints stuff..."

This reverts commit 4bd785de872269c0b27362ee800f17288b2b4789.

* Add some special constraints for pyodide

* Wheel pyemscripten collision

* Put delocate back in constraints (not needed tho)

* Drop Pyodide 0.27.7 (cp312-pyodide_wasm32)

* Add Pyodide 314.0.0a1 (cp314-pyodide_wasm32)

* Delete no-longer-needed Pyodide 312 constraints

* Update Pyodide 313 and 314 constraints

* Add back prerelease stuff and update tests

* Fix `test_pyodide_on_windows`

* Update and fix tests some more

* Also enable pyodide-prerelease in GHA sample build

* Update tests yet again (`SINGLE_PYTHON_VERSION` changes)

* Add a Pyodide-specific maintenance guide

* Add suggestions from Hood and Gyeongjae

Co-Authored-By: Hood Chatham <roberthoodchatham@gmail.com>
Co-Authored-By: Gyeongjae Choi <def6488@gmail.com>

* Add "Last updated: May 2026" to Pyodide document

* Add a Pyodide EOL filter

* Fix typo

* Update maintenance docs about EoL Pyodide

* Add Pyodide 312 constraints back

* Update Pyodide 313 and 314 constraints

* Remove "experimental" note about Pyodide

* Remove another experimental Pyodide sentence

* Add docs about the `pyodide-eol` enable option

---------

Co-authored-by: Hood Chatham <roberthoodchatham@gmail.com>
Co-authored-by: Gyeongjae Choi <def6488@gmail.com>
Co-authored-by: Henry Schreiner <henryfs@princeton.edu>
2026-05-09 14:01:41 -04:00

176 lines
5.6 KiB
Python

import contextlib
import os
import subprocess
import sys
import textwrap
from pathlib import Path
import pytest
from cibuildwheel.util.file import CIBW_CACHE_PATH
from . import test_projects, utils
pytestmark = pytest.mark.pyodide
CIBW_PLATFORM = os.environ.get("CIBW_PLATFORM", "pyodide")
if CIBW_PLATFORM != "pyodide":
pytest.skip(f"{CIBW_PLATFORM=}", allow_module_level=True)
basic_project = test_projects.new_c_project()
basic_project.files["check_node.py"] = r"""
import sys
import shutil
from pathlib import Path
from pyodide.code import run_js
def check_node():
# cibuildwheel adds a pinned node version to the PATH
# check it's in the PATH then, check it's the one that runs Pyodide
cibw_cache_path = Path(sys.argv[1]).resolve(strict=True)
# find the node executable in PATH
node = shutil.which("node")
assert node is not None, "node is None"
node_path = Path(node).resolve(strict=True)
# it shall be in cibuildwheel cache
assert cibw_cache_path in node_path.parents, f"{cibw_cache_path} not a parent of {node_path}"
# find the path to the node executable that runs pyodide
node_js = run_js("globalThis.process.execPath")
assert node_js is not None, "node_js is None"
node_js_path = Path(node_js).resolve(strict=True)
# it shall be the one pinned by cibuildwheel
assert node_js_path == node_path, f"{node_js_path} != {node_path}"
if __name__ == "__main__":
check_node()
"""
@pytest.mark.parametrize("use_pyproject_toml", [True, False])
def test_pyodide_build(tmp_path: Path, use_pyproject_toml: bool) -> None:
if sys.platform == "win32":
pytest.skip("pyodide-build doesn't work correctly on Windows")
if use_pyproject_toml:
basic_project.files["pyproject.toml"] = textwrap.dedent(
"""
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
"""
)
project_dir = tmp_path / "project"
basic_project.generate(project_dir)
# check for node in 1 case only to reduce CI load
add_env = {"CIBW_ENABLE": "pyodide-prerelease"}
if use_pyproject_toml:
add_env["CIBW_TEST_COMMAND"] = f"python {{project}}/check_node.py {CIBW_CACHE_PATH}"
# build the wheels
actual_wheels = utils.cibuildwheel_run(
project_dir,
add_args=["--platform", "pyodide"],
add_env=add_env,
)
# check that the expected wheels are produced
expected_wheels = [
"spam-0.1.0-cp313-cp313-pyemscripten_2025_0_wasm32.whl",
"spam-0.1.0-cp314-cp314-pyemscripten_2026_0_wasm32.whl",
]
print("actual_wheels", actual_wheels)
print("expected_wheels", expected_wheels)
assert set(actual_wheels) == set(expected_wheels)
def test_pyodide_version_incompatible(tmp_path: Path, capfd: pytest.CaptureFixture[str]) -> None:
if sys.platform == "win32":
pytest.skip("pyodide-build doesn't work correctly on Windows")
basic_project.generate(tmp_path)
with pytest.raises(subprocess.CalledProcessError):
utils.cibuildwheel_run(
tmp_path,
add_args=["--platform", "pyodide"],
add_env={
"CIBW_DEPENDENCY_VERSIONS": "packages: pyodide-build==0.31.2",
"CIBW_PYODIDE_VERSION": "0.26.0a6",
},
)
_, err = capfd.readouterr()
assert "is not compatible with the pyodide-build version" in err
@pytest.mark.parametrize("expect_failure", [True, False])
def test_pyodide_build_and_test(tmp_path: Path, expect_failure: bool) -> None:
if sys.platform == "win32":
pytest.skip("pyodide-build doesn't work correctly on Windows")
if expect_failure:
basic_project.files["test/spam_test.py"] = textwrap.dedent(r"""
def test_filter() -> None:
assert 0 == 1
""")
else:
basic_project.files["test/spam_test.py"] = textwrap.dedent(r"""
import spam
def test_filter() -> None:
assert spam.filter("spam") == 0
""")
basic_project.generate(tmp_path)
context = (
pytest.raises(subprocess.CalledProcessError) if expect_failure else contextlib.nullcontext()
)
with context:
# build the wheels
actual_wheels = utils.cibuildwheel_run(
tmp_path,
add_args=["--platform", "pyodide"],
add_env={
"CIBW_TEST_REQUIRES": "pytest",
"CIBW_TEST_COMMAND": "python -m pytest {project}",
"CIBW_ENABLE": "pyodide-prerelease",
},
)
# check that the expected wheels are produced
expected_wheels = [
"spam-0.1.0-cp313-cp313-pyemscripten_2025_0_wasm32.whl",
"spam-0.1.0-cp314-cp314-pyemscripten_2026_0_wasm32.whl",
]
assert set(actual_wheels) == set(expected_wheels)
def test_pyodide_repair_wheel(tmp_path: Path) -> None:
if sys.platform == "win32":
pytest.skip("pyodide-build doesn't work correctly on Windows")
project_dir = tmp_path / "project"
basic_project.generate(project_dir)
actual_wheels = utils.cibuildwheel_run(
project_dir,
add_args=["--platform", "pyodide"],
add_env={
"CIBW_REPAIR_WHEEL_COMMAND_PYODIDE": (
"pyodide auditwheel repair --libdir /path/to/libraries --output-dir {dest_dir} {wheel}"
),
},
single_python=True,
)
# check that the expected wheels are produced
expected_wheels = [
"spam-0.1.0-cp313-cp313-pyemscripten_2025_0_wasm32.whl",
]
assert set(actual_wheels) == set(expected_wheels)