|
| 1 | +import typing |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from libvcs.parse.base import MatcherRegistry |
| 6 | +from libvcs.parse.hg import DEFAULT_MATCHERS, PIP_DEFAULT_MATCHERS, HgURL |
| 7 | +from libvcs.projects.hg import MercurialProject |
| 8 | + |
| 9 | + |
| 10 | +class HgURLFixture(typing.NamedTuple): |
| 11 | + url: str |
| 12 | + is_valid: bool |
| 13 | + hg_location: HgURL |
| 14 | + |
| 15 | + |
| 16 | +TEST_FIXTURES: list[HgURLFixture] = [ |
| 17 | + HgURLFixture( |
| 18 | + url="https://bitbucket.com/vcs-python/libvcs", |
| 19 | + is_valid=True, |
| 20 | + hg_location=HgURL( |
| 21 | + url="https://bitbucket.com/vcs-python/libvcs", |
| 22 | + scheme="https", |
| 23 | + hostname="bitbucket.com", |
| 24 | + path="vcs-python/libvcs", |
| 25 | + ), |
| 26 | + ), |
| 27 | + HgURLFixture( |
| 28 | + url="https://bitbucket.com/vcs-python/libvcs", |
| 29 | + is_valid=True, |
| 30 | + hg_location=HgURL( |
| 31 | + url="https://bitbucket.com/vcs-python/libvcs", |
| 32 | + scheme="https", |
| 33 | + hostname="bitbucket.com", |
| 34 | + path="vcs-python/libvcs", |
| 35 | + ), |
| 36 | + ), |
| 37 | +] |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.parametrize( |
| 41 | + "url,is_valid,hg_location", |
| 42 | + TEST_FIXTURES, |
| 43 | +) |
| 44 | +def test_hg_location( |
| 45 | + url: str, |
| 46 | + is_valid: bool, |
| 47 | + hg_location: HgURL, |
| 48 | + hg_repo: MercurialProject, |
| 49 | +): |
| 50 | + url = url.format(local_repo=hg_repo.dir) |
| 51 | + hg_location.url = hg_location.url.format(local_repo=hg_repo.dir) |
| 52 | + |
| 53 | + assert HgURL.is_valid(url) == is_valid, f"{url} compatibility should be {is_valid}" |
| 54 | + assert HgURL(url) == hg_location |
| 55 | + |
| 56 | + |
| 57 | +class HgURLKwargs(typing.TypedDict): |
| 58 | + url: str |
| 59 | + |
| 60 | + |
| 61 | +class HgURLKwargsFixture(typing.NamedTuple): |
| 62 | + url: str |
| 63 | + is_valid: bool |
| 64 | + hg_location_kwargs: HgURLKwargs |
| 65 | + |
| 66 | + |
| 67 | +# |
| 68 | +# |
| 69 | +# Extensibility: pip(1) |
| 70 | +# w/ VCS prefixes, e.g. hg+https, hg+ssh, hg+file |
| 71 | +# https://pip.pypa.io/en/stable/topics/vcs-support/ |
| 72 | +# |
| 73 | +# |
| 74 | +PIP_TEST_FIXTURES: list[HgURLKwargsFixture] = [ |
| 75 | + HgURLKwargsFixture( |
| 76 | + url="hg+https://bitbucket.com/liuxinyu95/AlgoXY", |
| 77 | + is_valid=True, |
| 78 | + hg_location_kwargs=HgURLKwargs( |
| 79 | + url="hg+https://bitbucket.com/liuxinyu95/AlgoXY" |
| 80 | + ), |
| 81 | + ), |
| 82 | + HgURLKwargsFixture( |
| 83 | + url="hg+ssh://hg@bitbucket.com:tony/AlgoXY", |
| 84 | + is_valid=True, |
| 85 | + hg_location_kwargs=HgURLKwargs(url="hg+ssh://hg@bitbucket.com:tony/AlgoXY"), |
| 86 | + ), |
| 87 | + HgURLKwargsFixture( |
| 88 | + url="hg+file://{local_repo}", |
| 89 | + is_valid=True, |
| 90 | + hg_location_kwargs=HgURLKwargs(url="hg+file://{local_repo}"), |
| 91 | + ), |
| 92 | + # Incompatible |
| 93 | + HgURLKwargsFixture( |
| 94 | + url="hg+ssh://hg@bitbucket.com/tony/AlgoXY", |
| 95 | + is_valid=True, |
| 96 | + hg_location_kwargs=HgURLKwargs(url="hg+ssh://hg@bitbucket.com/tony/AlgoXY"), |
| 97 | + ), |
| 98 | +] |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.parametrize( |
| 102 | + "url,is_valid,hg_location_kwargs", |
| 103 | + PIP_TEST_FIXTURES, |
| 104 | +) |
| 105 | +def test_hg_location_extension_pip( |
| 106 | + url: str, |
| 107 | + is_valid: bool, |
| 108 | + hg_location_kwargs: HgURLKwargs, |
| 109 | + hg_repo: MercurialProject, |
| 110 | +): |
| 111 | + class HgURLWithPip(HgURL): |
| 112 | + matchers = MatcherRegistry = MatcherRegistry( |
| 113 | + _matchers={m.label: m for m in [*DEFAULT_MATCHERS, *PIP_DEFAULT_MATCHERS]} |
| 114 | + ) |
| 115 | + |
| 116 | + hg_location_kwargs["url"] = hg_location_kwargs["url"].format(local_repo=hg_repo.dir) |
| 117 | + url = url.format(local_repo=hg_repo.dir) |
| 118 | + hg_location = HgURLWithPip(**hg_location_kwargs) |
| 119 | + hg_location.url = hg_location.url.format(local_repo=hg_repo.dir) |
| 120 | + |
| 121 | + assert ( |
| 122 | + HgURL.is_valid(url) != is_valid |
| 123 | + ), f"{url} compatibility should work with core, expects {not is_valid}" |
| 124 | + assert ( |
| 125 | + HgURLWithPip.is_valid(url) == is_valid |
| 126 | + ), f"{url} compatibility should be {is_valid}" |
| 127 | + assert HgURLWithPip(url) == hg_location |
| 128 | + |
| 129 | + |
| 130 | +class ToURLFixture(typing.NamedTuple): |
| 131 | + hg_location: HgURL |
| 132 | + expected: str |
| 133 | + |
| 134 | + |
| 135 | +@pytest.mark.parametrize( |
| 136 | + "hg_location,expected", |
| 137 | + [ |
| 138 | + ToURLFixture( |
| 139 | + expected="https://bitbucket.com/vcs-python/libvcs", |
| 140 | + hg_location=HgURL( |
| 141 | + url="https://bitbucket.com/vcs-python/libvcs", |
| 142 | + scheme="https", |
| 143 | + hostname="bitbucket.com", |
| 144 | + path="vcs-python/libvcs", |
| 145 | + ), |
| 146 | + ), |
| 147 | + ToURLFixture( |
| 148 | + expected="https://bitbucket.com/vcs-python/libvcs", |
| 149 | + hg_location=HgURL( |
| 150 | + url="https://bitbucket.com/vcs-python/libvcs", |
| 151 | + scheme="https", |
| 152 | + hostname="bitbucket.com", |
| 153 | + path="vcs-python/libvcs", |
| 154 | + ), |
| 155 | + ), |
| 156 | + # |
| 157 | + # SCP-style URLs: |
| 158 | + # e.g. 'ssh://hg@example.com/foo/bar' |
| 159 | + # |
| 160 | + ToURLFixture( |
| 161 | + expected="ssh://hg@bitbucket.com/liuxinyu95/AlgoXY", |
| 162 | + hg_location=HgURL( |
| 163 | + url="ssh://hg@bitbucket.com/liuxinyu95/AlgoXY", |
| 164 | + user="hg", |
| 165 | + scheme="ssh", |
| 166 | + hostname="bitbucket.com", |
| 167 | + path="liuxinyu95/AlgoXY", |
| 168 | + ), |
| 169 | + ), |
| 170 | + ToURLFixture( |
| 171 | + expected="ssh://username@bitbucket.com/vcs-python/libvcs", |
| 172 | + hg_location=HgURL( |
| 173 | + url="username@bitbucket.com/vcs-python/libvcs", |
| 174 | + user="username", |
| 175 | + scheme="ssh", |
| 176 | + hostname="bitbucket.com", |
| 177 | + path="vcs-python/libvcs", |
| 178 | + ), |
| 179 | + ), |
| 180 | + ], |
| 181 | +) |
| 182 | +def test_hg_to_url( |
| 183 | + expected: str, |
| 184 | + hg_location: HgURL, |
| 185 | + hg_repo: MercurialProject, |
| 186 | +): |
| 187 | + """Test HgURL.to_url()""" |
| 188 | + hg_location.url = hg_location.url.format(local_repo=hg_repo.dir) |
| 189 | + |
| 190 | + assert hg_location.to_url() == expected |
0 commit comments