Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ dependencies = [
optional-dependencies.docs = [
]
optional-dependencies.test = [
"asgi-lifespan",
"httpx",
"httpx-ws",
"pytest>=6",
]
urls.Changelog = "https://github.com/sphinx-doc/sphinx-autobuild/blob/main/NEWS.rst"
Expand Down
4 changes: 2 additions & 2 deletions sphinx_autobuild/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ def __init__(
self.paths = [Path(path).resolve(strict=True) for path in paths]
self.ignore = ignore_filter
self.change_callback = change_callback
self.flag = asyncio.Event()
self.should_exit = asyncio.Event()

@asynccontextmanager
async def lifespan(self, _app) -> AbstractAsyncContextManager[None]:
self.flag = asyncio.Event()
self.should_exit = asyncio.Event()
task = asyncio.create_task(self.main())
yield
self.should_exit.set()
Expand Down
35 changes: 29 additions & 6 deletions tests/test_application.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to consider a PR with just these tests

Copy link
Author

@MtkN1 MtkN1 May 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do that since it's a good opportunity :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened this #191

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import shutil
from pathlib import Path

from starlette.testclient import TestClient
import anyio
import httpx
import pytest
from asgi_lifespan import LifespanManager
from httpx_ws import aconnect_ws
from httpx_ws.transport import ASGIWebSocketTransport

from sphinx_autobuild.__main__ import _create_app
from sphinx_autobuild.build import Builder
Expand All @@ -12,21 +17,39 @@
ROOT = Path(__file__).parent.parent


def test_application(tmp_path):
@pytest.fixture
def anyio_backend():
return "asyncio"


async def test_application(tmp_path, anyio_backend): # noqa: ARG001
src_dir = tmp_path / "docs"
out_dir = tmp_path / "build"
shutil.copytree(ROOT / "docs", tmp_path / "docs")
out_dir.mkdir(parents=True, exist_ok=True)
index_file = anyio.Path(src_dir / "index.rst")
await index_file.write_text("hello")

url_host = "127.0.0.1:7777"
ignore_handler = IgnoreFilter([out_dir], [])
builder = Builder(
[str(src_dir), str(out_dir)], url_host=url_host, pre_build_commands=[]
)
app = _create_app([src_dir], ignore_handler, builder, out_dir, url_host)
client = TestClient(app)

builder(changed_paths=())
async with (
LifespanManager(app) as manager,
httpx.AsyncClient(
transport=ASGIWebSocketTransport(manager.app), base_url="http://testserver"
) as client,
):
builder(changed_paths=())

response = await client.get("/")
assert response.status_code == 200

async with aconnect_ws("/websocket-reload", client) as websocket:
await index_file.write_text("world")

response = client.get("/")
assert response.status_code == 200
data = await websocket.receive_text()
assert data == "refresh"