-
Notifications
You must be signed in to change notification settings - Fork 297
/
test_build.py
678 lines (563 loc) · 23 KB
/
test_build.py
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
import os
from pathlib import Path
from shutil import copytree
import pytest
import sphinx.errors
from bs4 import BeautifulSoup
from sphinx.testing.path import path as sphinx_path
from sphinx.testing.util import SphinxTestApp
path_tests = Path(__file__).parent
class SphinxBuild:
def __init__(self, app: SphinxTestApp, src: Path):
self.app = app
self.src = src
def build(self):
self.app.build()
assert self.warnings == "", self.status
return self
@property
def status(self):
return self.app._status.getvalue()
@property
def warnings(self):
return self.app._warning.getvalue()
@property
def outdir(self):
return Path(self.app.outdir)
def html_tree(self, *path):
path_page = self.outdir.joinpath(*path)
if not path_page.exists():
raise ValueError(f"{path_page} does not exist")
return BeautifulSoup(path_page.read_text("utf8"), "html.parser")
@pytest.fixture()
def sphinx_build_factory(make_app, tmp_path):
def _func(src_folder, **kwargs):
copytree(path_tests / "sites" / src_folder, tmp_path / src_folder)
app = make_app(
srcdir=sphinx_path(os.path.abspath(tmp_path / src_folder)), **kwargs
)
return SphinxBuild(app, tmp_path / src_folder)
yield _func
def test_build_html(sphinx_build_factory, file_regression):
"""Test building the base html template and config."""
sphinx_build = sphinx_build_factory("base") # type: SphinxBuild
# Basic build with defaults
sphinx_build.build()
assert (sphinx_build.outdir / "index.html").exists(), sphinx_build.outdir.glob("*")
index_html = sphinx_build.html_tree("index.html")
subpage_html = sphinx_build.html_tree("section1/index.html")
# Navbar structure
navbar = index_html.select("div#navbar-center")[0]
file_regression.check(navbar.prettify(), basename="navbar_ix", extension=".html")
# Sidebar subpage
sidebar = subpage_html.select(".bd-sidebar")[0]
file_regression.check(
sidebar.prettify(), basename="sidebar_subpage", extension=".html"
)
# Secondary sidebar should not have in-page TOC if it is empty
assert not sphinx_build.html_tree("page1.html").select("div.onthispage")
# Secondary sidebar should not be present if page-level metadata given
assert not sphinx_build.html_tree("page2.html").select("div.bd-sidebar-secondary")
def test_toc_visibility(sphinx_build_factory):
# Test that setting TOC level visibility works as expected
confoverrides = {
"html_theme_options.show_toc_level": 2,
}
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build()
index_html = sphinx_build.html_tree("index.html")
# The 3rd level headers should be visible, but not the fourth-level
assert "visible" in index_html.select(".toc-h2 ul")[0].attrs["class"]
assert "visible" not in index_html.select(".toc-h3 ul")[0].attrs["class"]
def test_icon_links(sphinx_build_factory, file_regression):
html_theme_options_icon_links = {
"icon_links": [
{
"name": "FONTAWESOME",
"url": "https://site1.org",
"icon": "FACLASS",
"type": "fontawesome",
},
{
"name": "FONTAWESOME DEFAULT",
"url": "https://site2.org",
"icon": "FADEFAULTCLASS",
# No type so we can test that the default is fontawesome
},
{
"name": "LOCAL FILE",
"url": "https://site3.org",
"icon": "emptylogo.png", # Logo is our only test site img
"type": "local",
},
{
"name": "WRONG TYPE",
"url": "https://site4.org",
"icon": "WRONG TYPE",
# Because the type is inccorect, this should output an error `span`
"type": "incorrecttype",
},
{
"name": "URL",
"url": "https://site5.org",
"icon": "https://site5.org/image.svg",
"type": "url",
},
{
"name": "FONTAWESOME",
"url": "https://site1.org",
"icon": "FACLASS",
"type": "fontawesome",
"attributes": {
# This should over-ride the href above
"href": "https://override.com",
# This should add a new icon link attribute
"foo": "bar",
# CSS classes should be totally overwritten
"class": "overridden classes",
},
},
]
}
confoverrides = {"html_theme_options": html_theme_options_icon_links}
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build()
# Navbar should have the right icons
icon_links = sphinx_build.html_tree("index.html").select("#navbar-icon-links")[0]
file_regression.check(
icon_links.prettify(), basename="navbar_icon_links", extension=".html"
)
def test_logo_basic(sphinx_build_factory):
"""Test that the logo is shown by default, project title if no logo."""
sphinx_build = sphinx_build_factory("base").build()
# By default logo is shown
index_html = sphinx_build.html_tree("index.html")
assert index_html.select(".navbar-brand img")
assert "emptylogo" in str(index_html.select(".navbar-brand")[0])
assert not index_html.select(".navbar-brand")[0].text.strip()
def test_logo_no_image(sphinx_build_factory):
"""Test that the text is shown if no image specified."""
confoverrides = {"html_logo": ""}
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build()
index_html = sphinx_build.html_tree("index.html")
assert "PyData Tests" in index_html.select(".navbar-brand")[0].text.strip()
assert "emptylogo" not in str(index_html.select(".navbar-brand")[0])
def test_logo_two_images(sphinx_build_factory):
"""Test that the logo image / text is correct when both dark / light given."""
# Test with a specified title and a dark logo
confoverrides = {
"html_theme_options": {
"logo": {
"text": "Foo Title",
"image_dark": "emptydarklogo.png",
}
},
}
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build()
index_html = sphinx_build.html_tree("index.html")
index_str = str(index_html.select(".navbar-brand")[0])
assert "emptylogo" in index_str
assert "emptydarklogo" in index_str
assert "Foo Title" in index_str
def test_logo_external_link(sphinx_build_factory):
"""Test that the logo link is correct for external URLs."""
# Test with a specified external logo link
test_url = "https://secure.example.com"
confoverrides = {
"html_theme_options": {
"logo": {
"link": test_url,
}
},
}
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build()
index_html = sphinx_build.html_tree("index.html")
index_str = str(index_html.select(".navbar-brand")[0])
assert f'href="{test_url}"' in index_str
def test_logo_external_image(sphinx_build_factory):
"""Test that the logo link is correct for external URLs."""
# Test with a specified external logo image source
test_url = "https://pydata.org/wp-content/uploads/2019/06/pydata-logo-final.png"
confoverrides = {
"html_theme_options": {
"logo": {
"image_dark": test_url,
}
},
}
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build()
index_html = sphinx_build.html_tree("index.html")
index_str = str(index_html.select(".navbar-brand")[0])
assert f'src="{test_url}"' in index_str
def test_favicons(sphinx_build_factory):
"""Test that arbitrary favicons are included."""
html_theme_options_favicons = {
"favicons": [
{
"rel": "icon",
"sizes": "16x16",
"href": "https://secure.example.com/favicon/favicon-16x16.png",
},
{
"rel": "icon",
"sizes": "32x32",
"href": "favicon-32x32.png",
},
{
"rel": "apple-touch-icon",
"sizes": "180x180",
"href": "apple-touch-icon-180x180.png",
},
]
}
confoverrides = {"html_theme_options": html_theme_options_favicons}
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build()
index_html = sphinx_build.html_tree("index.html")
icon_16 = (
'<link href="https://secure.example.com/favicon/favicon-16x16.png" '
'rel="icon" sizes="16x16" type="image/png">'
)
icon_32 = (
'<link href="_static/favicon-32x32.png" rel="icon" sizes="32x32" '
'type="image/png">'
)
icon_180 = (
'<link href="_static/apple-touch-icon-180x180.png" '
'rel="apple-touch-icon" sizes="180x180" type="image/png">'
)
assert icon_16 in str(index_html.select("head")[0])
assert icon_32 in str(index_html.select("head")[0])
assert icon_180 in str(index_html.select("head")[0])
def test_navbar_align_default(sphinx_build_factory):
"""The navbar items align with the proper part of the page."""
sphinx_build = sphinx_build_factory("base").build()
index_html = sphinx_build.html_tree("index.html")
assert "col-lg-9" in index_html.select(".navbar-header-items")[0].attrs["class"]
def test_navbar_align_right(sphinx_build_factory):
"""The navbar items align with the proper part of the page."""
confoverrides = {"html_theme_options.navbar_align": "right"}
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build()
# Both the column alignment and the margin should be changed
index_html = sphinx_build.html_tree("index.html")
assert "col-lg-9" not in index_html.select(".navbar-header-items")[0].attrs["class"]
assert "ml-auto" in index_html.select("div#navbar-center")[0].attrs["class"]
def test_navbar_no_in_page_headers(sphinx_build_factory, file_regression):
# https://github.com/pydata/pydata-sphinx-theme/issues/302
sphinx_build = sphinx_build_factory("test_navbar_no_in_page_headers").build()
index_html = sphinx_build.html_tree("index.html")
navbar = index_html.select("ul#navbar-main-elements")[0]
file_regression.check(navbar.prettify(), extension=".html")
@pytest.mark.parametrize("n_links", (0, 4, 8)) # 0 = only dropdown, 8 = no dropdown
def test_navbar_header_dropdown(sphinx_build_factory, file_regression, n_links):
"""Test whether dropdown appears based on number of header links + config."""
extra_links = [{"url": f"https://{ii}.org", "name": ii} for ii in range(3)]
confoverrides = {
"html_theme_options": {
"external_links": extra_links,
"header_links_before_dropdown": n_links,
}
}
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build()
index_html = sphinx_build.html_tree("index.html")
navbar = index_html.select("ul#navbar-main-elements")[0]
if n_links == 0:
# There should be *only* a dropdown and no standalone links
assert navbar.select("div.dropdown") and not navbar.select(
".navbar-nav > li.nav-item"
) # noqa
if n_links == 4:
# There should be at least one standalone link, and a dropdown
assert navbar.select(".navbar-nav > li.nav-item") and navbar.select(
"div.dropdown"
) # noqa
if n_links == 8:
# There should be no dropdown and only standalone links
assert navbar.select(".navbar-nav > li.nav-item") and not navbar.select(
"div.dropdown"
) # noqa
def test_sidebars_captions(sphinx_build_factory, file_regression):
sphinx_build = sphinx_build_factory("sidebars").build()
subindex_html = sphinx_build.html_tree("section1/index.html")
# Sidebar structure with caption
sidebar = subindex_html.select("nav#bd-docs-nav")[0]
file_regression.check(sidebar.prettify(), extension=".html")
def test_sidebars_nested_page(sphinx_build_factory, file_regression):
sphinx_build = sphinx_build_factory("sidebars").build()
subindex_html = sphinx_build.html_tree("section1/subsection1/page1.html")
# For nested (uncollapsed) page, the label included `checked=""`
sidebar = subindex_html.select("nav#bd-docs-nav")[0]
file_regression.check(sidebar.prettify(), extension=".html")
def test_sidebars_level2(sphinx_build_factory, file_regression):
"""Sidebars in a second-level page w/ children"""
confoverrides = {"templates_path": ["_templates_sidebar_level2"]}
sphinx_build = sphinx_build_factory("sidebars", confoverrides=confoverrides).build()
subindex_html = sphinx_build.html_tree("section1/subsection1/index.html")
# Sidebar structure
sidebar = subindex_html.select("nav#bd-docs-nav")[0]
file_regression.check(sidebar.prettify(), extension=".html")
def test_sidebars_show_nav_level0(sphinx_build_factory, file_regression):
"""
Regression test for show_nav_level:0 when the toc is divided into parts.
Testing both home page and a subsection page for correct elements.
"""
confoverrides = {"html_theme_options.show_nav_level": 0}
sphinx_build = sphinx_build_factory("sidebars", confoverrides=confoverrides).build()
# 1. Home Page
index_html = sphinx_build.html_tree("section1/index.html")
sidebar = index_html.select("nav#bd-docs-nav")[0]
# check if top-level ul is present
ul = sidebar.find("ul")
assert "list-caption" in ul.attrs["class"]
# get all li elements
li = ul.select("li")
# part li
assert "toctree-l0 has-children" in " ".join(li[0].attrs["class"])
assert "caption-text" in li[0].select("p span")[0].attrs["class"]
assert "label-parts" in li[0].find("label").attrs["class"]
# basic checks on other levels
assert "toctree-l1 has-children" in " ".join(li[1].attrs["class"])
assert "toctree-l2" in li[2].attrs["class"]
# 2. Subsection Page
subsection_html = sphinx_build.html_tree("section1/subsection1/index.html")
sidebar = subsection_html.select("nav#bd-docs-nav")[0]
# get all input elements
input_elem = sidebar.select("input")
# all input elements should be collapsed in this page
for ii in input_elem:
assert "checked" in ii.attrs
def test_included_toc(sphinx_build_factory):
"""Test that Sphinx project containing TOC (.. toctree::) included
via .. include:: can be successfully built.
"""
# Regression test for bug resolved in #347.
# Tests mainly makes sure that the sphinx_build.build() does not raise exception.
# https://github.com/pydata/pydata-sphinx-theme/pull/347
sphinx_build = sphinx_build_factory("test_included_toc").build()
included_page_html = sphinx_build.html_tree("included-page.html")
assert included_page_html is not None
# html contexts for `show_edit_button`
# these are "good" context fragments that should yield a working link
good_edits = [
[
{
"github_user": "foo",
"github_repo": "bar",
"github_version": "HEAD",
"doc_path": "docs",
},
"https://github.com/foo/bar/edit/HEAD/docs/index.rst",
],
[
{
"gitlab_user": "foo",
"gitlab_repo": "bar",
"gitlab_version": "HEAD",
"doc_path": "docs",
},
"https://gitlab.com/foo/bar/-/edit/HEAD/docs/index.rst",
],
[
{
"bitbucket_user": "foo",
"bitbucket_repo": "bar",
"bitbucket_version": "HEAD",
"doc_path": "docs",
},
"https://bitbucket.org/foo/bar/src/HEAD/docs/index.rst?mode=edit",
],
]
# copy the "good" ones, ensure `doc_path` is agnostic to trailing slashes
slash_edits = [
[
{
# add slashes to doc_path:
key: f"{value}/" if key == "doc_path" else value
for key, value in html_context.items()
},
# the URL does not change
url,
]
for html_context, url in good_edits
]
# copy the "good" ones, provide a `<whatever>_url` based off the default
providers = [
[
dict(
# copy all the values
**html_context,
# add a provider url
**{f"{provider}_url": f"https://{provider}.example.com"},
),
f"""https://{provider}.example.com/foo/{url.split("/foo/")[1]}""",
]
for html_context, url in good_edits
for provider in ["gitlab", "bitbucket", "github"]
if provider in url
]
# missing any of the values should fail
bad_edits = [
[
{
# copy all the values
key: value
for key, value in html_context.items()
# but not `<provider>_version`
if "_version" not in key
},
None,
]
for html_context, url in good_edits
]
# a good custom URL template
good_custom = [
[
{
"edit_page_url_template": (
"https://dvcs.example.com/foo/bar/edit/HEAD/{{ file_name }}"
)
},
"https://dvcs.example.com/foo/bar/edit/HEAD/index.rst",
]
]
# a bad custom URL template
bad_custom = [
[
# it's missing a reference to {{ file_name }}
{"edit_page_url_template": "http://has-no-file-name"},
None,
]
]
all_edits = [
*good_edits,
*slash_edits,
*bad_edits,
*good_custom,
*bad_custom,
*providers,
]
@pytest.mark.parametrize("html_context,edit_url", all_edits)
def test_edit_page_url(sphinx_build_factory, html_context, edit_url):
confoverrides = {
"html_theme_options.use_edit_page_button": True,
"html_context": html_context,
}
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides)
if edit_url is None:
with pytest.raises(sphinx.errors.ThemeError):
sphinx_build.build()
return
sphinx_build.build()
index_html = sphinx_build.html_tree("index.html")
edit_link = index_html.select(".editthispage a")
assert edit_link, "no edit link found"
assert edit_link[0].attrs["href"] == edit_url, f"edit link didn't match {edit_link}"
@pytest.mark.parametrize(
"provider,tags",
[
# TODO: Deprecate old-style analytics config >= 0.12
# new_google_analytics_id
({"html_theme_options.google_analytics_id": "G-XXXXX"}, ["gtag", "G-XXXXX"]),
# old_google_analytics_id
({"html_theme_options.google_analytics_id": "UA-XXXXX"}, ["ga", "UA-XXXXX"]),
# google analytics
(
{"html_theme_options.analytics": {"google_analytics_id": "G-XXXXX"}},
["gtag", "G-XXXXX"],
),
# google and plausible
(
{
"html_theme_options.analytics": {
"google_analytics_id": "G-XXXXX",
"plausible_analytics_domain": "toto",
"plausible_analytics_url": "http://.../script.js",
}
},
["gtag", "G-XXXXX"],
),
# TODO: Deprecate old-style analytics config >= 0.12
(
{
"html_theme_options.analytics": {
"plausible_analytics_domain": "toto",
"plausible_analytics_url": "http://.../script.js",
},
"html_theme_options.google_analytics_id": "G-XXXXX",
},
["gtag", "G-XXXXX"],
),
],
)
def test_analytics(sphinx_build_factory, provider, tags):
confoverrides = provider
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides)
sphinx_build.build()
index_html = sphinx_build.html_tree("index.html")
# Search all the scripts and make sure one of them has the Google tag in there
tags_found = False
for script in index_html.select("script"):
if script.string and tags[0] in script.string and tags[1] in script.string:
tags_found = True
assert tags_found is True
def test_plausible(sphinx_build_factory):
provider = {
"html_theme_options.analytics": {
"plausible_analytics_domain": "toto",
"plausible_analytics_url": "http://.../script.js",
}
}
confoverrides = provider
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides)
sphinx_build.build()
index_html = sphinx_build.html_tree("index.html")
# Search all the scripts and make sure one of them has the Google tag in there
attr_found = False
for script in index_html.select("script"):
if script.attrs.get("data-domain") == "toto":
attr_found = True
assert attr_found is True
def test_show_nav_level(sphinx_build_factory):
"""The navbar items align with the proper part of the page."""
confoverrides = {"html_theme_options.show_nav_level": 2}
sphinx_build = sphinx_build_factory("sidebars", confoverrides=confoverrides).build()
# Both the column alignment and the margin should be changed
index_html = sphinx_build.html_tree("section1/index.html")
for checkbox in index_html.select("li.toctree-l1.has-children > input"):
assert "checked" in checkbox.attrs
def test_version_switcher(sphinx_build_factory, file_regression):
"""Regression test the version switcher dropdown HTML.
Note that a lot of the switcher HTML gets populated by JavaScript,
so we will not test the final behavior. This just tests for the basic
structure.
TODO: Find a way to test Javascript's behavior in populating the HTML.
"""
confoverrides = {
"html_theme_options": {
"navbar_end": ["version-switcher"],
"switcher": {
"json_url": "switcher.json",
"version_match": "0.7.1",
},
}
}
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build()
switcher = sphinx_build.html_tree("index.html").select(
".version-switcher__container"
)[
0
] # noqa
file_regression.check(
switcher.prettify(), basename="navbar_switcher", extension=".html"
)
def test_theme_switcher(sphinx_build_factory, file_regression):
"""Regression test the theme switcher btn HTML"""
sphinx_build = sphinx_build_factory("base").build()
switcher = sphinx_build.html_tree("index.html").select(".theme-switch-button")[0]
file_regression.check(
switcher.prettify(), basename="navbar_theme", extension=".html"
)
def test_shorten_link(sphinx_build_factory, file_regression):
"""regression test the shorten links html"""
sphinx_build = sphinx_build_factory("base").build()
github = sphinx_build.html_tree("page1.html").select(".github")[0]
file_regression.check(github.prettify(), basename="github_link", extension=".html")
gitlab = sphinx_build.html_tree("page1.html").select(".gitlab")[0]
file_regression.check(gitlab.prettify(), basename="gitlab_link", extension=".html")