2016-02-14 00:06:28 -06:00
|
|
|
"""
|
|
|
|
test_ext_inheritance_diagram
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Test sphinx.ext.inheritance_diagram extension.
|
|
|
|
|
2020-12-31 11:00:29 -06:00
|
|
|
:copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS.
|
2016-02-14 00:06:28 -06:00
|
|
|
:license: BSD, see LICENSE for details.
|
|
|
|
"""
|
|
|
|
|
2019-05-06 06:22:06 -05:00
|
|
|
import os
|
2020-11-11 05:00:27 -06:00
|
|
|
import re
|
2016-11-21 09:00:50 -06:00
|
|
|
import sys
|
2017-07-26 14:07:25 -05:00
|
|
|
|
2017-01-03 07:24:00 -06:00
|
|
|
import pytest
|
2016-02-14 00:06:28 -06:00
|
|
|
|
2020-11-11 05:00:27 -06:00
|
|
|
from sphinx.ext.inheritance_diagram import (InheritanceDiagram, InheritanceException,
|
|
|
|
import_classes)
|
2019-05-06 06:22:06 -05:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx(buildername="html", testroot="inheritance")
|
|
|
|
@pytest.mark.usefixtures('if_graphviz_found')
|
|
|
|
def test_inheritance_diagram(app, status, warning):
|
|
|
|
# monkey-patch InheritaceDiagram.run() so we can get access to its
|
|
|
|
# results.
|
|
|
|
orig_run = InheritanceDiagram.run
|
|
|
|
graphs = {}
|
|
|
|
|
|
|
|
def new_run(self):
|
|
|
|
result = orig_run(self)
|
|
|
|
node = result[0]
|
|
|
|
source = os.path.basename(node.document.current_source).replace(".rst", "")
|
|
|
|
graphs[source] = node['graph']
|
|
|
|
return result
|
|
|
|
|
|
|
|
InheritanceDiagram.run = new_run
|
|
|
|
|
|
|
|
try:
|
|
|
|
app.builder.build_all()
|
|
|
|
finally:
|
|
|
|
InheritanceDiagram.run = orig_run
|
|
|
|
|
|
|
|
assert app.statuscode == 0
|
|
|
|
|
|
|
|
html_warnings = warning.getvalue()
|
|
|
|
assert html_warnings == ""
|
|
|
|
|
|
|
|
# note: it is better to split these asserts into separate test functions
|
|
|
|
# but I can't figure out how to build only a specific .rst file
|
|
|
|
|
|
|
|
# basic inheritance diagram showing all classes
|
|
|
|
for cls in graphs['basic_diagram'].class_info:
|
|
|
|
# use in b/c traversing order is different sometimes
|
|
|
|
assert cls in [
|
|
|
|
('dummy.test.A', 'dummy.test.A', [], None),
|
|
|
|
('dummy.test.F', 'dummy.test.F', ['dummy.test.C'], None),
|
|
|
|
('dummy.test.C', 'dummy.test.C', ['dummy.test.A'], None),
|
|
|
|
('dummy.test.E', 'dummy.test.E', ['dummy.test.B'], None),
|
|
|
|
('dummy.test.D', 'dummy.test.D', ['dummy.test.B', 'dummy.test.C'], None),
|
|
|
|
('dummy.test.B', 'dummy.test.B', ['dummy.test.A'], None)
|
|
|
|
]
|
|
|
|
|
|
|
|
# inheritance diagram using :parts: 1 option
|
|
|
|
for cls in graphs['diagram_w_parts'].class_info:
|
|
|
|
assert cls in [
|
|
|
|
('A', 'dummy.test.A', [], None),
|
|
|
|
('F', 'dummy.test.F', ['C'], None),
|
|
|
|
('C', 'dummy.test.C', ['A'], None),
|
|
|
|
('E', 'dummy.test.E', ['B'], None),
|
|
|
|
('D', 'dummy.test.D', ['B', 'C'], None),
|
|
|
|
('B', 'dummy.test.B', ['A'], None)
|
|
|
|
]
|
|
|
|
|
|
|
|
# inheritance diagram with 1 top class
|
|
|
|
# :top-classes: dummy.test.B
|
|
|
|
# rendering should be
|
|
|
|
# A
|
|
|
|
# \
|
|
|
|
# B C
|
|
|
|
# / \ / \
|
|
|
|
# E D F
|
|
|
|
#
|
|
|
|
for cls in graphs['diagram_w_1_top_class'].class_info:
|
|
|
|
assert cls in [
|
|
|
|
('dummy.test.A', 'dummy.test.A', [], None),
|
|
|
|
('dummy.test.F', 'dummy.test.F', ['dummy.test.C'], None),
|
|
|
|
('dummy.test.C', 'dummy.test.C', ['dummy.test.A'], None),
|
|
|
|
('dummy.test.E', 'dummy.test.E', ['dummy.test.B'], None),
|
|
|
|
('dummy.test.D', 'dummy.test.D', ['dummy.test.B', 'dummy.test.C'], None),
|
|
|
|
('dummy.test.B', 'dummy.test.B', [], None)
|
|
|
|
]
|
|
|
|
|
|
|
|
# inheritance diagram with 2 top classes
|
|
|
|
# :top-classes: dummy.test.B, dummy.test.C
|
|
|
|
# Note: we're specifying separate classes, not the entire module here
|
|
|
|
# rendering should be
|
|
|
|
#
|
|
|
|
# B C
|
|
|
|
# / \ / \
|
|
|
|
# E D F
|
|
|
|
#
|
|
|
|
for cls in graphs['diagram_w_2_top_classes'].class_info:
|
|
|
|
assert cls in [
|
|
|
|
('dummy.test.F', 'dummy.test.F', ['dummy.test.C'], None),
|
|
|
|
('dummy.test.C', 'dummy.test.C', [], None),
|
|
|
|
('dummy.test.E', 'dummy.test.E', ['dummy.test.B'], None),
|
|
|
|
('dummy.test.D', 'dummy.test.D', ['dummy.test.B', 'dummy.test.C'], None),
|
|
|
|
('dummy.test.B', 'dummy.test.B', [], None)
|
|
|
|
]
|
|
|
|
|
2020-07-18 19:43:11 -05:00
|
|
|
# inheritance diagram with 2 top classes and specifying the entire module
|
2019-05-06 06:22:06 -05:00
|
|
|
# rendering should be
|
|
|
|
#
|
|
|
|
# A
|
|
|
|
# B C
|
|
|
|
# / \ / \
|
|
|
|
# E D F
|
|
|
|
#
|
|
|
|
# Note: dummy.test.A is included in the graph before its descendants are even processed
|
|
|
|
# b/c we've specified to load the entire module. The way InheritanceGraph works it is very
|
|
|
|
# hard to exclude parent classes once after they have been included in the graph.
|
|
|
|
# If you'd like to not show class A in the graph don't specify the entire module.
|
|
|
|
# this is a known issue.
|
|
|
|
for cls in graphs['diagram_module_w_2_top_classes'].class_info:
|
|
|
|
assert cls in [
|
|
|
|
('dummy.test.F', 'dummy.test.F', ['dummy.test.C'], None),
|
|
|
|
('dummy.test.C', 'dummy.test.C', [], None),
|
|
|
|
('dummy.test.E', 'dummy.test.E', ['dummy.test.B'], None),
|
|
|
|
('dummy.test.D', 'dummy.test.D', ['dummy.test.B', 'dummy.test.C'], None),
|
|
|
|
('dummy.test.B', 'dummy.test.B', [], None),
|
|
|
|
('dummy.test.A', 'dummy.test.A', [], None),
|
|
|
|
]
|
2017-07-26 14:07:25 -05:00
|
|
|
|
2020-02-11 10:42:05 -06:00
|
|
|
# inheritance diagram involving a base class nested within another class
|
|
|
|
for cls in graphs['diagram_w_nested_classes'].class_info:
|
|
|
|
assert cls in [
|
|
|
|
('dummy.test_nested.A', 'dummy.test_nested.A', [], None),
|
|
|
|
('dummy.test_nested.C', 'dummy.test_nested.C', ['dummy.test_nested.A.B'], None),
|
|
|
|
('dummy.test_nested.A.B', 'dummy.test_nested.A.B', [], None)
|
|
|
|
]
|
2017-07-26 14:07:25 -05:00
|
|
|
|
2016-02-14 00:06:28 -06:00
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
@pytest.mark.sphinx('html', testroot='ext-inheritance_diagram')
|
2017-01-03 07:24:00 -06:00
|
|
|
@pytest.mark.usefixtures('if_graphviz_found')
|
2018-04-20 23:59:27 -05:00
|
|
|
def test_inheritance_diagram_png_html(app, status, warning):
|
2016-02-14 00:06:28 -06:00
|
|
|
app.builder.build_all()
|
2016-03-10 22:20:05 -06:00
|
|
|
|
2020-01-31 20:58:51 -06:00
|
|
|
content = (app.outdir / 'index.html').read_text()
|
2016-03-10 22:20:05 -06:00
|
|
|
|
2019-05-06 05:44:36 -05:00
|
|
|
pattern = ('<div class="figure align-default" id="id1">\n'
|
2018-04-20 01:10:30 -05:00
|
|
|
'<div class="graphviz">'
|
2017-02-12 11:02:51 -06:00
|
|
|
'<img src="_images/inheritance-\\w+.png" alt="Inheritance diagram of test.Foo" '
|
2018-08-04 04:38:39 -05:00
|
|
|
'class="inheritance graphviz" /></div>\n<p class="caption">'
|
|
|
|
'<span class="caption-text">Test Foo!</span><a class="headerlink" href="#id1" '
|
2016-03-10 22:20:05 -06:00
|
|
|
'title="Permalink to this image">\xb6</a></p>')
|
|
|
|
assert re.search(pattern, content, re.M)
|
|
|
|
|
|
|
|
|
2018-04-20 23:59:27 -05:00
|
|
|
@pytest.mark.sphinx('html', testroot='ext-inheritance_diagram',
|
|
|
|
confoverrides={'graphviz_output_format': 'svg'})
|
|
|
|
@pytest.mark.usefixtures('if_graphviz_found')
|
|
|
|
def test_inheritance_diagram_svg_html(app, status, warning):
|
|
|
|
app.builder.build_all()
|
|
|
|
|
2020-01-31 20:58:51 -06:00
|
|
|
content = (app.outdir / 'index.html').read_text()
|
2018-04-20 23:59:27 -05:00
|
|
|
|
2019-05-06 05:44:36 -05:00
|
|
|
pattern = ('<div class="figure align-default" id="id1">\n'
|
2018-04-20 23:59:27 -05:00
|
|
|
'<div class="graphviz">'
|
|
|
|
'<object data="_images/inheritance-\\w+.svg" '
|
2018-08-04 04:38:39 -05:00
|
|
|
'type="image/svg\\+xml" class="inheritance graphviz">\n'
|
2018-04-20 23:59:27 -05:00
|
|
|
'<p class=\"warning\">Inheritance diagram of test.Foo</p>'
|
|
|
|
'</object></div>\n<p class="caption"><span class="caption-text">'
|
|
|
|
'Test Foo!</span><a class="headerlink" href="#id1" '
|
|
|
|
'title="Permalink to this image">\xb6</a></p>')
|
|
|
|
assert re.search(pattern, content, re.M)
|
|
|
|
|
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
@pytest.mark.sphinx('latex', testroot='ext-inheritance_diagram')
|
2017-01-03 07:24:00 -06:00
|
|
|
@pytest.mark.usefixtures('if_graphviz_found')
|
2016-03-10 22:20:05 -06:00
|
|
|
def test_inheritance_diagram_latex(app, status, warning):
|
|
|
|
app.builder.build_all()
|
|
|
|
|
2020-01-31 20:58:51 -06:00
|
|
|
content = (app.outdir / 'python.tex').read_text()
|
2016-03-10 22:20:05 -06:00
|
|
|
|
|
|
|
pattern = ('\\\\begin{figure}\\[htbp]\n\\\\centering\n\\\\capstart\n\n'
|
2018-02-11 08:32:59 -06:00
|
|
|
'\\\\sphinxincludegraphics\\[\\]{inheritance-\\w+.pdf}\n'
|
2016-12-08 15:14:48 -06:00
|
|
|
'\\\\caption{Test Foo!}\\\\label{\\\\detokenize{index:id1}}\\\\end{figure}')
|
2016-03-10 22:20:05 -06:00
|
|
|
assert re.search(pattern, content, re.M)
|
2016-11-22 22:22:38 -06:00
|
|
|
|
2016-11-21 09:00:50 -06:00
|
|
|
|
2017-07-26 14:07:25 -05:00
|
|
|
@pytest.mark.sphinx('html', testroot='ext-inheritance_diagram',
|
|
|
|
srcdir='ext-inheritance_diagram-alias')
|
|
|
|
@pytest.mark.usefixtures('if_graphviz_found')
|
|
|
|
def test_inheritance_diagram_latex_alias(app, status, warning):
|
|
|
|
app.config.inheritance_alias = {'test.Foo': 'alias.Foo'}
|
|
|
|
app.builder.build_all()
|
|
|
|
|
|
|
|
doc = app.env.get_and_resolve_doctree('index', app)
|
|
|
|
aliased_graph = doc.children[0].children[3]['graph'].class_info
|
|
|
|
assert len(aliased_graph) == 3
|
|
|
|
assert ('test.Baz', 'test.Baz', ['test.Bar'], None) in aliased_graph
|
|
|
|
assert ('test.Bar', 'test.Bar', ['alias.Foo'], None) in aliased_graph
|
|
|
|
assert ('alias.Foo', 'alias.Foo', [], None) in aliased_graph
|
|
|
|
|
2020-01-31 20:58:51 -06:00
|
|
|
content = (app.outdir / 'index.html').read_text()
|
2017-07-26 14:07:25 -05:00
|
|
|
|
2019-05-06 05:44:36 -05:00
|
|
|
pattern = ('<div class="figure align-default" id="id1">\n'
|
2018-04-20 01:10:30 -05:00
|
|
|
'<div class="graphviz">'
|
2017-07-26 14:07:25 -05:00
|
|
|
'<img src="_images/inheritance-\\w+.png" alt="Inheritance diagram of test.Foo" '
|
2018-08-04 04:38:39 -05:00
|
|
|
'class="inheritance graphviz" /></div>\n<p class="caption">'
|
|
|
|
'<span class="caption-text">Test Foo!</span><a class="headerlink" href="#id1" '
|
2017-07-26 14:07:25 -05:00
|
|
|
'title="Permalink to this image">\xb6</a></p>')
|
|
|
|
assert re.search(pattern, content, re.M)
|
|
|
|
|
|
|
|
|
2017-05-07 02:46:44 -05:00
|
|
|
def test_import_classes(rootdir):
|
2019-03-29 10:16:45 -05:00
|
|
|
from sphinx.parsers import Parser, RSTParser
|
2016-11-22 01:32:03 -06:00
|
|
|
from sphinx.util.i18n import CatalogInfo
|
2016-11-21 09:00:50 -06:00
|
|
|
|
|
|
|
try:
|
2017-05-07 02:46:44 -05:00
|
|
|
sys.path.append(rootdir / 'test-ext-inheritance_diagram')
|
2016-11-22 01:32:03 -06:00
|
|
|
from example.sphinx import DummyClass
|
2016-11-21 09:00:50 -06:00
|
|
|
|
|
|
|
# got exception for unknown class or module
|
2017-01-05 09:46:42 -06:00
|
|
|
with pytest.raises(InheritanceException):
|
|
|
|
import_classes('unknown', None)
|
|
|
|
with pytest.raises(InheritanceException):
|
|
|
|
import_classes('unknown.Unknown', None)
|
2016-11-21 09:00:50 -06:00
|
|
|
|
2018-02-01 13:29:18 -06:00
|
|
|
# got exception InheritanceException for wrong class or module
|
|
|
|
# not AttributeError (refs: #4019)
|
|
|
|
with pytest.raises(InheritanceException):
|
|
|
|
import_classes('unknown', '.')
|
|
|
|
with pytest.raises(InheritanceException):
|
|
|
|
import_classes('unknown.Unknown', '.')
|
|
|
|
with pytest.raises(InheritanceException):
|
|
|
|
import_classes('.', None)
|
|
|
|
|
2016-11-21 09:00:50 -06:00
|
|
|
# a module having no classes
|
|
|
|
classes = import_classes('sphinx', None)
|
|
|
|
assert classes == []
|
|
|
|
|
|
|
|
classes = import_classes('sphinx', 'foo')
|
|
|
|
assert classes == []
|
|
|
|
|
|
|
|
# all of classes in the module
|
2019-03-29 10:16:45 -05:00
|
|
|
classes = import_classes('sphinx.parsers', None)
|
|
|
|
assert set(classes) == {Parser, RSTParser}
|
2016-11-21 09:00:50 -06:00
|
|
|
|
|
|
|
# specified class in the module
|
2019-03-29 10:16:45 -05:00
|
|
|
classes = import_classes('sphinx.parsers.Parser', None)
|
|
|
|
assert classes == [Parser]
|
2016-11-21 09:00:50 -06:00
|
|
|
|
|
|
|
# specified class in current module
|
2019-03-29 10:16:45 -05:00
|
|
|
classes = import_classes('Parser', 'sphinx.parsers')
|
|
|
|
assert classes == [Parser]
|
2016-11-21 09:00:50 -06:00
|
|
|
|
2016-11-22 01:32:03 -06:00
|
|
|
# relative module name to current module
|
|
|
|
classes = import_classes('i18n.CatalogInfo', 'sphinx.util')
|
|
|
|
assert classes == [CatalogInfo]
|
2016-11-21 09:00:50 -06:00
|
|
|
|
|
|
|
# got exception for functions
|
2017-01-05 09:46:42 -06:00
|
|
|
with pytest.raises(InheritanceException):
|
|
|
|
import_classes('encode_uri', 'sphinx.util')
|
2016-11-21 09:00:50 -06:00
|
|
|
|
2016-11-22 01:32:03 -06:00
|
|
|
# import submodule on current module (refs: #3164)
|
2016-11-21 09:00:50 -06:00
|
|
|
classes = import_classes('sphinx', 'example')
|
2016-11-22 01:32:03 -06:00
|
|
|
assert classes == [DummyClass]
|
2016-11-21 09:00:50 -06:00
|
|
|
finally:
|
|
|
|
sys.path.pop()
|