Skip to content

Commit

Permalink
Bring back jupyterlite-xeus-python tests (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinRenou authored Jan 12, 2024
1 parent 21f50dd commit 36d47f8
Show file tree
Hide file tree
Showing 12 changed files with 255 additions and 18 deletions.
116 changes: 115 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,125 @@ jobs:
pip install "jupyterlab>=4.0.0,<5" jupyterlite_xeus*.whl
jupyter labextension list
jupyter labextension list 2>&1 | grep -ie "@jupyterlite/xeus.*OK"
python -m jupyterlab.browser_check --no-browser-test
python-tests-mamba-python:
needs: build
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- uses: actions/download-artifact@v2
with:
name: extension-artifacts

- name: Install Conda environment with Micromamba
uses: mamba-org/setup-micromamba@v1
with:
micromamba-version: '1.5.1-0'
environment-file: environment-dev.yaml
cache-environment: true

- name: Install mamba
run: conda install -c conda-forge mamba

- name: Make sure the Mamba Python API is available
run: |
mamba install mamba
python -c "from mamba.api import create"
- name: Install
run: pip install jupyterlite_xeus*.whl

- name: Run tests
run: pytest -rP test_xeus.py
working-directory: tests

python-tests-mamba:
needs: build
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- uses: actions/download-artifact@v2
with:
name: extension-artifacts

- name: Install Conda environment with Micromamba
uses: mamba-org/setup-micromamba@v1
with:
micromamba-version: '1.5.1-0'
environment-file: environment-dev.yaml
cache-environment: true

- name: Install mamba
run: conda install -c conda-forge mamba

- name: Install
run: pip install jupyterlite_xeus*.whl

- name: Run tests
run: pytest -rP test_xeus.py
working-directory: tests

python-tests-micromamba:
needs: build
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- uses: actions/download-artifact@v2
with:
name: extension-artifacts

- name: Install mamba
uses: mamba-org/provision-with-micromamba@main
with:
micromamba-version: '0.22.0'
environment-file: environment-dev.yaml
environment-name: xeus-lite-dev

- name: Install
run: pip install jupyterlite_xeus*.whl

- name: Run tests
run: pytest -rP test_xeus.py
working-directory: tests

python-tests-conda:
needs: build
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- uses: actions/download-artifact@v2
with:
name: extension-artifacts

- name: Install Conda environment with Micromamba
uses: mamba-org/setup-micromamba@v1
with:
micromamba-version: '1.5.1-0'
environment-file: environment-dev.yaml
cache-environment: true

- name: Install
run: pip install jupyterlite_xeus*.whl

- name: Run tests
run: pytest -rP test_xeus.py
working-directory: tests

integration-tests:
name: Integration tests
needs: build
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ This workflow usually starts with creating a local conda environment / prefix fo
micromamba create -n xeus-python-dev \
--platform=emscripten-wasm32 \
-c https://repo.mamba.pm/emscripten-forge \
-c https://repo.mamba.pm/conda-forge \
-c conda-forge \
--yes \
"python>=3.11" pybind11 nlohmann_json pybind11_json numpy pytest \
bzip2 sqlite zlib libffi xtl pyjs \
Expand Down
1 change: 0 additions & 1 deletion environment-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ dependencies:
- hatch-nodejs-version
- jupyterlab >=4.0
- jupyterlite-core
- mamba
- nodejs
- pip
- pytest
Expand Down
19 changes: 13 additions & 6 deletions jupyterlite_xeus/add_on.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
)
from traitlets import List, Unicode

from .create_conda_env import create_conda_env_from_yaml, create_conda_env_from_specs
from .create_conda_env import (
create_conda_env_from_env_file,
create_conda_env_from_specs,
)
from .constants import EXTENSION_NAME
from .constants import EXTENSION_NAME

from empack.pack import (
Expand Down Expand Up @@ -110,15 +114,18 @@ def post_build(self, manager):
def create_prefix(self):
# read the environment file
root_prefix = Path(self.cwd.name) / "env"
env_name = "xeus-env"
env_file = Path(self.environment_file)

# open the env yaml file
with open(env_file, "r") as file:
yaml_content = yaml.safe_load(file)

env_name = yaml_content.get("name", "xeus-env")
env_prefix = root_prefix / "envs" / env_name
self.prefix = str(env_prefix)

env_file = Path(self.environment_file)
if env_file.exists():
create_conda_env_from_yaml(
env_name=env_name, root_prefix=root_prefix, env_file=env_file
)
create_conda_env_from_env_file(root_prefix, yaml_content, env_file.parent)
# this is atm for debugging
else:
create_conda_env_from_specs(
Expand Down
18 changes: 9 additions & 9 deletions jupyterlite_xeus/create_conda_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from pathlib import Path
from subprocess import run as subprocess_run
import os
import yaml

from ._pip import _install_pip_dependencies

Expand Down Expand Up @@ -33,24 +32,25 @@ def _extract_specs(env_location, env_data):
# If it's a local Python package, make its path relative to the environment file
if (env_location / pip_dependency).is_dir():
pip_dependencies.append(
env_location.parent / pip_dependency
).resolve()
(env_location / pip_dependency).resolve()
)
else:
pip_dependencies.append(pip_dependency)

return specs, pip_dependencies


def create_conda_env_from_yaml(env_name, root_prefix, env_file):
# open the env yaml file
with open(env_file, "r") as file:
yaml_content = yaml.safe_load(file)
def create_conda_env_from_env_file(root_prefix, env_file_content, env_file_location):
# get the name of the environment
env_name = env_file_content.get("name", "xeus-env")

# get the channels
channels = yaml_content.get("channels", [])
channels = env_file_content.get(
"channels", ["https://repo.mamba.pm/emscripten-forge", "conda-forge"]
)

# get the specs
specs, pip_dependencies = _extract_specs(env_file.parent, yaml_content)
specs, pip_dependencies = _extract_specs(env_file_location, env_file_content)

create_conda_env_from_specs(
env_name=env_name,
Expand Down
16 changes: 16 additions & 0 deletions tests/environment-1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: xeus-python-kernel-1
channels:
- https://repo.mamba.pm/emscripten-forge
- conda-forge
dependencies:
- xeus-python
- xeus-lua
- numpy
- matplotlib
- pillow
- ipywidgets
- pip:
# Installing a python package that ships a lab extension under share/
- ipycanvas
# Installing a pure python package
- py2vega
9 changes: 9 additions & 0 deletions tests/environment-2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: xeus-python-kernel-2
channels:
- https://repo.mamba.pm/emscripten-forge
- conda-forge
dependencies:
- xeus-python
- pip:
# Installing NumPy with pip should fail
- numpy
9 changes: 9 additions & 0 deletions tests/test_package/environment-3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: xeus-python-kernel-3
channels:
- https://repo.mamba.pm/emscripten-forge
- conda-forge
dependencies:
- xeus-python
- numpy
- pip:
- .
3 changes: 3 additions & 0 deletions tests/test_package/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[project]
name = "test-package"
version = "0.1.0"
Empty file.
1 change: 1 addition & 0 deletions tests/test_package/test_package/hey.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("Hey")
79 changes: 79 additions & 0 deletions tests/test_xeus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"""Test creating Python envs for jupyterlite-xeus-python."""

import os
from tempfile import TemporaryDirectory
from pathlib import Path

import pytest

from jupyterlite_core.app import LiteStatusApp

from jupyterlite_xeus.add_on import XeusAddon


def test_python_env_from_file_1():
app = LiteStatusApp(log_level="DEBUG")
app.initialize()
manager = app.lite_manager

addon = XeusAddon(manager)
addon.environment_file = "environment-1.yml"

for step in addon.post_build(manager):
pass

# Check env
assert os.path.isdir(addon.prefix)

assert os.path.isfile(Path(addon.prefix) / "bin/xpython.js")
assert os.path.isfile(Path(addon.prefix) / "bin/xpython.wasm")

assert os.path.isfile(Path(addon.prefix) / "bin/xlua.js")
assert os.path.isfile(Path(addon.prefix) / "bin/xlua.wasm")

# Checking pip packages
assert os.path.isdir(Path(addon.prefix) / "lib/python3.11")
assert os.path.isdir(Path(addon.prefix) / "lib/python3.11/site-packages")
assert os.path.isdir(Path(addon.prefix) / "lib/python3.11/site-packages/ipywidgets")
assert os.path.isdir(Path(addon.prefix) / "lib/python3.11/site-packages/ipycanvas")
assert os.path.isdir(Path(addon.prefix) / "lib/python3.11/site-packages/py2vega")

# Checking labextensions
assert os.path.isdir(
Path(addon.prefix)
/ "share/jupyter/labextensions/@jupyter-widgets/jupyterlab-manager"
)
assert os.path.isdir(Path(addon.prefix) / "share/jupyter/labextensions/ipycanvas")


def test_python_env_from_file_3():
app = LiteStatusApp(log_level="DEBUG")
app.initialize()
manager = app.lite_manager

addon = XeusAddon(manager)
addon.environment_file = "test_package/environment-3.yml"

for step in addon.post_build(manager):
pass

# Test
assert os.path.isdir(
Path(addon.prefix) / "lib/python3.11/site-packages/test_package"
)
assert os.path.isfile(
Path(addon.prefix) / "lib/python3.11/site-packages/test_package/hey.py"
)


def test_python_env_from_file_2():
app = LiteStatusApp(log_level="DEBUG")
app.initialize()
manager = app.lite_manager

addon = XeusAddon(manager)
addon.environment_file = "environment-2.yml"

with pytest.raises(RuntimeError, match="Cannot install binary PyPI package"):
for step in addon.post_build(manager):
pass

0 comments on commit 36d47f8

Please sign in to comment.