2022-02-19 21:05:56 -06:00
|
|
|
"""Test the autosummary extension."""
|
2009-04-13 03:40:56 -05:00
|
|
|
|
2019-02-11 04:22:57 -06:00
|
|
|
import sys
|
2024-10-17 14:59:41 -05:00
|
|
|
from contextlib import chdir
|
2018-12-15 05:03:39 -06:00
|
|
|
from io import StringIO
|
2019-06-06 12:23:39 -05:00
|
|
|
from unittest.mock import Mock, patch
|
2024-09-25 02:57:11 -05:00
|
|
|
from xml.etree.ElementTree import Element
|
2018-12-15 05:03:39 -06:00
|
|
|
|
2018-02-17 10:51:57 -06:00
|
|
|
import pytest
|
2019-05-12 00:13:35 -05:00
|
|
|
from docutils import nodes
|
2014-04-29 07:20:56 -05:00
|
|
|
|
2019-05-12 00:13:35 -05:00
|
|
|
from sphinx import addnodes
|
2023-01-07 12:31:15 -06:00
|
|
|
from sphinx.ext.autosummary import (
|
|
|
|
autosummary_table,
|
|
|
|
autosummary_toc,
|
|
|
|
extract_summary,
|
|
|
|
import_by_name,
|
|
|
|
mangle_signature,
|
|
|
|
)
|
|
|
|
from sphinx.ext.autosummary.generate import (
|
|
|
|
AutosummaryEntry,
|
|
|
|
generate_autosummary_content,
|
|
|
|
generate_autosummary_docs,
|
|
|
|
)
|
2020-11-11 05:00:27 -06:00
|
|
|
from sphinx.ext.autosummary.generate import main as autogen_main
|
2019-05-12 00:13:35 -05:00
|
|
|
from sphinx.testing.util import assert_node, etree_parse
|
2019-03-07 08:35:36 -06:00
|
|
|
from sphinx.util.docutils import new_document
|
2023-03-05 09:25:47 -06:00
|
|
|
|
2014-06-21 03:05:31 -05:00
|
|
|
html_warnfile = StringIO()
|
|
|
|
|
2014-04-11 10:24:53 -05:00
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
defaults = {
|
|
|
|
'extensions': ['sphinx.ext.autosummary'],
|
|
|
|
'autosummary_generate': True,
|
|
|
|
'autosummary_generate_overwrite': False,
|
2014-09-21 10:17:02 -05:00
|
|
|
}
|
2014-04-11 10:24:53 -05:00
|
|
|
|
2009-04-04 14:05:32 -05:00
|
|
|
|
2023-07-28 01:04:35 -05:00
|
|
|
@pytest.fixture(autouse=True)
|
2023-02-17 17:46:31 -06:00
|
|
|
def _unload_target_module():
|
2019-11-13 11:23:06 -06:00
|
|
|
sys.modules.pop('target', None)
|
|
|
|
|
|
|
|
|
2009-04-04 14:05:32 -05:00
|
|
|
def test_mangle_signature():
|
2024-09-25 02:57:11 -05:00
|
|
|
TEST_SIGNATURE = """
|
2009-04-04 14:05:32 -05:00
|
|
|
() :: ()
|
|
|
|
(a, b, c, d, e) :: (a, b, c, d, e)
|
|
|
|
(a, b, c=1, d=2, e=3) :: (a, b[, c, d, e])
|
2009-04-13 03:40:56 -05:00
|
|
|
(a, b, aaa=1, bbb=1, ccc=1, eee=1, fff=1, ggg=1, hhh=1, iii=1, jjj=1)\
|
2009-05-31 12:50:29 -05:00
|
|
|
:: (a, b[, aaa, bbb, ccc, ...])
|
2009-04-04 14:05:32 -05:00
|
|
|
(a, b, c=(), d=<foo>) :: (a, b[, c, d])
|
|
|
|
(a, b, c='foobar()', d=123) :: (a, b[, c, d])
|
2011-06-29 14:26:43 -05:00
|
|
|
(a, b[, c]) :: (a, b[, c])
|
|
|
|
(a, b[, cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]) :: (a, b[, ...)
|
|
|
|
(a, b='c=d, e=f, g=h', c=3) :: (a[, b, c])
|
2019-05-11 03:49:27 -05:00
|
|
|
(a, b="c=d, e=f, g=h", c=3) :: (a[, b, c])
|
2011-06-29 14:26:43 -05:00
|
|
|
(a, b='c=d, \\'e=f,\\' g=h', c=3) :: (a[, b, c])
|
|
|
|
(a, b='c=d, ', e='\\\\' g=h, c=3) :: (a[, b, e, c])
|
|
|
|
(a, b={'c=d, ': 3, '\\\\': 3}) :: (a[, b])
|
|
|
|
(a=1, b=2, c=3) :: ([a, b, c])
|
|
|
|
(a=1, b=<SomeClass: a, b, c>, c=3) :: ([a, b, c])
|
2019-05-11 04:07:50 -05:00
|
|
|
(a=1, b=T(a=1, b=2), c=3) :: ([a, b, c])
|
2021-09-03 11:54:11 -05:00
|
|
|
(a: Tuple[int, str], b: int) -> str :: (a, b)
|
2009-04-04 14:05:32 -05:00
|
|
|
"""
|
|
|
|
|
2024-09-25 02:57:11 -05:00
|
|
|
TEST = [
|
|
|
|
list(map(str.strip, x.split('::')))
|
|
|
|
for x in TEST_SIGNATURE.split('\n')
|
|
|
|
if '::' in x
|
|
|
|
]
|
2009-04-04 14:05:32 -05:00
|
|
|
for inp, outp in TEST:
|
2024-08-11 08:58:56 -05:00
|
|
|
res = mangle_signature(inp).strip().replace('\u00a0', ' ')
|
|
|
|
assert res == outp, f"'{inp}' -> '{res}' != '{outp}'"
|
2014-02-03 21:35:04 -06:00
|
|
|
|
|
|
|
|
2018-03-01 07:21:43 -06:00
|
|
|
def test_extract_summary(capsys):
|
2024-08-11 08:58:56 -05:00
|
|
|
settings = Mock(
|
|
|
|
language_code='en',
|
|
|
|
id_prefix='',
|
|
|
|
auto_id_prefix='',
|
|
|
|
pep_reference=False,
|
|
|
|
rfc_reference=False,
|
|
|
|
)
|
2018-02-17 21:09:24 -06:00
|
|
|
document = new_document('', settings)
|
|
|
|
|
|
|
|
# normal case
|
2024-08-11 08:58:56 -05:00
|
|
|
doc = [
|
|
|
|
'',
|
|
|
|
'This is a first sentence. And second one.',
|
|
|
|
'',
|
|
|
|
'Second block is here',
|
|
|
|
]
|
2018-02-17 21:09:24 -06:00
|
|
|
assert extract_summary(doc, document) == 'This is a first sentence.'
|
|
|
|
|
|
|
|
# inliner case
|
2024-08-11 08:58:56 -05:00
|
|
|
doc = [
|
|
|
|
'This sentence contains *emphasis text having dots.*,',
|
|
|
|
'it does not break sentence.',
|
|
|
|
]
|
2018-02-17 21:09:24 -06:00
|
|
|
assert extract_summary(doc, document) == ' '.join(doc)
|
2018-02-17 10:51:57 -06:00
|
|
|
|
2018-03-01 02:37:16 -06:00
|
|
|
# abbreviations
|
|
|
|
doc = ['Blabla, i.e. bla.']
|
2020-06-27 09:33:43 -05:00
|
|
|
assert extract_summary(doc, document) == ' '.join(doc)
|
2018-03-01 02:37:16 -06:00
|
|
|
|
2023-08-07 16:20:30 -05:00
|
|
|
doc = ['Blabla, (i.e. bla).']
|
|
|
|
assert extract_summary(doc, document) == ' '.join(doc)
|
|
|
|
|
|
|
|
doc = ['Blabla, e.g. bla.']
|
|
|
|
assert extract_summary(doc, document) == ' '.join(doc)
|
|
|
|
|
|
|
|
doc = ['Blabla, (e.g. bla).']
|
|
|
|
assert extract_summary(doc, document) == ' '.join(doc)
|
|
|
|
|
2020-11-27 20:41:04 -06:00
|
|
|
doc = ['Blabla, et al. bla.']
|
|
|
|
assert extract_summary(doc, document) == ' '.join(doc)
|
|
|
|
|
2018-07-08 07:14:01 -05:00
|
|
|
# literal
|
|
|
|
doc = ['blah blah::']
|
|
|
|
assert extract_summary(doc, document) == 'blah blah.'
|
|
|
|
|
2018-07-16 04:44:28 -05:00
|
|
|
# heading
|
2024-08-11 08:58:56 -05:00
|
|
|
doc = [
|
|
|
|
'blah blah',
|
|
|
|
'=========',
|
|
|
|
]
|
2018-07-16 04:44:28 -05:00
|
|
|
assert extract_summary(doc, document) == 'blah blah'
|
2021-08-20 11:05:58 -05:00
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
doc = [
|
|
|
|
'=========',
|
|
|
|
'blah blah',
|
|
|
|
'=========',
|
|
|
|
]
|
2021-08-20 11:05:58 -05:00
|
|
|
assert extract_summary(doc, document) == 'blah blah'
|
2018-07-16 04:44:28 -05:00
|
|
|
|
2020-06-27 09:20:44 -05:00
|
|
|
# hyperlink target
|
2024-08-11 08:58:56 -05:00
|
|
|
doc = ['Do `this <https://www.sphinx-doc.org/>`_ and that. blah blah blah.']
|
|
|
|
extracted = extract_summary(doc, document)
|
|
|
|
assert extracted == 'Do `this <https://www.sphinx-doc.org/>`_ and that.'
|
2020-06-27 09:20:44 -05:00
|
|
|
|
2018-03-01 07:21:43 -06:00
|
|
|
_, err = capsys.readouterr()
|
|
|
|
assert err == ''
|
|
|
|
|
2018-02-17 10:51:57 -06:00
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
@pytest.mark.sphinx('dummy', testroot='autosummary', confoverrides=defaults.copy())
|
2017-06-17 20:25:34 -05:00
|
|
|
def test_get_items_summary(make_app, app_params):
|
|
|
|
import sphinx.ext.autosummary
|
|
|
|
import sphinx.ext.autosummary.generate
|
2024-08-11 08:58:56 -05:00
|
|
|
|
2017-06-17 20:25:34 -05:00
|
|
|
args, kwargs = app_params
|
|
|
|
app = make_app(*args, **kwargs)
|
2018-01-06 09:05:48 -06:00
|
|
|
sphinx.ext.autosummary.generate.setup_documenters(app)
|
2014-02-03 21:35:04 -06:00
|
|
|
# monkey-patch Autosummary.get_items so we can easily get access to it's
|
|
|
|
# results..
|
|
|
|
orig_get_items = sphinx.ext.autosummary.Autosummary.get_items
|
|
|
|
|
|
|
|
autosummary_items = {}
|
|
|
|
|
|
|
|
def new_get_items(self, names, *args, **kwargs):
|
|
|
|
results = orig_get_items(self, names, *args, **kwargs)
|
2024-07-22 09:05:15 -05:00
|
|
|
for name, result in zip(names, results, strict=True):
|
2024-01-03 22:01:52 -06:00
|
|
|
autosummary_items[name] = result # NoQA: PERF403
|
2014-02-03 21:35:04 -06:00
|
|
|
return results
|
|
|
|
|
2014-09-21 10:17:02 -05:00
|
|
|
def handler(app, what, name, obj, options, lines):
|
|
|
|
assert isinstance(lines, list)
|
2018-01-07 20:09:29 -06:00
|
|
|
|
|
|
|
# ensure no docstring is processed twice:
|
|
|
|
assert 'THIS HAS BEEN HANDLED' not in lines
|
|
|
|
lines.append('THIS HAS BEEN HANDLED')
|
2024-08-11 08:58:56 -05:00
|
|
|
|
2014-09-21 10:17:02 -05:00
|
|
|
app.connect('autodoc-process-docstring', handler)
|
|
|
|
|
2014-02-03 21:35:04 -06:00
|
|
|
sphinx.ext.autosummary.Autosummary.get_items = new_get_items
|
|
|
|
try:
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(force_all=True)
|
2014-02-03 21:35:04 -06:00
|
|
|
finally:
|
|
|
|
sphinx.ext.autosummary.Autosummary.get_items = orig_get_items
|
|
|
|
|
2017-06-17 20:25:34 -05:00
|
|
|
html_warnings = app._warning.getvalue()
|
2014-06-21 03:05:31 -05:00
|
|
|
assert html_warnings == ''
|
|
|
|
|
2014-02-03 21:35:04 -06:00
|
|
|
expected_values = {
|
|
|
|
'withSentence': 'I have a sentence which spans multiple lines.',
|
2018-02-17 21:09:24 -06:00
|
|
|
'noSentence': "this doesn't start with a capital.",
|
2024-08-11 08:58:56 -05:00
|
|
|
'emptyLine': 'This is the real summary',
|
2014-04-11 09:38:37 -05:00
|
|
|
'module_attr': 'This is a module attribute',
|
|
|
|
'C.class_attr': 'This is a class attribute',
|
2021-04-29 07:48:56 -05:00
|
|
|
'C.instance_attr': 'This is an instance attribute',
|
2014-04-11 09:38:37 -05:00
|
|
|
'C.prop_attr1': 'This is a function docstring',
|
|
|
|
'C.prop_attr2': 'This is a attribute docstring',
|
2014-06-21 03:05:31 -05:00
|
|
|
'C.C2': 'This is a nested inner class docstring',
|
2014-02-03 21:35:04 -06:00
|
|
|
}
|
2018-09-11 07:50:55 -05:00
|
|
|
for key, expected in expected_values.items():
|
2024-08-11 08:58:56 -05:00
|
|
|
assert (
|
|
|
|
autosummary_items[key][2] == expected
|
|
|
|
), f'Summary for {key} was {autosummary_items[key]!r} - expected {expected!r}'
|
2017-01-21 00:50:41 -06:00
|
|
|
|
|
|
|
# check an item in detail
|
|
|
|
assert 'func' in autosummary_items
|
2024-08-11 08:58:56 -05:00
|
|
|
func_attrs = (
|
|
|
|
'func',
|
|
|
|
'(arg_, *args, **kwargs)',
|
|
|
|
'Test function take an argument ended with underscore.',
|
|
|
|
'dummy_module.func',
|
|
|
|
)
|
2017-01-21 00:50:41 -06:00
|
|
|
assert autosummary_items['func'] == func_attrs
|
2017-03-25 16:16:28 -05:00
|
|
|
|
2017-05-09 07:57:36 -05:00
|
|
|
|
2024-09-25 02:57:11 -05:00
|
|
|
def str_content(elem: Element) -> str:
|
2017-03-25 16:16:28 -05:00
|
|
|
if elem.text is not None:
|
|
|
|
return elem.text
|
|
|
|
else:
|
|
|
|
return ''.join(str_content(e) for e in elem)
|
|
|
|
|
2017-05-09 07:57:36 -05:00
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
@pytest.mark.sphinx('xml', testroot='autosummary', confoverrides=defaults.copy())
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_escaping(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(force_all=True)
|
2017-03-25 16:16:28 -05:00
|
|
|
|
2024-09-17 21:50:27 -05:00
|
|
|
docpage = app.builder.outdir / 'underscore_module_.xml'
|
2017-03-25 16:16:28 -05:00
|
|
|
assert docpage.exists()
|
|
|
|
|
|
|
|
title = etree_parse(docpage).find('section/title')
|
|
|
|
assert str_content(title) == 'underscore_module_'
|
2017-07-02 04:26:41 -05:00
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
@pytest.mark.sphinx('html', testroot='ext-autosummary')
|
2020-05-17 09:07:41 -05:00
|
|
|
def test_autosummary_generate_content_for_module(app):
|
|
|
|
import autosummary_dummy_module
|
2024-08-11 08:58:56 -05:00
|
|
|
|
2020-05-17 09:07:41 -05:00
|
|
|
template = Mock()
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
generate_autosummary_content(
|
|
|
|
'autosummary_dummy_module',
|
|
|
|
autosummary_dummy_module,
|
|
|
|
None,
|
|
|
|
template,
|
|
|
|
None,
|
|
|
|
False,
|
|
|
|
app,
|
|
|
|
False,
|
|
|
|
{},
|
|
|
|
)
|
2020-05-17 09:07:41 -05:00
|
|
|
assert template.render.call_args[0][0] == 'module'
|
|
|
|
|
|
|
|
context = template.render.call_args[0][1]
|
2024-08-11 08:58:56 -05:00
|
|
|
assert context['members'] == [
|
|
|
|
'CONSTANT1',
|
|
|
|
'CONSTANT2',
|
|
|
|
'Exc',
|
|
|
|
'Foo',
|
|
|
|
'_Baz',
|
|
|
|
'_Exc',
|
|
|
|
'__all__',
|
|
|
|
'__builtins__',
|
|
|
|
'__cached__',
|
|
|
|
'__doc__',
|
|
|
|
'__file__',
|
|
|
|
'__name__',
|
|
|
|
'__package__',
|
|
|
|
'_quux',
|
|
|
|
'bar',
|
|
|
|
'non_imported_member',
|
|
|
|
'quuz',
|
|
|
|
'qux',
|
|
|
|
]
|
2020-05-17 09:07:41 -05:00
|
|
|
assert context['functions'] == ['bar']
|
|
|
|
assert context['all_functions'] == ['_quux', 'bar']
|
|
|
|
assert context['classes'] == ['Foo']
|
|
|
|
assert context['all_classes'] == ['Foo', '_Baz']
|
|
|
|
assert context['exceptions'] == ['Exc']
|
|
|
|
assert context['all_exceptions'] == ['Exc', '_Exc']
|
2022-03-14 17:31:33 -05:00
|
|
|
assert context['attributes'] == ['CONSTANT1', 'qux', 'quuz', 'non_imported_member']
|
2024-08-11 08:58:56 -05:00
|
|
|
assert context['all_attributes'] == [
|
|
|
|
'CONSTANT1',
|
|
|
|
'qux',
|
|
|
|
'quuz',
|
|
|
|
'non_imported_member',
|
|
|
|
]
|
2021-11-08 20:14:39 -06:00
|
|
|
assert context['fullname'] == 'autosummary_dummy_module'
|
|
|
|
assert context['module'] == 'autosummary_dummy_module'
|
|
|
|
assert context['objname'] == ''
|
|
|
|
assert context['name'] == ''
|
|
|
|
assert context['objtype'] == 'module'
|
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
@pytest.mark.sphinx('html', testroot='ext-autosummary')
|
2021-11-08 20:14:39 -06:00
|
|
|
def test_autosummary_generate_content_for_module___all__(app):
|
|
|
|
import autosummary_dummy_module
|
2024-08-11 08:58:56 -05:00
|
|
|
|
2021-11-08 20:14:39 -06:00
|
|
|
template = Mock()
|
2021-11-14 20:27:26 -06:00
|
|
|
app.config.autosummary_ignore_module_all = False
|
2021-11-08 20:14:39 -06:00
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
generate_autosummary_content(
|
|
|
|
'autosummary_dummy_module',
|
|
|
|
autosummary_dummy_module,
|
|
|
|
None,
|
|
|
|
template,
|
|
|
|
None,
|
|
|
|
False,
|
|
|
|
app,
|
|
|
|
False,
|
|
|
|
{},
|
|
|
|
)
|
2021-11-08 20:14:39 -06:00
|
|
|
assert template.render.call_args[0][0] == 'module'
|
|
|
|
|
|
|
|
context = template.render.call_args[0][1]
|
2024-08-11 08:58:56 -05:00
|
|
|
assert context['members'] == [
|
|
|
|
'CONSTANT1',
|
|
|
|
'Exc',
|
|
|
|
'Foo',
|
|
|
|
'_Baz',
|
|
|
|
'bar',
|
|
|
|
'qux',
|
|
|
|
'path',
|
|
|
|
]
|
2021-11-08 20:14:39 -06:00
|
|
|
assert context['functions'] == ['bar']
|
|
|
|
assert context['all_functions'] == ['bar']
|
|
|
|
assert context['classes'] == ['Foo']
|
|
|
|
assert context['all_classes'] == ['Foo', '_Baz']
|
|
|
|
assert context['exceptions'] == ['Exc']
|
|
|
|
assert context['all_exceptions'] == ['Exc']
|
2020-05-17 04:20:24 -05:00
|
|
|
assert context['attributes'] == ['CONSTANT1', 'qux']
|
|
|
|
assert context['all_attributes'] == ['CONSTANT1', 'qux']
|
2020-05-17 09:07:41 -05:00
|
|
|
assert context['fullname'] == 'autosummary_dummy_module'
|
|
|
|
assert context['module'] == 'autosummary_dummy_module'
|
|
|
|
assert context['objname'] == ''
|
|
|
|
assert context['name'] == ''
|
|
|
|
assert context['objtype'] == 'module'
|
|
|
|
|
2021-11-21 20:57:56 -06:00
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
@pytest.mark.sphinx('html', testroot='ext-autosummary')
|
2020-05-17 09:07:41 -05:00
|
|
|
def test_autosummary_generate_content_for_module_skipped(app):
|
|
|
|
import autosummary_dummy_module
|
2024-08-11 08:58:56 -05:00
|
|
|
|
2020-05-17 09:07:41 -05:00
|
|
|
template = Mock()
|
|
|
|
|
|
|
|
def skip_member(app, what, name, obj, skip, options):
|
|
|
|
if name in ('Foo', 'bar', 'Exc'):
|
|
|
|
return True
|
2023-02-17 19:53:14 -06:00
|
|
|
return None
|
2020-05-17 09:07:41 -05:00
|
|
|
|
|
|
|
app.connect('autodoc-skip-member', skip_member)
|
2024-08-11 08:58:56 -05:00
|
|
|
generate_autosummary_content(
|
|
|
|
'autosummary_dummy_module',
|
|
|
|
autosummary_dummy_module,
|
|
|
|
None,
|
|
|
|
template,
|
|
|
|
None,
|
|
|
|
False,
|
|
|
|
app,
|
|
|
|
False,
|
|
|
|
{},
|
|
|
|
)
|
2020-05-17 09:07:41 -05:00
|
|
|
context = template.render.call_args[0][1]
|
2024-08-11 08:58:56 -05:00
|
|
|
assert context['members'] == [
|
|
|
|
'CONSTANT1',
|
|
|
|
'CONSTANT2',
|
|
|
|
'_Baz',
|
|
|
|
'_Exc',
|
|
|
|
'__all__',
|
|
|
|
'__builtins__',
|
|
|
|
'__cached__',
|
|
|
|
'__doc__',
|
|
|
|
'__file__',
|
|
|
|
'__name__',
|
|
|
|
'__package__',
|
|
|
|
'_quux',
|
|
|
|
'non_imported_member',
|
|
|
|
'quuz',
|
|
|
|
'qux',
|
|
|
|
]
|
2020-05-17 09:07:41 -05:00
|
|
|
assert context['functions'] == []
|
|
|
|
assert context['classes'] == []
|
|
|
|
assert context['exceptions'] == []
|
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
@pytest.mark.sphinx('html', testroot='ext-autosummary')
|
2020-05-17 09:07:41 -05:00
|
|
|
def test_autosummary_generate_content_for_module_imported_members(app):
|
|
|
|
import autosummary_dummy_module
|
2024-08-11 08:58:56 -05:00
|
|
|
|
2020-05-17 09:07:41 -05:00
|
|
|
template = Mock()
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
generate_autosummary_content(
|
|
|
|
'autosummary_dummy_module',
|
|
|
|
autosummary_dummy_module,
|
|
|
|
None,
|
|
|
|
template,
|
|
|
|
None,
|
|
|
|
True,
|
|
|
|
app,
|
|
|
|
False,
|
|
|
|
{},
|
|
|
|
)
|
2020-05-17 09:07:41 -05:00
|
|
|
assert template.render.call_args[0][0] == 'module'
|
|
|
|
|
|
|
|
context = template.render.call_args[0][1]
|
2024-08-11 08:58:56 -05:00
|
|
|
assert context['members'] == [
|
|
|
|
'CONSTANT1',
|
|
|
|
'CONSTANT2',
|
|
|
|
'Class',
|
|
|
|
'Exc',
|
|
|
|
'Foo',
|
|
|
|
'Union',
|
|
|
|
'_Baz',
|
|
|
|
'_Exc',
|
|
|
|
'__all__',
|
|
|
|
'__builtins__',
|
|
|
|
'__cached__',
|
|
|
|
'__doc__',
|
|
|
|
'__file__',
|
|
|
|
'__loader__',
|
|
|
|
'__name__',
|
|
|
|
'__package__',
|
|
|
|
'__spec__',
|
|
|
|
'_quux',
|
|
|
|
'bar',
|
|
|
|
'considered_as_imported',
|
|
|
|
'non_imported_member',
|
|
|
|
'path',
|
|
|
|
'quuz',
|
|
|
|
'qux',
|
|
|
|
]
|
2020-05-17 09:07:41 -05:00
|
|
|
assert context['functions'] == ['bar']
|
|
|
|
assert context['all_functions'] == ['_quux', 'bar']
|
2022-03-14 17:31:33 -05:00
|
|
|
assert context['classes'] == ['Class', 'Foo']
|
|
|
|
assert context['all_classes'] == ['Class', 'Foo', '_Baz']
|
2020-05-17 09:07:41 -05:00
|
|
|
assert context['exceptions'] == ['Exc']
|
|
|
|
assert context['all_exceptions'] == ['Exc', '_Exc']
|
2022-03-14 17:31:33 -05:00
|
|
|
assert context['attributes'] == ['CONSTANT1', 'qux', 'quuz', 'non_imported_member']
|
2024-08-11 08:58:56 -05:00
|
|
|
assert context['all_attributes'] == [
|
|
|
|
'CONSTANT1',
|
|
|
|
'qux',
|
|
|
|
'quuz',
|
|
|
|
'non_imported_member',
|
|
|
|
]
|
2020-05-17 09:07:41 -05:00
|
|
|
assert context['fullname'] == 'autosummary_dummy_module'
|
|
|
|
assert context['module'] == 'autosummary_dummy_module'
|
|
|
|
assert context['objname'] == ''
|
|
|
|
assert context['name'] == ''
|
|
|
|
assert context['objtype'] == 'module'
|
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
@pytest.mark.sphinx('html', testroot='ext-autosummary')
|
2023-04-06 17:33:43 -05:00
|
|
|
def test_autosummary_generate_content_for_module_imported_members_inherited_module(app):
|
|
|
|
import autosummary_dummy_inherited_module
|
2024-08-11 08:58:56 -05:00
|
|
|
|
2023-04-06 17:33:43 -05:00
|
|
|
template = Mock()
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
generate_autosummary_content(
|
|
|
|
'autosummary_dummy_inherited_module',
|
|
|
|
autosummary_dummy_inherited_module,
|
|
|
|
None,
|
|
|
|
template,
|
|
|
|
None,
|
|
|
|
True,
|
|
|
|
app,
|
|
|
|
False,
|
|
|
|
{},
|
|
|
|
)
|
2023-04-06 17:33:43 -05:00
|
|
|
assert template.render.call_args[0][0] == 'module'
|
|
|
|
|
|
|
|
context = template.render.call_args[0][1]
|
2024-08-11 08:58:56 -05:00
|
|
|
assert context['members'] == [
|
|
|
|
'Foo',
|
|
|
|
'InheritedAttrClass',
|
|
|
|
'__all__',
|
|
|
|
'__builtins__',
|
|
|
|
'__cached__',
|
|
|
|
'__doc__',
|
|
|
|
'__file__',
|
|
|
|
'__loader__',
|
|
|
|
'__name__',
|
|
|
|
'__package__',
|
|
|
|
'__spec__',
|
|
|
|
]
|
2023-04-06 17:33:43 -05:00
|
|
|
assert context['functions'] == []
|
|
|
|
assert context['classes'] == ['Foo', 'InheritedAttrClass']
|
|
|
|
assert context['exceptions'] == []
|
|
|
|
assert context['all_exceptions'] == []
|
|
|
|
assert context['attributes'] == []
|
|
|
|
assert context['all_attributes'] == []
|
|
|
|
assert context['fullname'] == 'autosummary_dummy_inherited_module'
|
|
|
|
assert context['module'] == 'autosummary_dummy_inherited_module'
|
|
|
|
assert context['objname'] == ''
|
|
|
|
assert context['name'] == ''
|
|
|
|
assert context['objtype'] == 'module'
|
|
|
|
|
|
|
|
|
2017-07-02 04:26:41 -05:00
|
|
|
@pytest.mark.sphinx('dummy', testroot='ext-autosummary')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_autosummary_generate(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(force_all=True)
|
2017-07-02 04:26:41 -05:00
|
|
|
|
2019-05-12 00:13:35 -05:00
|
|
|
doctree = app.env.get_doctree('index')
|
2024-08-11 08:58:56 -05:00
|
|
|
assert_node(
|
|
|
|
doctree,
|
|
|
|
(
|
|
|
|
nodes.paragraph,
|
|
|
|
nodes.paragraph,
|
|
|
|
addnodes.tabular_col_spec,
|
|
|
|
autosummary_table,
|
|
|
|
[autosummary_toc, addnodes.toctree],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
assert_node(
|
|
|
|
doctree[3],
|
|
|
|
[
|
|
|
|
autosummary_table,
|
|
|
|
nodes.table,
|
|
|
|
nodes.tgroup,
|
|
|
|
(
|
|
|
|
nodes.colspec,
|
|
|
|
nodes.colspec,
|
|
|
|
[
|
|
|
|
nodes.tbody,
|
|
|
|
(
|
|
|
|
nodes.row,
|
|
|
|
nodes.row,
|
|
|
|
nodes.row,
|
|
|
|
nodes.row,
|
|
|
|
nodes.row,
|
|
|
|
nodes.row,
|
|
|
|
nodes.row,
|
|
|
|
nodes.row,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
assert_node(doctree[4][0], addnodes.toctree, caption='An autosummary')
|
2020-04-16 09:04:57 -05:00
|
|
|
|
2023-04-06 17:33:43 -05:00
|
|
|
assert len(doctree[3][0][0][2]) == 8
|
2019-05-12 00:13:35 -05:00
|
|
|
assert doctree[3][0][0][2][0].astext() == 'autosummary_dummy_module\n\n'
|
|
|
|
assert doctree[3][0][0][2][1].astext() == 'autosummary_dummy_module.Foo()\n\n'
|
2020-03-26 10:30:33 -05:00
|
|
|
assert doctree[3][0][0][2][2].astext() == 'autosummary_dummy_module.Foo.Bar()\n\n'
|
2024-08-11 08:58:56 -05:00
|
|
|
assert doctree[3][0][0][2][3].astext() == (
|
|
|
|
'autosummary_dummy_module.Foo.value\n\ndocstring'
|
|
|
|
)
|
2020-08-01 04:29:45 -05:00
|
|
|
assert doctree[3][0][0][2][4].astext() == 'autosummary_dummy_module.bar(x[, y])\n\n'
|
2024-08-11 08:58:56 -05:00
|
|
|
assert doctree[3][0][0][2][5].astext() == (
|
|
|
|
'autosummary_dummy_module.qux\n\na module-level attribute'
|
|
|
|
)
|
|
|
|
assert doctree[3][0][0][2][6].astext() == (
|
|
|
|
'autosummary_dummy_inherited_module.InheritedAttrClass()\n\n'
|
|
|
|
)
|
|
|
|
assert doctree[3][0][0][2][7].astext() == (
|
|
|
|
'autosummary_dummy_inherited_module.InheritedAttrClass.subclassattr\n\nother docstring'
|
|
|
|
)
|
|
|
|
|
|
|
|
path = app.srcdir / 'generated' / 'autosummary_dummy_module.rst'
|
|
|
|
module = path.read_text(encoding='utf8')
|
|
|
|
|
|
|
|
assert ' .. autosummary::\n \n Foo\n \n' in module
|
|
|
|
assert (
|
|
|
|
' .. autosummary::\n'
|
|
|
|
' \n'
|
|
|
|
' CONSTANT1\n'
|
|
|
|
' qux\n'
|
|
|
|
' quuz\n'
|
|
|
|
' non_imported_member\n'
|
|
|
|
' \n'
|
|
|
|
) in module
|
|
|
|
|
|
|
|
path = app.srcdir / 'generated' / 'autosummary_dummy_module.Foo.rst'
|
|
|
|
Foo = path.read_text(encoding='utf8')
|
2017-07-02 04:26:41 -05:00
|
|
|
assert '.. automethod:: __init__' in Foo
|
2024-08-11 08:58:56 -05:00
|
|
|
assert (
|
|
|
|
' .. autosummary::\n'
|
|
|
|
' \n'
|
|
|
|
' ~Foo.__init__\n'
|
|
|
|
' ~Foo.bar\n'
|
|
|
|
' \n'
|
|
|
|
) in Foo
|
|
|
|
assert (
|
|
|
|
' .. autosummary::\n'
|
|
|
|
' \n'
|
|
|
|
' ~Foo.CONSTANT3\n'
|
|
|
|
' ~Foo.CONSTANT4\n'
|
|
|
|
' ~Foo.baz\n'
|
|
|
|
' ~Foo.value\n'
|
|
|
|
' \n'
|
|
|
|
) in Foo
|
|
|
|
|
|
|
|
path = app.srcdir / 'generated' / 'autosummary_dummy_module.Foo.Bar.rst'
|
|
|
|
FooBar = path.read_text(encoding='utf8')
|
|
|
|
assert (
|
|
|
|
'.. currentmodule:: autosummary_dummy_module\n\n.. autoclass:: Foo.Bar\n'
|
|
|
|
) in FooBar
|
|
|
|
|
|
|
|
path = app.srcdir / 'generated' / 'autosummary_dummy_module.Foo.value.rst'
|
|
|
|
Foo_value = path.read_text(encoding='utf8')
|
|
|
|
assert (
|
|
|
|
'.. currentmodule:: autosummary_dummy_module\n'
|
|
|
|
'\n'
|
|
|
|
'.. autoattribute:: Foo.value'
|
|
|
|
) in Foo_value
|
|
|
|
|
|
|
|
path = app.srcdir / 'generated' / 'autosummary_dummy_module.qux.rst'
|
|
|
|
qux = path.read_text(encoding='utf8')
|
|
|
|
assert '.. currentmodule:: autosummary_dummy_module\n\n.. autodata:: qux' in qux
|
|
|
|
|
|
|
|
path = (
|
|
|
|
app.srcdir
|
|
|
|
/ 'generated'
|
|
|
|
/ 'autosummary_dummy_inherited_module.InheritedAttrClass.rst'
|
|
|
|
)
|
|
|
|
InheritedAttrClass = path.read_text(encoding='utf8')
|
2023-04-06 17:33:43 -05:00
|
|
|
print(InheritedAttrClass)
|
|
|
|
assert '.. automethod:: __init__' in Foo
|
2024-08-11 08:58:56 -05:00
|
|
|
assert (
|
|
|
|
' .. autosummary::\n'
|
|
|
|
' \n'
|
|
|
|
' ~InheritedAttrClass.__init__\n'
|
|
|
|
' ~InheritedAttrClass.bar\n'
|
|
|
|
' \n'
|
|
|
|
) in InheritedAttrClass
|
|
|
|
assert (
|
|
|
|
' .. autosummary::\n'
|
|
|
|
' \n'
|
|
|
|
' ~InheritedAttrClass.CONSTANT3\n'
|
|
|
|
' ~InheritedAttrClass.CONSTANT4\n'
|
|
|
|
' ~InheritedAttrClass.baz\n'
|
|
|
|
' ~InheritedAttrClass.subclassattr\n'
|
|
|
|
' ~InheritedAttrClass.value\n'
|
|
|
|
' \n'
|
|
|
|
) in InheritedAttrClass
|
|
|
|
|
|
|
|
path = (
|
|
|
|
app.srcdir
|
|
|
|
/ 'generated'
|
|
|
|
/ 'autosummary_dummy_inherited_module.InheritedAttrClass.subclassattr.rst'
|
|
|
|
)
|
|
|
|
InheritedAttrClass_subclassattr = path.read_text(encoding='utf8')
|
|
|
|
assert (
|
|
|
|
'.. currentmodule:: autosummary_dummy_inherited_module\n'
|
|
|
|
'\n'
|
|
|
|
'.. autoattribute:: InheritedAttrClass.subclassattr'
|
|
|
|
) in InheritedAttrClass_subclassattr
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx(
|
|
|
|
'dummy',
|
|
|
|
testroot='ext-autosummary',
|
|
|
|
confoverrides={'autosummary_generate_overwrite': False},
|
|
|
|
)
|
2019-06-01 05:38:33 -05:00
|
|
|
def test_autosummary_generate_overwrite1(app_params, make_app):
|
|
|
|
args, kwargs = app_params
|
|
|
|
srcdir = kwargs.get('srcdir')
|
2019-02-08 09:11:29 -06:00
|
|
|
|
2023-07-27 18:39:12 -05:00
|
|
|
(srcdir / 'generated').mkdir(parents=True, exist_ok=True)
|
2024-08-11 08:58:56 -05:00
|
|
|
(srcdir / 'generated' / 'autosummary_dummy_module.rst').write_text('', encoding='utf8') # fmt: skip
|
2019-06-01 05:38:33 -05:00
|
|
|
|
|
|
|
app = make_app(*args, **kwargs)
|
2024-08-11 08:58:56 -05:00
|
|
|
path = srcdir / 'generated' / 'autosummary_dummy_module.rst'
|
|
|
|
content = path.read_text(encoding='utf8')
|
2019-06-01 05:38:33 -05:00
|
|
|
assert content == ''
|
|
|
|
assert 'autosummary_dummy_module.rst' not in app._warning.getvalue()
|
|
|
|
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
@pytest.mark.sphinx(
|
|
|
|
'dummy',
|
|
|
|
testroot='ext-autosummary',
|
|
|
|
confoverrides={'autosummary_generate_overwrite': True},
|
|
|
|
)
|
2019-06-01 05:38:33 -05:00
|
|
|
def test_autosummary_generate_overwrite2(app_params, make_app):
|
2019-02-24 05:13:04 -06:00
|
|
|
args, kwargs = app_params
|
2019-06-01 05:38:33 -05:00
|
|
|
srcdir = kwargs.get('srcdir')
|
|
|
|
|
2023-07-27 18:39:12 -05:00
|
|
|
(srcdir / 'generated').mkdir(parents=True, exist_ok=True)
|
2024-08-11 08:58:56 -05:00
|
|
|
(srcdir / 'generated' / 'autosummary_dummy_module.rst').write_text('', encoding='utf8') # fmt: skip
|
2019-06-01 05:38:33 -05:00
|
|
|
|
|
|
|
app = make_app(*args, **kwargs)
|
2024-08-11 08:58:56 -05:00
|
|
|
path = srcdir / 'generated' / 'autosummary_dummy_module.rst'
|
|
|
|
content = path.read_text(encoding='utf8')
|
2019-06-01 05:38:33 -05:00
|
|
|
assert content != ''
|
|
|
|
assert 'autosummary_dummy_module.rst' not in app._warning.getvalue()
|
|
|
|
|
|
|
|
|
2020-04-21 09:22:05 -05:00
|
|
|
@pytest.mark.sphinx('dummy', testroot='ext-autosummary-recursive')
|
2024-08-11 08:58:56 -05:00
|
|
|
@pytest.mark.usefixtures('rollback_sysmodules')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_autosummary_recursive(app):
|
2020-12-30 19:55:45 -06:00
|
|
|
sys.modules.pop('package', None) # unload target module to clear the module cache
|
|
|
|
|
|
|
|
app.build()
|
|
|
|
|
|
|
|
# autosummary having :recursive: option
|
|
|
|
assert (app.srcdir / 'generated' / 'package.rst').exists()
|
|
|
|
assert (app.srcdir / 'generated' / 'package.module.rst').exists()
|
2024-08-11 08:58:56 -05:00
|
|
|
assert not (app.srcdir / 'generated' / 'package.module_importfail.rst').exists()
|
2020-12-30 19:55:45 -06:00
|
|
|
assert (app.srcdir / 'generated' / 'package.package.rst').exists()
|
|
|
|
assert (app.srcdir / 'generated' / 'package.package.module.rst').exists()
|
|
|
|
|
|
|
|
# autosummary not having :recursive: option
|
|
|
|
assert (app.srcdir / 'generated' / 'package2.rst').exists()
|
2024-08-11 08:58:56 -05:00
|
|
|
assert not (app.srcdir / 'generated' / 'package2.module.rst').exists()
|
2020-12-30 19:55:45 -06:00
|
|
|
|
|
|
|
# Check content of recursively generated stub-files
|
2022-04-26 21:04:19 -05:00
|
|
|
content = (app.srcdir / 'generated' / 'package.rst').read_text(encoding='utf8')
|
2024-07-17 15:42:21 -05:00
|
|
|
assert 'module' in content
|
|
|
|
assert 'package' in content
|
|
|
|
assert 'module_importfail' in content
|
|
|
|
# we no longer generate fully-qualified module names.
|
|
|
|
assert 'package.module' not in content
|
|
|
|
assert 'package.package' not in content
|
|
|
|
assert 'package.module_importfail' not in content
|
2020-12-30 19:55:45 -06:00
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
path = app.srcdir / 'generated' / 'package.package.rst'
|
|
|
|
content = path.read_text(encoding='utf8')
|
2024-07-17 15:42:21 -05:00
|
|
|
assert 'module' in content
|
|
|
|
assert 'package.package.module' not in content
|
|
|
|
|
|
|
|
warnings = app.warning.getvalue()
|
|
|
|
assert 'Summarised items should not include the current module.' not in warnings
|
2020-12-27 06:42:42 -06:00
|
|
|
|
2019-07-08 01:29:23 -05:00
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
@pytest.mark.sphinx(
|
|
|
|
'dummy',
|
|
|
|
testroot='ext-autosummary-recursive',
|
|
|
|
srcdir='test_autosummary_recursive_skips_mocked_modules',
|
|
|
|
confoverrides={'autosummary_mock_imports': ['package.package']},
|
|
|
|
)
|
|
|
|
@pytest.mark.usefixtures('rollback_sysmodules')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_autosummary_recursive_skips_mocked_modules(app):
|
2020-12-30 19:55:45 -06:00
|
|
|
sys.modules.pop('package', None) # unload target module to clear the module cache
|
|
|
|
app.build()
|
2020-12-27 06:42:42 -06:00
|
|
|
|
2020-12-30 19:55:45 -06:00
|
|
|
assert (app.srcdir / 'generated' / 'package.rst').exists()
|
|
|
|
assert (app.srcdir / 'generated' / 'package.module.rst').exists()
|
2024-08-11 08:58:56 -05:00
|
|
|
assert not (app.srcdir / 'generated' / 'package.package.rst').exists()
|
|
|
|
assert not (app.srcdir / 'generated' / 'package.package.module.rst').exists()
|
2019-07-08 01:29:23 -05:00
|
|
|
|
|
|
|
|
2020-07-09 20:30:06 -05:00
|
|
|
@pytest.mark.sphinx('dummy', testroot='ext-autosummary-filename-map')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_autosummary_filename_map(app):
|
2020-07-08 01:45:07 -05:00
|
|
|
app.build()
|
|
|
|
|
2020-07-08 07:36:40 -05:00
|
|
|
assert (app.srcdir / 'generated' / 'module_mangled.rst').exists()
|
|
|
|
assert not (app.srcdir / 'generated' / 'autosummary_dummy_module.rst').exists()
|
|
|
|
assert (app.srcdir / 'generated' / 'bar.rst').exists()
|
|
|
|
assert not (app.srcdir / 'generated' / 'autosummary_dummy_module.bar.rst').exists()
|
|
|
|
assert (app.srcdir / 'generated' / 'autosummary_dummy_module.Foo.rst').exists()
|
2020-07-08 01:45:07 -05:00
|
|
|
|
2020-07-15 03:46:51 -05:00
|
|
|
html_warnings = app._warning.getvalue()
|
|
|
|
assert html_warnings == ''
|
|
|
|
|
2020-07-08 01:45:07 -05:00
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
@pytest.mark.sphinx('latex', testroot='autosummary', confoverrides=defaults.copy())
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_autosummary_latex_table_colspec(app):
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(force_all=True)
|
2024-07-10 15:43:14 -05:00
|
|
|
result = (app.outdir / 'projectnamenotset.tex').read_text(encoding='utf8')
|
2024-07-23 09:35:55 -05:00
|
|
|
print(app.status.getvalue())
|
|
|
|
print(app.warning.getvalue())
|
2023-04-11 13:02:15 -05:00
|
|
|
assert r'\begin{longtable}{\X{1}{2}\X{1}{2}}' in result
|
2018-03-28 03:03:18 -05:00
|
|
|
assert r'p{0.5\linewidth}' not in result
|
|
|
|
|
|
|
|
|
2017-11-11 07:26:12 -06:00
|
|
|
def test_import_by_name():
|
|
|
|
import sphinx
|
|
|
|
import sphinx.ext.autosummary
|
|
|
|
|
|
|
|
prefixed_name, obj, parent, modname = import_by_name('sphinx')
|
|
|
|
assert prefixed_name == 'sphinx'
|
|
|
|
assert obj is sphinx
|
|
|
|
assert parent is None
|
|
|
|
assert modname == 'sphinx'
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
prefixed_name, obj, parent, modname = import_by_name(
|
|
|
|
'sphinx.ext.autosummary.__name__'
|
|
|
|
)
|
2017-11-11 07:26:12 -06:00
|
|
|
assert prefixed_name == 'sphinx.ext.autosummary.__name__'
|
|
|
|
assert obj is sphinx.ext.autosummary.__name__
|
|
|
|
assert parent is sphinx.ext.autosummary
|
|
|
|
assert modname == 'sphinx.ext.autosummary'
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
prefixed_name, obj, parent, modname = import_by_name(
|
|
|
|
'sphinx.ext.autosummary.Autosummary.get_items'
|
|
|
|
)
|
2017-11-11 07:26:12 -06:00
|
|
|
assert prefixed_name == 'sphinx.ext.autosummary.Autosummary.get_items'
|
|
|
|
assert obj == sphinx.ext.autosummary.Autosummary.get_items
|
|
|
|
assert parent is sphinx.ext.autosummary.Autosummary
|
|
|
|
assert modname == 'sphinx.ext.autosummary'
|
2019-02-11 04:22:57 -06:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx('dummy', testroot='ext-autosummary-mock_imports')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_autosummary_mock_imports(app):
|
2019-02-11 04:22:57 -06:00
|
|
|
try:
|
|
|
|
app.build()
|
2024-07-23 09:35:55 -05:00
|
|
|
assert app.warning.getvalue() == ''
|
2019-02-11 04:22:57 -06:00
|
|
|
|
|
|
|
# generated/foo is generated successfully
|
|
|
|
assert app.env.get_doctree('generated/foo')
|
|
|
|
finally:
|
|
|
|
sys.modules.pop('foo', None) # unload foo module
|
2019-03-27 08:48:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx('dummy', testroot='ext-autosummary-imported_members')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_autosummary_imported_members(app):
|
2019-03-27 08:48:26 -05:00
|
|
|
try:
|
|
|
|
app.build()
|
|
|
|
# generated/foo is generated successfully
|
|
|
|
assert app.env.get_doctree('generated/autosummary_dummy_package')
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
path = app.srcdir / 'generated' / 'autosummary_dummy_package.rst'
|
|
|
|
module = path.read_text(encoding='utf8')
|
|
|
|
assert ' .. autosummary::\n \n Bar\n ' in module
|
|
|
|
assert ' .. autosummary::\n \n foo\n ' in module
|
2019-03-27 08:48:26 -05:00
|
|
|
finally:
|
|
|
|
sys.modules.pop('autosummary_dummy_package', None)
|
2019-06-06 12:23:39 -05:00
|
|
|
|
|
|
|
|
2023-04-05 18:02:37 -05:00
|
|
|
@pytest.mark.sphinx('dummy', testroot='ext-autosummary-module_all')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_autosummary_module_all(app):
|
2023-04-05 18:02:37 -05:00
|
|
|
try:
|
|
|
|
app.build()
|
|
|
|
# generated/foo is generated successfully
|
|
|
|
assert app.env.get_doctree('generated/autosummary_dummy_package_all')
|
2024-08-11 08:58:56 -05:00
|
|
|
module = (
|
|
|
|
app.srcdir / 'generated' / 'autosummary_dummy_package_all.rst'
|
|
|
|
).read_text(encoding='utf8')
|
|
|
|
assert ' .. autosummary::\n \n PublicBar\n \n' in module
|
|
|
|
assert (
|
|
|
|
' .. autosummary::\n'
|
|
|
|
' \n'
|
|
|
|
' public_foo\n'
|
|
|
|
' public_baz\n'
|
|
|
|
' \n'
|
|
|
|
) in module
|
|
|
|
assert (
|
|
|
|
'.. autosummary::\n'
|
|
|
|
' :toctree:\n'
|
|
|
|
' :recursive:\n\n'
|
|
|
|
' extra_dummy_module\n'
|
|
|
|
) in module
|
2023-04-05 18:02:37 -05:00
|
|
|
finally:
|
|
|
|
sys.modules.pop('autosummary_dummy_package_all', None)
|
|
|
|
|
|
|
|
|
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-autodoc',
|
|
|
|
confoverrides={'extensions': ['sphinx.ext.autosummary']},
|
|
|
|
)
|
2019-06-06 12:23:39 -05:00
|
|
|
def test_generate_autosummary_docs_property(app):
|
|
|
|
with patch('sphinx.ext.autosummary.generate.find_autosummary_in_files') as mock:
|
2024-08-11 08:58:56 -05:00
|
|
|
mock.return_value = [
|
|
|
|
AutosummaryEntry('target.methods.Base.prop', 'prop', None, False)
|
|
|
|
]
|
2020-05-03 09:41:54 -05:00
|
|
|
generate_autosummary_docs([], output_dir=app.srcdir, app=app)
|
2019-06-06 12:23:39 -05:00
|
|
|
|
2022-04-26 21:04:19 -05:00
|
|
|
content = (app.srcdir / 'target.methods.Base.prop.rst').read_text(encoding='utf8')
|
2024-08-11 08:58:56 -05:00
|
|
|
assert content == (
|
|
|
|
'target.methods.Base.prop\n'
|
|
|
|
'========================\n'
|
|
|
|
'\n'
|
|
|
|
'.. currentmodule:: target.methods\n'
|
|
|
|
'\n'
|
|
|
|
'.. autoproperty:: Base.prop'
|
|
|
|
)
|
2019-06-18 11:54:37 -05:00
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
@pytest.mark.sphinx('html', testroot='ext-autosummary-skip-member')
|
2019-11-13 11:23:06 -06:00
|
|
|
def test_autosummary_skip_member(app):
|
|
|
|
app.build()
|
|
|
|
|
2022-04-26 21:04:19 -05:00
|
|
|
content = (app.srcdir / 'generate' / 'target.Foo.rst').read_text(encoding='utf8')
|
2019-11-13 11:23:06 -06:00
|
|
|
assert 'Foo.skipmeth' not in content
|
2019-11-16 03:15:53 -06:00
|
|
|
assert 'Foo._privatemeth' in content
|
2019-11-13 11:23:06 -06:00
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
@pytest.mark.sphinx('html', testroot='ext-autosummary-template')
|
2020-04-29 10:02:12 -05:00
|
|
|
def test_autosummary_template(app):
|
|
|
|
app.build()
|
|
|
|
|
2022-04-26 21:04:19 -05:00
|
|
|
content = (app.srcdir / 'generate' / 'target.Foo.rst').read_text(encoding='utf8')
|
2020-04-29 10:02:12 -05:00
|
|
|
assert 'EMPTY' in content
|
|
|
|
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
@pytest.mark.sphinx(
|
|
|
|
'dummy',
|
|
|
|
testroot='ext-autosummary',
|
|
|
|
confoverrides={'autosummary_generate': []},
|
|
|
|
)
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_empty_autosummary_generate(app):
|
2019-06-18 11:54:37 -05:00
|
|
|
app.build()
|
2024-08-11 08:58:56 -05:00
|
|
|
assert (
|
|
|
|
'WARNING: autosummary: failed to import autosummary_importfail'
|
|
|
|
) in app.warning.getvalue()
|
2019-06-21 11:34:47 -05:00
|
|
|
|
2019-06-30 00:55:22 -05:00
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
@pytest.mark.sphinx(
|
|
|
|
'dummy',
|
|
|
|
testroot='ext-autosummary',
|
|
|
|
confoverrides={'autosummary_generate': ['unknown']},
|
|
|
|
)
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_invalid_autosummary_generate(app):
|
2024-08-11 08:58:56 -05:00
|
|
|
assert (
|
|
|
|
'WARNING: autosummary_generate: file not found: unknown.rst'
|
|
|
|
) in app.warning.getvalue()
|
2020-04-22 11:59:17 -05:00
|
|
|
|
|
|
|
|
2023-07-27 18:39:12 -05:00
|
|
|
def test_autogen(rootdir, tmp_path):
|
2023-03-05 09:25:47 -06:00
|
|
|
with chdir(rootdir / 'test-templating'):
|
2023-07-27 18:39:12 -05:00
|
|
|
args = ['-o', str(tmp_path), '-t', '.', 'autosummary_templating.txt']
|
2020-04-22 11:59:17 -05:00
|
|
|
autogen_main(args)
|
2023-07-27 18:39:12 -05:00
|
|
|
assert (tmp_path / 'sphinx.application.TemplateBridge.rst').exists()
|
2024-06-21 12:29:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_autogen_remove_old(rootdir, tmp_path):
|
|
|
|
"""Test the ``--remove-old`` option."""
|
2024-07-29 12:21:01 -05:00
|
|
|
tmp_path.joinpath('other.rst').write_text('old content', encoding='utf-8')
|
2024-06-21 12:29:46 -05:00
|
|
|
with chdir(rootdir / 'test-templating'):
|
|
|
|
args = ['-o', str(tmp_path), '-t', '.', 'autosummary_templating.txt']
|
|
|
|
autogen_main(args)
|
|
|
|
assert set(tmp_path.iterdir()) == {
|
|
|
|
tmp_path / 'sphinx.application.TemplateBridge.rst',
|
2024-08-11 08:58:56 -05:00
|
|
|
tmp_path / 'other.rst',
|
2024-06-21 12:29:46 -05:00
|
|
|
}
|
|
|
|
autogen_main([*args, '--remove-old'])
|
2024-08-11 08:58:56 -05:00
|
|
|
assert set(tmp_path.iterdir()) == {
|
|
|
|
tmp_path / 'sphinx.application.TemplateBridge.rst'
|
|
|
|
}
|