Skip to content

Commit

Permalink
Test for valid links in PNG and SVG inheritance diagrams in multiple …
Browse files Browse the repository at this point in the history
…situations
  • Loading branch information
ayshih committed Feb 7, 2023
1 parent ab63155 commit d94de3d
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tests/roots/test-ext-inheritance_diagram/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

sys.path.insert(0, os.path.abspath('.'))

extensions = ['sphinx.ext.inheritance_diagram']
extensions = ['sphinx.ext.inheritance_diagram', 'sphinx.ext.intersphinx']
13 changes: 13 additions & 0 deletions tests/roots/test-ext-inheritance_diagram/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,16 @@ test-ext-inheritance_diagram
:caption: Test Foo!

.. inheritance-diagram:: test.Baz

.. py:class:: test.Bar
.. py:class:: test.Baz
.. py:class:: test.Qux
.. inheritance-diagram:: subdir.other.Bob

.. py:class:: test.Alice
.. toctree::
subdir/index
5 changes: 5 additions & 0 deletions tests/roots/test-ext-inheritance_diagram/subdir/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
=========================================
test-ext-inheritance_diagram subdirectory
=========================================

.. inheritance-diagram:: test.Qux
5 changes: 5 additions & 0 deletions tests/roots/test-ext-inheritance_diagram/subdir/other.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from test import Alice


class Bob(Alice):
pass
4 changes: 4 additions & 0 deletions tests/roots/test-ext-inheritance_diagram/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ class Baz(Bar):

class Qux(Foo):
pass


class Alice(object):
pass
75 changes: 73 additions & 2 deletions tests/test_ext_inheritance_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
InheritanceException,
import_classes,
)
from sphinx.ext.intersphinx import load_mappings, normalize_intersphinx_mapping


@pytest.mark.sphinx(buildername="html", testroot="inheritance")
Expand Down Expand Up @@ -135,12 +136,31 @@ def new_run(self):
]


# An external inventory to test intersphinx links in inheritance diagrams
subdir_inventory = b'''\
# Sphinx inventory version 1
# Project: subdir
# Version: 1.0
subdir.other.Bob class foo.html
'''


@pytest.mark.sphinx('html', testroot='ext-inheritance_diagram')
@pytest.mark.usefixtures('if_graphviz_found')
def test_inheritance_diagram_png_html(app, status, warning):
def test_inheritance_diagram_png_html(tempdir, app, status, warning):
inv_file = tempdir / 'inventory'
inv_file.write_bytes(subdir_inventory)
app.config.intersphinx_mapping = {
'https://example.org': inv_file,
}
app.config.intersphinx_cache_limit = 0
normalize_intersphinx_mapping(app, app.config)
load_mappings(app)

app.builder.build_all()

content = (app.outdir / 'index.html').read_text(encoding='utf8')
base_maps = re.findall('<map .+\n.+\n</map>', content)

pattern = ('<figure class="align-default" id="id1">\n'
'<div class="graphviz">'
Expand All @@ -150,14 +170,44 @@ def test_inheritance_diagram_png_html(app, status, warning):
'title="Permalink to this image">\xb6</a></p>\n</figcaption>\n</figure>\n')
assert re.search(pattern, content, re.M)

subdir_content = (app.outdir / 'subdir/index.html').read_text(encoding='utf8')
subdir_maps = re.findall('<map .+\n.+\n</map>', subdir_content)
subdir_maps = [re.sub('href="(\\S+)"', 'href="subdir/\\g<1>"', s) for s in subdir_maps]

# Go through the clickmap for every PNG inheritance diagram
for diagram_content in base_maps + subdir_maps:
# Verify that an intersphinx link was created via the external inventory
if 'subdir.' in diagram_content:
assert "https://example.org" in diagram_content

# Extract every link in the inheritance diagram
for href in re.findall('href="(\\S+?)"', diagram_content):
if '://' in href:
# Verify that absolute URLs are not prefixed with ../
assert href.startswith("https://example.org/")
else:
# Verify that relative URLs point to existing documents
reluri = href.rsplit('#', 1)[0] # strip the anchor at the end
assert (app.outdir / reluri).exists()


@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):
def test_inheritance_diagram_svg_html(tempdir, app, status, warning):
inv_file = tempdir / 'inventory'
inv_file.write_bytes(subdir_inventory)
app.config.intersphinx_mapping = {
"subdir": ('https://example.org', inv_file),
}
app.config.intersphinx_cache_limit = 0
normalize_intersphinx_mapping(app, app.config)
load_mappings(app)

app.builder.build_all()

content = (app.outdir / 'index.html').read_text(encoding='utf8')
base_svgs = re.findall('<object data="(_images/inheritance-\\w+.svg?)"', content)

pattern = ('<figure class="align-default" id="id1">\n'
'<div class="graphviz">'
Expand All @@ -170,6 +220,27 @@ def test_inheritance_diagram_svg_html(app, status, warning):

assert re.search(pattern, content, re.M)

subdir_content = (app.outdir / 'subdir/index.html').read_text(encoding='utf8')
subdir_svgs = re.findall('<object data="../(_images/inheritance-\\w+.svg?)"', subdir_content)

# Go through every SVG inheritance diagram
for diagram in base_svgs + subdir_svgs:
diagram_content = (app.outdir / diagram).read_text(encoding='utf8')

# Verify that an intersphinx link was created via the external inventory
if 'subdir.' in diagram_content:
assert "https://example.org" in diagram_content

# Extract every link in the inheritance diagram
for href in re.findall('href="(\\S+?)"', diagram_content):
if '://' in href:
# Verify that absolute URLs are not prefixed with ../
assert href.startswith("https://example.org/")
else:
# Verify that relative URLs point to existing documents
reluri = href.rsplit('#', 1)[0] # strip the anchor at the end
assert (app.outdir / app.builder.imagedir / reluri).exists()


@pytest.mark.sphinx('latex', testroot='ext-inheritance_diagram')
@pytest.mark.usefixtures('if_graphviz_found')
Expand Down

0 comments on commit d94de3d

Please sign in to comment.