Skip to content

Commit 7f45227

Browse files
committed
tests: Test plugin's "multiple URLs" warnings and use of first URL
1 parent 54a02a7 commit 7f45227

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

tests/test_plugin.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
from __future__ import annotations
44

5+
import functools
6+
57
import pytest
68

7-
from mkdocs_autorefs.plugin import AutorefsPlugin
9+
from mkdocs_autorefs.plugin import AutorefsConfig, AutorefsPlugin
10+
from mkdocs_autorefs.references import fix_refs
811

912

1013
def test_url_registration() -> None:
@@ -91,3 +94,26 @@ def test_register_secondary_url() -> None:
9194
plugin = AutorefsPlugin()
9295
plugin.register_anchor(identifier="foo", page="foo.html", primary=False)
9396
assert plugin._secondary_url_map == {"foo": ["foo.html#foo"]}
97+
98+
99+
def test_warn_multiple_urls(caplog: pytest.LogCaptureFixture) -> None:
100+
"""Warn when multiple URLs are found for the same identifier."""
101+
plugin = AutorefsPlugin()
102+
plugin.config = AutorefsConfig()
103+
plugin.register_anchor(identifier="foo", page="foo.html", primary=True)
104+
plugin.register_anchor(identifier="foo", page="bar.html", primary=True)
105+
url_mapper = functools.partial(plugin.get_item_url, from_url="/hello")
106+
fix_refs('<autoref identifier="foo">Foo</autoref>', url_mapper, _legacy_refs=False)
107+
assert "Multiple primary URLs found for 'foo': ['foo.html#foo', 'bar.html#foo']" in caplog.text
108+
109+
110+
def test_use_closest_url(caplog: pytest.LogCaptureFixture) -> None:
111+
"""Use the closest URL when multiple URLs are found for the same identifier."""
112+
plugin = AutorefsPlugin()
113+
plugin.config = AutorefsConfig()
114+
plugin.config.resolve_closest = True
115+
plugin.register_anchor(identifier="foo", page="foo.html", primary=True)
116+
plugin.register_anchor(identifier="foo", page="bar.html", primary=True)
117+
url_mapper = functools.partial(plugin.get_item_url, from_url="/hello")
118+
fix_refs('<autoref identifier="foo">Foo</autoref>', url_mapper, _legacy_refs=False)
119+
assert "Multiple primary URLs found for 'foo': ['foo.html#foo', 'bar.html#foo']" not in caplog.text

0 commit comments

Comments
 (0)