From 7f4522775edec4cd009463a560070c03a34c6976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Fri, 10 Jan 2025 17:15:49 +0100 Subject: [PATCH] tests: Test plugin's "multiple URLs" warnings and use of first URL --- tests/test_plugin.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 55ebffd..04c6f1e 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -2,9 +2,12 @@ from __future__ import annotations +import functools + import pytest -from mkdocs_autorefs.plugin import AutorefsPlugin +from mkdocs_autorefs.plugin import AutorefsConfig, AutorefsPlugin +from mkdocs_autorefs.references import fix_refs def test_url_registration() -> None: @@ -91,3 +94,26 @@ def test_register_secondary_url() -> None: plugin = AutorefsPlugin() plugin.register_anchor(identifier="foo", page="foo.html", primary=False) assert plugin._secondary_url_map == {"foo": ["foo.html#foo"]} + + +def test_warn_multiple_urls(caplog: pytest.LogCaptureFixture) -> None: + """Warn when multiple URLs are found for the same identifier.""" + plugin = AutorefsPlugin() + plugin.config = AutorefsConfig() + plugin.register_anchor(identifier="foo", page="foo.html", primary=True) + plugin.register_anchor(identifier="foo", page="bar.html", primary=True) + url_mapper = functools.partial(plugin.get_item_url, from_url="/hello") + fix_refs('Foo', url_mapper, _legacy_refs=False) + assert "Multiple primary URLs found for 'foo': ['foo.html#foo', 'bar.html#foo']" in caplog.text + + +def test_use_closest_url(caplog: pytest.LogCaptureFixture) -> None: + """Use the closest URL when multiple URLs are found for the same identifier.""" + plugin = AutorefsPlugin() + plugin.config = AutorefsConfig() + plugin.config.resolve_closest = True + plugin.register_anchor(identifier="foo", page="foo.html", primary=True) + plugin.register_anchor(identifier="foo", page="bar.html", primary=True) + url_mapper = functools.partial(plugin.get_item_url, from_url="/hello") + fix_refs('Foo', url_mapper, _legacy_refs=False) + assert "Multiple primary URLs found for 'foo': ['foo.html#foo', 'bar.html#foo']" not in caplog.text