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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
|
import os
import pytest
import sphinx
import shutil
import subprocess
from utils import _get_css_html_link_tag, _get_js_html_link_tag
srcdir = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'examples',
'default',
)
rstsrcdir = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'examples',
'404rst',
)
extensiondir = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'examples',
'extension',
)
@pytest.fixture(autouse=True, scope='function')
def remove_sphinx_build_output():
"""Remove _build/ folder, if exist."""
for path in (srcdir, rstsrcdir, extensiondir):
build_path = os.path.join(path, '_build')
if os.path.exists(build_path):
shutil.rmtree(build_path)
@pytest.mark.sphinx(srcdir=srcdir)
def test_parallel_build():
# TODO: migrate to `app.build(..., parallel=2)` after merging
# https://github.com/sphinx-doc/sphinx/pull/8257
subprocess.check_call('sphinx-build -j 2 -W -b html tests/examples/parallel-build build', shell=True)
@pytest.mark.sphinx(srcdir=srcdir)
def test_404_page_created(app, status, warning):
app.build()
path = app.outdir / '404.html'
assert path.exists()
@pytest.mark.sphinx('epub', srcdir=srcdir)
def test_404_page_not_created(app, status, warning):
assert app.builder.embedded
app.build()
path = app.outdir / '404.html'
assert not path.exists()
@pytest.mark.sphinx(srcdir=srcdir, confoverrides={'html_theme': 'alabaster'})
def test_default_settings(app, status, warning):
app.build()
path = app.outdir / '404.html'
assert path.exists()
content = path.read_text()
if sphinx.version_info < (6, 0):
cssclass = "shortcut "
else:
cssclass = ""
if sphinx.version_info < (7, 4):
alt = "Logo"
else:
alt = "Logo of Python"
chunks = [
'<h1>Page not found</h1>',
"Unfortunately we couldn't find the content you were looking for.",
'<title>Page not found — Python documentation</title>',
# favicon and logo
f'<link rel="{cssclass}icon" href="/en/latest/_static/favicon.png"/>',
f'<img class="logo" src="/en/latest/_static/logo.svg" alt="{alt}"/>',
# sidebar URLs
'<h1 class="logo"><a href="/en/latest/index.html">Python</a></h1>',
'<form class="search" action="/en/latest/search.html" method="get">',
'<li><a href="/en/latest/index.html">Documentation overview</a><ul>',
# resources
_get_css_html_link_tag(app, 'en', 'latest', 'alabaster.css'),
_get_css_html_link_tag(app, 'en', 'latest', 'pygments.css'),
'<link rel="stylesheet" href="/en/latest/_static/custom.css" type="text/css" />',
]
for chunk in chunks:
assert chunk in content
@pytest.mark.sphinx(
srcdir=srcdir,
confoverrides={
'notfound_context': {'title': 'My custom title', 'body': '<h1>Boo!</h1>My bad.'},
},
)
def test_context_settings(app, status, warning):
app.build()
path = app.outdir / '404.html'
assert path.exists()
content = path.read_text()
chunks = [
'<h1>Boo!</h1>',
'My bad.',
'<title>My custom title — Python documentation</title>',
]
for chunk in chunks:
assert chunk in content
@pytest.mark.sphinx(
srcdir=srcdir,
confoverrides={
'notfound_pagename': '500',
},
)
def test_pagename_setting(app, status, warning):
app.build()
path = app.outdir / '500.html'
assert path.exists()
@pytest.mark.sphinx(
srcdir=srcdir,
confoverrides={
'notfound_urls_prefix': '/language/version/',
'html_theme': 'alabaster',
},
)
def test_urls_prefix_setting(app, status, warning):
app.build()
path = app.outdir / '404.html'
assert path.exists()
content = path.read_text()
if sphinx.version_info < (6, 0):
cssclass = "shortcut "
else:
cssclass = ""
if sphinx.version_info < (7, 4):
alt = "Logo"
else:
alt = "Logo of Python"
chunks = [
# sidebar URLs
'<h1 class="logo"><a href="/language/version/index.html">Python</a></h1>',
'<form class="search" action="/language/version/search.html" method="get">',
'<li><a href="/language/version/index.html">Documentation overview</a><ul>',
# favicon and logo
f'<link rel="{cssclass}icon" href="/language/version/_static/favicon.png"/>',
f'<img class="logo" src="/language/version/_static/logo.svg" alt="{alt}"/>',
# resources
_get_css_html_link_tag(app, 'language', 'version', 'alabaster.css'),
_get_css_html_link_tag(app, 'language', 'version', 'pygments.css'),
'<link rel="stylesheet" href="/language/version/_static/custom.css" type="text/css" />',
]
for chunk in chunks:
assert chunk in content
@pytest.mark.sphinx(
srcdir=srcdir,
confoverrides={
'notfound_urls_prefix': None,
'html_theme': 'alabaster',
},
)
def test_urls_prefix_setting_none(app, status, warning):
app.build()
path = app.outdir / '404.html'
assert path.exists()
content = path.read_text()
if sphinx.version_info < (6, 0):
cssclass = "shortcut "
else:
cssclass = ""
if sphinx.version_info < (7, 4):
alt = "Logo"
else:
alt = "Logo of Python"
chunks = [
# sidebar URLs
'<h1 class="logo"><a href="/index.html">Python</a></h1>',
'<form class="search" action="/search.html" method="get">',
'<li><a href="/index.html">Documentation overview</a><ul>',
# favicon and logo
f'<link rel="{cssclass}icon" href="/_static/favicon.png"/>',
f'<img class="logo" src="/_static/logo.svg" alt="{alt}"/>',
# resources
_get_css_html_link_tag(app, '', '', 'alabaster.css'),
_get_css_html_link_tag(app, '', '', 'pygments.css'),
'<link rel="stylesheet" href="/_static/custom.css" type="text/css" />',
]
for chunk in chunks:
assert chunk in content
assert "The config value `notfound_urls_prefix' has type `NoneType', defaults to `str'" not in warning.getvalue()
assert "build succeeded." in status.getvalue()
@pytest.mark.sphinx(
srcdir=srcdir,
confoverrides={
'notfound_template': 'template.html',
'notfound_context': {
'body': 'The body goes here',
'title': 'Custom title',
'special_setting': 'a special value',
},
},
)
def test_template_setting(app, status, warning):
app.build()
path = app.outdir / '404.html'
assert path.exists()
content = path.read_text()
chunks = [
'Custom title',
'The body goes here',
'<p>This is rendered using a custom template</p>',
'<p>... which has a custom context as well: a special value</p>',
]
for chunk in chunks:
assert chunk in content
@pytest.mark.sphinx(
srcdir=rstsrcdir,
confoverrides={
'version': '2.5.1',
'html_theme': 'alabaster',
},
)
def test_custom_404_rst_source(app, status, warning):
app.build()
path = app.outdir / '404.html'
assert path.exists()
content = path.read_text()
chunks = [
# custom 404.rst file content
'<title>Oh, oh - Page not found — Python documentation</title>',
'<p>This is a custom 404.rst file.</p>',
'<p>This file should be rendered instead of the default one.</p>',
"<p>Variables Sphinx substitution should be allowed here.\nExample, version: 2.5.1.</p>",
# sidebar URLs
'<h1 class="logo"><a href="/en/latest/index.html">Python</a></h1>',
'<form class="search" action="/en/latest/search.html" method="get">',
'<li><a href="/en/latest/index.html">Documentation overview</a><ul>',
# resources
_get_css_html_link_tag(app, 'en', 'latest', 'alabaster.css'),
_get_css_html_link_tag(app, 'en', 'latest', 'pygments.css'),
'<link rel="stylesheet" href="/en/latest/_static/custom.css" type="text/css" />',
]
for chunk in chunks:
assert chunk in content
@pytest.mark.sphinx(srcdir=rstsrcdir)
def test_image_on_404_rst_source(app, status, warning):
app.build()
# Check the image was added to the builder/environment images
assert 'test.png' in app.builder.images
assert 'test.png' in app.env.images
# Check the image was copied into the output dir
path = app.outdir / '_images' / 'test.png'
assert path.exists()
path = app.outdir / '_images' / 'loudly-crying-face.png'
assert path.exists()
path = app.outdir / '404.html'
assert path.exists()
content = path.read_text()
chunks = [
# .. image::
'<img alt="An image" src="/en/latest/_images/test.png" />',
'<img alt="Image from folder" src="/en/latest/_images/loudly-crying-face.png" />',
]
if sphinx.version_info < (7, 2):
chunks.extend([
# .. figure::
'<figure class="align-default" id="id1">\n<img alt="/en/latest/_images/test.png" src="/en/latest/_images/test.png" />\n<figcaption>\n<p><span class="caption-text">Description.</span><a class="headerlink" href="#id1" title="Permalink to this image">¶</a></p>\n</figcaption>\n</figure>',
])
else:
chunks.extend([
# .. figure::
'<figure class="align-default" id="id1">\n<img alt="/en/latest/_images/test.png" src="/en/latest/_images/test.png" />\n<figcaption>\n<p><span class="caption-text">Description.</span><a class="headerlink" href="#id1" title="Link to this image">¶</a></p>\n</figcaption>\n</figure>',
])
for chunk in chunks:
assert chunk in content
@pytest.mark.sphinx(srcdir=rstsrcdir)
def test_image_looks_like_absolute_url(app, status, warning):
app.build()
path = app.outdir / '_images' / 'https.png'
assert path.exists()
path = app.outdir / '404.html'
assert path.exists()
content = path.read_text()
chunks = [
'<img alt="PATH looking as an URL" src="/en/latest/_images/https.png" />',
]
for chunk in chunks:
assert chunk in content
@pytest.mark.sphinx(srcdir=rstsrcdir)
def test_image_absolute_url(app, status, warning):
app.build()
path = app.outdir / '404.html'
assert path.exists()
content = path.read_text()
chunks = [
'<img alt="Read the Docs Logo" src="https://read-the-docs-guidelines.readthedocs-hosted.com/_images/logo-dark.png" />',
]
for chunk in chunks:
assert chunk in content
@pytest.mark.sphinx(
srcdir=srcdir,
buildername='dirhtml',
confoverrides={'html_theme': 'alabaster'},
)
def test_urls_for_dirhtml_builder(app, status, warning):
app.build()
path = app.outdir / '404' / 'index.html'
assert path.exists()
content = path.read_text()
chunks = [
# sidebar URLs
'<form class="search" action="/en/latest/search/" method="get">',
'<li class="toctree-l1"><a class="reference internal" href="/en/latest/chapter/">Chapter</a></li>',
# resources
_get_css_html_link_tag(app, 'en', 'latest', 'alabaster.css'),
_get_css_html_link_tag(app, 'en', 'latest', 'pygments.css'),
'<link rel="stylesheet" href="/en/latest/_static/custom.css" type="text/css" />',
]
for chunk in chunks:
assert chunk in content
@pytest.mark.sphinx(srcdir=srcdir)
def test_sphinx_resource_urls(app, status, warning):
app.build()
path = app.outdir / '404.html'
assert path.exists()
content = path.read_text()
chunks = [
# Sphinx's resources URLs
_get_js_html_link_tag(app, 'en', 'latest', 'doctools.js'),
]
if sphinx.version_info < (6, 0):
chunks.extend([
_get_js_html_link_tag(app, 'en', 'latest', 'underscore.js'),
_get_js_html_link_tag(app, 'en', 'latest', 'jquery.js'),
])
for chunk in chunks:
assert chunk in content
@pytest.mark.sphinx(
srcdir=srcdir,
confoverrides={
'notfound_urls_prefix': '/ja/default/',
'html_theme': 'alabaster',
},
)
def test_toctree_urls_notfound_default(app, status, warning):
app.build()
path = app.outdir / '404.html'
assert path.exists()
content = path.read_text()
chunks = [
# sidebar URLs
'<form class="search" action="/ja/default/search.html" method="get">',
'<li class="toctree-l1"><a class="reference internal" href="/ja/default/chapter.html">Chapter</a></li>',
# resources
_get_css_html_link_tag(app, 'ja', 'default', 'alabaster.css'),
_get_css_html_link_tag(app, 'ja', 'default', 'pygments.css'),
'<link rel="stylesheet" href="/ja/default/_static/custom.css" type="text/css" />',
]
for chunk in chunks:
assert chunk in content
@pytest.mark.sphinx(
srcdir=srcdir,
confoverrides={'html_theme': 'alabaster'},
)
def test_toctree_links(app, status, warning):
app.build()
path = app.outdir / '404.html'
assert path.exists()
content = path.read_text()
chunks = [
'<h3>Navigation</h3>',
'<li class="toctree-l1"><a class="reference internal" href="/en/latest/chapter-i.html">Chapter I</a></li>',
]
for chunk in chunks:
assert chunk in content
@pytest.mark.sphinx(
srcdir=srcdir,
confoverrides={
'notfound_urls_prefix': '/pt-br/stable/',
'html_theme': 'alabaster',
},
)
def test_toctree_links_custom_settings(app, status, warning):
app.build()
path = app.outdir / '404.html'
assert path.exists()
content = path.read_text()
chunks = [
'<h3>Navigation</h3>',
'<li class="toctree-l1"><a class="reference internal" href="/pt-br/stable/chapter-i.html">Chapter I</a></li>',
]
for chunk in chunks:
assert chunk in content
@pytest.mark.sphinx(
srcdir=rstsrcdir,
)
def test_automatic_orphan(app, status, warning):
app.build()
assert app.env.metadata['404'] == {'orphan': True, 'nosearch': True}
@pytest.mark.sphinx(srcdir=extensiondir)
def test_resources_from_extension(app, status, warning):
app.build()
path = app.outdir / '404.html'
assert path.exists()
content = path.read_text()
chunks = [
'<link rel="stylesheet" type="text/css" href="/en/latest/_static/css_added_by_extension.css" />',
'<link rel="stylesheet" type="text/css" href="/en/latest/_static/css_added_by_extension.css" />',
_get_js_html_link_tag(app, 'en', 'latest', 'js_added_by_extension.js'),
]
for chunk in chunks:
assert chunk in content
@pytest.mark.environ(
READTHEDOCS='True',
)
@pytest.mark.sphinx(srcdir=rstsrcdir)
def test_special_readthedocs_urls(environ, app, status, warning):
app.add_js_file('/_/static/javascript/readthedocs-doc-embed.js')
app.build()
path = app.outdir / '404.html'
assert path.exists()
content = path.read_text()
chunks = [
# Link included manually
'<a class="reference external" href="/_/static/javascript/readthedocs-doc-embed.js">readthedocs-doc-embed.js</a>',
]
# Javascript library loaded via Sphinx
chunks.append('<script src="/_/static/javascript/readthedocs-doc-embed.js"></script>')
for chunk in chunks:
assert chunk in content
|