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