|
3 | 3 | import shutil |
4 | 4 | from pathlib import Path |
5 | 5 |
|
6 | | -from starlette.testclient import TestClient |
| 6 | +import anyio |
| 7 | +import httpx |
| 8 | +import pytest |
| 9 | +from asgi_lifespan import LifespanManager |
| 10 | +from httpx_ws import aconnect_ws |
| 11 | +from httpx_ws.transport import ASGIWebSocketTransport |
7 | 12 |
|
8 | 13 | from sphinx_autobuild.__main__ import _create_app |
9 | 14 | from sphinx_autobuild.build import Builder |
|
12 | 17 | ROOT = Path(__file__).parent.parent |
13 | 18 |
|
14 | 19 |
|
15 | | -def test_application(tmp_path): |
| 20 | +@pytest.fixture |
| 21 | +def anyio_backend(): |
| 22 | + return "asyncio" |
| 23 | + |
| 24 | + |
| 25 | +async def test_application(tmp_path, anyio_backend): # noqa: ARG001 |
16 | 26 | src_dir = tmp_path / "docs" |
17 | 27 | out_dir = tmp_path / "build" |
18 | 28 | shutil.copytree(ROOT / "docs", tmp_path / "docs") |
19 | 29 | out_dir.mkdir(parents=True, exist_ok=True) |
| 30 | + index_file = anyio.Path(src_dir / "index.rst") |
| 31 | + await index_file.write_text("hello") |
20 | 32 |
|
21 | 33 | url_host = "127.0.0.1:7777" |
22 | 34 | ignore_handler = IgnoreFilter([out_dir], []) |
23 | 35 | builder = Builder( |
24 | 36 | [str(src_dir), str(out_dir)], url_host=url_host, pre_build_commands=[] |
25 | 37 | ) |
26 | 38 | app = _create_app([src_dir], ignore_handler, builder, out_dir, url_host) |
27 | | - client = TestClient(app) |
28 | 39 |
|
29 | | - builder(changed_paths=()) |
| 40 | + async with ( |
| 41 | + LifespanManager(app) as manager, |
| 42 | + httpx.AsyncClient( |
| 43 | + transport=ASGIWebSocketTransport(manager.app), base_url="http://testserver" |
| 44 | + ) as client, |
| 45 | + ): |
| 46 | + builder(changed_paths=()) |
| 47 | + |
| 48 | + response = await client.get("/") |
| 49 | + assert response.status_code == 200 |
| 50 | + |
| 51 | + async with aconnect_ws("/websocket-reload", client) as websocket: |
| 52 | + await index_file.write_text("world") |
30 | 53 |
|
31 | | - response = client.get("/") |
32 | | - assert response.status_code == 200 |
| 54 | + data = await websocket.receive_text() |
| 55 | + assert data == "refresh" |
0 commit comments