|
| 1 | +import ast |
1 | 2 | import os |
2 | 3 | from pathlib import Path |
3 | 4 | from unittest.mock import patch |
4 | 5 |
|
5 | 6 | import pytest |
| 7 | +import toml |
| 8 | +from sphinx.cmd.build import build_main |
6 | 9 |
|
7 | 10 | import yardang.build as build_module |
8 | 11 | from yardang.build import BUNDLED_THEMES, _resolve_custom_asset, generate_docs_configuration |
@@ -85,6 +88,135 @@ def test_unconfigured_theme_is_fuma(self, tmp_path): |
85 | 88 | assert "sphinx_fuma" in conf |
86 | 89 |
|
87 | 90 |
|
| 91 | +class TestProjectLinks: |
| 92 | + @pytest.fixture |
| 93 | + def project(self, tmp_path, monkeypatch): |
| 94 | + monkeypatch.chdir(tmp_path) |
| 95 | + (tmp_path / "README.md").write_text("# Test Project\n") |
| 96 | + return toml.loads(MINIMAL_PYPROJECT) |
| 97 | + |
| 98 | + def _options(self, project, **kwargs): |
| 99 | + Path("pyproject.toml").write_text(toml.dumps(project)) |
| 100 | + with generate_docs_configuration(**kwargs) as config_dir: |
| 101 | + conf = Path(config_dir, "conf.py").read_text() |
| 102 | + line = next(line for line in conf.splitlines() if line.startswith("html_theme_options = ")) |
| 103 | + return ast.literal_eval(line.split(" = ", 1)[1]) |
| 104 | + |
| 105 | + @pytest.mark.parametrize( |
| 106 | + ("urls", "expected"), |
| 107 | + [ |
| 108 | + ({}, {}), |
| 109 | + ({"Homepage": "https://example.com"}, {}), |
| 110 | + ({"Repository": "https://gitlab.com/owner/repo"}, {}), |
| 111 | + ({"Homepage": "https://owner.github.io/repo"}, {}), |
| 112 | + ({"Repository": "https://github.com.evil.test/owner/repo"}, {}), |
| 113 | + ({"Repository": "https://github.com/owner"}, {}), |
| 114 | + ({"Repository": "ftp://github.com/owner/repo"}, {}), |
| 115 | + ({"Repository": "https://github.com/owner/repo"}, {"github_url": "https://github.com/owner/repo"}), |
| 116 | + ({"Homepage": "https://github.com/owner/repo/"}, {"github_url": "https://github.com/owner/repo"}), |
| 117 | + ({"Source Code": "https://github.com/owner/repo.git"}, {"github_url": "https://github.com/owner/repo"}), |
| 118 | + ({"Bug Tracker": "https://github.com/owner/repo/issues?q=open#top"}, {"github_url": "https://github.com/owner/repo"}), |
| 119 | + ( |
| 120 | + {"Homepage": "https://github.com/owner/home", "rEpOsItOrY": "https://github.com/owner/repo"}, |
| 121 | + {"github_url": "https://github.com/owner/repo"}, |
| 122 | + ), |
| 123 | + ( |
| 124 | + {"Homepage": "https://github.com/owner/home", "Source": "https://github.com/owner/repo"}, |
| 125 | + {"github_url": "https://github.com/owner/repo"}, |
| 126 | + ), |
| 127 | + ( |
| 128 | + {"Repository": "https://example.com/repo", "Homepage": "https://github.com/owner/repo"}, |
| 129 | + {"github_url": "https://github.com/owner/repo"}, |
| 130 | + ), |
| 131 | + ], |
| 132 | + ) |
| 133 | + def test_infers_github_repository(self, project, urls, expected): |
| 134 | + project["project"]["urls"] = urls |
| 135 | + assert self._options(project).get("github_url") == expected.get("github_url") |
| 136 | + |
| 137 | + @pytest.mark.parametrize( |
| 138 | + ("urls", "expected"), |
| 139 | + [ |
| 140 | + ({}, []), |
| 141 | + ({"Homepage": "https://example.com"}, [{"title": "Home", "url": "https://example.com"}]), |
| 142 | + ({"Documentation": "https://docs.example.com"}, [{"title": "Docs", "url": "https://docs.example.com"}]), |
| 143 | + ({"Docs": "https://docs.example.com"}, [{"title": "Docs", "url": "https://docs.example.com"}]), |
| 144 | + ( |
| 145 | + {"hOmEpAgE": "https://example.com", "dOcUmEnTaTiOn": "https://docs.example.com/guide"}, |
| 146 | + [{"title": "Home", "url": "https://example.com"}, {"title": "Docs", "url": "https://docs.example.com/guide"}], |
| 147 | + ), |
| 148 | + ( |
| 149 | + {"Docs": "https://old.example.com", "Documentation": "https://docs.example.com"}, |
| 150 | + [{"title": "Docs", "url": "https://docs.example.com"}], |
| 151 | + ), |
| 152 | + ( |
| 153 | + {"Homepage": "https://example.com/", "Documentation": "https://example.com"}, |
| 154 | + [{"title": "Home", "url": "https://example.com/"}], |
| 155 | + ), |
| 156 | + ({"Homepage": "https://github.com/owner/repo/"}, []), |
| 157 | + ], |
| 158 | + ) |
| 159 | + def test_infers_home_and_docs_links(self, project, urls, expected): |
| 160 | + project["project"]["urls"] = urls |
| 161 | + assert self._options(project).get("nav_links", []) == expected |
| 162 | + |
| 163 | + @pytest.mark.parametrize("nav_links", [[], [{"title": "Blog", "url": "https://example.com/blog"}], "Blog|https://example.com/blog"]) |
| 164 | + @pytest.mark.parametrize("option_key", ["html_theme_options", "html-theme-options"]) |
| 165 | + def test_explicit_navigation_takes_precedence(self, project, nav_links, option_key): |
| 166 | + project["project"]["urls"] = { |
| 167 | + "Repository": "https://github.com/owner/repo", |
| 168 | + "Homepage": "https://example.com", |
| 169 | + "Documentation": "https://docs.example.com", |
| 170 | + } |
| 171 | + project["tool"]["yardang"][option_key] = {"nav_links": nav_links} |
| 172 | + assert self._options(project) == {"nav_links": nav_links, "github_url": "https://github.com/owner/repo"} |
| 173 | + |
| 174 | + @pytest.mark.parametrize("github_url", ["https://github.com/other/repo", ""]) |
| 175 | + @pytest.mark.parametrize("option_key", ["html_theme_options", "html-theme-options"]) |
| 176 | + def test_explicit_options_take_precedence(self, project, github_url, option_key): |
| 177 | + project["project"]["urls"] = {"Repository": "https://github.com/owner/repo", "Homepage": "https://example.com"} |
| 178 | + options = {"github_url": github_url, "toc_style": "clerk"} |
| 179 | + project["tool"]["yardang"][option_key] = options |
| 180 | + assert self._options(project) == {**options, "nav_links": [{"title": "Home", "url": "https://example.com"}]} |
| 181 | + |
| 182 | + @pytest.mark.parametrize("theme", [theme for theme in BUNDLED_THEMES if theme != "fuma"]) |
| 183 | + def test_other_themes_do_not_receive_project_links(self, project, theme): |
| 184 | + project["project"]["urls"] = { |
| 185 | + "Repository": "https://github.com/owner/repo", |
| 186 | + "Homepage": "https://example.com", |
| 187 | + "Documentation": "https://docs.example.com", |
| 188 | + } |
| 189 | + assert self._options(project, theme=theme) == {} |
| 190 | + |
| 191 | + def test_custom_config_base_and_existing_options(self, project): |
| 192 | + project["project"]["urls"] = {"Repository": "https://github.com/owner/repo", "Documentation": "https://docs.example.com"} |
| 193 | + project["tool"]["custom"] = project["tool"].pop("yardang") |
| 194 | + project["tool"]["custom"]["html-theme-options"] = {"toc_style": "clerk"} |
| 195 | + assert self._options(project, config_base="tool.custom") == { |
| 196 | + "toc_style": "clerk", |
| 197 | + "github_url": "https://github.com/owner/repo", |
| 198 | + "nav_links": [{"title": "Docs", "url": "https://docs.example.com"}], |
| 199 | + } |
| 200 | + |
| 201 | + def test_inferred_urls_render_project_links(self, project, tmp_path): |
| 202 | + project["project"]["urls"] = { |
| 203 | + "Repository": "https://github.com/owner/repo", |
| 204 | + "Homepage": "https://example.com", |
| 205 | + "Documentation": "https://docs.example.com", |
| 206 | + } |
| 207 | + self._options(project) |
| 208 | + output = tmp_path / "html" |
| 209 | + with generate_docs_configuration(html_output_dir=str(output)) as config_dir: |
| 210 | + result = build_main(["-q", "-b", "html", "-c", str(config_dir), ".", str(output)]) |
| 211 | + assert result == 0 |
| 212 | + html = (output / "index.html").read_text() |
| 213 | + assert "<title>Test Project" in html |
| 214 | + assert 'href="https://github.com/owner/repo"' in html |
| 215 | + assert 'href="https://github.com/owner/repo/edit/HEAD/index.md"' in html |
| 216 | + assert html.count('href="https://example.com" target="_blank" rel="noopener">Home</a>') == 2 |
| 217 | + assert html.count('href="https://docs.example.com" target="_blank" rel="noopener">Docs</a>') == 2 |
| 218 | + |
| 219 | + |
88 | 220 | class TestResolveCustomAsset: |
89 | 221 | """Layered resolution of bundled and user-supplied CSS/JS.""" |
90 | 222 |
|
|
0 commit comments