2023-06-29 11:37:03 +00:00
|
|
|
import os
|
2023-02-22 02:56:35 +00:00
|
|
|
import shutil
|
2023-01-14 13:59:42 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import pytest
|
2023-06-29 16:45:06 +00:00
|
|
|
from pyodide_lock import PyodideLockSpec
|
2023-01-14 13:59:42 +00:00
|
|
|
|
2023-06-29 11:37:03 +00:00
|
|
|
from conftest import ROOT_PATH
|
|
|
|
from pyodide_build import build_env
|
|
|
|
from pyodide_build.common import chdir
|
2023-02-22 02:56:35 +00:00
|
|
|
|
2023-01-14 13:59:42 +00:00
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def temp_python_lib(tmp_path_factory):
|
|
|
|
libdir = tmp_path_factory.mktemp("python")
|
|
|
|
|
|
|
|
path = Path(libdir)
|
|
|
|
|
|
|
|
(path / "test").mkdir()
|
|
|
|
(path / "test" / "test_blah.py").touch()
|
|
|
|
(path / "distutils").mkdir()
|
|
|
|
(path / "turtle.py").touch()
|
|
|
|
|
|
|
|
(path / "module1.py").touch()
|
|
|
|
(path / "module2.py").touch()
|
|
|
|
|
|
|
|
(path / "hello_pyodide.py").write_text("def hello(): return 'hello'")
|
|
|
|
|
|
|
|
yield libdir
|
2023-02-22 02:56:35 +00:00
|
|
|
|
|
|
|
|
2023-03-04 10:05:12 +00:00
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def temp_python_lib2(tmp_path_factory):
|
|
|
|
libdir = tmp_path_factory.mktemp("python")
|
|
|
|
|
|
|
|
path = Path(libdir)
|
|
|
|
|
|
|
|
(path / "module3.py").touch()
|
|
|
|
(path / "module4.py").touch()
|
|
|
|
|
|
|
|
(path / "bye_pyodide.py").write_text("def bye(): return 'bye'")
|
|
|
|
|
|
|
|
yield libdir
|
|
|
|
|
|
|
|
|
2023-06-29 16:45:06 +00:00
|
|
|
def mock_pyodide_lock() -> PyodideLockSpec:
|
|
|
|
return PyodideLockSpec(
|
|
|
|
info={
|
2023-02-22 02:56:35 +00:00
|
|
|
"version": "0.22.1",
|
2023-06-29 16:45:06 +00:00
|
|
|
"arch": "wasm32",
|
|
|
|
"platform": "emscripten_xxx",
|
|
|
|
"python": "3.11",
|
2023-02-22 02:56:35 +00:00
|
|
|
},
|
2023-06-29 16:45:06 +00:00
|
|
|
packages={},
|
|
|
|
)
|
2023-02-22 02:56:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def temp_xbuildenv(tmp_path_factory):
|
|
|
|
"""
|
|
|
|
Create a temporary xbuildenv archive
|
|
|
|
"""
|
|
|
|
base = tmp_path_factory.mktemp("base")
|
|
|
|
|
|
|
|
path = Path(base)
|
|
|
|
|
|
|
|
xbuildenv = path / "xbuildenv"
|
|
|
|
xbuildenv.mkdir()
|
|
|
|
|
|
|
|
pyodide_root = xbuildenv / "pyodide-root"
|
|
|
|
site_packages_extra = xbuildenv / "site-packages-extras"
|
|
|
|
requirements_txt = xbuildenv / "requirements.txt"
|
|
|
|
|
|
|
|
pyodide_root.mkdir()
|
|
|
|
site_packages_extra.mkdir()
|
|
|
|
requirements_txt.touch()
|
|
|
|
|
|
|
|
(pyodide_root / "Makefile.envs").write_text(
|
|
|
|
"""
|
|
|
|
export HOSTSITEPACKAGES=$(PYODIDE_ROOT)/packages/.artifacts/lib/python$(PYMAJOR).$(PYMINOR)/site-packages
|
|
|
|
|
|
|
|
.output_vars:
|
|
|
|
set
|
2023-03-13 21:45:06 +00:00
|
|
|
""" # noqa: W191
|
2023-02-22 02:56:35 +00:00
|
|
|
)
|
|
|
|
(pyodide_root / "dist").mkdir()
|
2023-06-29 16:45:06 +00:00
|
|
|
mock_pyodide_lock().to_json(pyodide_root / "dist" / "pyodide-lock.json")
|
2023-02-22 02:56:35 +00:00
|
|
|
|
|
|
|
with chdir(base):
|
|
|
|
archive_name = shutil.make_archive("xbuildenv", "tar")
|
|
|
|
|
|
|
|
yield base, archive_name
|
2023-06-29 11:37:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
def reset_env_vars():
|
|
|
|
# Will reset the environment variables to their original values after each test.
|
|
|
|
|
|
|
|
os.environ.pop("PYODIDE_ROOT", None)
|
|
|
|
old_environ = dict(os.environ)
|
|
|
|
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
os.environ.clear()
|
|
|
|
os.environ.update(old_environ)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
def reset_cache():
|
|
|
|
# Will remove all caches before each test.
|
|
|
|
|
|
|
|
build_env.get_pyodide_root.cache_clear()
|
|
|
|
build_env.get_build_environment_vars.cache_clear()
|
|
|
|
build_env.get_unisolated_packages.cache_clear()
|
|
|
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
def xbuildenv(selenium, tmp_path, reset_env_vars, reset_cache):
|
|
|
|
import subprocess as sp
|
|
|
|
|
|
|
|
assert "PYODIDE_ROOT" not in os.environ
|
|
|
|
|
|
|
|
envpath = Path(tmp_path) / ".pyodide-xbuildenv"
|
|
|
|
result = sp.run(
|
|
|
|
[
|
|
|
|
"pyodide",
|
|
|
|
"xbuildenv",
|
|
|
|
"create",
|
|
|
|
str(envpath),
|
|
|
|
"--root",
|
|
|
|
ROOT_PATH,
|
|
|
|
"--skip-missing-files",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result.returncode == 0
|
|
|
|
|
|
|
|
cur_dir = os.getcwd()
|
|
|
|
|
|
|
|
os.chdir(tmp_path)
|
|
|
|
|
|
|
|
try:
|
|
|
|
yield tmp_path
|
|
|
|
finally:
|
|
|
|
os.chdir(cur_dir)
|