Skip to content

Commit

Permalink
fix: pyodide test suite
Browse files Browse the repository at this point in the history
Instead of trying to run the entire pytest testsuite in the pyodide
runtime, in selenium we now only run the end-to-end tests "outside" of
pyodide.

By passing the ``lsp-runtime pyodide`` argument to pytest, rather than
launching the server under test via CPython, the test suite will use
NodeJS and a small wrapper script to execute the given server in the
Pyodide runtime.
  • Loading branch information
alcarney committed Jan 4, 2024
1 parent 9001694 commit 75ea1b6
Show file tree
Hide file tree
Showing 13 changed files with 341 additions and 467 deletions.
41 changes: 25 additions & 16 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ jobs:
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: '1.5.1'
virtualenvs-in-project: true
- name: Load cached venv
id: cached-poetry-dependencies
Expand All @@ -54,7 +53,7 @@ jobs:
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --all-extras
run: poetry install --with test
- name: Run tests
run: |
source $VENV # Only needed because of Github Action caching
Expand All @@ -65,27 +64,37 @@ jobs:
if: needs.pre_job.outputs.should_skip != 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Use Python "3.10"
uses: actions/setup-python@v4
- uses: actions/checkout@v4

- uses: 'actions/setup-node@v4'
with:
python-version: "3.10"
node-version: 18.x
cache: 'npm'
cache-dependency-path: 'tests/pyodide/package-lock.json'

- name: Use Python "3.12"
id: setup-python
uses: actions/setup-python@v5
with:
python-version: "3.12"

- name: Install Poetry
uses: snok/install-poetry@v1
with:
virtualenvs-in-project: true

- name: Install Dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: |
poetry install --with pyodide
- name: Run Testsuite
uses: nick-fields/retry@v2
with:
timeout_minutes: 10
max_attempts: 6
command: |
source $VENV
poe test-pyodide || true
poetry install --with test
poetry build
cd tests/pyodide
npm ci
- name: Run tests
run: |
source $VENV
poe test-pyodide
lint:
needs: pre_job
Expand Down
218 changes: 2 additions & 216 deletions poetry.lock

Large diffs are not rendered by default.

7 changes: 1 addition & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ ws = ["websockets"]
[tool.poetry.group.dev.dependencies]
# Replaces (amongst many other things) flake8 and bandit
ruff = ">=0.1.6"
# TODO `poethepoet>=0.20` needs python 3.8
poethepoet = ">=0.24.4"
mypy = ">=1.7.1"
# TODO `black>=23` needs python 3.8
black = ">=23.11.0"

[tool.poetry.group.test.dependencies]
Expand All @@ -46,14 +44,11 @@ pytest-asyncio = ">=0.21.0"
sphinx = ">=7.1.2"
sphinx-rtd-theme = ">=1.3.0"

[tool.poetry.group.pyodide.dependencies]
selenium = "^4.15.2"

[tool.pytest.ini_options]
asyncio_mode = "auto"

[tool.poe.tasks]
test-pyodide = "python tests/pyodide_testrunner/run.py"
test-pyodide = "pytest tests/e2e --lsp-runtime pyodide"
ruff = "ruff check ."
mypy = "mypy -p pygls"
check_generated_client = "python scripts/check_client_is_uptodate.py"
Expand Down
76 changes: 73 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,18 @@ def fn(*args):
fpath = pathlib.Path(base_dir, *args)
assert fpath.exists()

if runtime != "cpython":
raise NotImplementedError(f"uri_for: {runtime=}")
if runtime == "pyodide":
# Pyodide cannot see the whole file system, so this needs to be made relative to
# the workspace's parent folder
path = str(fpath).replace(str(WORKSPACE_DIR.parent), "")
return uris.from_fs_path(path)

elif runtime == "wasi":
# WASI cannot see the whole filesystem, so this needs to be made relative to
# the repo root
path = str(fpath).replace(str(REPO_DIR), "")
return uris.from_fs_path(path)

return uris.from_fs_path(str(fpath))

return fn
Expand Down Expand Up @@ -106,6 +116,60 @@ async def fn(server_name: str):
return fn


def get_client_for_pyodide_server(uri_fixture):
"""Return a client configured to communicate with a server running under Pyodide.
This assumes that the pyodide environment has already been bootstrapped.
"""

async def fn(server_name: str):
client = LanguageClient("pygls-test-suite", "v1")

PYODIDE_DIR = REPO_DIR / "tests" / "pyodide"
server_py = str(SERVER_DIR / server_name)

await client.start_io("node", str(PYODIDE_DIR / "run_server.js"), server_py)

response = await client.initialize(
types.InitializeParams(
capabilities=types.ClientCapabilities(),
root_uri=uri_fixture(""),
)
)
assert response is not None
return client, response

return fn


def get_client_for_wasi_server(uri_fixture):
"""Return a client configured to communicate with a server running under WASI.
This assumes the ``wasmtime`` executable is available to be used as the WASI host.
"""

async def fn(server_name: str):
client = LanguageClient("pygls-test-suite", "v1")

# WASI cannot see the whole filesystem, so this needs to be made relative to the
# repo root
server_py = str(SERVER_DIR / server_name).replace(str(REPO_DIR), "")

# TODO: Un-nixfiy this
await client.start_io("python-wasi", server_py)

response = await client.initialize(
types.InitializeParams(
capabilities=types.ClientCapabilities(),
root_uri=uri_fixture(""),
)
)
assert response is not None
return client, response

return fn


@pytest.fixture(scope="session")
def get_client_for(runtime, uri_for):
"""Return a client configured to communicate with the specified server.
Expand All @@ -115,9 +179,15 @@ def get_client_for(runtime, uri_for):
It's the consuming fixture's responsibility to stop the client.
"""
# TODO: Add TCP/WS support.
if runtime != "cpython":
if runtime not in {"cpython", "pyodide", "wasi"}:
raise NotImplementedError(f"get_client_for: {runtime=}")

elif runtime == "pyodide":
return get_client_for_pyodide_server(uri_for)

elif runtime == "wasi":
return get_client_for_wasi_server(uri_for)

return get_client_for_cpython_server(uri_for)


Expand Down
2 changes: 2 additions & 0 deletions tests/pyodide/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.log
node_modules/
81 changes: 81 additions & 0 deletions tests/pyodide/bootstrap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import pathlib
import subprocess
import sys
from typing import Optional

# Common paths
REPO = pathlib.Path(__file__).parent.parent.parent
PYODIDE_DIR = REPO / "tests" / "pyodide"
PYODIDE_INDEX = PYODIDE_DIR / "node_modules" / "pyodide"


def download_dependencies():
"""Download pygls' dependencies so that we have the wheels locally for pyodide to
use."""
requirements = PYODIDE_DIR / "requirements.txt"

run(
"poetry",
"export",
"-f",
"requirements.txt",
"--output",
str(requirements),
cwd=str(REPO),
)

# Ensure that pip uses packages compatible with the pyodide runtime.
run(
"pip",
"download",
"--no-deps",
"--python-version",
"3.11", # The version of Python pyodide compiled to WASM
"--implementation",
"py", # Use only pure python packages.
"-r",
str(requirements),
"--dest",
str(PYODIDE_INDEX),
cwd=str(REPO),
)


def run(*cmd, cwd: Optional[str] = None, capture: bool = False) -> Optional[str]:
"""Run a command."""

result = subprocess.run(cmd, cwd=cwd, capture_output=capture)
if result.returncode != 0:
if capture:
sys.stdout.buffer.write(result.stdout)
sys.stdout.flush()
sys.stderr.buffer.write(result.stderr)
sys.stderr.flush()

sys.exit(result.returncode)

if capture:
return result.stdout.decode("utf8").strip()

return None


def main():
"""Bootstrap the pyodide environment."""

# NOTE: Disabled for now as it's non-trivial to get mircopip to look in the local
# folder in the general case - we'd need to implement PyPi's JSON API!
#
# download_dependencies()

# Install pyodide
run("npm", "ci", cwd=str(PYODIDE_DIR))

# Build pygls
run("poetry", "build", cwd=str(REPO))

return 0


if __name__ == "__main__":
sys.exit(main())
49 changes: 49 additions & 0 deletions tests/pyodide/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/pyodide/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "pyodide_tests",
"version": "0.0.0",
"description": "Simple wrapper that executes pygls servers in Pyodide",
"main": "run_server.js",
"author": "openlawlibrary",
"dependencies": {
"pyodide": "^0.24.1"
}
}
Loading

0 comments on commit 75ea1b6

Please sign in to comment.