|
| 1 | +import typing as t |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from libvcs.url import parse |
| 6 | +from libvcs.url.git import GitURL |
| 7 | +from libvcs.url.hg import HgURL |
| 8 | +from libvcs.url.svn import SvnURL |
| 9 | + |
| 10 | +if t.TYPE_CHECKING: |
| 11 | + from typing_extensions import TypeAlias |
| 12 | + |
| 13 | + ParserMatchLazy: TypeAlias = t.Callable[[str], parse.ParserMatch] |
| 14 | + DetectVCSFixtureExpectedMatch: TypeAlias = t.Union[ |
| 15 | + parse.ParserMatch, ParserMatchLazy |
| 16 | + ] |
| 17 | + |
| 18 | + |
| 19 | +class DetectVCSFixture(t.NamedTuple): |
| 20 | + url: str |
| 21 | + expected_matches_lazy: t.List["DetectVCSFixtureExpectedMatch"] |
| 22 | + is_explicit: bool |
| 23 | + |
| 24 | + |
| 25 | +TEST_FIXTURES: list[DetectVCSFixture] = [ |
| 26 | + *[ |
| 27 | + DetectVCSFixture( |
| 28 | + url=url, |
| 29 | + expected_matches_lazy=[ |
| 30 | + lambda url: parse.ParserMatch(vcs="git", match=GitURL(url)) |
| 31 | + ], |
| 32 | + is_explicit=True, |
| 33 | + ) |
| 34 | + for url in [ |
| 35 | + "git+https://github.com/vcs-python/libvcs", |
| 36 | + "git+https://github.com/vcs-python/libvcs.git", |
| 37 | + "git+https://github.com:vcs-python/libvcs.git", |
| 38 | + "git+ssh://git@github.com:vcs-python/libvcs.git", |
| 39 | + "git+ssh://git@github.com:vcs-python/libvcs", |
| 40 | + ] |
| 41 | + ], |
| 42 | + *[ |
| 43 | + DetectVCSFixture( |
| 44 | + url=url, |
| 45 | + expected_matches_lazy=[ |
| 46 | + lambda url: parse.ParserMatch(vcs="hg", match=HgURL(url)) |
| 47 | + ], |
| 48 | + is_explicit=True, |
| 49 | + ) |
| 50 | + for url in [ |
| 51 | + "hg+http://hg.example.com/MyProject@da39a3ee5e6b", |
| 52 | + "hg+ssh://hg.example.com:MyProject@da39a3ee5e6b", |
| 53 | + ] |
| 54 | + ], |
| 55 | + *[ |
| 56 | + DetectVCSFixture( |
| 57 | + url=url, |
| 58 | + expected_matches_lazy=[ |
| 59 | + lambda url: parse.ParserMatch(vcs="svn", match=SvnURL(url)) |
| 60 | + ], |
| 61 | + is_explicit=True, |
| 62 | + ) |
| 63 | + for url in [ |
| 64 | + "svn+http://svn.example.com/MyProject@da39a3ee5e6b", |
| 65 | + "svn+ssh://svn.example.com:MyProject@da39a3ee5e6b", |
| 66 | + ] |
| 67 | + ], |
| 68 | +] |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.parametrize( |
| 72 | + list(DetectVCSFixture._fields), |
| 73 | + TEST_FIXTURES, |
| 74 | +) |
| 75 | +def test_registry( |
| 76 | + url: str, |
| 77 | + expected_matches_lazy: t.List["DetectVCSFixtureExpectedMatch"], |
| 78 | + is_explicit: bool, |
| 79 | +) -> None: |
| 80 | + assert url |
| 81 | + assert parse.detect_vcs |
| 82 | + |
| 83 | + matches = parse.detect_vcs.match(url, is_explicit=is_explicit) |
| 84 | + |
| 85 | + # Just add water |
| 86 | + expected_matches: t.List["DetectVCSFixtureExpectedMatch"] = [] |
| 87 | + for idx, expected_match in enumerate(expected_matches_lazy): |
| 88 | + if callable(expected_match): |
| 89 | + assert callable(expected_match) |
| 90 | + expected_matches.append(expected_match(url)) |
| 91 | + |
| 92 | + assert matches == expected_matches |
0 commit comments