File: test_ext_viewcode.py

package info (click to toggle)
sphinx 8.2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 26,712 kB
  • sloc: python: 105,846; javascript: 6,474; perl: 451; makefile: 178; sh: 37; xml: 19; ansic: 2
file content (194 lines) | stat: -rw-r--r-- 6,844 bytes parent folder | download | duplicates (3)
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
"""Test sphinx.ext.viewcode extension."""

from __future__ import annotations

import re
import shutil
from typing import TYPE_CHECKING

import pygments
import pytest

from sphinx.testing.util import SphinxTestApp

if TYPE_CHECKING:
    from sphinx.testing.util import SphinxTestApp


def check_viewcode_output(app: SphinxTestApp) -> str:
    if tuple(map(int, pygments.__version__.split('.')[:2])) >= (2, 19):
        sp = '<span> </span>'
    else:
        sp = ' '

    warnings = re.sub(r'\\+', '/', app.warning.getvalue())
    assert re.findall(
        r"index.rst:\d+: WARNING: Object named 'func1' not found in include "
        r"file .*/spam/__init__.py'",
        warnings,
    )

    result = (app.outdir / 'index.html').read_text(encoding='utf8')
    assert result.count('href="_modules/spam/mod1.html#func1"') == 2
    assert result.count('href="_modules/spam/mod2.html#func2"') == 2
    assert result.count('href="_modules/spam/mod1.html#Class1"') == 2
    assert result.count('href="_modules/spam/mod2.html#Class2"') == 2
    assert result.count('@decorator') == 1

    # test that the class attribute is correctly documented
    assert result.count('this is Class3') == 2
    assert 'this is the class attribute class_attr' in result
    # the next assert fails, until the autodoc bug gets fixed
    assert result.count('this is the class attribute class_attr') == 2

    result = (app.outdir / '_modules/spam/mod1.html').read_text(encoding='utf8')
    # filter pygments classes
    result = re.sub('<span class="[^"]{,2}">', '<span>', result)
    assert (
        '<div class="viewcode-block" id="Class1">\n'
        '<a class="viewcode-back" href="../../index.html#spam.Class1">[docs]</a>\n'
    ) in result
    assert '<span>@decorator</span>\n' in result
    assert f'<span>class</span>{sp}<span>Class1</span><span>:</span>\n' in result
    assert (
        '<span>    </span>'
        '<span>&quot;&quot;&quot;this is Class1&quot;&quot;&quot;</span></div>\n'
    ) in result

    return result


@pytest.mark.sphinx(
    'html',
    testroot='ext-viewcode',
    freshenv=True,
    confoverrides={'viewcode_line_numbers': True},
)
@pytest.mark.usefixtures('rollback_sysmodules')
def test_viewcode_linenos(app: SphinxTestApp) -> None:
    shutil.rmtree(app.outdir / '_modules', ignore_errors=True)
    app.build(force_all=True)

    result = check_viewcode_output(app)
    assert '<span class="linenos"> 1</span>' in result


@pytest.mark.sphinx(
    'html',
    testroot='ext-viewcode',
    freshenv=True,
    confoverrides={'viewcode_line_numbers': False},
)
@pytest.mark.usefixtures('rollback_sysmodules')
def test_viewcode(app: SphinxTestApp) -> None:
    shutil.rmtree(app.outdir / '_modules', ignore_errors=True)
    app.build(force_all=True)

    result = check_viewcode_output(app)
    assert 'class="linenos">' not in result


@pytest.mark.sphinx('epub', testroot='ext-viewcode')
@pytest.mark.usefixtures('rollback_sysmodules')
def test_viewcode_epub_default(app: SphinxTestApp) -> None:
    shutil.rmtree(app.outdir)
    app.build(force_all=True)

    assert not (app.outdir / '_modules/spam/mod1.xhtml').exists()

    result = (app.outdir / 'index.xhtml').read_text(encoding='utf8')
    assert result.count('href="_modules/spam/mod1.xhtml#func1"') == 0


@pytest.mark.sphinx(
    'epub',
    testroot='ext-viewcode',
    confoverrides={'viewcode_enable_epub': True},
)
@pytest.mark.usefixtures('rollback_sysmodules')
def test_viewcode_epub_enabled(app: SphinxTestApp) -> None:
    app.build(force_all=True)

    assert (app.outdir / '_modules/spam/mod1.xhtml').exists()

    result = (app.outdir / 'index.xhtml').read_text(encoding='utf8')
    assert result.count('href="_modules/spam/mod1.xhtml#func1"') == 2


@pytest.mark.sphinx('html', testroot='ext-viewcode', tags=['test_linkcode'])
def test_linkcode(app: SphinxTestApp) -> None:
    app.build(filenames=[app.srcdir / 'objects.rst'])

    stuff = (app.outdir / 'objects.html').read_text(encoding='utf8')

    assert 'https://foobar/source/foolib.py' in stuff
    assert 'https://foobar/js/' in stuff
    assert 'https://foobar/c/' in stuff
    assert 'https://foobar/cpp/' in stuff
    assert 'http://foobar/rst/' in stuff


@pytest.mark.sphinx('html', testroot='ext-viewcode-find', freshenv=True)
def test_local_source_files(app: SphinxTestApp) -> None:
    def find_source(app, modname):
        if modname == 'not_a_package':
            source = app.srcdir / 'not_a_package/__init__.py'
            tags = {
                'func1': ('def', 1, 1),
                'Class1': ('class', 1, 1),
                'not_a_package.submodule.func1': ('def', 1, 1),
                'not_a_package.submodule.Class1': ('class', 1, 1),
            }
        else:
            source = app.srcdir / 'not_a_package/submodule.py'
            tags = {
                'not_a_package.submodule.func1': ('def', 11, 15),
                'Class1': ('class', 19, 22),
                'not_a_package.submodule.Class1': ('class', 19, 22),
                'Class3': ('class', 25, 30),
                'not_a_package.submodule.Class3.class_attr': ('other', 29, 29),
            }
        return source.read_text(encoding='utf8'), tags

    app.connect('viewcode-find-source', find_source)
    app.build(force_all=True)

    warnings = re.sub(r'\\+', '/', app.warning.getvalue())
    assert re.findall(
        r"index.rst:\d+: WARNING: Object named 'func1' not found in include "
        r"file .*/not_a_package/__init__.py'",
        warnings,
    )

    result = (app.outdir / 'index.html').read_text(encoding='utf8')
    for needle in (
        'href="_modules/not_a_package.html#func1"',
        'href="_modules/not_a_package.html#not_a_package.submodule.func1"',
        'href="_modules/not_a_package/submodule.html#Class1"',
        'href="_modules/not_a_package/submodule.html#Class3"',
        'href="_modules/not_a_package/submodule.html#not_a_package.submodule.Class1"',
        'href="_modules/not_a_package/submodule.html#not_a_package.submodule.Class3.class_attr"',
        'This is the class attribute class_attr',
    ):
        assert result.count(needle) == 1


@pytest.mark.sphinx('html', testroot='ext-viewcode-find-package', freshenv=True)
def test_find_local_package_import_path(app, status, warning):
    app.build(force_all=True)
    result = (app.outdir / 'index.html').read_text(encoding='utf8')

    count_func1 = result.count(
        'href="_modules/main_package/subpackage/_subpackage2/submodule.html#func1"'
    )
    assert count_func1 == 1

    count_class1 = result.count(
        'href="_modules/main_package/subpackage/_subpackage2/submodule.html#Class1"'
    )
    assert count_class1 == 1

    count_class3 = result.count(
        'href="_modules/main_package/subpackage/_subpackage2/submodule.html#Class3"'
    )
    assert count_class3 == 1