1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
"""Pytest conftest module containing common test configuration and fixtures."""
import json
import os.path
import secrets
import shutil
import socket
import string
import subprocess
import sys
import tempfile
from pathlib import Path
from typing import Any
import pytest
from docutils.nodes import document
from sphinx import version_info
from sphinx.application import Sphinx
from sphinx.testing.path import path
from sphinx.testing.util import SphinxTestApp
from sphinx.util.console import strip_colors
from syrupy.extensions.single_file import SingleFileSnapshotExtension, WriteMode
from xprocess import ProcessStarter
pytest_plugins = "sphinx.testing.fixtures"
def generate_random_string() -> str:
"""
Generate a random string of 10 characters consisting of letters (both uppercase and lowercase) and digits.
:return: A random string.
"""
characters = string.ascii_letters + string.digits
return "".join(secrets.choice(characters) for i in range(10))
def copy_srcdir_to_tmpdir(srcdir: path, tmp: path) -> path:
"""
Copy Source Directory to Temporary Directory.
This function copies the contents of a source directory to a temporary
directory. It generates a random subdirectory within the temporary directory
to avoid conflicts and enable parallel processes to run without conflicts.
:param srcdir: Path to the source directory.
:param tmp: Path to the temporary directory.
:return: Path to the newly created directory in the temporary directory.
"""
srcdir = path(__file__).parent.abspath() / srcdir
tmproot = tmp.joinpath(generate_random_string()) / path(srcdir).basename()
shutil.copytree(srcdir, tmproot)
return tmproot
def get_abspath(relpath: str) -> str:
"""
Get the absolute path from a relative path.
This function returns an absolute path relative to the conftest.py file.
:param relpath: The relative path to convert.
:return: The absolute path, or the input if it's not a valid relative path.
"""
if isinstance(relpath, str) and relpath:
abspath = Path(__file__).parent.joinpath(relpath).resolve()
return str(abspath)
return relpath
@pytest.fixture(scope="session")
def test_server(xprocess, sphinx_test_tempdir):
"""
Fixture to start and manage the test server process.
:param sphinx_test_tempdir: The directory to serve.
:return: Information about the server process.
"""
addr = "127.0.0.1"
port = 62343
class Starter(ProcessStarter):
pattern = "Serving HTTP on [0-9.]+ port 62343|Address already in use"
timeout = 20
terminate_on_interrupt = True
args = [
"python3",
"-m",
"http.server",
"--directory",
sphinx_test_tempdir,
"--bind",
addr,
port,
]
env = {"PYTHONUNBUFFERED": "1"}
def check_server_connection(log_path: str):
"""
Checks the connection status to a server.
:param log_path: The path to the log file.
:return: True if the server connection is successful, False otherwise.
"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((addr, port))
sock.close()
if result == 0:
with open(str(log_path), "wb", 0) as stdout:
stdout.write(
bytes(
"Serving HTTP on 127.0.0.1 port 62343 (http://127.0.0.1:62343/) ...\n",
"utf8",
)
)
return True
return False
if not check_server_connection(log_path=xprocess.getinfo("http_server").logpath):
# Start the process and ensure it is running
_, logfile = xprocess.ensure("http_server", Starter, persist_logs=False)
http_server_process = xprocess.getinfo("http_server")
server_url = f"http://{addr}:{port}"
http_server_process.url = server_url
yield http_server_process
# clean up whole process tree afterward
xprocess.getinfo("http_server").terminate()
def test_js(self) -> dict[str, Any]:
"""
Executes Cypress tests using the specified `spec_pattern`.
:param self: An instance of the :class:`Sphinx` application object this function is bounded to.
:return: A dictionary with test execution information.
Keys:
- 'returncode': Return code of the Cypress test execution.
- 'stdout': Standard output of the Cypress test execution.
- 'stderr': Standard error of the Cypress test execution.
"""
cypress_testpath = get_abspath(self.spec_pattern)
if not cypress_testpath or not (
os.path.isabs(cypress_testpath) and os.path.exists(cypress_testpath)
):
return {
"returncode": 1,
"stdout": None,
"stderr": f"The spec_pattern '{self.spec_pattern}' cannot be found.",
}
_, out_dir = str(self.outdir).split("sn_test_build_data")
srcdir_url = f"http://127.0.0.1:62343/{out_dir.lstrip('/')}/"
js_test_config = {
"specPattern": cypress_testpath,
"supportFile": get_abspath("js_test/cypress/support/e2e.js"),
"fixturesFolder": False,
"baseUrl": srcdir_url,
}
cypress_config = json.dumps(js_test_config)
cypress_config_file = get_abspath("js_test/cypress.config.js")
# Run the Cypress test command
completed_process = subprocess.run(
[
"npx",
"cypress",
"run",
# "--browser",
# "chrome",
"--config-file",
rf"{cypress_config_file}",
"--config",
rf"{cypress_config}",
],
capture_output=True,
)
# Send back return code, stdout, and stderr
stdout = completed_process.stdout.decode("utf-8")
stderr = completed_process.stderr.decode("utf-8")
if completed_process.returncode != 0:
print(stdout)
print(stderr, file=sys.stderr)
return {
"returncode": completed_process.returncode,
"stdout": stdout,
"stderr": stderr,
}
def pytest_addoption(parser):
parser.addoption(
"--sn-build-dir",
action="store",
default=None,
help="Base directory for sphinx-needs builds",
)
@pytest.fixture(scope="session")
def sphinx_test_tempdir(request) -> path:
"""
Fixture to provide a temporary directory for Sphinx testing.
This function creates a custom temporary folder to avoid potential conflicts
with utility functions from Sphinx and pytest.
:return Path: Path object representing the temporary directory.
"""
# We create a temp-folder on our own, as the util-functions from sphinx and pytest make troubles.
# It seems like they reuse certain-temp names
temp_base = os.path.abspath(
request.config.getoption("--sn-build-dir") or tempfile.gettempdir()
)
sphinx_test_tempdir = path(temp_base).joinpath("sn_test_build_data")
utils_dir = sphinx_test_tempdir.joinpath("utils")
# if not (sphinx_test_tempdir.exists() and sphinx_test_tempdir.isdir()):
sphinx_test_tempdir.makedirs(exist_ok=True)
# if not (utils_dir.exists() and utils_dir.isdir()):
utils_dir.makedirs(exist_ok=True)
# copy plantuml.jar to current test tempdir. We want to do this once
# since the same plantuml.jar is used for each test
plantuml_jar_file = path(__file__).parent.abspath() / "doc_test/utils"
shutil.copytree(plantuml_jar_file, utils_dir, dirs_exist_ok=True)
return sphinx_test_tempdir
@pytest.fixture(scope="function")
def test_app(make_app, sphinx_test_tempdir, request):
"""
Fixture for creating a Sphinx application for testing.
This fixture creates a Sphinx application with specified builder parameters and
config overrides. It also copies the test source directory to the test temporary
directory. The fixture yields the Sphinx application, and cleans up the temporary
source directory after the test function has executed.
:param make_app: A fixture for creating Sphinx applications.
:param sphinx_test_tempdir: A fixture for providing the Sphinx test temporary directory.
:param request: A pytest request object for accessing fixture parameters.
:return: A Sphinx application object.
"""
builder_params = request.param
sphinx_conf_overrides = builder_params.get("confoverrides", {})
if not builder_params.get("no_plantuml", False):
# Since we don't want copy the plantuml.jar file for each test function,
# we need to override the plantuml conf variable and set it to what we have already
plantuml = "java -Djava.awt.headless=true -jar {}".format(
os.path.join(sphinx_test_tempdir, "utils", "plantuml.jar")
)
sphinx_conf_overrides.update(plantuml=plantuml)
# copy test srcdir to test temporary directory sphinx_test_tempdir
srcdir = builder_params.get("srcdir")
src_dir = copy_srcdir_to_tmpdir(srcdir, sphinx_test_tempdir)
parent_path = Path(str(src_dir.parent.abspath()))
if version_info >= (7, 2):
src_dir = Path(str(src_dir))
# return sphinx.testing fixture make_app and new srcdir which is in sphinx_test_tempdir
app: SphinxTestApp = make_app(
buildername=builder_params.get("buildername", "html"),
srcdir=src_dir,
freshenv=builder_params.get("freshenv"),
confoverrides=sphinx_conf_overrides,
status=builder_params.get("status"),
warning=builder_params.get("warning"),
tags=builder_params.get("tags"),
docutilsconf=builder_params.get("docutilsconf"),
parallel=builder_params.get("parallel", 0),
)
# Add the Sphinx warning as list to the app
# Somehow "app._warning" seems to be just a boolean, if the builder is "latex" or "singlehtml".
# In this case we don't catch the warnings.
if builder_params.get("buildername", "html") == "html":
app.warning_list = strip_colors(
app._warning.getvalue().replace(str(app.srcdir) + os.sep, "srcdir/")
).splitlines()
else:
app.warning_list = None
# Add the spec_pattern as an attribute to the Sphinx app object
app.spec_pattern = builder_params.get("spec_pattern", "*.cy.js")
# Add the ``test_js`` function as an attribute to the Sphinx app object
# This is done by accessing the special method ``__get__`` which allows the ``test_js`` function
# to be bound to the Sphinx app object, enabling it to access the object's attributes.
# We can later call ``test_js`` function as an attribute of the Sphinx app object.
# Since we've bound the ``test_js`` function to the Sphinx object using ``__get__``,
# ``test_js`` behaves like a method.
app.test_js = test_js.__get__(app, Sphinx)
yield app
app.cleanup()
# Clean up the srcdir of each Sphinx app after the test function has executed
if request.config.getoption("--sn-build-dir") is None:
shutil.rmtree(parent_path, ignore_errors=True)
class DoctreeSnapshotExtension(SingleFileSnapshotExtension):
_write_mode = WriteMode.TEXT
_file_extension = "doctree.xml"
def serialize(self, data, **kwargs):
if not isinstance(data, document):
raise TypeError(f"Expected document, got {type(data)}")
doc = data.deepcopy()
doc["source"] = "<source>" # this will be a temp path
doc.attributes.pop("translation_progress", None) # added in sphinx 7.1
return doc.pformat()
@pytest.fixture
def snapshot_doctree(snapshot):
"""Snapshot fixture for doctrees.
Here we try to sanitize the doctree, to make the snapshots reproducible.
"""
try:
return snapshot.with_defaults(extension_class=DoctreeSnapshotExtension)
except AttributeError:
# fallback for older versions of pytest-snapshot
return snapshot.use_extension(DoctreeSnapshotExtension)
|