Skip to content

Commit

Permalink
lsp: Re-enable and fix diagnostic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alcarney committed Apr 17, 2024
1 parent 7966b8a commit d8e7d20
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 66 deletions.
4 changes: 2 additions & 2 deletions lib/esbonio/tests/e2e/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async def client(lsp_client: LanguageClient, uri_for, tmp_path_factory):
),
),
initialization_options={
"server": {"logLevel": "debug"},
"logging": {"level": "debug"},
"sphinx": {
"buildCommand": [
"sphinx-build",
Expand Down Expand Up @@ -67,7 +67,7 @@ async def client(lsp_client: LanguageClient, uri_for, tmp_path_factory):
)
)

await lsp_client.wait_for_notification("sphinx/clientCreated")
await lsp_client.wait_for_notification("sphinx/appCreated")

# Save the document to trigger a build
lsp_client.text_document_did_save(
Expand Down
79 changes: 48 additions & 31 deletions lib/esbonio/tests/e2e/test_e2e_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
TEST_DIR = pathlib.Path(__file__).parent.parent


@pytest.mark.asyncio
@pytest.mark.skip
async def test_document_diagnostic(pull_client: LanguageClient, uri_for):
"""Ensure that we can get the diagnostics for a single document correctly."""
@pytest.mark.asyncio(scope="session")
async def test_rst_document_diagnostic(client: LanguageClient, uri_for):
"""Ensure that we can get the diagnostics for a single rst document correctly."""

workspace_uri = uri_for("sphinx-default", "workspace")
test_uri = workspace_uri / "definitions.rst"
workspace_uri = uri_for("workspaces", "demo")
test_uri = workspace_uri / "rst" / "diagnostics.rst"
report = await client.text_document_diagnostic_async(
types.DocumentDiagnosticParams(
text_document=types.TextDocumentIdentifier(uri=str(test_uri))
Expand All @@ -30,30 +29,50 @@ async def test_document_diagnostic(pull_client: LanguageClient, uri_for):
# test cases.
messages = {d.message for d in report.items}
assert messages == {
"image file not readable: _static/bad.png",
"unknown document: '/changelog'",
"image file not readable: not-an-image.png",
}

assert len(client.diagnostics) == 0, "Server should not publish diagnostics"


@pytest.mark.asyncio
@pytest.mark.skip
async def test_workspace_diagnostic(pull_client: LanguageClient, uri_for):
@pytest.mark.asyncio(scope="session")
async def test_myst_document_diagnostic(client: LanguageClient, uri_for):
"""Ensure that we can get the diagnostics for a single myst document correctly."""

workspace_uri = uri_for("workspaces", "demo")
test_uri = workspace_uri / "myst" / "diagnostics.md"
report = await client.text_document_diagnostic_async(
types.DocumentDiagnosticParams(
text_document=types.TextDocumentIdentifier(uri=str(test_uri))
)
)

assert report.kind == "full"

# We will only check the diagnostic message, full details will be handled by other
# test cases.
messages = {d.message for d in report.items}
assert messages == {
"image file not readable: not-an-image.png",
}

assert len(client.diagnostics) == 0, "Server should not publish diagnostics"


@pytest.mark.asyncio(scope="session")
async def test_workspace_diagnostic(client: LanguageClient, uri_for):
"""Ensure that we can get diagnostics for the whole workspace correctly."""
report = await client.workspace_diagnostic_async(
types.WorkspaceDiagnosticParams(previous_result_ids=[])
)

workspace_uri = uri_for("sphinx-default", "workspace")
workspace_uri = uri_for("workspaces", "demo")
expected = {
str(workspace_uri / "definitions.rst"): {
"image file not readable: _static/bad.png",
"unknown document: '/changelog'",
str(workspace_uri / "rst" / "diagnostics.rst"): {
"image file not readable: not-an-image.png",
},
str(workspace_uri / "directive_options.rst"): {
"image file not readable: filename.png",
"document isn't included in any toctree",
str(workspace_uri / "myst" / "diagnostics.md"): {
"image file not readable: not-an-image.png",
},
}
assert len(report.items) == len(expected)
Expand All @@ -71,9 +90,10 @@ async def test_workspace_diagnostic(pull_client: LanguageClient, uri_for):
)
async def pub_client(lsp_client: LanguageClient, uri_for, tmp_path_factory):
"""A client that does **not** support the pull-diagnostics model."""

build_dir = tmp_path_factory.mktemp("build")
workspace_uri = uri_for("sphinx-default", "workspace")
test_uri = workspace_uri / "definitions.rst"
workspace_uri = uri_for("workspaces", "demo")
test_uri = workspace_uri / "rst" / "diagnostics.rst"

await lsp_client.initialize_session(
types.InitializeParams(
Expand All @@ -84,7 +104,7 @@ async def pub_client(lsp_client: LanguageClient, uri_for, tmp_path_factory):
),
),
initialization_options={
"server": {"logLevel": "debug"},
"logging": {"level": "debug"},
"sphinx": {
"buildCommand": [
"sphinx-build",
Expand All @@ -97,7 +117,7 @@ async def pub_client(lsp_client: LanguageClient, uri_for, tmp_path_factory):
},
},
workspace_folders=[
types.WorkspaceFolder(uri=str(workspace_uri), name="sphinx-default"),
types.WorkspaceFolder(uri=str(workspace_uri), name="demo"),
],
)
)
Expand Down Expand Up @@ -135,19 +155,16 @@ async def pub_client(lsp_client: LanguageClient, uri_for, tmp_path_factory):
await lsp_client.shutdown_session()


@pytest.mark.asyncio
@pytest.mark.skip
@pytest.mark.asyncio(scope="module")
async def test_publish_diagnostics(pub_client: LanguageClient, uri_for):
"""Ensure that the server publishes the diagnostics it finds"""
workspace_uri = uri_for("sphinx-default", "workspace")
workspace_uri = uri_for("workspaces", "demo")
expected = {
str(workspace_uri / "definitions.rst"): {
"image file not readable: _static/bad.png",
"unknown document: '/changelog'",
str(workspace_uri / "rst" / "diagnostics.rst"): {
"image file not readable: not-an-image.png",
},
str(workspace_uri / "directive_options.rst"): {
"image file not readable: filename.png",
"document isn't included in any toctree",
str(workspace_uri / "myst" / "diagnostics.md"): {
"image file not readable: not-an-image.png",
},
}

Expand Down
10 changes: 10 additions & 0 deletions lib/esbonio/tests/sphinx-agent/handlers/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,22 @@ async def test_files_table(client: SubprocessSphinxClient, project: Project):
expected = {
(anuri(src, "index.rst"), "index", "index.html"),
(anuri(src, "rst", "directives.rst"), "rst/directives", "rst/directives.html"),
(
anuri(src, "rst", "diagnostics.rst"),
"rst/diagnostics",
"rst/diagnostics.html",
),
(anuri(src, "rst", "symbols.rst"), "rst/symbols", "rst/symbols.html"),
(
anuri(src, "myst", "directives.md"),
"myst/directives",
"myst/directives.html",
),
(
anuri(src, "myst", "diagnostics.md"),
"myst/diagnostics",
"myst/diagnostics.html",
),
(anuri(src, "myst", "symbols.md"), "myst/symbols", "myst/symbols.html"),
(anuri(src, "demo_rst.rst"), "demo_rst", "demo_rst.html"),
(anuri(src, "demo_myst.md"), "demo_myst", "demo_myst.html"),
Expand Down
50 changes: 18 additions & 32 deletions lib/esbonio/tests/sphinx-agent/handlers/test_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pygls.protocol import default_converter

from esbonio.server import Uri
from esbonio.server.features.project_manager import Project
from esbonio.server.features.sphinx_manager.client_subprocess import (
SubprocessSphinxClient,
)
Expand All @@ -29,43 +30,26 @@ def check_diagnostics(


@pytest.mark.asyncio
@pytest.mark.skip
async def test_diagnostics(client: SubprocessSphinxClient, uri_for):
async def test_diagnostics(client: SubprocessSphinxClient, project: Project, uri_for):
"""Ensure that the sphinx agent reports diagnostics collected during the build, and
that they are correctly reset when fixed."""
definitions_uri = uri_for("sphinx-default/workspace/definitions.rst")
options_uri = uri_for("sphinx-default/workspace/directive_options.rst")
rst_diagnostics_uri = uri_for("workspaces/demo/rst/diagnostics.rst")
myst_diagnostics_uri = uri_for("workspaces/demo/myst/diagnostics.md")

expected = {
definitions_uri: [
rst_diagnostics_uri: [
types.Diagnostic(
message="image file not readable: _static/bad.png",
message="image file not readable: not-an-image.png",
severity=types.DiagnosticSeverity.Warning,
range=types.Range(
start=types.Position(line=28, character=0),
end=types.Position(line=29, character=0),
),
),
types.Diagnostic(
message="unknown document: '/changelog'",
severity=types.DiagnosticSeverity.Warning,
range=types.Range(
start=types.Position(line=13, character=0),
end=types.Position(line=14, character=0),
start=types.Position(line=5, character=0),
end=types.Position(line=6, character=0),
),
),
],
options_uri: [
types.Diagnostic(
message="image file not readable: filename.png",
severity=types.DiagnosticSeverity.Warning,
range=types.Range(
start=types.Position(line=0, character=0),
end=types.Position(line=1, character=0),
),
),
myst_diagnostics_uri: [
types.Diagnostic(
message="document isn't included in any toctree",
message="image file not readable: not-an-image.png",
severity=types.DiagnosticSeverity.Warning,
range=types.Range(
start=types.Position(line=0, character=0),
Expand All @@ -75,24 +59,26 @@ async def test_diagnostics(client: SubprocessSphinxClient, uri_for):
],
}

actual = await client.get_diagnostics()
actual = await project.get_diagnostics()
check_diagnostics(expected, actual)

await client.build(
content_overrides={definitions_uri.fs_path: "My Custom Title\n==============="}
content_overrides={
rst_diagnostics_uri.fs_path: "My Custom Title\n===============\n\nThere are no images here"
}
)

actual = await client.get_diagnostics()
check_diagnostics({options_uri: expected[options_uri]}, actual)
actual = await project.get_diagnostics()
check_diagnostics({myst_diagnostics_uri: expected[myst_diagnostics_uri]}, actual)

# The original diagnostics should be reported when the issues are re-introduced.
#
# Note: We have to "override" the contents of the file with the original text to
# trick Sphinx into re-building the file.
await client.build(
content_overrides={
definitions_uri.fs_path: pathlib.Path(definitions_uri).read_text()
rst_diagnostics_uri.fs_path: pathlib.Path(rst_diagnostics_uri).read_text()
}
)
actual = await client.get_diagnostics()
actual = await project.get_diagnostics()
check_diagnostics(expected, actual)
1 change: 0 additions & 1 deletion lib/esbonio/tests/workspaces/demo/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"source_branch": "develop",
"source_directory": "lib/esbonio/tests/workspaces/demo/",
}
html_static_path = ["_static"]


def lsp_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
Expand Down
6 changes: 6 additions & 0 deletions lib/esbonio/tests/workspaces/demo/myst/diagnostics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Diagnostics

The language server has support for diagnostics, highlighting errors/warnings reported by Sphinx.

```{image} /not-an-image.png
```
6 changes: 6 additions & 0 deletions lib/esbonio/tests/workspaces/demo/rst/diagnostics.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Diagnostics
===========

The language server has support for diagnostics, highlighting errors/warnings reported by Sphinx.

.. image:: /not-an-image.png

0 comments on commit d8e7d20

Please sign in to comment.