|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import functools |
| 6 | + |
5 | 7 | import pytest |
6 | 8 |
|
7 | | -from mkdocs_autorefs.plugin import AutorefsPlugin |
| 9 | +from mkdocs_autorefs.plugin import AutorefsConfig, AutorefsPlugin |
| 10 | +from mkdocs_autorefs.references import fix_refs |
8 | 11 |
|
9 | 12 |
|
10 | 13 | def test_url_registration() -> None: |
@@ -91,3 +94,26 @@ def test_register_secondary_url() -> None: |
91 | 94 | plugin = AutorefsPlugin() |
92 | 95 | plugin.register_anchor(identifier="foo", page="foo.html", primary=False) |
93 | 96 | 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