Debian Codegraph @

docs/tests/test.py

raw
1 #
2 # Run tests after the documentation is executed.
3 #
4 # These tests are designed to be executed inside tox, after sphinx-build.
5 #
6 import os
7 import pathlib
8 from typing import List
9 import xml.etree.ElementTree as ET
10 import json
11
12 from lunr.index import Index
13
14 from sphinx.ext.intersphinx import inspect_main
15
16 from pydoctor import __version__
17
18
19 BASE_DIR = pathlib.Path(os.environ.get('TOX_INI_DIR', os.getcwd())) / 'build' / 'docs'
20
21
22 def test_rtd_pydoctor_call():
23 """
24 With the pydoctor Sphinx extension, the pydoctor API HTML files are
25 generated.
26 """
27 # The pydoctor index is generated and overwrites the Sphinx files.
28 with open(BASE_DIR / 'api' / 'index.html', 'r') as stream:
29 page = stream.read()
30 assert 'moduleIndex.html' in page, page
31
32
33 def test_rtd_pydoctor_multiple_call():
34 """
35 With the pydoctor Sphinx extension can call pydoctor for more than one
36 API doc source.
37 """
38 with open(BASE_DIR / 'docformat' / 'epytext_demo' / 'index.html', 'r') as stream:
39 page = stream.read()
40 assert '<a href="../epytext.html" class="projecthome">pydoctor-epytext-demo</a>' in page, page
41
42
43 def test_rtd_extension_inventory():
44 """
45 The Sphinx inventory is available during normal sphinx-build.
46 """
47 with open(BASE_DIR / 'sphinx-integration.html', 'r') as stream:
48 page = stream.read()
49 assert 'href="/en/latest/api/pydoctor.sphinx_ext.build_apidocs.html"' in page, page
50
51
52 def test_sphinx_object_inventory_version(capsys):
53 """
54 The Sphinx inventory is generated with the project version in the header.
55 """
56 # The pydoctor own inventory.
57 apidocs_inv = BASE_DIR / 'api' / 'objects.inv'
58 with open(apidocs_inv, 'rb') as stream:
59 page = stream.read()
60 assert page.startswith(
61 b'# Sphinx inventory version 2\n'
62 b'# Project: pydoctor\n'
63 b'# Version: ' + __version__.encode() + b'\n'
64 ), page
65
66 # Check that inventory can be parsed by Sphinx own extension.
67 inspect_main([str(apidocs_inv)])
68 out, err = capsys.readouterr()
69
70 assert '' == err
71 assert 'pydoctor.driver.main' in out, out
72
73
74 def test_sphinx_object_inventory_version_epytext_demo():
75 """
76 The Sphinx inventory for demo/showcase code has a fixed version and name,
77 passed via docs/source/conf.py.
78 """
79 with open(BASE_DIR / 'docformat' / 'epytext_demo' / 'objects.inv', 'rb') as stream:
80 page = stream.read()
81 assert page.startswith(
82 b'# Sphinx inventory version 2\n'
83 b'# Project: pydoctor-epytext-demo\n'
84 b'# Version: 1.3.0\n'
85 ), page
86
87
88 def test_page_contains_infos():
89 """
90 Test if pydoctor.driver.html contains the following informations:
91
92 - meta generator tag
93 - nav and links to modules, classes, names
94 - js script source
95 - pydoctor github link in the footer
96 - canonical link
97 """
98
99 infos = (f'<meta name="generator" content="pydoctor {__version__}"',
100 '<nav class="navbar navbar-default mainnavbar"',
101 '<a href="moduleIndex.html"',
102 '<a href="classIndex.html"',
103 '<a href="nameIndex.html"',
104 '<script src="pydoctor.js" type="text/javascript"></script>',
105 '<a href="https://github.com/twisted/pydoctor/">pydoctor</a>',
106 '<link rel="canonical" href="https://pydoctor.readthedocs.io/en/latest/api/pydoctor.driver.html"',)
107
108 with open(BASE_DIR / 'api' / 'pydoctor.driver.html', 'r', encoding='utf-8') as stream:
109 page = stream.read()
110 for i in infos:
111 assert i in page, page
112
113 def test_custom_template_contains_infos():
114 """
115 Test if the custom template index.html contains the following informations:
116
117 - meta generator tag
118 - nav and links to modules, classes, names
119 - pydoctor github link in the footer
120 - the custom header
121 - link to teh extra.css
122 - canonical link
123 """
124
125 infos = (f'<meta name="generator" content="pydoctor {__version__}"',
126 '<nav class="navbar navbar-default mainnavbar"',
127 '<a href="moduleIndex.html"',
128 '<a href="classIndex.html"',
129 '<a href="nameIndex.html"',
130 '<a href="https://github.com/twisted/pydoctor/">pydoctor</a>',
131 '<img src="https://twistedmatrix.com/trac/chrome/common/trac_banner.png" alt="Twisted" />',
132 '<link rel="stylesheet" type="text/css" href="extra.css" />',
133 '<link rel="canonical" href="https://pydoctor.readthedocs.io/en/latest/custom_template_demo/index.html"',)
134
135 with open(BASE_DIR / 'custom_template_demo' / 'index.html', 'r', encoding='utf-8') as stream:
136 page = stream.read()
137 for i in infos:
138 assert i in page, page
139
140 def test_meta_pydoctor_template_version_tag_gets_removed():
141 """
142 Test if the index.html effectively do not contains the meta pydoctor template version tag
143 """
144 with open(BASE_DIR / 'api' / 'index.html', 'r', encoding='utf-8') as stream:
145 page = stream.read()
146 assert '<meta name="pydoctor-template-version" content="' not in page, page
147
148 def test_incons_get_copied():
149 """
150 Test if the icons the fonts directory get copid to the ouput folder
151
152 Incons from https://primer.style/octicons/
153 """
154
155 assert (BASE_DIR / 'api' / 'fonts' / 'info.svg').is_file()
156 assert (BASE_DIR / 'api' / 'fonts' / 'x-circle.svg').is_file()
157
158 def test_search_index_generated():
159 """
160 Test if the icons the fonts directory get copid to the ouput folder
161
162 Incons from https://primer.style/octicons/
163 """
164
165 assert (BASE_DIR / 'api' / 'searchindex.json').is_file()
166 assert (BASE_DIR / 'api' / 'fullsearchindex.json').is_file()
167 assert (BASE_DIR / 'api' / 'all-documents.html').is_file()
168
169 def test_lunr_index() -> None:
170 """
171 Run some searches on the lunr index to test it's validity.
172 """
173
174 with (BASE_DIR / 'api' / 'searchindex.json').open() as fobj:
175 index_data = json.load(fobj)
176 index = Index.load(index_data)
177
178 def test_search(query:str, expected:List[str], order_is_important:bool=True) -> None:
179 if order_is_important:
180 assert [r["ref"] for r in index.search(query)] == expected
181 else:
182 assert sorted([r["ref"] for r in index.search(query)]) == sorted(expected)
183
184 test_search('+qname:pydoctor', ['pydoctor'])
185 test_search('+qname:pydoctor.epydoc2stan', ['pydoctor.epydoc2stan'])
186 test_search('_colorize_re_pattern', ['pydoctor.epydoc.markup._pyval_repr.PyvalColorizer._colorize_re_pattern'])
187
188 test_search('+name:Class',
189 ['pydoctor.model.Class',
190 'pydoctor.factory.Factory.Class',
191 'pydoctor.model.DocumentableKind.CLASS',
192 'pydoctor.model.System.Class',
193 ])
194
195 to_stan_results = [
196 'pydoctor.epydoc.markup.ParsedDocstring.to_stan',
197 'pydoctor.epydoc.markup.plaintext.ParsedPlaintextDocstring.to_stan',
198 'pydoctor.epydoc.markup._types.ParsedTypeDocstring.to_stan',
199 'pydoctor.epydoc.markup._pyval_repr.ColorizedPyvalRepr.to_stan',
200 ]
201 test_search('to_stan*', to_stan_results, order_is_important=False)
202 test_search('to_stan', to_stan_results, order_is_important=False)
203
204 to_node_results = [
205 'pydoctor.epydoc.markup.ParsedDocstring.to_node',
206 'pydoctor.epydoc.markup.plaintext.ParsedPlaintextDocstring.to_node',
207 'pydoctor.epydoc.markup._types.ParsedTypeDocstring.to_node',
208 'pydoctor.epydoc.markup.restructuredtext.ParsedRstDocstring.to_node',
209 'pydoctor.epydoc.markup.epytext.ParsedEpytextDocstring.to_node',
210 ]
211 test_search('to_node*', to_node_results, order_is_important=False)
212 test_search('to_node', to_node_results, order_is_important=False)
213
214 test_search('qname:pydoctor.epydoc.markup.restructuredtext.ParsedRstDocstring',
215 ['pydoctor.epydoc.markup.restructuredtext.ParsedRstDocstring'])
216 test_search('pydoctor.epydoc.markup.restructuredtext.ParsedRstDocstring',
217 ['pydoctor.epydoc.markup.restructuredtext.ParsedRstDocstring'])
218
219 def test_pydoctor_test_is_hidden():
220 """
221 Test that option --privacy=HIDDEN:pydoctor.test makes everything under pydoctor.test HIDDEN.
222 """
223
224 def getText(node: ET.Element) -> str:
225 return ''.join(node.itertext()).strip()
226
227 with open(BASE_DIR / 'api' / 'all-documents.html', 'r', encoding='utf-8') as stream:
228 document = ET.fromstring(stream.read())
229 for liobj in document.findall('body/div/ul/li[@id]'):
230
231 if not str(liobj.get("id")).startswith("pydoctor"):
232 continue # not a all-documents list item, maybe in the menu or whatever.
233
234 # figure obj name
235 fullName = getText(liobj.findall('./div[@class=\'fullName\']')[0])
236
237 if fullName.startswith("pydoctor.test"):
238 # figure obj privacy
239 privacy = getText(liobj.findall('./div[@class=\'privacy\']')[0])
240 # check that it's indeed private
241 assert privacy == 'HIDDEN'
242
243 def test_missing_subclasses():
244 """
245 Test for missing subclasses of ParsedDocstring, issue https://github.com/twisted/pydoctor/issues/528.
246 """
247
248 infos = ('pydoctor.epydoc.markup._types.ParsedTypeDocstring',
249 'pydoctor.epydoc.markup.epytext.ParsedEpytextDocstring',
250 'pydoctor.epydoc.markup.plaintext.ParsedPlaintextDocstring',
251 'pydoctor.epydoc.markup.restructuredtext.ParsedRstDocstring', )
252
253 with open(BASE_DIR / 'api' / 'pydoctor.epydoc.markup.ParsedDocstring.html', 'r', encoding='utf-8') as stream:
254 page = stream.read()
255 for i in infos:
256 assert i in page, page