Skip to content

Commit

Permalink
tests: Test plugin's "multiple URLs" warnings and use of first URL
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Jan 10, 2025
1 parent 54a02a7 commit 7f45227
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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('<autoref identifier="foo">Foo</autoref>', 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('<autoref identifier="foo">Foo</autoref>', url_mapper, _legacy_refs=False)
assert "Multiple primary URLs found for 'foo': ['foo.html#foo', 'bar.html#foo']" not in caplog.text

0 comments on commit 7f45227

Please sign in to comment.