Skip to content

Commit 674264e

Browse files
authored
Add a basic integration test (#160)
1 parent 81375b3 commit 674264e

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ dependencies = [
5353
docs = [
5454
]
5555
test = [
56+
"httpx",
5657
"pytest>=6",
5758
]
5859
[project.urls]

sphinx_autobuild/__main__.py

+19-14
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
from sphinx_autobuild.utils import find_free_port, open_browser, show
2424

2525

26-
def main():
26+
def main(argv=()):
2727
"""Actual application logic."""
2828
colorama.init()
2929

30-
args, build_args = _parse_args(sys.argv[1:])
30+
args, build_args = _parse_args(list(argv))
3131

3232
src_dir = args.sourcedir
3333
out_dir = args.outdir
@@ -47,20 +47,12 @@ def main():
4747
)
4848

4949
watch_dirs = [src_dir] + args.additional_watched_dirs
50+
ignore_dirs = args.ignore + [out_dir, args.warnings_file, args.doctree_dir]
5051
ignore_handler = IgnoreFilter(
51-
[p for p in args.ignore + [out_dir, args.warnings_file, args.doctree_dir] if p],
52+
[p for p in ignore_dirs if p],
5253
args.re_ignore,
5354
)
54-
watcher = RebuildServer(watch_dirs, ignore_handler, change_callback=builder)
55-
56-
app = Starlette(
57-
routes=[
58-
WebSocketRoute("/websocket-reload", watcher, name="reload"),
59-
Mount("/", app=StaticFiles(directory=out_dir, html=True), name="static"),
60-
],
61-
middleware=[Middleware(JavascriptInjectorMiddleware, ws_url=url_host)],
62-
lifespan=watcher.lifespan,
63-
)
55+
app = _create_app(watch_dirs, ignore_handler, builder, out_dir, url_host)
6456

6557
if not args.no_initial_build:
6658
show(context="Starting initial build")
@@ -76,6 +68,19 @@ def main():
7668
show(context="Server ceasing operations. Cheerio!")
7769

7870

71+
def _create_app(watch_dirs, ignore_handler, builder, out_dir, url_host):
72+
watcher = RebuildServer(watch_dirs, ignore_handler, change_callback=builder)
73+
74+
return Starlette(
75+
routes=[
76+
WebSocketRoute("/websocket-reload", watcher, name="reload"),
77+
Mount("/", app=StaticFiles(directory=out_dir, html=True), name="static"),
78+
],
79+
middleware=[Middleware(JavascriptInjectorMiddleware, ws_url=url_host)],
80+
lifespan=watcher.lifespan,
81+
)
82+
83+
7984
def _parse_args(argv):
8085
# Parse once with the Sphinx parser to emit errors
8186
# and capture the ``-d`` and ``-w`` options.
@@ -197,4 +202,4 @@ def _add_autobuild_arguments(parser):
197202

198203

199204
if __name__ == "__main__":
200-
main()
205+
main(sys.argv[1:])

tests/test_application.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""A very basic test that the application works."""
2+
3+
import shutil
4+
from pathlib import Path
5+
6+
from starlette.testclient import TestClient
7+
8+
from sphinx_autobuild.__main__ import _create_app
9+
from sphinx_autobuild.build import Builder
10+
from sphinx_autobuild.filter import IgnoreFilter
11+
12+
ROOT = Path(__file__).parent.parent
13+
14+
15+
def test_application(tmp_path):
16+
src_dir = tmp_path / "docs"
17+
out_dir = tmp_path / "build"
18+
shutil.copytree(ROOT / "docs", tmp_path / "docs")
19+
out_dir.mkdir(parents=True, exist_ok=True)
20+
21+
url_host = "127.0.0.1:7777"
22+
ignore_handler = IgnoreFilter([out_dir], [])
23+
builder = Builder(
24+
[str(src_dir), str(out_dir)], url_host=url_host, pre_build_commands=[]
25+
)
26+
app = _create_app([src_dir], ignore_handler, builder, out_dir, url_host)
27+
client = TestClient(app)
28+
29+
builder(rebuild=False)
30+
31+
response = client.get("/")
32+
assert response.status_code == 200

0 commit comments

Comments
 (0)