From 8c164d2bc4d3e140339257fcbe8a0d7e500d6521 Mon Sep 17 00:00:00 2001 From: Gyeongjae Choi Date: Tue, 1 Nov 2022 08:24:58 +0900 Subject: [PATCH] Run tests with local micropip (#20) --- .github/workflows/main.yml | 2 +- .gitignore | 1 + pyproject.toml | 5 +++-- tests/test_micropip.py | 45 ++++++++++++++++++++++++++++++++------ 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f688766..2d1a7fa 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -89,7 +89,7 @@ jobs: - uses: codecov/codecov-action@v3 if: ${{ github.event.repo.name == 'pyodide/micropip' || github.event_name == 'pull_request' }} with: - fail_ci_if_error: true + fail_ci_if_error: false deploy: runs-on: ubuntu-latest diff --git a/.gitignore b/.gitignore index 2344625..c2f003b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ __pycache__ micropip/_version.py *.egg-info dist/ +build/ diff --git a/pyproject.toml b/pyproject.toml index 81ea888..7056baa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,7 +17,8 @@ dependencies = [] [project.optional-dependencies] test = [ "pytest-pyodide", - "pytest-cov" + "pytest-cov", + "build", ] @@ -28,7 +29,7 @@ test = [ [build-system] -requires = ["setuptools>=42", "setuptools_scm[toml]>=6.2"] +requires = ["setuptools>=42", "setuptools_scm[toml]>=6.2", "wheel"] build-backend = "setuptools.build_meta" diff --git a/tests/test_micropip.py b/tests/test_micropip.py index e9fa6cd..0a6d6b3 100644 --- a/tests/test_micropip.py +++ b/tests/test_micropip.py @@ -195,17 +195,48 @@ def mock_fetch(monkeypatch, mock_importlib, wheel_base): return result +@pytest.fixture(scope="module") +def wheel_path(tmp_path_factory): + # Build a micropip wheel for testing + import build + from build.env import IsolatedEnvBuilder + + output_dir = tmp_path_factory.mktemp("wheel") + + with IsolatedEnvBuilder() as env: + builder = build.ProjectBuilder(Path(__file__).parent.parent) + builder.python_executable = env.executable + builder.scripts_dir = env.scripts_dir + env.install(builder.build_system_requires) + builder.build("wheel", output_directory=output_dir) + + yield output_dir + + @pytest.fixture -def selenium_standalone_micropip(selenium_standalone): +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. """ - selenium_standalone.run_js( - """ - await pyodide.loadPackage("micropip"); - pyodide.runPython("import micropip"); - """ - ) + + 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", "pyparsing"]); + pyodide.runPython("import micropip"); + """ + ) + yield selenium_standalone