Skip to content

Commit 24c7a90

Browse files
committed
tests(parser,git): Tests for URL detection
1 parent 70baf7b commit 24c7a90

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

libvcs/parse/git.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,19 @@ def is_valid(cls, url: str, is_explicit: Optional[bool] = None) -> bool:
292292
293293
>>> GitURL.is_valid(url='notaurl')
294294
False
295+
296+
**Unambiguous VCS detection**
297+
298+
Sometimes you may want to match a VCS exclusively, without any change for, e.g.
299+
in order to outright detect the VCS system being used.
300+
301+
>>> GitURL.is_valid(
302+
... url='git@github.com:vcs-python/libvcs.git', is_explicit=True
303+
... )
304+
False
305+
306+
In this case, check :meth:`GitPipURL.is_valid` or :meth:`GitURL.is_valid`'s
307+
examples.
295308
"""
296309
if is_explicit is not None:
297310
return any(
@@ -411,6 +424,44 @@ def to_url(self) -> str:
411424

412425
return url
413426

427+
@classmethod
428+
def is_valid(cls, url: str, is_explicit: Optional[bool] = None) -> bool:
429+
"""Whether URL is compatible with Pip Git's VCS URL pattern or not.
430+
431+
Examples
432+
--------
433+
434+
Will not match normal ``git(1)`` URLs, use :meth:`GitURL.is_valid` for that.
435+
436+
>>> GitPipURL.is_valid(url='https://github.com/vcs-python/libvcs.git')
437+
False
438+
439+
>>> GitPipURL.is_valid(url='git@github.com:vcs-python/libvcs.git')
440+
False
441+
442+
Pip-style URLs:
443+
444+
>>> GitPipURL.is_valid(url='git+https://github.com/vcs-python/libvcs.git')
445+
True
446+
447+
>>> GitPipURL.is_valid(url='git+ssh://git@github.com:vcs-python/libvcs.git')
448+
True
449+
450+
>>> GitPipURL.is_valid(url='notaurl')
451+
False
452+
453+
**Explicit VCS detection**
454+
455+
Pip-style URLs are prefixed with the VCS name in front, so its matchers can
456+
unambigously narrow the type of VCS:
457+
458+
>>> GitPipURL.is_valid(
459+
... url='git+ssh://git@github.com:vcs-python/libvcs.git', is_explicit=True
460+
... )
461+
True
462+
"""
463+
return super().is_valid(url=url, is_explicit=is_explicit)
464+
414465

415466
@dataclasses.dataclass(repr=False)
416467
class GitURL(GitPipURL, GitBaseURL, URLProtocol, SkipDefaultFieldsReprMixin):

0 commit comments

Comments
 (0)