| 1 |
"""Test the HTML builder and check output against XPath.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import re |
| 6 |
from pathlib import Path |
| 7 |
from typing import TYPE_CHECKING |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
import sphinx.builders.html |
| 12 |
from sphinx.builders.html._assets import _file_checksum |
| 13 |
from sphinx.errors import ThemeError |
| 14 |
|
| 15 |
if TYPE_CHECKING: |
| 16 |
from sphinx.testing.util import SphinxTestApp |
| 17 |
|
| 18 |
|
| 19 |
@pytest.mark.sphinx('html', testroot='html_assets') |
| 20 |
def test_html_assets(app: SphinxTestApp) -> None: |
| 21 |
app.build(force_all=True) |
| 22 |
|
| 23 |
# exclude_path and its family |
| 24 |
assert not (app.outdir / 'static' / 'index.html').exists() |
| 25 |
assert not (app.outdir / 'extra' / 'index.html').exists() |
| 26 |
|
| 27 |
# html_static_path |
| 28 |
assert not (app.outdir / '_static' / '.htaccess').exists() |
| 29 |
assert not (app.outdir / '_static' / '.htpasswd').exists() |
| 30 |
assert (app.outdir / '_static' / 'API.html').exists() |
| 31 |
assert (app.outdir / '_static' / 'API.html').read_text( |
| 32 |
encoding='utf8' |
| 33 |
) == 'Sphinx-1.4.4' |
| 34 |
assert (app.outdir / '_static' / 'css' / 'style.css').exists() |
| 35 |
assert (app.outdir / '_static' / 'js' / 'custom.js').exists() |
| 36 |
assert (app.outdir / '_static' / 'rimg.png').exists() |
| 37 |
assert not (app.outdir / '_static' / '_build' / 'index.html').exists() |
| 38 |
assert (app.outdir / '_static' / 'background.png').exists() |
| 39 |
assert not (app.outdir / '_static' / 'subdir' / '.htaccess').exists() |
| 40 |
assert not (app.outdir / '_static' / 'subdir' / '.htpasswd').exists() |
| 41 |
|
| 42 |
# html_extra_path |
| 43 |
assert (app.outdir / '.htaccess').exists() |
| 44 |
assert not (app.outdir / '.htpasswd').exists() |
| 45 |
assert (app.outdir / 'API.html.jinja').exists() |
| 46 |
assert (app.outdir / 'css/style.css').exists() |
| 47 |
assert (app.outdir / 'rimg.png').exists() |
| 48 |
assert not (app.outdir / '_build' / 'index.html').exists() |
| 49 |
assert (app.outdir / 'background.png').exists() |
| 50 |
assert (app.outdir / 'subdir' / '.htaccess').exists() |
| 51 |
assert not (app.outdir / 'subdir' / '.htpasswd').exists() |
| 52 |
|
| 53 |
# html_css_files |
| 54 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 55 |
assert ( |
| 56 |
'<link rel="stylesheet" type="text/css" href="_static/css/style.css" />' |
| 57 |
) in content |
| 58 |
assert ( |
| 59 |
'<link media="print" rel="stylesheet" title="title" type="text/css" ' |
| 60 |
'href="https://example.com/custom.css" />' |
| 61 |
) in content |
| 62 |
|
| 63 |
# html_js_files |
| 64 |
assert '<script src="_static/js/custom.js"></script>' in content |
| 65 |
assert ( |
| 66 |
'<script async="async" src="https://example.com/script.js"></script>' |
| 67 |
) in content |
| 68 |
|
| 69 |
|
| 70 |
@pytest.mark.sphinx('html', testroot='html_assets') |
| 71 |
def test_assets_order(app: SphinxTestApp, monkeypatch: pytest.MonkeyPatch) -> None: |
| 72 |
monkeypatch.setattr(sphinx.builders.html, '_file_checksum', lambda o, f: '') |
| 73 |
|
| 74 |
app.add_css_file('normal.css') |
| 75 |
app.add_css_file('early.css', priority=100) |
| 76 |
app.add_css_file('late.css', priority=750) |
| 77 |
app.add_css_file('lazy.css', priority=900) |
| 78 |
app.add_js_file('normal.js') |
| 79 |
app.add_js_file('early.js', priority=100) |
| 80 |
app.add_js_file('late.js', priority=750) |
| 81 |
app.add_js_file('lazy.js', priority=900) |
| 82 |
|
| 83 |
app.build(force_all=True) |
| 84 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 85 |
|
| 86 |
# css_files |
| 87 |
expected = [ |
| 88 |
'_static/early.css', |
| 89 |
'_static/pygments.css', |
| 90 |
'https://example.com/custom.css', |
| 91 |
'_static/normal.css', |
| 92 |
'_static/late.css', |
| 93 |
'_static/css/style.css', |
| 94 |
'_static/lazy.css', |
| 95 |
] |
| 96 |
pattern = '.*'.join(f'href="{re.escape(f)}"' for f in expected) |
| 97 |
assert re.search(pattern, content, re.DOTALL), content |
| 98 |
|
| 99 |
# js_files |
| 100 |
expected = [ |
| 101 |
'_static/early.js', |
| 102 |
'_static/doctools.js', |
| 103 |
'_static/sphinx_highlight.js', |
| 104 |
'https://example.com/script.js', |
| 105 |
'_static/normal.js', |
| 106 |
'_static/late.js', |
| 107 |
'_static/js/custom.js', |
| 108 |
'_static/lazy.js', |
| 109 |
] |
| 110 |
pattern = '.*'.join(f'src="{re.escape(f)}"' for f in expected) |
| 111 |
assert re.search(pattern, content, re.DOTALL), content |
| 112 |
|
| 113 |
|
| 114 |
@pytest.mark.sphinx('html', testroot='html_file_checksum') |
| 115 |
def test_file_checksum(app: SphinxTestApp) -> None: |
| 116 |
app.add_css_file('stylesheet-a.css') |
| 117 |
app.add_css_file('stylesheet-b.css') |
| 118 |
app.add_css_file('https://example.com/custom.css') |
| 119 |
app.add_js_file('script.js') |
| 120 |
app.add_js_file('empty.js') |
| 121 |
app.add_js_file('https://example.com/script.js') |
| 122 |
|
| 123 |
app.build(force_all=True) |
| 124 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 125 |
|
| 126 |
# checksum for local files |
| 127 |
assert ( |
| 128 |
'<link rel="stylesheet" type="text/css" href="_static/stylesheet-a.css?v=e575b6df" />' |
| 129 |
) in content |
| 130 |
assert ( |
| 131 |
'<link rel="stylesheet" type="text/css" href="_static/stylesheet-b.css?v=a2d5cc0f" />' |
| 132 |
) in content |
| 133 |
assert '<script src="_static/script.js?v=48278d48"></script>' in content |
| 134 |
|
| 135 |
# empty files have no checksum |
| 136 |
assert '<script src="_static/empty.js"></script>' in content |
| 137 |
|
| 138 |
# no checksum for hyperlinks |
| 139 |
assert ( |
| 140 |
'<link rel="stylesheet" type="text/css" href="https://example.com/custom.css" />' |
| 141 |
) in content |
| 142 |
assert '<script src="https://example.com/script.js"></script>' in content |
| 143 |
|
| 144 |
|
| 145 |
def test_file_checksum_query_string() -> None: |
| 146 |
with pytest.raises( |
| 147 |
ThemeError, |
| 148 |
match='Local asset file paths must not contain query strings', |
| 149 |
): |
| 150 |
_file_checksum(Path(), 'with_query_string.css?dead_parrots=1') |
| 151 |
|
| 152 |
with pytest.raises( |
| 153 |
ThemeError, |
| 154 |
match='Local asset file paths must not contain query strings', |
| 155 |
): |
| 156 |
_file_checksum(Path(), 'with_query_string.js?dead_parrots=1') |
| 157 |
|
| 158 |
with pytest.raises( |
| 159 |
ThemeError, |
| 160 |
match='Local asset file paths must not contain query strings', |
| 161 |
): |
| 162 |
_file_checksum(Path.cwd(), '_static/with_query_string.css?dead_parrots=1') |
| 163 |
|
| 164 |
with pytest.raises( |
| 165 |
ThemeError, |
| 166 |
match='Local asset file paths must not contain query strings', |
| 167 |
): |
| 168 |
_file_checksum(Path.cwd(), '_static/with_query_string.js?dead_parrots=1') |
| 169 |
|
| 170 |
|
| 171 |
@pytest.mark.sphinx('html', testroot='html_assets') |
| 172 |
def test_javscript_loading_method(app: SphinxTestApp) -> None: |
| 173 |
app.add_js_file('normal.js') |
| 174 |
app.add_js_file('early.js', loading_method='async') |
| 175 |
app.add_js_file('late.js', loading_method='defer') |
| 176 |
|
| 177 |
app.build(force_all=True) |
| 178 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 179 |
|
| 180 |
assert '<script src="_static/normal.js"></script>' in content |
| 181 |
assert '<script async="async" src="_static/early.js"></script>' in content |
| 182 |
assert '<script defer="defer" src="_static/late.js"></script>' in content |
| 183 |
|
| 184 |
|
| 185 |
@pytest.mark.sphinx('html', testroot='ext_static_dir') |
| 186 |
def test_extension_static_dir(app: SphinxTestApp) -> None: |
| 187 |
"""Test that extension static directories are copied.""" |
| 188 |
app.build(force_all=True) |
| 189 |
|
| 190 |
# Verify extension static files were copied to _static |
| 191 |
assert (app.outdir / '_static' / 'js' / 'myext.js').is_file() |
| 192 |
assert (app.outdir / '_static' / 'css' / 'myext.css').is_file() |
| 193 |
|
| 194 |
# Verify JS and CSS are included in the HTML output |
| 195 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 196 |
assert '_static/js/myext.js' in content |
| 197 |
assert '_static/css/myext.css' in content |
| 198 |
|
| 199 |
|
| 200 |
@pytest.mark.sphinx('html', testroot='ext_static_dir') |
| 201 |
def test_extension_static_dir_registry(app: SphinxTestApp) -> None: |
| 202 |
"""Test that extension static directories are registered in the registry.""" |
| 203 |
# Verify the static dir is registered |
| 204 |
assert len(app.registry.static_dirs) == 1 |
| 205 |
assert app.registry.static_dirs[0].name == 'static' |