" link shortening."""
sphinx_build = sphinx_build_factory("base").build()
+ codeberg = sphinx_build.html_tree("page1.html").select(".codeberg-container")[0]
+ file_regression.check(
+ codeberg.prettify(), basename="codeberg_links", extension=".html"
+ )
+
+ forgejo = sphinx_build.html_tree("page1.html").select(".forgejo-container")[0]
+ file_regression.check(
+ forgejo.prettify(), basename="forgejo_links", extension=".html"
+ )
+
+ gitea = sphinx_build.html_tree("page1.html").select(".gitea-container")[0]
+ file_regression.check(gitea.prettify(), basename="gitea_links", extension=".html")
+
github = sphinx_build.html_tree("page1.html").select(".github-container")[0]
file_regression.check(github.prettify(), basename="github_links", extension=".html")
diff --git a/tests/test_build/codeberg_links.html b/tests/test_build/codeberg_links.html
new file mode 100644
index 0000000000..e0e5aef0d2
--- /dev/null
+++ b/tests/test_build/codeberg_links.html
@@ -0,0 +1,46 @@
+
diff --git a/tests/test_build/forgejo_links.html b/tests/test_build/forgejo_links.html
new file mode 100644
index 0000000000..7f3ff90114
--- /dev/null
+++ b/tests/test_build/forgejo_links.html
@@ -0,0 +1,46 @@
+
diff --git a/tests/test_build/gitea_links.html b/tests/test_build/gitea_links.html
new file mode 100644
index 0000000000..3611780a54
--- /dev/null
+++ b/tests/test_build/gitea_links.html
@@ -0,0 +1,46 @@
+
diff --git a/tests/test_edit.py b/tests/test_edit.py
new file mode 100644
index 0000000000..6ad9db70dd
--- /dev/null
+++ b/tests/test_edit.py
@@ -0,0 +1,70 @@
+"""Edit button unit tests."""
+
+import pytest
+
+from pydata_sphinx_theme.edit_this_page import adjust_forge_params
+
+
+@pytest.fixture
+def default_forge_urls():
+ """Setup default edit URLs."""
+ return {
+ "forgejo_url": "https://codeberg.org",
+ "gitea_url": "https://gitea.com",
+ "gitlab_url": "https://gitlab.com",
+ }
+
+
+@pytest.mark.parametrize(
+ "html_theme_options,modified_urls",
+ [
+ (
+ {"forgejo_url": "https://my-forgejo.com/f-proj/f-repo"},
+ {"forgejo_url": "https://my-forgejo.com"},
+ ),
+ (
+ {"gitea_url": "https://my-gitea.com/g-proj/g-repo"},
+ {"gitea_url": "https://my-gitea.com"},
+ ),
+ (
+ {
+ "gitlab_url": "https://my-gitlab.org/gl-proj/gl-repo",
+ "forgejo_url": "https://my-forgejo.com/f-proj/f-repo",
+ },
+ {
+ "forgejo_url": "https://my-forgejo.com",
+ "gitlab_url": "https://my-gitlab.org",
+ },
+ ),
+ ],
+)
+def test_adjust_forge_params_replace_urls(
+ default_forge_urls, html_theme_options, modified_urls
+):
+ """Unit test for adjust_forge_params() url replacing."""
+ forge_urls, forge_labels = adjust_forge_params(
+ default_forge_urls, {}, html_theme_options
+ )
+ expected_urls = default_forge_urls | modified_urls
+
+ assert forge_urls == expected_urls
+ assert forge_labels == {}
+
+
+@pytest.mark.parametrize(
+ "html_theme_options,expected_labels",
+ [
+ ({"forgejo_url": "https://noreplace.com"}, {}),
+ ({"forgejo_url": "https://codeberg.org"}, {"forgejo": "Codeberg"}),
+ ],
+)
+def test_adjust_forge_params_relabel(
+ default_forge_urls, html_theme_options, expected_labels
+):
+ """Unit test for adjust_forge_params() relabeling."""
+ forge_urls, forge_labels = adjust_forge_params(
+ default_forge_urls, {}, html_theme_options
+ )
+
+ assert forge_urls == default_forge_urls
+ assert forge_labels == expected_labels
diff --git a/tests/test_short_url.py b/tests/test_short_url.py
index e5f92bdc83..7c2de68a8e 100644
--- a/tests/test_short_url.py
+++ b/tests/test_short_url.py
@@ -89,6 +89,54 @@ class Mock:
"https://gitlab.com/gitlab-com/gl-infra/production/-/issues/6788",
"gitlab-com/gl-infra/production#6788",
),
+ # codeberg
+ (
+ "codeberg",
+ "https://codeberg.org/",
+ "codeberg",
+ ),
+ (
+ "codeberg",
+ "https://codeberg.org/f-org/f-proj/issues/42",
+ "f-org/f-proj#42",
+ ),
+ # forgejo
+ (
+ "forgejo",
+ "https://my-forgejo.com/",
+ "forgejo",
+ ),
+ (
+ "forgejo",
+ "https://forgejo-host.org/f-org/f-proj/issues/42",
+ "f-org/f-proj#42",
+ ),
+ (
+ "forgejo",
+ "https://forgejo-host.org/f-org/f-proj/pulls/43",
+ "f-org/f-proj#43",
+ ),
+ # gitea
+ (
+ "gitea",
+ "https://gitea.com",
+ "gitea",
+ ),
+ (
+ "gitea",
+ "https://my-gitea.com/",
+ "gitea",
+ ),
+ (
+ "gitea",
+ "https://gitea.com/gitea-org/g-proj/issues/42",
+ "gitea-org/g-proj#42",
+ ),
+ (
+ "gitea",
+ "https://gitea.com/gitea-org/g-proj/pulls/43",
+ "gitea-org/g-proj#43",
+ ),
],
)
def test_shorten(platform, url, expected):
@@ -107,3 +155,73 @@ def test_shorten(platform, url, expected):
URI = urlparse(url)
assert sl.parse_url(URI) == expected
+
+
+@pytest.fixture(scope="session")
+def shortener():
+ """Setup ShortenLinkTransform object for testing."""
+ document = Mock()
+ document.settings = Mock()
+ document.settings.language_code = "en"
+ document.reporter = None
+ return ShortenLinkTransform(document)
+
+
+@pytest.mark.parametrize(
+ "url,html_options,expected",
+ [
+ # forgejo
+ (
+ "https://forgejo-instance.com/f-org/f-proj/pulls/43",
+ {},
+ "forgejo",
+ ),
+ (
+ "https://forgejo-instance.com/f-org/",
+ {},
+ None,
+ ),
+ (
+ "https://forgejo-instance.com/f-org/",
+ {"forgejo_url": "https://forgejo-instance.com/f-org/f-proj"},
+ "forgejo",
+ ),
+ # gitea
+ (
+ "https://gitea-instance.com/g-org/g-proj/pulls/43",
+ {},
+ "gitea",
+ ),
+ (
+ "https://gitea-instance.com/g-org/",
+ {},
+ None,
+ ),
+ (
+ "https://gitea-instance.com/g-org/",
+ {"gitea_url": "https://gitea-instance.com/g-org/g-proj"},
+ "gitea",
+ ),
+ # gitlab
+ (
+ "https://gitlab-instance.com/g-org/g-proj/-/merge_requests/43",
+ {},
+ "gitlab",
+ ),
+ (
+ "https://gitlab-instance.com/g-org/",
+ {},
+ None,
+ ),
+ (
+ "https://gitlab-instance.com/g-org/",
+ {"gitlab_url": "https://gitlab-instance.com/g-org/g-proj"},
+ "gitlab",
+ ),
+ ],
+)
+def test_identify_selfhosted(url, html_options, expected, shortener):
+ """Unit test for self-hosted forges identification."""
+ url = urlparse(url)
+
+ assert shortener.identify_selfhosted(url, html_options) == expected