Skip to content

Commit

Permalink
lsp: Initial pytest-lsp integration, live preview test case
Browse files Browse the repository at this point in the history
  • Loading branch information
alcarney committed Sep 9, 2023
1 parent dc40282 commit 5075614
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 68 deletions.
142 changes: 104 additions & 38 deletions lib/esbonio/tests/sphinx-agent/test_sa_build.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
import logging
import pathlib
import sys

import pytest
import pytest_asyncio
import pytest_lsp
from lsprotocol.types import WorkspaceFolder
from pygls.workspace import Workspace
from pytest_lsp import ClientServerConfig

from esbonio.server import Uri
from esbonio.server.features.sphinx_manager.client_subprocess import (
SubprocessSphinxClient,
)
from esbonio.server.features.sphinx_manager.client_subprocess import (
make_test_sphinx_client,
)
from esbonio.server.features.sphinx_manager.config import SphinxConfig


@pytest_asyncio.fixture(scope="module")
async def sphinx_client(uri_for, tmp_path_factory):
# TODO: Integrate with `pytest_lsp`
logger = logging.getLogger("sphinx_client")
logger.setLevel(logging.INFO)

client = SubprocessSphinxClient()

@client.feature("window/logMessage")
def _(params):
logger.info("%s", params.message)

@pytest_lsp.fixture(
scope="module",
params=["html", "dirhtml"],
config=ClientServerConfig(
server_command=[sys.executable, "-m", "esbonio.sphinx_agent"],
client_factory=make_test_sphinx_client,
),
)
async def client(
request, sphinx_client: SubprocessSphinxClient, uri_for, tmp_path_factory
):
build_dir = tmp_path_factory.mktemp("build")
test_uri = uri_for("sphinx-default", "workspace", "index.rst")
sd_workspace = uri_for("sphinx-default", "workspace")
Expand All @@ -36,45 +40,107 @@ def _(params):
],
)
config = SphinxConfig(
build_command=["sphinx-build", "-M", "html", ".", str(build_dir)],
python_command=[sys.executable],
env_passthrough=["PYTHONPATH"],
build_command=[
"sphinx-build",
"-M",
request.param,
sd_workspace.fs_path,
str(build_dir),
],
)
resolved = config.resolve(test_uri, workspace, client.logger)
resolved = config.resolve(test_uri, workspace, sphinx_client.logger)
assert resolved is not None

info = await client.create_application(resolved)
info = await sphinx_client.create_application(resolved)
assert info is not None

yield client
yield


@pytest.mark.asyncio
async def test_build_includes_webview_js(client: SubprocessSphinxClient, uri_for):
"""Ensure that builds include the ``webview.js`` script."""

out = client.build_uri
src = client.src_uri
assert out is not None and src is not None

if not client.stopped:
await client.stop()
await client.build()

# Ensure the script is included in the build output
webview_js = pathlib.Path(out / "_static" / "webview.js")
assert webview_js.exists()
assert "editor/scroll" in webview_js.read_text()

# Ensure the script is included in the page
index_html = pathlib.Path(out / "index.html")
assert '<script src="_static/webview.js"></script>' in index_html.read_text()


@pytest.mark.asyncio
async def test_build_file_map(sphinx_client, uri_for):
async def test_build_file_map(client: SubprocessSphinxClient, uri_for):
"""Ensure that we can trigger a Sphinx build correctly and the returned
build_file_map is correct."""

src = sphinx_client.src_uri
src = client.src_uri
assert src is not None

build_file_map = {
(src / "index.rst").fs_path: "index.html",
(src / "definitions.rst").fs_path: "definitions.html",
(src / "directive_options.rst").fs_path: "directive_options.html",
(src / "glossary.rst").fs_path: "glossary.html",
(src / "math.rst").fs_path: "theorems/pythagoras.html",
(src / "code" / "cpp.rst").fs_path: "code/cpp.html",
(src / "theorems" / "index.rst").fs_path: "theorems/index.html",
(src / "theorems" / "pythagoras.rst").fs_path: "theorems/pythagoras.html",
# It looks like non-existant files show up in this mapping as well
(src / ".." / "badfile.rst").fs_path: "definitions.html",
}

result = await sphinx_client.build()
if client.builder == "dirhtml":
build_file_map = {
(src / "index.rst").fs_path: "",
(src / "definitions.rst").fs_path: "definitions/",
(src / "directive_options.rst").fs_path: "directive_options/",
(src / "glossary.rst").fs_path: "glossary/",
(src / "math.rst").fs_path: "theorems/pythagoras/",
(src / "code" / "cpp.rst").fs_path: "code/cpp/",
(src / "theorems" / "index.rst").fs_path: "theorems/",
(src / "theorems" / "pythagoras.rst").fs_path: "theorems/pythagoras/",
# It looks like non-existant files show up in this mapping as well
(src / ".." / "badfile.rst").fs_path: "definitions/",
}
else:
build_file_map = {
(src / "index.rst").fs_path: "index.html",
(src / "definitions.rst").fs_path: "definitions.html",
(src / "directive_options.rst").fs_path: "directive_options.html",
(src / "glossary.rst").fs_path: "glossary.html",
(src / "math.rst").fs_path: "theorems/pythagoras.html",
(src / "code" / "cpp.rst").fs_path: "code/cpp.html",
(src / "theorems" / "index.rst").fs_path: "theorems/index.html",
(src / "theorems" / "pythagoras.rst").fs_path: "theorems/pythagoras.html",
# It looks like non-existant files show up in this mapping as well
(src / ".." / "badfile.rst").fs_path: "definitions.html",
}

result = await client.build()
assert result.build_file_map == build_file_map

build_uri_map = {Uri.for_file(src): out for src, out in build_file_map.items()}
assert sphinx_client.build_file_map == build_uri_map
assert client.build_file_map == build_uri_map


@pytest.mark.asyncio
async def test_build_content_override(client: SubprocessSphinxClient, uri_for):
"""Ensure that we can override the contents of a given src file when
required."""

out = client.build_uri
src = client.src_uri
assert out is not None and src is not None

await client.build()

# Before
expected = "Welcome to Defaults’s documentation!"
index_html = pathlib.Path(out / "index.html")
assert expected in index_html.read_text()

await client.build(
content_overrides={
(src / "index.rst").fs_path: "My Custom Title\n==============="
}
)

# Ensure the override was applied
expected = "My Custom Title"
assert expected in index_html.read_text()
54 changes: 24 additions & 30 deletions lib/esbonio/tests/sphinx-agent/test_sa_create_app.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
import asyncio
import logging
import sys

import pytest
import pytest_lsp
from lsprotocol.types import WorkspaceFolder
from pygls.workspace import Workspace
from pytest_lsp import ClientServerConfig

from esbonio.server.features.sphinx_manager.client_subprocess import (
SubprocessSphinxClient,
)
from esbonio.server.features.sphinx_manager.client_subprocess import (
make_test_sphinx_client,
)
from esbonio.server.features.sphinx_manager.config import SphinxConfig


@pytest.fixture()
def sphinx_client():
# TODO: Integrate with `pytest_lsp`
logger = logging.getLogger("sphinx_client")
logger.setLevel(logging.INFO)

client = SubprocessSphinxClient()

@client.feature("window/logMessage")
def _(params):
logger.info("%s", params.message)

return client
@pytest_lsp.fixture(
config=ClientServerConfig(
server_command=[sys.executable, "-m", "esbonio.sphinx_agent"],
client_factory=make_test_sphinx_client,
)
)
async def client(sphinx_client: SubprocessSphinxClient):
yield


@pytest.mark.asyncio
async def test_create_application(sphinx_client, uri_for):
async def test_create_application(client: SubprocessSphinxClient, uri_for):
"""Ensure that we can create a Sphinx application instance correctly."""

test_uri = uri_for("sphinx-default", "workspace", "index.rst")
Expand All @@ -41,21 +42,14 @@ async def test_create_application(sphinx_client, uri_for):
WorkspaceFolder(uri=str(sd_workspace), name="sphinx-default"),
],
)
config = SphinxConfig(
python_command=[sys.executable],
env_passthrough=["PYTHONPATH"],
)
resolved = config.resolve(test_uri, workspace, sphinx_client.logger)
config = SphinxConfig()
resolved = config.resolve(test_uri, workspace, client.logger)
assert resolved is not None

try:
info = await sphinx_client.create_application(resolved)
assert info is not None

assert info.builder_name == "dirhtml"
assert info.src_dir == sd_workspace.fs_path
assert info.conf_dir == sd_workspace.fs_path
assert "cache" in info.build_dir.lower()
finally:
if not sphinx_client.stopped:
await sphinx_client.stop()
info = await client.create_application(resolved)
assert info is not None

assert info.builder_name == "dirhtml"
assert info.src_dir == sd_workspace.fs_path
assert info.conf_dir == sd_workspace.fs_path
assert "cache" in info.build_dir.lower()
4 changes: 4 additions & 0 deletions lib/esbonio/tests/sphinx-agent/test_sa_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def application_args(**kwargs) -> Dict[str, Any]:
"warningiserror": False,
}

for arg in {"srcdir", "outdir", "confdir", "doctreedir"}:
if arg in kwargs:
kwargs[arg] = str(pathlib.Path(kwargs[arg]).resolve())

# Order matters, kwargs will override any keys found in defaults.
return {**defaults, **kwargs}

Expand Down

0 comments on commit 5075614

Please sign in to comment.