Skip to content

Commit

Permalink
Add a basic integration test (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner authored May 6, 2024
1 parent 81375b3 commit 674264e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ dependencies = [
docs = [
]
test = [
"httpx",
"pytest>=6",
]
[project.urls]
Expand Down
33 changes: 19 additions & 14 deletions sphinx_autobuild/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
from sphinx_autobuild.utils import find_free_port, open_browser, show


def main():
def main(argv=()):
"""Actual application logic."""
colorama.init()

args, build_args = _parse_args(sys.argv[1:])
args, build_args = _parse_args(list(argv))

src_dir = args.sourcedir
out_dir = args.outdir
Expand All @@ -47,20 +47,12 @@ def main():
)

watch_dirs = [src_dir] + args.additional_watched_dirs
ignore_dirs = args.ignore + [out_dir, args.warnings_file, args.doctree_dir]
ignore_handler = IgnoreFilter(
[p for p in args.ignore + [out_dir, args.warnings_file, args.doctree_dir] if p],
[p for p in ignore_dirs if p],
args.re_ignore,
)
watcher = RebuildServer(watch_dirs, ignore_handler, change_callback=builder)

app = Starlette(
routes=[
WebSocketRoute("/websocket-reload", watcher, name="reload"),
Mount("/", app=StaticFiles(directory=out_dir, html=True), name="static"),
],
middleware=[Middleware(JavascriptInjectorMiddleware, ws_url=url_host)],
lifespan=watcher.lifespan,
)
app = _create_app(watch_dirs, ignore_handler, builder, out_dir, url_host)

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


def _create_app(watch_dirs, ignore_handler, builder, out_dir, url_host):
watcher = RebuildServer(watch_dirs, ignore_handler, change_callback=builder)

return Starlette(
routes=[
WebSocketRoute("/websocket-reload", watcher, name="reload"),
Mount("/", app=StaticFiles(directory=out_dir, html=True), name="static"),
],
middleware=[Middleware(JavascriptInjectorMiddleware, ws_url=url_host)],
lifespan=watcher.lifespan,
)


def _parse_args(argv):
# Parse once with the Sphinx parser to emit errors
# and capture the ``-d`` and ``-w`` options.
Expand Down Expand Up @@ -197,4 +202,4 @@ def _add_autobuild_arguments(parser):


if __name__ == "__main__":
main()
main(sys.argv[1:])
32 changes: 32 additions & 0 deletions tests/test_application.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""A very basic test that the application works."""

import shutil
from pathlib import Path

from starlette.testclient import TestClient

from sphinx_autobuild.__main__ import _create_app
from sphinx_autobuild.build import Builder
from sphinx_autobuild.filter import IgnoreFilter

ROOT = Path(__file__).parent.parent


def test_application(tmp_path):
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)

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(rebuild=False)

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

0 comments on commit 674264e

Please sign in to comment.