2022-02-19 21:05:56 -06:00
|
|
|
"""Test sphinx.ext.viewcode extension."""
|
2014-09-21 10:17:02 -05:00
|
|
|
|
2024-07-22 20:22:58 -05:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2014-09-21 10:17:02 -05:00
|
|
|
import re
|
2023-04-06 16:40:33 -05:00
|
|
|
import shutil
|
2024-07-22 20:22:58 -05:00
|
|
|
from typing import TYPE_CHECKING
|
2014-09-21 10:17:02 -05:00
|
|
|
|
2025-01-06 00:56:10 -06:00
|
|
|
import pygments
|
2017-01-05 10:14:47 -06:00
|
|
|
import pytest
|
2014-09-21 10:17:02 -05:00
|
|
|
|
2024-07-22 20:22:58 -05:00
|
|
|
if TYPE_CHECKING:
|
2024-07-23 09:35:55 -05:00
|
|
|
from sphinx.testing.util import SphinxTestApp
|
2024-07-22 20:22:58 -05:00
|
|
|
|
|
|
|
|
2024-07-23 09:35:55 -05:00
|
|
|
def check_viewcode_output(app: SphinxTestApp) -> str:
|
2025-01-06 00:56:10 -06:00
|
|
|
if tuple(map(int, pygments.__version__.split('.')[:2])) >= (2, 19):
|
|
|
|
sp = '<span> </span>'
|
|
|
|
else:
|
|
|
|
sp = ' '
|
|
|
|
|
2024-07-23 09:35:55 -05:00
|
|
|
warnings = re.sub(r'\\+', '/', app.warning.getvalue())
|
2014-09-21 10:17:02 -05:00
|
|
|
assert re.findall(
|
2024-08-10 18:39:35 -05:00
|
|
|
r"index.rst:\d+: WARNING: Object named 'func1' not found in include "
|
2014-09-21 10:17:02 -05:00
|
|
|
r"file .*/spam/__init__.py'",
|
2023-02-17 16:11:14 -06:00
|
|
|
warnings,
|
2014-09-21 10:17:02 -05:00
|
|
|
)
|
|
|
|
|
2022-04-26 21:04:19 -05:00
|
|
|
result = (app.outdir / 'index.html').read_text(encoding='utf8')
|
2014-09-21 10:17:02 -05:00
|
|
|
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
|
2017-03-11 15:16:23 -06:00
|
|
|
assert result.count('@decorator') == 1
|
2014-09-21 10:34:16 -05:00
|
|
|
|
2016-02-05 10:15:27 -06:00
|
|
|
# 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
|
|
|
|
|
2022-04-26 21:04:19 -05:00
|
|
|
result = (app.outdir / '_modules/spam/mod1.html').read_text(encoding='utf8')
|
2024-08-11 08:58:56 -05:00
|
|
|
# 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
|
2023-07-27 23:52:48 -05:00
|
|
|
assert '<span>@decorator</span>\n' in result
|
2025-01-06 00:56:10 -06:00
|
|
|
assert f'<span>class</span>{sp}<span>Class1</span><span>:</span>\n' in result
|
2025-01-14 09:55:02 -06:00
|
|
|
assert (
|
|
|
|
'<span> </span>'
|
|
|
|
'<span>"""this is Class1"""</span></div>\n'
|
|
|
|
) in result
|
2023-07-27 23:52:48 -05:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
@pytest.mark.sphinx(
|
2024-08-12 16:34:03 -05:00
|
|
|
'html',
|
2024-08-11 08:58:56 -05:00
|
|
|
testroot='ext-viewcode',
|
|
|
|
freshenv=True,
|
|
|
|
confoverrides={'viewcode_line_numbers': True},
|
|
|
|
)
|
|
|
|
@pytest.mark.usefixtures('rollback_sysmodules')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_viewcode_linenos(app):
|
2023-07-27 23:52:48 -05:00
|
|
|
shutil.rmtree(app.outdir / '_modules', ignore_errors=True)
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(force_all=True)
|
2023-07-27 23:52:48 -05:00
|
|
|
|
2024-07-23 09:35:55 -05:00
|
|
|
result = check_viewcode_output(app)
|
2023-07-27 23:52:48 -05:00
|
|
|
assert '<span class="linenos"> 1</span>' in result
|
|
|
|
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
@pytest.mark.sphinx(
|
2024-08-12 16:34:03 -05:00
|
|
|
'html',
|
2024-08-11 08:58:56 -05:00
|
|
|
testroot='ext-viewcode',
|
|
|
|
freshenv=True,
|
|
|
|
confoverrides={'viewcode_line_numbers': False},
|
|
|
|
)
|
|
|
|
@pytest.mark.usefixtures('rollback_sysmodules')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_viewcode(app):
|
2023-07-27 23:52:48 -05:00
|
|
|
shutil.rmtree(app.outdir / '_modules', ignore_errors=True)
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(force_all=True)
|
2023-07-27 23:52:48 -05:00
|
|
|
|
2024-07-23 09:35:55 -05:00
|
|
|
result = check_viewcode_output(app)
|
2023-07-27 23:52:48 -05:00
|
|
|
assert 'class="linenos">' not in result
|
2018-03-05 08:46:34 -06:00
|
|
|
|
2014-09-21 10:34:16 -05:00
|
|
|
|
2021-01-21 09:33:39 -06:00
|
|
|
@pytest.mark.sphinx('epub', testroot='ext-viewcode')
|
2024-08-11 08:58:56 -05:00
|
|
|
@pytest.mark.usefixtures('rollback_sysmodules')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_viewcode_epub_default(app):
|
2023-04-06 16:40:33 -05:00
|
|
|
shutil.rmtree(app.outdir)
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(force_all=True)
|
2021-01-21 09:33:39 -06:00
|
|
|
|
|
|
|
assert not (app.outdir / '_modules/spam/mod1.xhtml').exists()
|
|
|
|
|
2022-04-26 21:04:19 -05:00
|
|
|
result = (app.outdir / 'index.xhtml').read_text(encoding='utf8')
|
2021-01-18 12:18:39 -06:00
|
|
|
assert result.count('href="_modules/spam/mod1.xhtml#func1"') == 0
|
|
|
|
|
2021-01-21 09:33:39 -06:00
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
@pytest.mark.sphinx(
|
|
|
|
'epub',
|
|
|
|
testroot='ext-viewcode',
|
|
|
|
confoverrides={'viewcode_enable_epub': True},
|
|
|
|
)
|
|
|
|
@pytest.mark.usefixtures('rollback_sysmodules')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_viewcode_epub_enabled(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(force_all=True)
|
2021-01-21 09:33:39 -06:00
|
|
|
|
|
|
|
assert (app.outdir / '_modules/spam/mod1.xhtml').exists()
|
|
|
|
|
2022-04-26 21:04:19 -05:00
|
|
|
result = (app.outdir / 'index.xhtml').read_text(encoding='utf8')
|
2021-01-18 12:18:39 -06:00
|
|
|
assert result.count('href="_modules/spam/mod1.xhtml#func1"') == 2
|
|
|
|
|
2021-01-21 09:33:39 -06:00
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
@pytest.mark.sphinx('html', testroot='ext-viewcode', tags=['test_linkcode'])
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_linkcode(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(filenames=[app.srcdir / 'objects.rst'])
|
2014-09-21 10:34:16 -05:00
|
|
|
|
2022-04-26 21:04:19 -05:00
|
|
|
stuff = (app.outdir / 'objects.html').read_text(encoding='utf8')
|
2014-09-21 10:34:16 -05:00
|
|
|
|
2024-01-13 22:18:57 -06:00
|
|
|
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
|
2025-01-03 15:28:34 -06:00
|
|
|
assert 'http://foobar/rst/' in stuff
|
2018-04-05 17:40:09 -05:00
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
@pytest.mark.sphinx('html', testroot='ext-viewcode-find', freshenv=True)
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_local_source_files(app):
|
2018-04-05 17:40:09 -05:00
|
|
|
def find_source(app, modname):
|
|
|
|
if modname == 'not_a_package':
|
2024-08-11 08:58:56 -05:00
|
|
|
source = app.srcdir / 'not_a_package/__init__.py'
|
2018-04-05 17:40:09 -05:00
|
|
|
tags = {
|
2018-12-17 05:47:49 -06:00
|
|
|
'func1': ('def', 1, 1),
|
|
|
|
'Class1': ('class', 1, 1),
|
|
|
|
'not_a_package.submodule.func1': ('def', 1, 1),
|
|
|
|
'not_a_package.submodule.Class1': ('class', 1, 1),
|
2018-04-05 17:40:09 -05:00
|
|
|
}
|
|
|
|
else:
|
2024-08-11 08:58:56 -05:00
|
|
|
source = app.srcdir / 'not_a_package/submodule.py'
|
2018-04-05 17:40:09 -05:00
|
|
|
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),
|
|
|
|
}
|
2024-08-11 08:58:56 -05:00
|
|
|
return source.read_text(encoding='utf8'), tags
|
2018-04-05 17:40:09 -05:00
|
|
|
|
|
|
|
app.connect('viewcode-find-source', find_source)
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(force_all=True)
|
2018-04-05 17:40:09 -05:00
|
|
|
|
2024-07-23 09:35:55 -05:00
|
|
|
warnings = re.sub(r'\\+', '/', app.warning.getvalue())
|
2018-04-05 17:40:09 -05:00
|
|
|
assert re.findall(
|
2024-08-10 18:39:35 -05:00
|
|
|
r"index.rst:\d+: WARNING: Object named 'func1' not found in include "
|
2018-04-05 17:40:09 -05:00
|
|
|
r"file .*/not_a_package/__init__.py'",
|
2023-02-17 16:11:14 -06:00
|
|
|
warnings,
|
2018-04-05 17:40:09 -05:00
|
|
|
)
|
|
|
|
|
2022-04-26 21:04:19 -05:00
|
|
|
result = (app.outdir / 'index.html').read_text(encoding='utf8')
|
2024-08-11 08:58:56 -05:00
|
|
|
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
|
2025-01-05 04:43:57 -06:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx('html', testroot='ext-viewcode-find-package', freshenv=True)
|
|
|
|
def test_find_local_package_import_path(app, status, warning):
|
2025-01-18 12:38:59 -06:00
|
|
|
app.build(force_all=True)
|
2025-01-05 04:43:57 -06:00
|
|
|
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
|