Debian Codegraph @

tests/test_extensions/test_ext_viewcode.py

raw
1 """Test sphinx.ext.viewcode extension."""
2
3 from __future__ import annotations
4
5 import re
6 import shutil
7 from typing import TYPE_CHECKING
8
9 import pygments
10 import pytest
11
12 if TYPE_CHECKING:
13 from sphinx.application import Sphinx
14 from sphinx.testing.util import SphinxTestApp
15
16
17 def check_viewcode_output(app: SphinxTestApp) -> str:
18 if tuple(map(int, pygments.__version__.split('.')[:2])) >= (2, 19):
19 sp = '<span> </span>'
20 else:
21 sp = ' '
22
23 warnings = re.sub(r'\\+', '/', app.warning.getvalue())
24 assert re.findall(
25 r"index.rst:\d+: WARNING: Object named 'func1' not found in include "
26 r"file .*/spam/__init__.py'",
27 warnings,
28 )
29
30 result = (app.outdir / 'index.html').read_text(encoding='utf8')
31 assert result.count('href="_modules/spam/mod1.html#func1"') == 2
32 assert result.count('href="_modules/spam/mod2.html#func2"') == 2
33 assert result.count('href="_modules/spam/mod1.html#Class1"') == 2
34 assert result.count('href="_modules/spam/mod2.html#Class2"') == 2
35 assert result.count('@decorator') == 1
36
37 # test that the class attribute is correctly documented
38 assert result.count('this is Class3') == 2
39 assert 'this is the class attribute class_attr' in result
40 # the next assert fails, until the autodoc bug gets fixed
41 assert result.count('this is the class attribute class_attr') == 2
42
43 result = (app.outdir / '_modules/spam/mod1.html').read_text(encoding='utf8')
44 # filter pygments classes
45 result = re.sub('<span class="[^"]{,2}">', '<span>', result)
46 assert (
47 '<div class="viewcode-block" id="Class1">\n'
48 '<a class="viewcode-back" href="../../index.html#spam.Class1">[docs]</a>\n'
49 ) in result
50 assert '<span>@decorator</span>\n' in result
51 assert f'<span>class</span>{sp}<span>Class1</span><span>:</span>\n' in result
52 assert (
53 '<span> </span>'
54 '<span>&quot;&quot;&quot;this is Class1&quot;&quot;&quot;</span></div>\n'
55 ) in result
56
57 return result
58
59
60 @pytest.mark.sphinx(
61 'html',
62 testroot='ext-viewcode',
63 freshenv=True,
64 confoverrides={'viewcode_line_numbers': True},
65 )
66 @pytest.mark.usefixtures('rollback_sysmodules')
67 def test_viewcode_linenos(app: SphinxTestApp) -> None:
68 shutil.rmtree(app.outdir / '_modules', ignore_errors=True)
69 app.build(force_all=True)
70
71 result = check_viewcode_output(app)
72 assert '<span class="linenos"> 1</span>' in result
73
74
75 @pytest.mark.sphinx(
76 'html',
77 testroot='ext-viewcode',
78 freshenv=True,
79 confoverrides={'viewcode_line_numbers': False},
80 )
81 @pytest.mark.usefixtures('rollback_sysmodules')
82 def test_viewcode(app: SphinxTestApp) -> None:
83 shutil.rmtree(app.outdir / '_modules', ignore_errors=True)
84 app.build(force_all=True)
85
86 result = check_viewcode_output(app)
87 assert 'class="linenos">' not in result
88
89
90 @pytest.mark.sphinx('epub', testroot='ext-viewcode')
91 @pytest.mark.usefixtures('rollback_sysmodules')
92 def test_viewcode_epub_default(app: SphinxTestApp) -> None:
93 shutil.rmtree(app.outdir)
94 app.build(force_all=True)
95
96 assert not (app.outdir / '_modules/spam/mod1.xhtml').exists()
97
98 result = (app.outdir / 'index.xhtml').read_text(encoding='utf8')
99 assert result.count('href="_modules/spam/mod1.xhtml#func1"') == 0
100
101
102 @pytest.mark.sphinx(
103 'epub',
104 testroot='ext-viewcode',
105 confoverrides={'viewcode_enable_epub': True},
106 )
107 @pytest.mark.usefixtures('rollback_sysmodules')
108 def test_viewcode_epub_enabled(app: SphinxTestApp) -> None:
109 app.build(force_all=True)
110
111 assert (app.outdir / '_modules/spam/mod1.xhtml').exists()
112
113 result = (app.outdir / 'index.xhtml').read_text(encoding='utf8')
114 assert result.count('href="_modules/spam/mod1.xhtml#func1"') == 2
115
116
117 @pytest.mark.sphinx('html', testroot='ext-viewcode', tags=['test_linkcode'])
118 def test_linkcode(app: SphinxTestApp) -> None:
119 app.build(filenames=[app.srcdir / 'objects.rst'])
120
121 stuff = (app.outdir / 'objects.html').read_text(encoding='utf8')
122
123 assert 'https://foobar/source/foolib.py' in stuff
124 assert 'https://foobar/js/' in stuff
125 assert 'https://foobar/c/' in stuff
126 assert 'https://foobar/cpp/' in stuff
127 assert 'http://foobar/rst/' in stuff
128
129
130 @pytest.mark.sphinx('html', testroot='ext-viewcode-find', freshenv=True)
131 def test_local_source_files(app: SphinxTestApp) -> None:
132 def find_source(
133 app: Sphinx, modname: str
134 ) -> tuple[str, dict[str, tuple[str, int, int]]]:
135 if modname == 'not_a_package':
136 source = app.srcdir / 'not_a_package/__init__.py'
137 tags = {
138 'func1': ('def', 1, 1),
139 'Class1': ('class', 1, 1),
140 'not_a_package.submodule.func1': ('def', 1, 1),
141 'not_a_package.submodule.Class1': ('class', 1, 1),
142 }
143 else:
144 source = app.srcdir / 'not_a_package/submodule.py'
145 tags = {
146 'not_a_package.submodule.func1': ('def', 11, 15),
147 'Class1': ('class', 19, 22),
148 'not_a_package.submodule.Class1': ('class', 19, 22),
149 'Class3': ('class', 25, 30),
150 'not_a_package.submodule.Class3.class_attr': ('other', 29, 29),
151 }
152 return source.read_text(encoding='utf8'), tags
153
154 app.connect('viewcode-find-source', find_source)
155 app.build(force_all=True)
156
157 warnings = re.sub(r'\\+', '/', app.warning.getvalue())
158 assert re.findall(
159 r"index.rst:\d+: WARNING: Object named 'func1' not found in include "
160 r"file .*/not_a_package/__init__.py'",
161 warnings,
162 )
163
164 result = (app.outdir / 'index.html').read_text(encoding='utf8')
165 for needle in (
166 'href="_modules/not_a_package.html#func1"',
167 'href="_modules/not_a_package.html#not_a_package.submodule.func1"',
168 'href="_modules/not_a_package/submodule.html#Class1"',
169 'href="_modules/not_a_package/submodule.html#Class3"',
170 'href="_modules/not_a_package/submodule.html#not_a_package.submodule.Class1"',
171 'href="_modules/not_a_package/submodule.html#not_a_package.submodule.Class3.class_attr"',
172 'This is the class attribute class_attr',
173 ):
174 assert result.count(needle) == 1
175
176
177 @pytest.mark.sphinx('html', testroot='ext-viewcode-find-package', freshenv=True)
178 def test_find_local_package_import_path(app: Sphinx) -> None:
179 app.build(force_all=True)
180 result = (app.outdir / 'index.html').read_text(encoding='utf8')
181
182 count_func1 = result.count(
183 'href="_modules/main_package/subpackage/_subpackage2/submodule.html#func1"'
184 )
185 assert count_func1 == 1
186
187 count_class1 = result.count(
188 'href="_modules/main_package/subpackage/_subpackage2/submodule.html#Class1"'
189 )
190 assert count_class1 == 1
191
192 count_class3 = result.count(
193 'href="_modules/main_package/subpackage/_subpackage2/submodule.html#Class3"'
194 )
195 assert count_class3 == 1