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
|
from __future__ import annotations
import json
import os.path
import sys
from pathlib import Path
import pytest
from lxml import html as html_parser
from sphinx import version_info
from sphinx.testing.util import SphinxTestApp
from syrupy.filters import props
@pytest.mark.parametrize(
"test_app",
[{"buildername": "html", "srcdir": "doc_test/doc_basic", "no_plantuml": True}],
indirect=True,
)
def test_build_html(test_app: SphinxTestApp, snapshot_doctree):
app = test_app
app.build()
assert app._warning.getvalue() == ""
# Check if doctree is correct.
assert app.env.get_doctree("index") == snapshot_doctree
# Basic checks for the generated html.
html = Path(app.outdir, "index.html").read_text()
assert "<h1>TEST DOCUMENT" in html
assert "ST_001" in html
# Check if static files got copied correctly.
build_dir = Path(app.outdir) / "_static" / "sphinx-needs" / "libs" / "html"
files = [f for f in build_dir.glob("**/*") if f.is_file()]
assert build_dir / "sphinx_needs_collapse.js" in files
assert build_dir / "datatables_loader.js" in files
@pytest.mark.skipif(
sys.platform == "win32", reason="assert fails on windows, need to fix later."
)
@pytest.mark.parametrize(
"test_app",
[{"buildername": "html", "srcdir": "doc_test/doc_basic", "no_plantuml": True}],
indirect=True,
)
def test_html_head_files(test_app: SphinxTestApp):
app = test_app
app.build()
assert app._warning.getvalue() == ""
# check usage in project root level
html_path = str(Path(app.outdir, "index.html"))
root_tree = html_parser.parse(html_path)
script_nodes = root_tree.xpath("/html/head/script")
script_files = [x.attrib["src"].rsplit("?", 1)[0] for x in script_nodes]
assert script_files.count("_static/sphinx-needs/libs/html/datatables.min.js") == 1
link_nodes = root_tree.xpath("/html/head/link")
link_files = [x.attrib["href"].rsplit("?", 1)[0] for x in link_nodes]
assert link_files.count("_static/sphinx-needs/modern.css") == 1
# Checks if not \ (Backslash) is found as path of js/css files
# This can happen when working on Windows (would be a bug ;) )
for head_file in script_files + link_files:
assert "\\" not in head_file
@pytest.mark.parametrize(
"test_app",
[
{
"buildername": "singlehtml",
"srcdir": "doc_test/doc_basic",
"no_plantuml": True,
}
],
indirect=True,
)
def test_build_singlehtml(test_app: SphinxTestApp):
app = test_app
app.build()
assert app._warning.getvalue() == ""
@pytest.mark.parametrize(
"test_app",
[{"buildername": "latex", "srcdir": "doc_test/doc_basic", "no_plantuml": True}],
indirect=True,
)
def test_build_latex(test_app: SphinxTestApp):
app = test_app
app.build()
assert app._warning.getvalue() == ""
print([p.name for p in Path(app.outdir).iterdir()])
latex_file = Path(app.outdir, "needs.tex")
assert latex_file
latex_content = latex_file.read_text()
# Check table generated by Sphinxneeds has correct caption
assert (
"\\sphinxcaption{Table from sphinx\\sphinxhyphen{}needs \\textquotesingle"
"{}needtable\\textquotesingle{} directive}" in latex_content
)
# Check that the ST_001 label is only created once in the LaTeX output. Split
# on the string, which should split latex_content into two.
assert len(latex_content.split(r"\label{\detokenize{index:ST_001}}")) == 2
@pytest.mark.parametrize(
"test_app",
[{"buildername": "epub", "srcdir": "doc_test/doc_basic", "no_plantuml": True}],
indirect=True,
)
def test_build_epub(test_app: SphinxTestApp):
app = test_app
app.build()
assert app._warning.getvalue() == ""
@pytest.mark.parametrize(
"test_app",
[{"buildername": "json", "srcdir": "doc_test/doc_basic", "no_plantuml": True}],
indirect=True,
)
def test_build_json(test_app: SphinxTestApp):
app = test_app
app.build()
assert app._warning.getvalue() == ""
@pytest.mark.parametrize(
"test_app",
[{"buildername": "needs", "srcdir": "doc_test/doc_basic", "no_plantuml": True}],
indirect=True,
)
def test_build_needs(test_app: SphinxTestApp, snapshot):
app = test_app
app.build()
assert app._warning.getvalue() == ""
json_text = Path(app.outdir, "needs.json").read_text()
needs_data = json.loads(json_text)
assert needs_data == snapshot(exclude=props("created", "project", "creator"))
def test_sphinx_api_build(tmp_path: Path, make_app: type[SphinxTestApp]):
"""
Tests a build via the Sphinx Build API.
It looks like that there are scenarios where this specific build makes trouble but no others.
"""
src_dir = os.path.join(os.path.dirname(__file__), "doc_test", "doc_basic")
if version_info >= (7, 2):
src_dir = Path(src_dir)
else:
from sphinx.testing.path import path
src_dir = path(src_dir)
tmp_path = path(str(tmp_path))
sphinx_app = make_app(
srcdir=src_dir,
builddir=tmp_path,
buildername="html",
parallel=4,
freshenv=True,
)
sphinx_app.build()
assert sphinx_app.statuscode == 0
|