-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/") |