|
| 1 | +import re |
| 2 | +from unittest.mock import MagicMock |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from mkdocs_site_urls import SiteUrlsPlugin |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def mock_plugin_config(): |
| 11 | + return {"attributes": ["src", "href"], "prefix": "site:"} |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def create_plugin(mock_plugin_config): |
| 16 | + """Factory function to create the plugin with the prescribed configuration options.""" |
| 17 | + |
| 18 | + def _plugin(config=mock_plugin_config, **kwargs): |
| 19 | + plugin = SiteUrlsPlugin() |
| 20 | + plugin.load_config(config) |
| 21 | + for key, value in kwargs.items(): |
| 22 | + setattr(plugin, key, value) |
| 23 | + return plugin |
| 24 | + |
| 25 | + return _plugin |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.parametrize( |
| 29 | + "attributes, prefix, string_to_match, match_group1, match_group3, should_match", |
| 30 | + [ |
| 31 | + ( |
| 32 | + ["href", "src"], |
| 33 | + "/docs/", |
| 34 | + 'src = "/docs/image.png"', |
| 35 | + "src", |
| 36 | + "image.png", |
| 37 | + True, |
| 38 | + ), # valid1 |
| 39 | + ( |
| 40 | + ["href", "src"], |
| 41 | + "/", |
| 42 | + "href ='/docs/image.png'", |
| 43 | + "href", |
| 44 | + "docs/image.png", |
| 45 | + True, |
| 46 | + ), # valid2 |
| 47 | + ( |
| 48 | + ["data"], |
| 49 | + "site:", |
| 50 | + "data= 'site:image.png'", |
| 51 | + "data", |
| 52 | + "image.png", |
| 53 | + True, |
| 54 | + ), # valid3 |
| 55 | + ( |
| 56 | + ["href", "src"], |
| 57 | + "/docs/", |
| 58 | + 'src = "/image.png"', |
| 59 | + None, |
| 60 | + None, |
| 61 | + False, |
| 62 | + ), # invalid_different_prefix |
| 63 | + ( |
| 64 | + ["href", "src"], |
| 65 | + "/", |
| 66 | + "href ='site:docs/image.png'", |
| 67 | + None, |
| 68 | + None, |
| 69 | + False, |
| 70 | + ), # invalid_different_prefix2 |
| 71 | + ( |
| 72 | + ["data"], |
| 73 | + "/", |
| 74 | + "href='/image.png'", |
| 75 | + None, |
| 76 | + None, |
| 77 | + False, |
| 78 | + ), # invalid_different_attribute |
| 79 | + ], |
| 80 | + ids=[ |
| 81 | + "valid1", |
| 82 | + "valid2", |
| 83 | + "valid3", |
| 84 | + "invalid_different_prefix", |
| 85 | + "invalid_different_prefix2", |
| 86 | + "invalid_different_attribute", |
| 87 | + ], |
| 88 | +) |
| 89 | +def test_on_config_sets_regex( |
| 90 | + create_plugin, |
| 91 | + attributes, |
| 92 | + prefix, |
| 93 | + string_to_match, |
| 94 | + match_group1, |
| 95 | + match_group3, |
| 96 | + should_match, |
| 97 | +): |
| 98 | + """Test the on_config method of the SiteUrlsPlugin.""" |
| 99 | + plugin_config = { |
| 100 | + "attributes": attributes, |
| 101 | + "prefix": prefix, |
| 102 | + } |
| 103 | + plugin = create_plugin(plugin_config) |
| 104 | + plugin.on_config(MagicMock()) |
| 105 | + |
| 106 | + # Check that the regex is compiled |
| 107 | + assert isinstance(plugin._regex, re.Pattern) |
| 108 | + match = plugin._regex.search(string_to_match) |
| 109 | + |
| 110 | + # Check regex is correct |
| 111 | + if should_match: |
| 112 | + assert match is not None |
| 113 | + assert match.group(1) == match_group1 |
| 114 | + assert match.group(3) == match_group3 |
| 115 | + else: |
| 116 | + assert match is None |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.parametrize( |
| 120 | + "site_url", |
| 121 | + [ |
| 122 | + "https://example.com/docs/subpage/", |
| 123 | + "https://example.com/docs/subpage", |
| 124 | + ], |
| 125 | + ids=["trailing_slash", "no_trailing_slash"], |
| 126 | +) |
| 127 | +def test_on_page_content(create_plugin, site_url): |
| 128 | + """Test the on_page_content method of the SiteUrlsPlugin.""" |
| 129 | + plugin = create_plugin( |
| 130 | + { |
| 131 | + "attributes": ["src", "data"], |
| 132 | + "prefix": "prefix", |
| 133 | + } |
| 134 | + ) |
| 135 | + page = MagicMock() |
| 136 | + config = MagicMock() |
| 137 | + config.__getitem__.side_effect = lambda key: site_url if key == "site_url" else None |
| 138 | + files = MagicMock() |
| 139 | + html = """ |
| 140 | + <img src ="prefixexample.png" alt="Image"> |
| 141 | + <img data = \'prefix/image.png\'> |
| 142 | + <img data="site:docs/image.svg" class="example"> |
| 143 | + <img attr ="prefix/docs/image.png" > |
| 144 | + """ |
| 145 | + |
| 146 | + plugin.on_config(config) |
| 147 | + result = plugin.on_page_content(html, page, config, files) |
| 148 | + expected_result = """ |
| 149 | + <img src="/docs/subpage/example.png" alt="Image"> |
| 150 | + <img data="/docs/subpage//image.png"> |
| 151 | + <img data="site:docs/image.svg" class="example"> |
| 152 | + <img attr ="prefix/docs/image.png" > |
| 153 | + """ |
| 154 | + assert result == expected_result |
0 commit comments