From dd557a513463c6eed467a1756566596efd385a92 Mon Sep 17 00:00:00 2001 From: "Albert Y. Shih" Date: Fri, 24 Jun 2022 11:06:42 -0400 Subject: [PATCH] Test for valid links in PNG and SVG inheritance diagrams in multiple situations --- .../test-ext-inheritance_diagram/conf.py | 2 +- .../test-ext-inheritance_diagram/index.rst | 13 ++++ .../subdir/index.rst | 5 ++ .../subdir/other.py | 4 + .../test-ext-inheritance_diagram/test.py | 4 + tests/test_ext_inheritance_diagram.py | 75 ++++++++++++++++++- 6 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 tests/roots/test-ext-inheritance_diagram/subdir/index.rst create mode 100644 tests/roots/test-ext-inheritance_diagram/subdir/other.py diff --git a/tests/roots/test-ext-inheritance_diagram/conf.py b/tests/roots/test-ext-inheritance_diagram/conf.py index 9953494a59f..d3778d5cfb0 100644 --- a/tests/roots/test-ext-inheritance_diagram/conf.py +++ b/tests/roots/test-ext-inheritance_diagram/conf.py @@ -3,4 +3,4 @@ sys.path.insert(0, os.path.abspath('.')) -extensions = ['sphinx.ext.inheritance_diagram'] +extensions = ['sphinx.ext.inheritance_diagram', 'sphinx.ext.intersphinx'] diff --git a/tests/roots/test-ext-inheritance_diagram/index.rst b/tests/roots/test-ext-inheritance_diagram/index.rst index 8e25eee5b65..8c6344cbe31 100644 --- a/tests/roots/test-ext-inheritance_diagram/index.rst +++ b/tests/roots/test-ext-inheritance_diagram/index.rst @@ -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 diff --git a/tests/roots/test-ext-inheritance_diagram/subdir/index.rst b/tests/roots/test-ext-inheritance_diagram/subdir/index.rst new file mode 100644 index 00000000000..50be30f38e6 --- /dev/null +++ b/tests/roots/test-ext-inheritance_diagram/subdir/index.rst @@ -0,0 +1,5 @@ +========================================= +test-ext-inheritance_diagram subdirectory +========================================= + +.. inheritance-diagram:: test.Qux diff --git a/tests/roots/test-ext-inheritance_diagram/subdir/other.py b/tests/roots/test-ext-inheritance_diagram/subdir/other.py new file mode 100644 index 00000000000..f05e91e4b14 --- /dev/null +++ b/tests/roots/test-ext-inheritance_diagram/subdir/other.py @@ -0,0 +1,4 @@ +from test import Alice + +class Bob(Alice): + pass diff --git a/tests/roots/test-ext-inheritance_diagram/test.py b/tests/roots/test-ext-inheritance_diagram/test.py index 7d5cabeefe2..7bd5ca37a35 100644 --- a/tests/roots/test-ext-inheritance_diagram/test.py +++ b/tests/roots/test-ext-inheritance_diagram/test.py @@ -12,3 +12,7 @@ class Baz(Bar): class Qux(Foo): pass + + +class Alice(object): + pass diff --git a/tests/test_ext_inheritance_diagram.py b/tests/test_ext_inheritance_diagram.py index 4f99ef5da94..4e9c8ea3a9d 100644 --- a/tests/test_ext_inheritance_diagram.py +++ b/tests/test_ext_inheritance_diagram.py @@ -9,6 +9,7 @@ from sphinx.ext.inheritance_diagram import (InheritanceDiagram, InheritanceException, import_classes) +from sphinx.ext.intersphinx import load_mappings, normalize_intersphinx_mapping @pytest.mark.sphinx(buildername="html", testroot="inheritance") @@ -133,12 +134,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('', content) if docutils.__version_info__ < (0, 17): pattern = ('
\n' @@ -156,14 +176,44 @@ def test_inheritance_diagram_png_html(app, status, warning): 'title="Permalink to this image">\xb6

\n\n\n') assert re.search(pattern, content, re.M) + subdir_content = (app.outdir / 'subdir/index.html').read_text(encoding='utf8') + subdir_maps = re.findall('', 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('\n' @@ -186,6 +236,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('