| 1 |
"""All the tests performed in the pydata-sphinx-theme test suite.""" |
| 2 |
|
| 3 |
import re |
| 4 |
|
| 5 |
from pathlib import Path |
| 6 |
|
| 7 |
import pytest |
| 8 |
import sphinx.errors |
| 9 |
|
| 10 |
from pydata_sphinx_theme.utils import escape_ansi |
| 11 |
|
| 12 |
|
| 13 |
COMMON_CONF_OVERRIDES = dict( |
| 14 |
surface_warnings=True, |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
def test_theme_loaded_as_extension(sphinx_build_factory) -> None: |
| 19 |
"""Theme must not crash when loaded via extensions= instead of html_theme=.""" |
| 20 |
sphinx_build = sphinx_build_factory( |
| 21 |
"base", confoverrides={"extensions": ["pydata_sphinx_theme"]} |
| 22 |
) |
| 23 |
sphinx_build.build(no_warning=False) |
| 24 |
|
| 25 |
|
| 26 |
def test_build_html(sphinx_build_factory, file_regression) -> None: |
| 27 |
"""Test building the base html template and config.""" |
| 28 |
sphinx_build = sphinx_build_factory("base") |
| 29 |
|
| 30 |
# Basic build with defaults |
| 31 |
sphinx_build.build() |
| 32 |
assert (sphinx_build.outdir / "index.html").exists(), sphinx_build.outdir.glob("*") |
| 33 |
|
| 34 |
index_html = sphinx_build.html_tree("index.html") |
| 35 |
subpage_html = sphinx_build.html_tree("section1/index.html") |
| 36 |
|
| 37 |
# Navbar structure |
| 38 |
navbar = index_html.select("div.navbar-header-items__center")[0] |
| 39 |
file_regression.check(navbar.prettify(), basename="navbar_ix", extension=".html") |
| 40 |
|
| 41 |
# Sidebar subpage |
| 42 |
sidebar = subpage_html.select(".bd-sidebar")[0] |
| 43 |
file_regression.check( |
| 44 |
sidebar.prettify(), basename="sidebar_subpage", extension=".html" |
| 45 |
) |
| 46 |
|
| 47 |
# Secondary sidebar should not have in-page TOC if it is empty |
| 48 |
assert not sphinx_build.html_tree("page1.html").select("div.onthispage") |
| 49 |
|
| 50 |
# Secondary sidebar should not be present if page-level metadata given |
| 51 |
assert not sphinx_build.html_tree("page2.html").select("div.bd-sidebar-secondary") |
| 52 |
|
| 53 |
|
| 54 |
def test_toc_visibility(sphinx_build_factory) -> None: |
| 55 |
"""Test that setting TOC level visibility works as expected.""" |
| 56 |
confoverrides = { |
| 57 |
"html_theme_options.show_toc_level": 2, |
| 58 |
} |
| 59 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build() |
| 60 |
index_html = sphinx_build.html_tree("index.html") |
| 61 |
|
| 62 |
# The 3rd level headers should be visible, but not the fourth-level |
| 63 |
assert "pst-show_toc_level" in index_html.select(".toc-h2 ul")[0].attrs["class"] |
| 64 |
assert "pst-show_toc_level" not in index_html.select(".toc-h3 ul")[0].attrs["class"] |
| 65 |
|
| 66 |
|
| 67 |
def test_icon_links(sphinx_build_factory, file_regression) -> None: |
| 68 |
"""Test that setting icon links are rendered in the documentation.""" |
| 69 |
html_theme_options_icon_links = { |
| 70 |
"icon_links": [ |
| 71 |
{ |
| 72 |
"name": "FONTAWESOME", |
| 73 |
"url": "https://site1.org", |
| 74 |
"icon": "FACLASS", |
| 75 |
"type": "fontawesome", |
| 76 |
}, |
| 77 |
{ |
| 78 |
"name": "FONTAWESOME DEFAULT", |
| 79 |
"url": "https://site2.org", |
| 80 |
"icon": "FADEFAULTCLASS", |
| 81 |
# No type so we can test that the default is fontawesome |
| 82 |
}, |
| 83 |
{ |
| 84 |
"name": "LOCAL FILE", |
| 85 |
"url": "https://site3.org", |
| 86 |
"icon": "emptylogo.png", # Logo is our only test site img |
| 87 |
"type": "local", |
| 88 |
}, |
| 89 |
{ |
| 90 |
"name": "WRONG TYPE", |
| 91 |
"url": "https://site4.org", |
| 92 |
"icon": "WRONG TYPE", |
| 93 |
# Because the type is inccorect, this should output an error `span` |
| 94 |
"type": "incorrecttype", |
| 95 |
}, |
| 96 |
{ |
| 97 |
"name": "URL", |
| 98 |
"url": "https://site5.org", |
| 99 |
"icon": "https://site5.org/image.svg", |
| 100 |
"type": "url", |
| 101 |
}, |
| 102 |
{ |
| 103 |
"name": "FONTAWESOME", |
| 104 |
"url": "https://site1.org", |
| 105 |
"icon": "FACLASS", |
| 106 |
"type": "fontawesome", |
| 107 |
"attributes": { |
| 108 |
# This should over-ride the href above |
| 109 |
"href": "https://override.com", |
| 110 |
# This should add a new icon link attribute |
| 111 |
"foo": "bar", |
| 112 |
# CSS classes should be totally overwritten |
| 113 |
"class": "overridden classes", |
| 114 |
}, |
| 115 |
}, |
| 116 |
], |
| 117 |
} |
| 118 |
confoverrides = {"html_theme_options": html_theme_options_icon_links} |
| 119 |
|
| 120 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build() |
| 121 |
# Navbar should have the right icons |
| 122 |
icon_links = sphinx_build.html_tree("index.html").select(".navbar-icon-links")[0] |
| 123 |
file_regression.check( |
| 124 |
icon_links.prettify(), basename="navbar_icon_links", extension=".html" |
| 125 |
) |
| 126 |
|
| 127 |
|
| 128 |
@pytest.mark.parametrize( |
| 129 |
"confoverrides,expected_alt_text", |
| 130 |
( |
| 131 |
[dict(), "PyData Tests documentation - Home"], |
| 132 |
[dict(html_theme_options=dict(logo=dict(text="Foo"))), ""], |
| 133 |
[dict(html_theme_options=dict(logo=dict(text="Foo", alt_text="Bar"))), "Bar"], |
| 134 |
), |
| 135 |
) |
| 136 |
def test_logo_alt_text(sphinx_build_factory, confoverrides, expected_alt_text) -> None: |
| 137 |
"""Test our alt-text fallback mechanism.""" |
| 138 |
confoverrides["html_theme_options"] = ( |
| 139 |
confoverrides.get("html_theme_options", False) or dict() |
| 140 |
) |
| 141 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build() |
| 142 |
index_html = sphinx_build.html_tree("index.html") |
| 143 |
logo_image = index_html.select(".navbar-brand img")[0] |
| 144 |
assert logo_image.attrs["alt"] == expected_alt_text |
| 145 |
|
| 146 |
|
| 147 |
def test_logo_basic(sphinx_build_factory) -> None: |
| 148 |
"""Test that the logo is shown by default, project title if no logo.""" |
| 149 |
sphinx_build = sphinx_build_factory("base").build() |
| 150 |
|
| 151 |
# By default logo is shown |
| 152 |
index_html = sphinx_build.html_tree("index.html") |
| 153 |
assert index_html.select(".navbar-brand img") |
| 154 |
assert "emptylogo" in str(index_html.select(".navbar-brand")[0]) |
| 155 |
assert not index_html.select(".navbar-brand")[0].text.strip() |
| 156 |
|
| 157 |
|
| 158 |
def test_logo_no_image(sphinx_build_factory) -> None: |
| 159 |
"""Test that the text is shown if no image specified.""" |
| 160 |
confoverrides = { |
| 161 |
"html_logo": "", |
| 162 |
} |
| 163 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build() |
| 164 |
index_html = sphinx_build.html_tree("index.html") |
| 165 |
assert "PyData Tests" in index_html.select(".navbar-brand")[0].text.strip() |
| 166 |
assert "emptylogo" not in str(index_html.select(".navbar-brand")[0]) |
| 167 |
|
| 168 |
|
| 169 |
def test_logo_two_images(sphinx_build_factory) -> None: |
| 170 |
"""Test that the logo image / text is correct when both dark / light given.""" |
| 171 |
# Test with a specified title and a dark logo |
| 172 |
confoverrides = { |
| 173 |
"html_theme_options": { |
| 174 |
"logo": { |
| 175 |
"text": "Foo Title", |
| 176 |
"image_dark": "_static/emptydarklogo.png", |
| 177 |
}, |
| 178 |
}, |
| 179 |
} |
| 180 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build() |
| 181 |
index_html = sphinx_build.html_tree("index.html") |
| 182 |
index_str = str(index_html.select(".navbar-brand")[0]) |
| 183 |
assert "emptylogo" in index_str |
| 184 |
assert "emptydarklogo" in index_str |
| 185 |
assert "Foo Title" in index_str |
| 186 |
|
| 187 |
|
| 188 |
def test_primary_logo_is_light_when_no_default_mode(sphinx_build_factory) -> None: |
| 189 |
"""Test that the primary logo image is light when no default mode is set.""" |
| 190 |
# Ensure no default mode is set |
| 191 |
confoverrides = { |
| 192 |
"html_context": {}, |
| 193 |
} |
| 194 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build() |
| 195 |
index_html = sphinx_build.html_tree("index.html") |
| 196 |
navbar_brand = index_html.select(".navbar-brand")[0] |
| 197 |
assert navbar_brand.find("img", class_="only-light") is not None |
| 198 |
assert navbar_brand.find("img", class_="only-dark") is not None |
| 199 |
|
| 200 |
|
| 201 |
def test_primary_logo_is_light_when_default_mode_is_set_to_auto( |
| 202 |
sphinx_build_factory, |
| 203 |
) -> None: |
| 204 |
"""Test that the primary logo image is light when default is set to auto.""" |
| 205 |
# Ensure no default mode is set |
| 206 |
confoverrides = { |
| 207 |
"html_context": {"default_mode": "auto"}, |
| 208 |
} |
| 209 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build() |
| 210 |
index_html = sphinx_build.html_tree("index.html") |
| 211 |
navbar_brand = index_html.select(".navbar-brand")[0] |
| 212 |
assert navbar_brand.find("img", class_="only-light") is not None |
| 213 |
assert navbar_brand.find("img", class_="only-dark") is not None |
| 214 |
|
| 215 |
|
| 216 |
def test_primary_logo_is_light_when_default_mode_is_light(sphinx_build_factory) -> None: |
| 217 |
"""Test that the primary logo image is light when default mode is set to light.""" |
| 218 |
# Ensure no default mode is set |
| 219 |
confoverrides = { |
| 220 |
"html_context": {"default_mode": "light"}, |
| 221 |
} |
| 222 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build() |
| 223 |
index_html = sphinx_build.html_tree("index.html") |
| 224 |
navbar_brand = index_html.select(".navbar-brand")[0] |
| 225 |
assert navbar_brand.find("img", class_="only-light") is not None |
| 226 |
assert navbar_brand.find("img", class_="only-dark") is not None |
| 227 |
|
| 228 |
|
| 229 |
def test_primary_logo_is_dark_when_default_mode_is_dark(sphinx_build_factory) -> None: |
| 230 |
"""Test that the primary logo image is dark when default mode is set to dark.""" |
| 231 |
# Ensure no default mode is set |
| 232 |
confoverrides = { |
| 233 |
"html_context": {"default_mode": "dark"}, |
| 234 |
} |
| 235 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build() |
| 236 |
index_html = sphinx_build.html_tree("index.html") |
| 237 |
navbar_brand = index_html.select(".navbar-brand")[0] |
| 238 |
assert navbar_brand.find("img", class_="only-dark") is not None |
| 239 |
assert navbar_brand.find("img", class_="only-light") is not None |
| 240 |
|
| 241 |
|
| 242 |
def test_logo_missing_image(sphinx_build_factory) -> None: |
| 243 |
"""Test that a missing image will raise a warning.""" |
| 244 |
# Test with a specified title and a dark logo |
| 245 |
confoverrides = { |
| 246 |
"html_theme_options": { |
| 247 |
**COMMON_CONF_OVERRIDES, |
| 248 |
"logo": { |
| 249 |
# The logo is actually in _static |
| 250 |
"image_dark": "emptydarklogo.png", |
| 251 |
}, |
| 252 |
}, |
| 253 |
} |
| 254 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build( |
| 255 |
no_warning=False |
| 256 |
) |
| 257 |
assert "image logo does not exist" in escape_ansi(sphinx_build.warnings).strip() |
| 258 |
|
| 259 |
|
| 260 |
def test_logo_external_link(sphinx_build_factory) -> None: |
| 261 |
"""Test that the logo link is correct for external URLs.""" |
| 262 |
# Test with a specified external logo link |
| 263 |
test_url = "https://secure.example.com" |
| 264 |
confoverrides = { |
| 265 |
"html_theme_options": { |
| 266 |
"logo": { |
| 267 |
"link": test_url, |
| 268 |
}, |
| 269 |
}, |
| 270 |
} |
| 271 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build() |
| 272 |
index_html = sphinx_build.html_tree("index.html") |
| 273 |
index_str = str(index_html.select(".navbar-brand")[0]) |
| 274 |
assert f'href="{test_url}"' in index_str |
| 275 |
|
| 276 |
|
| 277 |
def test_logo_external_image(sphinx_build_factory) -> None: |
| 278 |
"""Test that the logo link is correct for external URLs.""" |
| 279 |
# Test with a specified external logo image source |
| 280 |
test_url = "https://pydata.org/wp-content/uploads/2019/06/pydata-logo-final.png" |
| 281 |
confoverrides = { |
| 282 |
"html_theme_options": { |
| 283 |
"logo": { |
| 284 |
"image_dark": test_url, |
| 285 |
}, |
| 286 |
}, |
| 287 |
} |
| 288 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build() |
| 289 |
index_html = sphinx_build.html_tree("index.html") |
| 290 |
index_str = str(index_html.select(".navbar-brand")[0]) |
| 291 |
assert f'src="{test_url}"' in index_str |
| 292 |
|
| 293 |
|
| 294 |
def test_logo_template_rejected(sphinx_build_factory) -> None: |
| 295 |
"""Test that dynamic Sphinx templates are not accepted as logo files.""" |
| 296 |
# Test with a specified external logo image source |
| 297 |
confoverrides = { |
| 298 |
"html_theme_options": { |
| 299 |
"logo": { |
| 300 |
"image_dark": "image_dark_t", |
| 301 |
}, |
| 302 |
}, |
| 303 |
} |
| 304 |
with pytest.raises(sphinx.errors.ExtensionError, match="static logo image"): |
| 305 |
sphinx_build_factory("base", confoverrides=confoverrides).build() |
| 306 |
|
| 307 |
|
| 308 |
def test_sticky_header(sphinx_build_factory): |
| 309 |
"""Regression test, see #1630. Sticky header should be direct descendant of body.""" |
| 310 |
sphinx_build = sphinx_build_factory("test_navbar_no_in_page_headers").build() |
| 311 |
index_html = sphinx_build.html_tree("index.html") |
| 312 |
assert index_html.select_one("body > .bd-header") |
| 313 |
|
| 314 |
|
| 315 |
def test_local_announcement_banner(sphinx_build_factory) -> None: |
| 316 |
"""If announcement is not a URL, it should be rendered at build time.""" |
| 317 |
confoverrides = { |
| 318 |
"html_theme_options.announcement": "Hello, world!", |
| 319 |
} |
| 320 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build() |
| 321 |
index_html = sphinx_build.html_tree("index.html") |
| 322 |
results = index_html.find_all(class_="bd-header-announcement") |
| 323 |
|
| 324 |
# Template should only render one announcement banner |
| 325 |
assert len(results) == 1 |
| 326 |
banner = results[0] |
| 327 |
|
| 328 |
# Announcement banner should contain the value from the config |
| 329 |
assert banner.text.strip() == "Hello, world!" |
| 330 |
|
| 331 |
|
| 332 |
def test_remote_announcement_banner(sphinx_build_factory) -> None: |
| 333 |
"""If announcement is a URL, it should not be rendered at build time.""" |
| 334 |
confoverrides = { |
| 335 |
"html_theme_options.announcement": "http://example.com/announcement", |
| 336 |
} |
| 337 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build() |
| 338 |
index_html = sphinx_build.html_tree("index.html") |
| 339 |
results = index_html.find_all(class_="bd-header-announcement") |
| 340 |
|
| 341 |
# Template should only render one announcement banner |
| 342 |
assert len(results) == 1 |
| 343 |
banner = results[0] |
| 344 |
|
| 345 |
# Remote announcement banner URL should be stored as data attribute |
| 346 |
assert banner["data-pst-announcement-url"] == "http://example.com/announcement" |
| 347 |
|
| 348 |
# Remote announcement should be empty at build time (filled at run time) |
| 349 |
assert not banner.find_all() |
| 350 |
|
| 351 |
# Remote announcement banner should be inside the async banner revealer |
| 352 |
assert "pst-async-banner-revealer" in banner.parent["class"] |
| 353 |
|
| 354 |
|
| 355 |
@pytest.mark.parametrize( |
| 356 |
"align,klass", |
| 357 |
[ |
| 358 |
("content", ("col-lg-3", "col-lg-9", "me-auto")), |
| 359 |
("left", ("", "", "me-auto")), |
| 360 |
("right", ("", "", "ms-auto")), |
| 361 |
], |
| 362 |
) |
| 363 |
def test_navbar_align(align, klass, sphinx_build_factory) -> None: |
| 364 |
"""The navbar items align with the proper part of the page.""" |
| 365 |
confoverrides = { |
| 366 |
"html_theme_options.navbar_align": align, |
| 367 |
} |
| 368 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build() |
| 369 |
index_html = sphinx_build.html_tree("index.html") |
| 370 |
if klass[0]: |
| 371 |
header_start = index_html.select(".navbar-header-items__start")[0] |
| 372 |
assert klass[0] in header_start.attrs["class"] |
| 373 |
if klass[1]: |
| 374 |
header_items = index_html.select(".navbar-header-items")[0] |
| 375 |
assert klass[1] in header_items.attrs["class"] |
| 376 |
header_center = index_html.select(".navbar-header-items__center")[0] |
| 377 |
assert klass[2] in header_center.attrs["class"] |
| 378 |
|
| 379 |
|
| 380 |
def test_navbar_no_in_page_headers(sphinx_build_factory, file_regression) -> None: |
| 381 |
"""Test navbar elements did not change (regression test).""" |
| 382 |
# https://github.com/pydata/pydata-sphinx-theme/issues/302 |
| 383 |
sphinx_build = sphinx_build_factory("test_navbar_no_in_page_headers").build() |
| 384 |
|
| 385 |
index_html = sphinx_build.html_tree("index.html") |
| 386 |
navbar = index_html.select("ul.bd-navbar-elements")[0] |
| 387 |
file_regression.check(navbar.prettify(), extension=".html") |
| 388 |
|
| 389 |
|
| 390 |
@pytest.mark.parametrize("n_links", (0, 4, 8)) # 0 = only dropdown, 8 = no dropdown |
| 391 |
def test_navbar_header_dropdown(sphinx_build_factory, n_links) -> None: |
| 392 |
"""Test whether dropdown appears based on number of header links + config.""" |
| 393 |
extra_links = [{"url": f"https://{ii}.org", "name": ii} for ii in range(3)] |
| 394 |
|
| 395 |
confoverrides = { |
| 396 |
"html_theme_options": { |
| 397 |
"external_links": extra_links, |
| 398 |
"header_links_before_dropdown": n_links, |
| 399 |
} |
| 400 |
} |
| 401 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build() |
| 402 |
index_html = sphinx_build.html_tree("index.html") |
| 403 |
|
| 404 |
# classic navbar: |
| 405 |
navbar = index_html.select("ul.bd-navbar-elements")[0] |
| 406 |
dropdowns = navbar.select("li.dropdown") |
| 407 |
standalone_links = navbar.select(".navbar-nav > li.nav-item:not(.dropdown)") |
| 408 |
if n_links == 0: |
| 409 |
# There should be *only* a dropdown and no standalone links |
| 410 |
assert len(dropdowns) == 1 and not standalone_links |
| 411 |
if n_links == 4: |
| 412 |
# There should be `n_links` standalone links, and a dropdown |
| 413 |
assert len(standalone_links) == n_links and len(dropdowns) == 1 |
| 414 |
if n_links == 8: |
| 415 |
# There should be no dropdown and only standalone links |
| 416 |
assert standalone_links and not dropdowns |
| 417 |
|
| 418 |
# sidebar nav should never have dropdown |
| 419 |
navbar = index_html.select("ul.bd-navbar-elements")[1] |
| 420 |
dropdowns = navbar.select("li.dropdown") |
| 421 |
standalone_links = navbar.select(".navbar-nav > li.nav-item:not(.dropdown)") |
| 422 |
assert len(standalone_links) == 7 |
| 423 |
assert len(dropdowns) == 0 |
| 424 |
|
| 425 |
|
| 426 |
@pytest.mark.parametrize("dropdown_text", (None, "Other")) # None -> default "More" |
| 427 |
def test_navbar_header_dropdown_button(sphinx_build_factory, dropdown_text) -> None: |
| 428 |
"""Test whether dropdown button text is configurable.""" |
| 429 |
if dropdown_text: |
| 430 |
confoverrides = { |
| 431 |
"html_theme_options": { |
| 432 |
"header_links_before_dropdown": 2, |
| 433 |
"header_dropdown_text": dropdown_text, |
| 434 |
} |
| 435 |
} |
| 436 |
else: |
| 437 |
confoverrides = { |
| 438 |
"html_theme_options": { |
| 439 |
"header_links_before_dropdown": 2, |
| 440 |
} |
| 441 |
} |
| 442 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build() |
| 443 |
index_html = sphinx_build.html_tree("index.html") |
| 444 |
navbar = index_html.select("ul.bd-navbar-elements")[0] |
| 445 |
button_text = str(navbar.select("button")[0]) |
| 446 |
if dropdown_text: |
| 447 |
# The dropdown text should be "Other" |
| 448 |
assert dropdown_text in button_text |
| 449 |
else: |
| 450 |
# The dropdown text should be "More" |
| 451 |
assert "More" in button_text |
| 452 |
|
| 453 |
|
| 454 |
def test_sidebars_captions(sphinx_build_factory, file_regression) -> None: |
| 455 |
"""Test that the captions are rendered.""" |
| 456 |
sphinx_build = sphinx_build_factory("sidebars").build() |
| 457 |
|
| 458 |
subindex_html = sphinx_build.html_tree("section1/index.html") |
| 459 |
|
| 460 |
# Sidebar structure with caption |
| 461 |
sidebar = subindex_html.select("nav.bd-docs-nav")[0] |
| 462 |
file_regression.check(sidebar.prettify(), extension=".html") |
| 463 |
|
| 464 |
|
| 465 |
def test_sidebars_nested_page(sphinx_build_factory, file_regression) -> None: |
| 466 |
"""Test that nested pages are shown in the sidebar.""" |
| 467 |
sphinx_build = sphinx_build_factory("sidebars").build() |
| 468 |
|
| 469 |
subindex_html = sphinx_build.html_tree("section1/subsection1/page1.html") |
| 470 |
|
| 471 |
# For nested (uncollapsed) page, the disclosure widget should be open |
| 472 |
sidebar = subindex_html.select("nav.bd-docs-nav")[0] |
| 473 |
file_regression.check(sidebar.prettify(), extension=".html") |
| 474 |
|
| 475 |
|
| 476 |
def test_sidebars_level2(sphinx_build_factory, file_regression) -> None: |
| 477 |
"""Test sidebars in a second-level page w/ children.""" |
| 478 |
confoverrides = { |
| 479 |
"templates_path": ["_templates_sidebar_level2"], |
| 480 |
} |
| 481 |
sphinx_build = sphinx_build_factory("sidebars", confoverrides=confoverrides).build() |
| 482 |
|
| 483 |
subindex_html = sphinx_build.html_tree("section1/subsection1/index.html") |
| 484 |
|
| 485 |
# Sidebar structure |
| 486 |
sidebar = subindex_html.select("nav.bd-docs-nav")[0] |
| 487 |
file_regression.check(sidebar.prettify(), extension=".html") |
| 488 |
|
| 489 |
|
| 490 |
def test_sidebars_show_nav_level0(sphinx_build_factory) -> None: |
| 491 |
"""Regression test for show_nav_level:0 when the toc is divided into parts. |
| 492 |
|
| 493 |
Testing both home page and a subsection page for correct elements. |
| 494 |
""" |
| 495 |
confoverrides = { |
| 496 |
"html_theme_options.show_nav_level": 0, |
| 497 |
} |
| 498 |
sphinx_build = sphinx_build_factory("sidebars", confoverrides=confoverrides).build() |
| 499 |
|
| 500 |
# 1. Home Page |
| 501 |
index_html = sphinx_build.html_tree("section1/index.html") |
| 502 |
sidebar = index_html.select("nav.bd-docs-nav")[0] |
| 503 |
|
| 504 |
# check if top-level ul is present |
| 505 |
ul = sidebar.find("ul") |
| 506 |
assert "list-caption" in ul.attrs["class"] |
| 507 |
|
| 508 |
# get all li elements |
| 509 |
li = ul.select("li") |
| 510 |
|
| 511 |
# part li |
| 512 |
assert "toctree-l0 has-children" in " ".join(li[0].attrs["class"]) |
| 513 |
assert "caption-text" in li[0].select("p span")[0].attrs["class"] |
| 514 |
|
| 515 |
# basic checks on other levels |
| 516 |
assert "toctree-l1 has-children" in " ".join(li[1].attrs["class"]) |
| 517 |
assert "toctree-l2" in li[2].attrs["class"] |
| 518 |
|
| 519 |
# 2. Subsection Page |
| 520 |
subsection_html = sphinx_build.html_tree("section1/subsection1/index.html") |
| 521 |
sidebar = subsection_html.select("nav.bd-docs-nav")[0] |
| 522 |
|
| 523 |
# get all <details> elements |
| 524 |
details_elem = sidebar.select("details") |
| 525 |
assert len(details_elem) > 0, "There must be at least one details element" |
| 526 |
|
| 527 |
# all <details> elements should be open in this page |
| 528 |
for ii in details_elem: |
| 529 |
assert "open" in ii.attrs |
| 530 |
|
| 531 |
|
| 532 |
def test_included_toc(sphinx_build_factory) -> None: |
| 533 |
""" |
| 534 |
Test that Sphinx project containing TOC (.. toctree::) included via .. include:: |
| 535 |
can be successfully built. |
| 536 |
""" |
| 537 |
# Regression test for bug resolved in #347. |
| 538 |
# Tests mainly makes sure that the sphinx_build.build() does not raise exception. |
| 539 |
# https://github.com/pydata/pydata-sphinx-theme/pull/347 |
| 540 |
|
| 541 |
sphinx_build = sphinx_build_factory("test_included_toc").build() |
| 542 |
included_page_html = sphinx_build.html_tree("included-page.html") |
| 543 |
assert included_page_html is not None |
| 544 |
|
| 545 |
|
| 546 |
def test_footer(sphinx_build_factory) -> None: |
| 547 |
"""Test for expected footer contents.""" |
| 548 |
overrides = { |
| 549 |
"html_theme_options.footer_start": ["copyright.html"], |
| 550 |
"html_theme_options.footer_center": ["sphinx-version.html"], |
| 551 |
} |
| 552 |
sphinx_build = sphinx_build_factory("base", confoverrides=overrides).build() |
| 553 |
index_html = sphinx_build.html_tree("index.html") |
| 554 |
footer_sta = index_html.select("div.footer-items__start")[0] |
| 555 |
footer_ctr = index_html.select("div.footer-items__center")[0] |
| 556 |
footer_end = index_html.select("div.footer-items__end")[0] |
| 557 |
assert "Pydata community" in footer_sta.text |
| 558 |
assert "Created using" in footer_ctr.text |
| 559 |
assert "Built with the" in footer_end.text |
| 560 |
|
| 561 |
|
| 562 |
# html contexts for `show_edit_button` |
| 563 |
|
| 564 |
# these are "good" context fragments that should yield a working link |
| 565 |
good_edits = [ |
| 566 |
[ |
| 567 |
{ |
| 568 |
"github_user": "foo", |
| 569 |
"github_repo": "bar", |
| 570 |
"github_version": "HEAD", |
| 571 |
"doc_path": "docs", |
| 572 |
}, |
| 573 |
("Edit on GitHub", "https://github.com/foo/bar/edit/HEAD/docs/index.rst"), |
| 574 |
], |
| 575 |
[ |
| 576 |
{ |
| 577 |
"gitlab_user": "foo", |
| 578 |
"gitlab_repo": "bar", |
| 579 |
"gitlab_version": "HEAD", |
| 580 |
"doc_path": "docs", |
| 581 |
}, |
| 582 |
("Edit on GitLab", "https://gitlab.com/foo/bar/-/edit/HEAD/docs/index.rst"), |
| 583 |
], |
| 584 |
[ |
| 585 |
{ |
| 586 |
"bitbucket_user": "foo", |
| 587 |
"bitbucket_repo": "bar", |
| 588 |
"bitbucket_version": "HEAD", |
| 589 |
"doc_path": "docs", |
| 590 |
}, |
| 591 |
( |
| 592 |
"Edit on Bitbucket", |
| 593 |
"https://bitbucket.org/foo/bar/src/HEAD/docs/index.rst?mode=edit", |
| 594 |
), |
| 595 |
], |
| 596 |
] |
| 597 |
|
| 598 |
|
| 599 |
# copy the "good" ones, ensure `doc_path` is agnostic to trailing slashes |
| 600 |
slash_edits = [ |
| 601 |
[ |
| 602 |
{ |
| 603 |
# add slashes to doc_path: |
| 604 |
key: f"{value}/" if key == "doc_path" else value |
| 605 |
for key, value in html_context.items() |
| 606 |
}, |
| 607 |
# the text and URL do not change |
| 608 |
text_and_url, |
| 609 |
] |
| 610 |
for html_context, text_and_url in good_edits |
| 611 |
] |
| 612 |
|
| 613 |
# copy the "good" ones, provide a `<whatever>_url` based off the default |
| 614 |
providers = [ |
| 615 |
[ |
| 616 |
dict( |
| 617 |
# copy all the values |
| 618 |
**html_context, |
| 619 |
# add a provider url |
| 620 |
**{f"{provider}_url": f"https://{provider}.example.com"}, |
| 621 |
), |
| 622 |
( |
| 623 |
text, |
| 624 |
f"""https://{provider}.example.com/foo/{url.split("/foo/")[1]}""", |
| 625 |
), |
| 626 |
] |
| 627 |
for html_context, (text, url) in good_edits |
| 628 |
for provider in ["github", "gitlab", "bitbucket"] |
| 629 |
if provider in text.lower() |
| 630 |
] |
| 631 |
|
| 632 |
# missing any of the values should fail |
| 633 |
bad_edits = [ |
| 634 |
[ |
| 635 |
{ |
| 636 |
# copy all the values |
| 637 |
key: value |
| 638 |
for key, value in html_context.items() |
| 639 |
# but not `<provider>_version` |
| 640 |
if "_version" not in key |
| 641 |
}, |
| 642 |
None, |
| 643 |
] |
| 644 |
for html_context, _ in good_edits |
| 645 |
] |
| 646 |
|
| 647 |
# a good custom URL template |
| 648 |
good_custom = [ |
| 649 |
[ |
| 650 |
{ |
| 651 |
"edit_page_url_template": ( |
| 652 |
"https://dvcs.example.com/foo/bar/edit/HEAD/{{ file_name }}" |
| 653 |
) |
| 654 |
}, |
| 655 |
("Edit", "https://dvcs.example.com/foo/bar/edit/HEAD/index.rst"), |
| 656 |
] |
| 657 |
] |
| 658 |
|
| 659 |
# a good custom URL template with an additional provider name |
| 660 |
good_custom_with_provider = [ |
| 661 |
[ |
| 662 |
{ |
| 663 |
"edit_page_url_template": ( |
| 664 |
"https://dvcs.example.com/foo/bar/edit/HEAD/{{ file_name }}" |
| 665 |
), |
| 666 |
"edit_page_provider_name": "FooProvider", |
| 667 |
}, |
| 668 |
("Edit on FooProvider", "https://dvcs.example.com/foo/bar/edit/HEAD/index.rst"), |
| 669 |
] |
| 670 |
] |
| 671 |
|
| 672 |
# a bad custom URL template |
| 673 |
bad_custom = [ |
| 674 |
[ |
| 675 |
# it's missing a reference to {{ file_name }} |
| 676 |
{"edit_page_url_template": "http://has-no-file-name"}, |
| 677 |
None, |
| 678 |
] |
| 679 |
] |
| 680 |
|
| 681 |
all_edits = [ |
| 682 |
*good_edits, |
| 683 |
*slash_edits, |
| 684 |
*bad_edits, |
| 685 |
*good_custom, |
| 686 |
*bad_custom, |
| 687 |
*providers, |
| 688 |
] |
| 689 |
|
| 690 |
|
| 691 |
@pytest.mark.parametrize("html_context,edit_text_and_url", all_edits) |
| 692 |
def test_edit_page_url(sphinx_build_factory, html_context, edit_text_and_url) -> None: |
| 693 |
"""Test the edit this page generated link.""" |
| 694 |
confoverrides = { |
| 695 |
"html_theme_options.use_edit_page_button": True, |
| 696 |
"html_context": html_context, |
| 697 |
} |
| 698 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides) |
| 699 |
|
| 700 |
if edit_text_and_url is None: |
| 701 |
with pytest.raises( |
| 702 |
sphinx.errors.ExtensionError, match="Missing required value" |
| 703 |
): |
| 704 |
sphinx_build.build() |
| 705 |
return |
| 706 |
|
| 707 |
edit_text, edit_url = edit_text_and_url |
| 708 |
sphinx_build.build() |
| 709 |
index_html = sphinx_build.html_tree("index.html") |
| 710 |
edit_link = index_html.select(".editthispage a") |
| 711 |
assert edit_link, "no edit link found" |
| 712 |
assert edit_link[0].attrs["href"] == edit_url, f"edit link didn't match {edit_link}" |
| 713 |
# First child is the icon |
| 714 |
assert list(edit_link[0].strings)[1].strip() == edit_text, ( |
| 715 |
f"edit text didn't match {edit_text}" |
| 716 |
) |
| 717 |
|
| 718 |
|
| 719 |
@pytest.mark.parametrize( |
| 720 |
"provider,tags", |
| 721 |
[ |
| 722 |
# google analytics |
| 723 |
( |
| 724 |
{"html_theme_options.analytics": {"google_analytics_id": "G-XXXXX"}}, |
| 725 |
["gtag", "G-XXXXX"], |
| 726 |
), |
| 727 |
# google and plausible |
| 728 |
( |
| 729 |
{ |
| 730 |
"html_theme_options.analytics": { |
| 731 |
"google_analytics_id": "G-XXXXX", |
| 732 |
"plausible_analytics_domain": "toto", |
| 733 |
"plausible_analytics_url": "http://.../script.js", |
| 734 |
} |
| 735 |
}, |
| 736 |
["gtag", "G-XXXXX"], |
| 737 |
), |
| 738 |
], |
| 739 |
) |
| 740 |
def test_analytics(sphinx_build_factory, provider, tags) -> None: |
| 741 |
"""Check the Google analytics.""" |
| 742 |
confoverrides = provider |
| 743 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides) |
| 744 |
sphinx_build.build() |
| 745 |
index_html = sphinx_build.html_tree("index.html") |
| 746 |
|
| 747 |
# Search all the scripts and make sure one of them has the Google tag in there |
| 748 |
tags_found = False |
| 749 |
for script in index_html.select("script"): |
| 750 |
if script.string and tags[0] in script.string and tags[1] in script.string: |
| 751 |
# If the tag is found, make sure the consent mode is also there |
| 752 |
if tags[0] == "gtag": |
| 753 |
assert "gtag('consent', 'default', {" in script.string |
| 754 |
assert "'ad_storage': 'denied'," in script.string |
| 755 |
assert "'ad_user_data': 'denied'," in script.string |
| 756 |
assert "'ad_personalization': 'denied'," in script.string |
| 757 |
assert "'analytics_storage': 'denied'" in script.string |
| 758 |
tags_found = True |
| 759 |
assert tags_found is True |
| 760 |
|
| 761 |
|
| 762 |
def test_plausible(sphinx_build_factory) -> None: |
| 763 |
"""Test the Plausible analytics.""" |
| 764 |
provider = { |
| 765 |
"html_theme_options.analytics": { |
| 766 |
"plausible_analytics_domain": "toto", |
| 767 |
"plausible_analytics_url": "http://.../script.js", |
| 768 |
}, |
| 769 |
} |
| 770 |
confoverrides = provider |
| 771 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides) |
| 772 |
sphinx_build.build() |
| 773 |
index_html = sphinx_build.html_tree("index.html") |
| 774 |
|
| 775 |
# Search all the scripts and make sure one of them has the Google tag in there |
| 776 |
attr_found = False |
| 777 |
for script in index_html.select("script"): |
| 778 |
if script.attrs.get("data-domain") == "toto": |
| 779 |
attr_found = True |
| 780 |
assert attr_found is True |
| 781 |
|
| 782 |
|
| 783 |
def test_show_nav_level(sphinx_build_factory) -> None: |
| 784 |
"""The navbar items align with the proper part of the page.""" |
| 785 |
confoverrides = { |
| 786 |
"html_theme_options.show_nav_level": 2, |
| 787 |
} |
| 788 |
sphinx_build = sphinx_build_factory("sidebars", confoverrides=confoverrides).build() |
| 789 |
|
| 790 |
# Both the column alignment and the margin should be changed |
| 791 |
index_html = sphinx_build.html_tree("section1/index.html") |
| 792 |
|
| 793 |
for details_elem in index_html.select("li.toctree-l1.has-children > details"): |
| 794 |
assert "open" in details_elem.attrs |
| 795 |
|
| 796 |
|
| 797 |
# the switcher files tested in test_version_switcher_error_states, not all of them exist |
| 798 |
switcher_files = ["switcher.json", "http://a.b/switcher.json", "missing_url.json"] |
| 799 |
|
| 800 |
|
| 801 |
@pytest.mark.parametrize("url", switcher_files) |
| 802 |
def test_version_switcher_error_states( |
| 803 |
sphinx_build_factory, file_regression, url |
| 804 |
) -> None: |
| 805 |
"""Regression test the version switcher dropdown HTML. |
| 806 |
|
| 807 |
Note that a lot of the switcher HTML gets populated by JavaScript, |
| 808 |
so we will not test the final behavior. This just tests for the basic |
| 809 |
structure. |
| 810 |
|
| 811 |
TODO: Find a way to test Javascript's behavior in populating the HTML. |
| 812 |
""" |
| 813 |
confoverrides = { |
| 814 |
"html_theme_options": { |
| 815 |
**COMMON_CONF_OVERRIDES, |
| 816 |
"navbar_end": ["version-switcher"], |
| 817 |
"switcher": { |
| 818 |
"json_url": url, |
| 819 |
"version_match": "0.7.1", |
| 820 |
}, |
| 821 |
} |
| 822 |
} |
| 823 |
factory = sphinx_build_factory("base", confoverrides=confoverrides) |
| 824 |
sphinx_build = factory.build(no_warning=False) |
| 825 |
|
| 826 |
if url == "switcher.json": # this should work |
| 827 |
index = sphinx_build.html_tree("index.html") |
| 828 |
switcher = index.select(".navbar-header-items")[0].find( |
| 829 |
"div", class_="version-switcher__container" |
| 830 |
) |
| 831 |
assert switcher is not None |
| 832 |
file_regression.check( |
| 833 |
switcher.prettify(), basename="navbar_switcher", extension=".html" |
| 834 |
) |
| 835 |
|
| 836 |
elif url == "http://a.b/switcher.json": # this file doesn't exist" |
| 837 |
not_read = ( |
| 838 |
'WARNING: The version switcher "http://a.b/switcher.json"' |
| 839 |
" file cannot be read due to the following error:\n" |
| 840 |
) |
| 841 |
assert not_read in escape_ansi(sphinx_build.warnings).strip() |
| 842 |
|
| 843 |
elif url == "missing_url.json": # this file is missing the url key for one version |
| 844 |
missing_url = ( |
| 845 |
'WARNING: The version switcher "missing_url.json" file is malformed; ' |
| 846 |
'at least one of the items is missing the "url" or "version" key' |
| 847 |
) |
| 848 |
assert escape_ansi(sphinx_build.warnings).strip() == missing_url |
| 849 |
|
| 850 |
|
| 851 |
def test_theme_switcher(sphinx_build_factory, file_regression) -> None: |
| 852 |
"""Regression test for the theme switcher button.""" |
| 853 |
sphinx_build = sphinx_build_factory("base").build() |
| 854 |
switcher = sphinx_build.html_tree("index.html").find(class_="theme-switch-button") |
| 855 |
file_regression.check( |
| 856 |
switcher.prettify(), basename="navbar_theme", extension=".html" |
| 857 |
) |
| 858 |
|
| 859 |
|
| 860 |
def test_shorten_link(sphinx_build_factory, file_regression) -> None: |
| 861 |
"""Regression test for "edit on <provider>" link shortening.""" |
| 862 |
confoverrides = { |
| 863 |
"html_theme_options": {"shorten_urls": True}, |
| 864 |
} |
| 865 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build() |
| 866 |
|
| 867 |
github = sphinx_build.html_tree("page1.html").select(".github-container")[0] |
| 868 |
file_regression.check(github.prettify(), basename="github_links", extension=".html") |
| 869 |
|
| 870 |
gitlab = sphinx_build.html_tree("page1.html").select(".gitlab-container")[0] |
| 871 |
file_regression.check(gitlab.prettify(), basename="gitlab_links", extension=".html") |
| 872 |
|
| 873 |
|
| 874 |
def test_dont_shorten_link(sphinx_build_factory, file_regression) -> None: |
| 875 |
"""Regression test for setting shorten_urls to false .""" |
| 876 |
confoverrides = { |
| 877 |
"html_theme_options": {"shorten_urls": False}, |
| 878 |
} |
| 879 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build() |
| 880 |
|
| 881 |
github = sphinx_build.html_tree("page1.html").select(".github-container")[0] |
| 882 |
file_regression.check( |
| 883 |
github.prettify(), basename="github_links_not_shortened", extension=".html" |
| 884 |
) |
| 885 |
|
| 886 |
gitlab = sphinx_build.html_tree("page1.html").select(".gitlab-container")[0] |
| 887 |
file_regression.check( |
| 888 |
gitlab.prettify(), basename="gitlab_links_not_shortened", extension=".html" |
| 889 |
) |
| 890 |
|
| 891 |
|
| 892 |
def test_math_header_item(sphinx_build_factory, file_regression) -> None: |
| 893 |
"""Regression test for math items in a header title.""" |
| 894 |
sphinx_build = sphinx_build_factory("base").build() |
| 895 |
li = sphinx_build.html_tree("page2.html").select(".bd-navbar-elements li")[1] |
| 896 |
file_regression.check(li.prettify(), basename="math_header_item", extension=".html") |
| 897 |
|
| 898 |
|
| 899 |
@pytest.mark.parametrize( |
| 900 |
("style_names", "keyword_colors"), |
| 901 |
( |
| 902 |
pytest.param(("fake_foo", "fake_bar"), ("#204a87", "#66d9ef"), id="fake"), |
| 903 |
pytest.param( |
| 904 |
("a11y-high-contrast-light", "a11y-high-contrast-dark"), |
| 905 |
("#6730c5", "#dcc6e0"), |
| 906 |
id="real", |
| 907 |
), |
| 908 |
), |
| 909 |
) |
| 910 |
def test_pygments_fallbacks(sphinx_build_factory, style_names, keyword_colors) -> None: |
| 911 |
"""Test that setting color themes works. |
| 912 |
|
| 913 |
NOTE: the expected keyword colors for fake_foo and fake_bar are the colors |
| 914 |
from the fallback styles (tango and monokai, respectively). |
| 915 |
""" |
| 916 |
confoverrides = { |
| 917 |
"html_theme_options": { |
| 918 |
**COMMON_CONF_OVERRIDES, |
| 919 |
"pygments_light_style": style_names[0], |
| 920 |
"pygments_dark_style": style_names[1], |
| 921 |
}, |
| 922 |
} |
| 923 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build( |
| 924 |
no_warning=False |
| 925 |
) |
| 926 |
warnings = sphinx_build.warnings.strip().split("\n") |
| 927 |
# see if our warnings worked |
| 928 |
if style_names[0].startswith("fake"): |
| 929 |
assert len(warnings) == 2 |
| 930 |
assert re.search(r"Highlighting style fake_foo.*tango", warnings[0]) |
| 931 |
assert re.search(r"Highlighting style fake_bar.*monokai", warnings[1]) |
| 932 |
else: |
| 933 |
assert warnings == [""] |
| 934 |
# test that the rendered HTML has highlighting spans |
| 935 |
page_two = sphinx_build.html_tree("page2.html") |
| 936 |
keyword = ( |
| 937 |
page_two.select(".highlight-python")[0].select(".highlight")[0].select(".k")[0] |
| 938 |
) |
| 939 |
assert str(keyword) == '<span class="k">as</span>' |
| 940 |
# test that the pygments CSS file specifies the expected colors |
| 941 |
css = Path(sphinx_build.outdir) / "_static" / "pygments.css" |
| 942 |
with open(css) as css_file: |
| 943 |
lines = css_file.readlines() |
| 944 |
assert lines[0].startswith('html[data-theme="light"]') |
| 945 |
for mode, color in dict(zip(["light", "dark"], keyword_colors)).items(): |
| 946 |
regexp = re.compile( |
| 947 |
r'html\[data-theme="' + mode + r'"\].*\.k .*color:\s?' + color, |
| 948 |
re.IGNORECASE, |
| 949 |
) |
| 950 |
matches = [regexp.search(line) is not None for line in lines] |
| 951 |
assert sum(matches) == 1, f"expected {mode}: {color}\n" + "\n".join(lines) |
| 952 |
|
| 953 |
|
| 954 |
def test_deprecated_build_html(sphinx_build_factory, file_regression) -> None: |
| 955 |
"""Test building the base html template with all the deprecated configs.""" |
| 956 |
sphinx_build = sphinx_build_factory("deprecated") |
| 957 |
|
| 958 |
# Basic build with defaults |
| 959 |
sphinx_build.build(no_warning=False) |
| 960 |
assert (sphinx_build.outdir / "index.html").exists(), sphinx_build.outdir.glob("*") |
| 961 |
|
| 962 |
# check the deprecation warnings |
| 963 |
warnings = sphinx_build.warnings.strip("\n").split("\n") |
| 964 |
warnings = [w.lstrip("\x1b[91m").rstrip("\x1b[39;49;00m\n") for w in warnings] |
| 965 |
expected_warnings = ( |
| 966 |
'The parameter "pygment_dark_style" was renamed to', |
| 967 |
'The parameter "pygment_light_style" was renamed to', |
| 968 |
) |
| 969 |
assert len(warnings) == len(expected_warnings) |
| 970 |
for exp_warn in expected_warnings: |
| 971 |
assert exp_warn in sphinx_build.warnings |
| 972 |
|
| 973 |
index_html = sphinx_build.html_tree("index.html") |
| 974 |
subpage_html = sphinx_build.html_tree("section1/index.html") |
| 975 |
|
| 976 |
# Navbar structure |
| 977 |
navbar = index_html.select("div.navbar-header-items__center")[0] |
| 978 |
file_regression.check(navbar.prettify(), basename="navbar_ix", extension=".html") |
| 979 |
|
| 980 |
# Sidebar subpage |
| 981 |
# This re-uses the same HTML template used above (w/o deprecated config) |
| 982 |
# because they should still be the same. |
| 983 |
sidebar = subpage_html.select(".bd-sidebar")[0] |
| 984 |
file_regression.check( |
| 985 |
sidebar.prettify(), basename="sidebar_subpage", extension=".html" |
| 986 |
) |
| 987 |
|
| 988 |
# Secondary sidebar should not have in-page TOC if it is empty |
| 989 |
assert not sphinx_build.html_tree("page1.html").select("div.onthispage") |
| 990 |
|
| 991 |
# Secondary sidebar should not be present if page-level metadata given |
| 992 |
assert not sphinx_build.html_tree("page2.html").select("div.bd-sidebar-secondary") |
| 993 |
|
| 994 |
|
| 995 |
def test_empty_templates(sphinx_build_factory) -> None: |
| 996 |
"""If a template is empty (e.g., via a config), it should be removed.""" |
| 997 |
# When configured to be gone, the template should be removed w/ its parent. |
| 998 |
# ABlog needs to be added so we can test that template rendering works w/ it. |
| 999 |
confoverrides = { |
| 1000 |
"html_show_sourcelink": False, |
| 1001 |
} |
| 1002 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build() |
| 1003 |
html = sphinx_build.html_tree("page1.html") |
| 1004 |
|
| 1005 |
# We've set this to false in the config so the template shouldn't show up at all |
| 1006 |
assert not html.select(".tocsection.sourcelink") |
| 1007 |
|
| 1008 |
# Should not be any icon link wrapper because none are given in conf |
| 1009 |
assert not html.select(".navbar-icon-links") |
| 1010 |
|
| 1011 |
|
| 1012 |
def test_translations(sphinx_build_factory) -> None: |
| 1013 |
"""Test that basic translation functionality works. |
| 1014 |
|
| 1015 |
This will build our test site with the French language, and test |
| 1016 |
that a few phrases are in French. |
| 1017 |
|
| 1018 |
We use this test to catch regressions if we change wording without |
| 1019 |
changing the translation files. |
| 1020 |
""" |
| 1021 |
confoverrides = { |
| 1022 |
"language": "fr", |
| 1023 |
"html_context": { |
| 1024 |
"github_user": "pydata", |
| 1025 |
"github_repo": "pydata-sphinx-theme", |
| 1026 |
"github_version": "main", |
| 1027 |
}, |
| 1028 |
"html_theme_options": { |
| 1029 |
"use_edit_page_button": True, |
| 1030 |
}, |
| 1031 |
} |
| 1032 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build() |
| 1033 |
|
| 1034 |
# Use a section page so that we have section navigation in the sidebar |
| 1035 |
index = sphinx_build.html_tree("section1/index.html") |
| 1036 |
|
| 1037 |
sidebar_primary = index.select(".bd-sidebar-primary")[0] |
| 1038 |
assert "Navigation dans la section" in str(sidebar_primary) |
| 1039 |
|
| 1040 |
sidebar_secondary = index.select(".bd-sidebar-secondary")[0] |
| 1041 |
assert "Montrer le code source" in str(sidebar_secondary) |
| 1042 |
assert "Modifier sur GitHub" in str(sidebar_secondary) |
| 1043 |
|
| 1044 |
header = index.select(".bd-header")[0] |
| 1045 |
assert "Thème" in str(header) |
| 1046 |
|
| 1047 |
footer = index.select(".bd-footer")[0] |
| 1048 |
assert "Copyright" in str(footer) |
| 1049 |
assert "Créé en utilisant" in str(footer) |
| 1050 |
assert "Construit avec le" in str(footer) |
| 1051 |
|
| 1052 |
footer_article = index.select(".prev-next-area")[0] |
| 1053 |
assert "précédent" in str(footer_article) |
| 1054 |
assert "page suivante" in str(footer_article) |
| 1055 |
|
| 1056 |
# Search bar |
| 1057 |
# TODO: Add translations where there are english phrases below |
| 1058 |
assert "Search the docs" in str(index.select(".bd-search")[0]) |
| 1059 |
|
| 1060 |
|
| 1061 |
def test_render_secondary_sidebar_list(sphinx_build_factory) -> None: |
| 1062 |
"""Test that the secondary sidebar can be built with a list of templates.""" |
| 1063 |
confoverrides = { |
| 1064 |
"html_context": { |
| 1065 |
"github_user": "pydata", |
| 1066 |
"github_repo": "pydata-sphinx-theme", |
| 1067 |
"github_version": "main", |
| 1068 |
}, |
| 1069 |
"html_theme_options": { |
| 1070 |
"use_edit_page_button": True, |
| 1071 |
"secondary_sidebar_items": ["page-toc", "edit-this-page"], |
| 1072 |
}, |
| 1073 |
} |
| 1074 |
sphinx_build = sphinx_build_factory("sidebars", confoverrides=confoverrides) |
| 1075 |
# Basic build with defaults |
| 1076 |
sphinx_build.build() |
| 1077 |
|
| 1078 |
# Check that the page-toc template gets rendered |
| 1079 |
assert sphinx_build.html_tree("index.html").select("div.page-toc") |
| 1080 |
assert sphinx_build.html_tree("section1/index.html").select("div.page-toc") |
| 1081 |
assert sphinx_build.html_tree("section2/index.html").select("div.page-toc") |
| 1082 |
|
| 1083 |
# Check that the edit-this-page template gets rendered |
| 1084 |
assert sphinx_build.html_tree("index.html").select("div.editthispage") |
| 1085 |
assert sphinx_build.html_tree("section1/index.html").select("div.editthispage") |
| 1086 |
assert sphinx_build.html_tree("section2/index.html").select("div.editthispage") |
| 1087 |
|
| 1088 |
# Check that sourcelink is not rendered |
| 1089 |
assert not sphinx_build.html_tree("index.html").select("div.sourcelink") |
| 1090 |
assert not sphinx_build.html_tree("section1/index.html").select("div.sourcelink") |
| 1091 |
assert not sphinx_build.html_tree("section2/index.html").select("div.sourcelink") |
| 1092 |
|
| 1093 |
|
| 1094 |
def test_render_secondary_sidebar_dict(sphinx_build_factory) -> None: |
| 1095 |
"""Test that the secondary sidebar can be built with a dict of templates.""" |
| 1096 |
confoverrides = { |
| 1097 |
"html_context": { |
| 1098 |
"github_user": "pydata", |
| 1099 |
"github_repo": "pydata-sphinx-theme", |
| 1100 |
"github_version": "main", |
| 1101 |
}, |
| 1102 |
"html_theme_options": { |
| 1103 |
**COMMON_CONF_OVERRIDES, |
| 1104 |
"use_edit_page_button": True, |
| 1105 |
"secondary_sidebar_items": { |
| 1106 |
"**": ["page-toc", "edit-this-page"], |
| 1107 |
"section1/index": [], |
| 1108 |
"section2/index": ["sourcelink"], |
| 1109 |
}, |
| 1110 |
}, |
| 1111 |
} |
| 1112 |
sphinx_build = sphinx_build_factory("sidebars", confoverrides=confoverrides) |
| 1113 |
# Basic build with defaults |
| 1114 |
sphinx_build.build() |
| 1115 |
|
| 1116 |
# Check that the page-toc template gets rendered |
| 1117 |
# (but not for section1/index or section2/*) |
| 1118 |
assert sphinx_build.html_tree("index.html").select("div.page-toc") |
| 1119 |
assert not sphinx_build.html_tree("section1/index.html").select("div.page-toc") |
| 1120 |
assert not sphinx_build.html_tree("section2/index.html").select("div.page-toc") |
| 1121 |
|
| 1122 |
# Check that the edit-this-page template gets rendered |
| 1123 |
# (but not for section1/index or section2/*) |
| 1124 |
assert sphinx_build.html_tree("index.html").select("div.editthispage") |
| 1125 |
assert not sphinx_build.html_tree("section1/index.html").select("div.editthispage") |
| 1126 |
assert not sphinx_build.html_tree("section2/index.html").select("div.editthispage") |
| 1127 |
|
| 1128 |
# Check that sourcelink is only rendered for section2/* |
| 1129 |
assert not sphinx_build.html_tree("index.html").select("div.sourcelink") |
| 1130 |
assert not sphinx_build.html_tree("section1/index.html").select("div.sourcelink") |
| 1131 |
assert sphinx_build.html_tree("section2/index.html").select("div.sourcelink") |
| 1132 |
|
| 1133 |
|
| 1134 |
def test_render_secondary_sidebar_dict_glob_subdir(sphinx_build_factory) -> None: |
| 1135 |
""" |
| 1136 |
Test that the secondary sidebar can be built with a dict of templates that globs a |
| 1137 |
subdir. |
| 1138 |
""" |
| 1139 |
confoverrides = { |
| 1140 |
"html_context": { |
| 1141 |
"github_user": "pydata", |
| 1142 |
"github_repo": "pydata-sphinx-theme", |
| 1143 |
"github_version": "main", |
| 1144 |
}, |
| 1145 |
"html_theme_options": { |
| 1146 |
**COMMON_CONF_OVERRIDES, |
| 1147 |
"use_edit_page_button": True, |
| 1148 |
"secondary_sidebar_items": { |
| 1149 |
"section1/index": [], |
| 1150 |
"section2/*": ["sourcelink"], |
| 1151 |
}, |
| 1152 |
}, |
| 1153 |
} |
| 1154 |
sphinx_build = sphinx_build_factory("sidebars", confoverrides=confoverrides) |
| 1155 |
# Basic build with defaults |
| 1156 |
sphinx_build.build() |
| 1157 |
|
| 1158 |
# Check that the no page-toc template gets rendered |
| 1159 |
assert not sphinx_build.html_tree("section1/index.html").select("div.page-toc") |
| 1160 |
assert not sphinx_build.html_tree("section2/index.html").select("div.page-toc") |
| 1161 |
assert not sphinx_build.html_tree("section2/page1.html").select("div.page-toc") |
| 1162 |
|
| 1163 |
# Check that no edit-this-page template gets rendered |
| 1164 |
assert not sphinx_build.html_tree("section1/index.html").select("div.editthispage") |
| 1165 |
assert not sphinx_build.html_tree("section2/index.html").select("div.editthispage") |
| 1166 |
assert not sphinx_build.html_tree("section2/page1.html").select("div.editthispage") |
| 1167 |
|
| 1168 |
# Check that sourcelink is only rendered for section2/* |
| 1169 |
assert not sphinx_build.html_tree("section1/index.html").select("div.sourcelink") |
| 1170 |
assert sphinx_build.html_tree("section2/index.html").select("div.sourcelink") |
| 1171 |
assert sphinx_build.html_tree("section2/page1.html").select("div.sourcelink") |
| 1172 |
|
| 1173 |
|
| 1174 |
def test_render_secondary_sidebar_dict_multiple_glob_matches( |
| 1175 |
sphinx_build_factory, |
| 1176 |
) -> None: |
| 1177 |
""" |
| 1178 |
Test that the secondary sidebar builds with a template dict with two conflicting |
| 1179 |
globs. |
| 1180 |
|
| 1181 |
The last specified glob pattern should win, but a warning should be emitted with the |
| 1182 |
offending pattern and affected pagenames. |
| 1183 |
""" |
| 1184 |
confoverrides = { |
| 1185 |
"html_context": { |
| 1186 |
"github_user": "pydata", |
| 1187 |
"github_repo": "pydata-sphinx-theme", |
| 1188 |
"github_version": "main", |
| 1189 |
}, |
| 1190 |
"html_theme_options": { |
| 1191 |
**COMMON_CONF_OVERRIDES, |
| 1192 |
"use_edit_page_button": True, |
| 1193 |
"secondary_sidebar_items": { |
| 1194 |
"**": [ |
| 1195 |
"page-toc", |
| 1196 |
"edit-this-page", |
| 1197 |
], # <-- Some pages match both patterns |
| 1198 |
"section1/index": [], |
| 1199 |
"section2/*": ["sourcelink"], # <-- Some pages match both patterns |
| 1200 |
}, |
| 1201 |
}, |
| 1202 |
} |
| 1203 |
sphinx_build = sphinx_build_factory( |
| 1204 |
"sidebars", |
| 1205 |
confoverrides=confoverrides, |
| 1206 |
) |
| 1207 |
# Basic build with defaults |
| 1208 |
sphinx_build.build(no_warning=False) |
| 1209 |
|
| 1210 |
# Check that the proper warnings are emitted for the affected pages |
| 1211 |
for page in ["section2/index", "section2/page1"]: |
| 1212 |
assert ( |
| 1213 |
f"WARNING: Page {page} matches two wildcard patterns " |
| 1214 |
"in secondary_sidebar_items: ** and section2/*" |
| 1215 |
) in sphinx_build.warnings |
| 1216 |
|
| 1217 |
# Check that the page-toc template gets rendered |
| 1218 |
# (but not for section1/index or section2/*) |
| 1219 |
assert sphinx_build.html_tree("index.html").select("div.page-toc") |
| 1220 |
assert not sphinx_build.html_tree("section1/index.html").select("div.page-toc") |
| 1221 |
assert not sphinx_build.html_tree("section2/index.html").select("div.page-toc") |
| 1222 |
assert not sphinx_build.html_tree("section2/page1.html").select("div.page-toc") |
| 1223 |
|
| 1224 |
# Check that the edit-this-page template gets rendered |
| 1225 |
# (but not for section1/index or section2/*) |
| 1226 |
assert sphinx_build.html_tree("index.html").select("div.editthispage") |
| 1227 |
assert not sphinx_build.html_tree("section1/index.html").select("div.editthispage") |
| 1228 |
assert not sphinx_build.html_tree("section2/index.html").select("div.editthispage") |
| 1229 |
assert not sphinx_build.html_tree("section2/page1.html").select("div.editthispage") |
| 1230 |
|
| 1231 |
# Check that sourcelink is only rendered for section2/* |
| 1232 |
assert not sphinx_build.html_tree("index.html").select("div.sourcelink") |
| 1233 |
assert not sphinx_build.html_tree("section1/index.html").select("div.sourcelink") |
| 1234 |
assert sphinx_build.html_tree("section2/index.html").select("div.sourcelink") |
| 1235 |
assert sphinx_build.html_tree("section2/page1.html").select("div.sourcelink") |
| 1236 |
|
| 1237 |
|
| 1238 |
def test_role_main_for_search_highlights(sphinx_build_factory): |
| 1239 |
"""Sphinx searchtools.js looks for [role="main"], so make sure it's there. |
| 1240 |
|
| 1241 |
This is a regression test. See #1676. |
| 1242 |
""" |
| 1243 |
sphinx_build = sphinx_build_factory("base").build() |
| 1244 |
assert sphinx_build.html_tree("index.html").select_one('[role="main"]') |
| 1245 |
|
| 1246 |
|
| 1247 |
def test_sidebar_secondary_templates_all_empty(sphinx_build_factory) -> None: |
| 1248 |
"""Test that the secondary sidebar is removed if all templates are empty.""" |
| 1249 |
confoverrides = { |
| 1250 |
"html_theme_options": { |
| 1251 |
**COMMON_CONF_OVERRIDES, |
| 1252 |
"secondary_sidebar_items": ["page-toc", "sourcelink"], |
| 1253 |
}, |
| 1254 |
"html_show_sourcelink": False, |
| 1255 |
} |
| 1256 |
|
| 1257 |
# The page-toc component is empty because there is no in-page toc for page1 |
| 1258 |
# The sourcelink component is empty because we disabled it in configuration |
| 1259 |
# Hence the secondary sidebar has all its templates empty and should be removed |
| 1260 |
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build() |
| 1261 |
assert not sphinx_build.html_tree("page1.html").select("div.bd-sidebar-secondary") |