Skip to content

Commit

Permalink
sphinx-agent: Use pathlib.Path
Browse files Browse the repository at this point in the history
When comparing filepaths we should use `pathlib.Path` objects rather
than plain strings as they appear to handle case-insenstive filepaths
correctly for us when running on Windows.
  • Loading branch information
alcarney committed Oct 17, 2023
1 parent ca15eda commit 05c077d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
12 changes: 8 additions & 4 deletions lib/esbonio/esbonio/sphinx_agent/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self):
self.log_handler: Optional[SphinxLogHandler] = None
"""The logging handler"""

self._content_overrides: Dict[str, str] = {}
self._content_overrides: Dict[pathlib.Path, str] = {}
"""Holds any additional content to inject into a build."""

self._handlers: Dict[str, Tuple[Type, Callable]] = self._register_handlers()
Expand Down Expand Up @@ -147,7 +147,7 @@ def _cb_env_before_read_docs(self, app: Sphinx, env, docnames: List[str]):
is_building = set(docnames)

for docname in env.found_docs - is_building:
filepath = env.doc2path(docname, base=True)
filepath = pathlib.Path(env.doc2path(docname, base=True))
if filepath in self._content_overrides:
docnames.append(docname)

Expand All @@ -161,7 +161,8 @@ def _cb_source_read(self, app: Sphinx, docname: str, source):
self.log_handler.diagnostics.pop(filepath, None)

# Override file contents if necessary
if (content := self._content_overrides.get(filepath)) is not None:
path = pathlib.Path(filepath)
if (content := self._content_overrides.get(path)) is not None:
source[0] = content

def setup_logging(self, config: SphinxConfig, app: Sphinx, status: IO, warning: IO):
Expand Down Expand Up @@ -201,7 +202,10 @@ def build_sphinx_app(self, request: types.BuildRequest):
send_error(id=request.id, code=-32803, message="Sphinx app not initialized")
return

self._content_overrides = request.params.content_overrides
self._content_overrides = {
pathlib.Path(p): content
for p, content in request.params.content_overrides.items()
}

try:
self.app.build()
Expand Down
7 changes: 5 additions & 2 deletions lib/esbonio/tests/sphinx-agent/test_sa_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,15 @@ async def test_diagnostics(client: SubprocessSphinxClient, uri_for):
],
}
result = await client.build()
actual = {pathlib.Path(p): items for p, items in result.diagnostics.items()}

assert set(result.diagnostics.keys()) == set(expected.keys())
actual_keys = set(actual.keys())
expected_keys = set(pathlib.Path(k) for k in expected.keys())
assert actual_keys == expected_keys

for k, ex_diags in expected.items():
# Order of results is not important
assert set(result.diagnostics[k]) == set(ex_diags)
assert set(actual[pathlib.Path(k)]) == set(ex_diags)


@pytest.mark.asyncio
Expand Down

0 comments on commit 05c077d

Please sign in to comment.