| 1 |
"""Configuration of the pytest session.""" |
| 2 |
|
| 3 |
import re |
| 4 |
import time |
| 5 |
|
| 6 |
from http.client import HTTPConnection |
| 7 |
from os import environ |
| 8 |
from pathlib import Path |
| 9 |
from shutil import copytree |
| 10 |
from subprocess import PIPE, Popen |
| 11 |
from typing import Callable |
| 12 |
|
| 13 |
import pytest |
| 14 |
import sphinx |
| 15 |
|
| 16 |
from bs4 import BeautifulSoup |
| 17 |
from sphinx.testing.util import SphinxTestApp |
| 18 |
from typing_extensions import Self |
| 19 |
|
| 20 |
|
| 21 |
pytest_plugins = "sphinx.testing.fixtures" |
| 22 |
|
| 23 |
tests_path = Path(__file__).parent |
| 24 |
repo_path = tests_path.parent |
| 25 |
docs_build_path = repo_path / "docs" / "_build" / "html" |
| 26 |
|
| 27 |
# -- Utils method ------------------------------------------------------------ |
| 28 |
|
| 29 |
|
| 30 |
def escape_ansi(string: str) -> str: |
| 31 |
"""Helper function to remove ansi coloring from sphinx warnings.""" |
| 32 |
ansi_escape = re.compile(r"(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]") |
| 33 |
return ansi_escape.sub("", string) |
| 34 |
|
| 35 |
|
| 36 |
# -- global fixture to build sphinx tmp docs --------------------------------- |
| 37 |
|
| 38 |
|
| 39 |
class SphinxBuild: |
| 40 |
"""Helper class to build a test documentation.""" |
| 41 |
|
| 42 |
def __init__(self, app: SphinxTestApp, src: Path): |
| 43 |
self.app = app |
| 44 |
self.src = src |
| 45 |
|
| 46 |
def build(self, no_warning: bool = True) -> Self: |
| 47 |
"""Build the application.""" |
| 48 |
self.app.build() |
| 49 |
if no_warning is True: |
| 50 |
assert self.warnings == "", self.status |
| 51 |
return self |
| 52 |
|
| 53 |
@property |
| 54 |
def status(self) -> str: |
| 55 |
"""Returns the status of the current build.""" |
| 56 |
return self.app._status.getvalue() |
| 57 |
|
| 58 |
@property |
| 59 |
def warnings(self) -> str: |
| 60 |
"""Returns the warnings raised by the current build.""" |
| 61 |
return self.app._warning.getvalue() |
| 62 |
|
| 63 |
@property |
| 64 |
def outdir(self) -> Path: |
| 65 |
"""Returns the output directory of the current build.""" |
| 66 |
return Path(self.app.outdir) |
| 67 |
|
| 68 |
def html_tree(self, *path) -> str: |
| 69 |
"""Returns the html tree of the current build.""" |
| 70 |
path_page = self.outdir.joinpath(*path) |
| 71 |
if not path_page.exists(): |
| 72 |
raise ValueError(f"{path_page} does not exist") |
| 73 |
return BeautifulSoup(path_page.read_text("utf8"), "html.parser") |
| 74 |
|
| 75 |
|
| 76 |
@pytest.fixture() |
| 77 |
def sphinx_build_factory(make_app: Callable, tmp_path: Path, request) -> Callable: |
| 78 |
"""Return a factory builder pointing to the tmp directory.""" |
| 79 |
|
| 80 |
def _func(src_folder: str, **kwargs) -> SphinxBuild: |
| 81 |
"""Create the Sphinxbuild from the source folder.""" |
| 82 |
no_temp = environ.get("PST_TEST_HTML_DIR") |
| 83 |
nonlocal tmp_path |
| 84 |
if no_temp is not None: |
| 85 |
tmp_path = Path(no_temp) / request.node.name / str(src_folder) |
| 86 |
srcdir = tmp_path / src_folder |
| 87 |
if sphinx.version_info < (7, 2): |
| 88 |
from sphinx.testing.path import path as sphinx_path |
| 89 |
|
| 90 |
srcdir = sphinx_path(srcdir) |
| 91 |
|
| 92 |
copytree(tests_path / "sites" / src_folder, tmp_path / src_folder) |
| 93 |
app = make_app(srcdir=srcdir, **kwargs) |
| 94 |
return SphinxBuild(app, tmp_path / src_folder) |
| 95 |
|
| 96 |
yield _func |
| 97 |
|
| 98 |
|
| 99 |
@pytest.fixture(scope="module") |
| 100 |
def url_base(): |
| 101 |
"""Start local server on built docs and return the localhost URL as the base URL.""" |
| 102 |
# Use a port that is not commonly used during development or else you will |
| 103 |
# force the developer to stop running their dev server in order to run the |
| 104 |
# tests. |
| 105 |
port = "8213" |
| 106 |
host = "localhost" |
| 107 |
url = f"http://{host}:{port}" |
| 108 |
|
| 109 |
# Try starting the server |
| 110 |
process = Popen( |
| 111 |
["python", "-m", "http.server", port, "--directory", docs_build_path], |
| 112 |
stdout=PIPE, |
| 113 |
) |
| 114 |
|
| 115 |
# Try connecting to the server |
| 116 |
retries = 5 |
| 117 |
while retries > 0: |
| 118 |
conn = HTTPConnection(host, port) |
| 119 |
try: |
| 120 |
conn.request("HEAD", "/") |
| 121 |
response = conn.getresponse() |
| 122 |
if response is not None: |
| 123 |
yield url |
| 124 |
break |
| 125 |
except ConnectionRefusedError: |
| 126 |
time.sleep(1) |
| 127 |
retries -= 1 |
| 128 |
|
| 129 |
# If the code above never yields a URL, then we were never able to connect |
| 130 |
# to the server and retries == 0. |
| 131 |
if not retries: |
| 132 |
raise RuntimeError("Failed to start http server in 5 seconds") |
| 133 |
else: |
| 134 |
# Otherwise the server started and this fixture is done now and we clean |
| 135 |
# up by stopping the server. |
| 136 |
process.terminate() |
| 137 |
process.wait() |