|
| 1 | +"""Shortening url tests.""" |
| 2 | + |
| 3 | +from urllib.parse import urlparse |
| 4 | + |
| 5 | +import pytest |
| 6 | +from pydata_sphinx_theme.short_link import ShortenLinkTransform |
| 7 | + |
| 8 | + |
| 9 | +class Mock: |
| 10 | + """mock object.""" |
| 11 | + |
| 12 | + pass |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.parametrize( |
| 16 | + "platform,url,expected", |
| 17 | + [ |
| 18 | + # TODO, I belive this is wrong as both github.com and github.com/github |
| 19 | + # shorten to just github. |
| 20 | + ("github", "https://github.com", "github"), |
| 21 | + ("github", "https://github.com/github", "github"), |
| 22 | + ("github", "https://github.com/pydata", "pydata"), |
| 23 | + ( |
| 24 | + "github", |
| 25 | + "https://github.com/pydata/pydata-sphinx-theme", |
| 26 | + "pydata/pydata-sphinx-theme", |
| 27 | + ), |
| 28 | + ( |
| 29 | + "github", |
| 30 | + "https://github.com/pydata/pydata-sphinx-theme/pull/1012", |
| 31 | + "pydata/pydata-sphinx-theme#1012", |
| 32 | + ), |
| 33 | + # TODO, I belive this is wrong as both orgs/pydata/projects/2 and pydata/projects/issue/2 |
| 34 | + # shorten to the same |
| 35 | + ("github", "https://github.com/orgs/pydata/projects/2", "pydata/projects#2"), |
| 36 | + ("github", "https://github.com/pydata/projects/pull/2", "pydata/projects#2"), |
| 37 | + # issues and pulls are athe same, so it's ok to normalise to the same here |
| 38 | + ("github", "https://github.com/pydata/projects/issues/2", "pydata/projects#2"), |
| 39 | + # Gitlab |
| 40 | + ("gitlab", "https://gitlab.com/tezos/tezos/-/issues", "tezos/tezos/issues"), |
| 41 | + ("gitlab", "https://gitlab.com/tezos/tezos/issues", "tezos/tezos/issues"), |
| 42 | + ( |
| 43 | + "gitlab", |
| 44 | + "https://gitlab.com/gitlab-org/gitlab/-/issues/375583", |
| 45 | + "gitlab-org/gitlab#375583", |
| 46 | + ), |
| 47 | + ( |
| 48 | + # TODO, non canonical url, discuss if should maybe be shortened to |
| 49 | + # gitlab-org/gitlab#375583 |
| 50 | + "gitlab", |
| 51 | + "https://gitlab.com/gitlab-org/gitlab/issues/375583", |
| 52 | + "gitlab-org/gitlab/issues/375583", |
| 53 | + ), |
| 54 | + ( |
| 55 | + "gitlab", |
| 56 | + "https://gitlab.com/gitlab-org/gitlab/-/issues/", |
| 57 | + "gitlab-org/gitlab/issues", |
| 58 | + ), |
| 59 | + ( |
| 60 | + # TODO, non canonical url, discuss if should maybe be shortened to |
| 61 | + # gitlab-org/gitlab/issues (no trailing slash) |
| 62 | + "gitlab", |
| 63 | + "https://gitlab.com/gitlab-org/gitlab/issues/", |
| 64 | + "gitlab-org/gitlab/issues/", |
| 65 | + ), |
| 66 | + ( |
| 67 | + "gitlab", |
| 68 | + "https://gitlab.com/gitlab-org/gitlab/-/issues", |
| 69 | + "gitlab-org/gitlab/issues", |
| 70 | + ), |
| 71 | + ( |
| 72 | + "gitlab", |
| 73 | + "https://gitlab.com/gitlab-org/gitlab/issues", |
| 74 | + "gitlab-org/gitlab/issues", |
| 75 | + ), |
| 76 | + ( |
| 77 | + "gitlab", |
| 78 | + "https://gitlab.com/gitlab-org/gitlab/-/merge_requests/84669", |
| 79 | + "gitlab-org/gitlab!84669", |
| 80 | + ), |
| 81 | + ( |
| 82 | + "gitlab", |
| 83 | + "https://gitlab.com/gitlab-org/gitlab/-/pipelines/511894707", |
| 84 | + "gitlab-org/gitlab/-/pipelines/511894707", |
| 85 | + ), |
| 86 | + ( |
| 87 | + "gitlab", |
| 88 | + "https://gitlab.com/gitlab-com/gl-infra/production/-/issues/6788", |
| 89 | + "gitlab-com/gl-infra/production#6788", |
| 90 | + ), |
| 91 | + ], |
| 92 | +) |
| 93 | +def test_shorten(platform, url, expected): |
| 94 | + """Unit test for url shortening. |
| 95 | +
|
| 96 | + Usually you also want a build test in `test_build.py` |
| 97 | + """ |
| 98 | + document = Mock() |
| 99 | + document.settings = Mock() |
| 100 | + document.settings.language_code = "en" |
| 101 | + document.reporter = None |
| 102 | + |
| 103 | + sl = ShortenLinkTransform(document) |
| 104 | + sl.platform = platform |
| 105 | + |
| 106 | + URI = urlparse(url) |
| 107 | + |
| 108 | + assert sl.parse_url(URI) == expected |
0 commit comments