Skip to content

Commit

Permalink
ci: Add e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
attakei committed May 12, 2024
1 parent 9a7a9af commit 82a3eba
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ Document = "https://atsphinx.github.io/mini18n/"
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.pytest.ini_options]
addopts = "--browser webkit"

[tool.rye]
managed = true
dev-dependencies = [
Expand All @@ -49,12 +52,14 @@ dev-dependencies = [
"beautifulsoup4~=4.12.3",
"sphinx-intl~=2.1.0",
"sphinx-revealjs~=3.0.2",
"pytest-playwright~=0.5.0",
]

[tool.rye.scripts]
setup = {chain = ["setup:sync", "setup:pre-commit"]}
setup = {chain = ["setup:sync", "setup:pre-commit", "setup:playwright"]}
"setup:sync" = "rye sync --no-lock --all-features"
"setup:pre-commit" = "pre-commit install"
"setup:playwright" = "rye run playwright install webkit"
doc = "make -C doc"

[tool.ruff.lint]
Expand Down
17 changes: 17 additions & 0 deletions requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ exceptiongroup==1.2.0
# via cattrs
# via pytest
furo==2024.1.29
greenlet==3.0.3
# via playwright
idna==3.6
# via requests
imagesize==1.4.1
Expand All @@ -60,8 +62,12 @@ packaging==24.0
# via sphinx-revealjs
platformdirs==4.2.0
# via esbonio
playwright==1.43.0
# via pytest-playwright
pluggy==1.4.0
# via pytest
pyee==11.1.0
# via playwright
pygls==1.3.0
# via esbonio
pygments==2.17.2
Expand All @@ -71,9 +77,17 @@ pygments==2.17.2
pyspellchecker==0.8.1
# via esbonio
pytest==7.4.4
# via pytest-base-url
# via pytest-playwright
pytest-base-url==2.1.0
# via pytest-playwright
pytest-playwright==0.5.0
python-slugify==8.0.4
# via pytest-playwright
pytz==2024.1
# via babel
requests==2.31.0
# via pytest-base-url
# via sphinx
setuptools==69.2.0
# via sphinx-intl
Expand Down Expand Up @@ -110,12 +124,15 @@ sphinxcontrib-qthelp==1.0.3
# via sphinx
sphinxcontrib-serializinghtml==1.1.5
# via sphinx
text-unidecode==1.3
# via python-slugify
tomli==2.0.1
# via pytest
tornado==6.4
# via livereload
typing-extensions==4.10.0
# via cattrs
# via pyee
urllib3==2.2.1
# via requests
zipp==3.18.1
Expand Down
62 changes: 62 additions & 0 deletions tests/test_javascript.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""Tests for JavaScript layer."""

import http.server
import socket
import threading
import time
from pathlib import Path
from typing import Tuple

import pytest
from playwright.sync_api import Page
from sphinx.testing.util import SphinxTestApp


class ServerOnPytest(http.server.ThreadingHTTPServer):
"""Wrapper class to work using pytest tmpdir and random port."""

def _is_port_in_use(self, port: int) -> bool:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(("localhost", port)) == 0

def __init__(self, directory: Path, port_range: Tuple[int, int]):
"""Initialize server with found usable port."""
self.directory = directory
for v in range(port_range[0], port_range[1] + 1):
if not self._is_port_in_use(v):
self.port = v
break
raise ValueError("All ports are already used.")
return super().__init__(("", self.port), http.server.SimpleHTTPRequestHandler)

def finish_request(self, request, client_address): # noqa: D102
self.RequestHandlerClass(
request, client_address, self, directory=self.directory
)

def __enter__(self): # noqa: D105
self._thread = threading.Thread(target=self.serve_forever)
self._thread.daemon = True
self._thread.start()
return super().__enter__()

def __exit__(self, *args, **kwargs): # noqa: D105
print(args, kwargs)
super().__exit__()
self.shutdown()


@pytest.mark.sphinx(
"mini18n-html",
confoverrides={
"mini18n_default_language": "en",
"mini18n_support_languages": ["en", "ja"],
},
)
def test__root(app: SphinxTestApp, page: Page):
app.build()
with ServerOnPytest(app.outdir, (30000, 50000)) as server:
url = f"http://localhost:{server.port}/"
page.goto(url)
time.sleep(0.5)
assert page.url.endswith("/en/")

0 comments on commit 82a3eba

Please sign in to comment.