From 87d1e32f6be2cf360d5d922110e526a48e881ca9 Mon Sep 17 00:00:00 2001 From: MtkN1 <51289448+MtkN1@users.noreply.github.com> Date: Wed, 28 May 2025 17:35:56 +0900 Subject: [PATCH] Add hot reloading test --- pyproject.toml | 2 ++ tests/test_application.py | 35 +++++++++++++++++++++++++++++------ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index c95a7c1..9225119 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -50,7 +50,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" diff --git a/tests/test_application.py b/tests/test_application.py index b937c13..12e5531 100644 --- a/tests/test_application.py +++ b/tests/test_application.py @@ -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 @@ -12,11 +17,18 @@ 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], []) @@ -24,9 +36,20 @@ def test_application(tmp_path): [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"