Debian Codegraph @

tests/test_builders.py

raw
1 import os
2
3 import pytest
4 import sphinx
5 from sphinx import addnodes
6 try:
7 # Available from Sphinx 2.0
8 from sphinx.builders.dirhtml import DirectoryHTMLBuilder
9 from sphinx.builders.singlehtml import SingleFileHTMLBuilder
10 except ImportError:
11 from sphinx.builders.html import (
12 DirectoryHTMLBuilder,
13 SingleFileHTMLBuilder,
14 )
15
16 from .util import build_all
17
18
19 def test_basic():
20 for (app, status, warning) in build_all('test-basic'):
21 assert app.env.get_doctree('index').findall(addnodes.toctree)
22 content = open(os.path.join(app.outdir, 'index.html')).read()
23
24 if isinstance(app.builder, DirectoryHTMLBuilder):
25 search = (
26 '<div class="toctree-wrapper compound">\n'
27 '<ul>\n'
28 '<li class="toctree-l1">'
29 '<a class="reference internal" href="foo/">foo</a>'
30 '<ul>\n'
31 '<li class="toctree-l2">'
32 '<a class="reference internal" href="bar/">bar</a></li>\n'
33 '</ul>\n'
34 '</li>\n'
35 '</ul>\n'
36 '</div>'
37 )
38 assert search in content
39 elif isinstance(app.builder, SingleFileHTMLBuilder):
40 internal_ref = '#document-foo'
41 if sphinx.version_info[:3] < (7, 3, 0):
42 internal_ref = 'index.html' + internal_ref
43 search = (
44 '<ul>\n'
45 '<li class="toctree-l1">'
46 f'<a class="reference internal" href="{internal_ref}">foo</a>'
47 '</li>\n'
48 '</ul>'
49 )
50 assert search in content
51 else:
52 search = (
53 '<div class="toctree-wrapper compound">\n'
54 '<ul>\n'
55 '<li class="toctree-l1">'
56 '<a class="reference internal" href="foo.html">foo</a>'
57 '<ul>\n'
58 '<li class="toctree-l2">'
59 '<a class="reference internal" href="bar.html">bar</a></li>\n'
60 '</ul>\n'
61 '</li>\n'
62 '</ul>\n'
63 '</div>'
64 )
65 assert search in content, ('Missing search with builder {0}'
66 .format(app.builder.name))
67
68
69 def test_empty():
70 """Local TOC is showing, as toctree was empty"""
71 for (app, status, warning) in build_all('test-empty'):
72 assert app.env.get_doctree('index').findall(addnodes.toctree)
73 content = open(os.path.join(app.outdir, 'index.html')).read()
74 global_toc = '<div class="toctree-wrapper compound">\n</div>'
75 local_toc = (
76 '<div class="local-toc"><ul>\n'
77 '<li><a class="reference internal" href="#">test-empty</a></li>'
78 '</ul>\n</div>'
79 )
80 assert global_toc in content
81 assert local_toc not in content
82
83
84 def test_missing_toctree():
85 """Local TOC is showing, as toctree was missing"""
86 for (app, status, warning) in build_all('test-missing-toctree'):
87 assert list(app.env.get_doctree('index').findall(addnodes.toctree)) == []
88 content = open(os.path.join(app.outdir, 'index.html')).read()
89 assert '<div class="toctree' not in content
90 assert '<div class="local-toc">' in content