Skip to content

Commit

Permalink
add wasi e2e test!!
Browse files Browse the repository at this point in the history
Needs to be de-nixified and added to CI though...
  • Loading branch information
alcarney committed Jan 3, 2024
1 parent bdacc64 commit e1d447d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/servers-next/code_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
def code_actions(params: types.CodeActionParams):
items = []
document_uri = params.text_document.uri
document = server.workspace.get_document(document_uri)
document = server.workspace.get_text_document(document_uri)

start_line = params.range.start.line
end_line = params.range.end.line
Expand Down
65 changes: 57 additions & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from pygls import IS_PYODIDE
from pygls import uris
from pygls.feature_manager import FeatureManager
from pygls.lsp._client import LanguageClient
from pygls.lsp._client import LanguageClient as LanguageClient_
from pygls.workspace import Workspace

DOC = """document
Expand All @@ -36,7 +36,21 @@
with "😋" unicode.
"""
DOC_URI = uris.from_fs_path(__file__) or ""
SERVER_DIR = pathlib.Path(__file__, "..", "..", "examples", "servers-next").resolve()
REPO_DIR = pathlib.Path(__file__, "..", "..").resolve()
SERVER_DIR = REPO_DIR / "examples" / "servers-next"
WORKSPACE_DIR = REPO_DIR / "examples" / "servers" / "workspace"


class LanguageClient(LanguageClient_):
"""Language client to use for testing."""

async def server_exit(self, server: asyncio.subprocess.Process):
# -15: terminated (probably by the client)
# 0: all ok
if server.returncode not in {-15, 0}:
if server.stderr is not None:
err = await server.stderr.read()
self.logger.debug("stderr:\n%s", err.decode("utf8"))


def pytest_addoption(parser):
Expand Down Expand Up @@ -65,16 +79,20 @@ def uri_for(runtime):
Takes into account the runtime.
"""
base_dir = pathlib.Path(
__file__, "..", "..", "examples", "servers", "workspace"
).resolve()

def fn(*args):
fpath = pathlib.Path(base_dir, *args)
fpath = pathlib.Path(WORKSPACE_DIR, *args)
assert fpath.exists()

if runtime != "cpython":
if runtime == "pyodide":
raise NotImplementedError(f"uri_for: {runtime=}")

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 +124,34 @@ async def fn(server_name: str):
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 +161,12 @@ 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 == "pyodide":
raise NotImplementedError(f"get_client_for: {runtime=}")

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

return get_client_for_cpython_server(uri_for)


Expand Down

0 comments on commit e1d447d

Please sign in to comment.