Skip to content

Commit

Permalink
test(parsers,project): doctests + error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdoret committed Nov 1, 2023
1 parent 61433ea commit cf6d2df
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
48 changes: 45 additions & 3 deletions gimie/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ def extract(self) -> Graph:


def split_git_url(url) -> Tuple[str, str]:
"""Split a git URL into base URL and project path.
Examples
--------
>>> split_git_url("https://gitlab.com/foo/bar")
('https://gitlab.com', 'foo/bar')
"""
base_url = urlparse(url).scheme + "://" + urlparse(url).netloc
project = urlparse(url).path.strip("/")
return base_url, project
Expand All @@ -119,7 +126,14 @@ def get_extractor(
The base URL of the git remote.
local_path
If applicable, the path to the directory where the
repository is cloned.
repository is located.
Examples
--------
>>> extractor = get_extractor(
... "https://github.com/SDSC-ORD/gimie",
... "github"
... )
"""
try:
return GIT_PROVIDERS[source](
Expand All @@ -136,7 +150,25 @@ def get_parsers(
uri: str, parser_names: Optional[Iterable[str]] = None
) -> List[Parser]:
"""Instantiate the correct parsers for a given URI.
If parser_names is None, all parsers are used."""
If parser_names is None, all parsers are used.
Parameters
-----------
uri
The URI of the associated repository.
See :class:`gimie.parsers.Parser`.
parser_names
Names of file parsers to use.
Examples
--------
>>> parsers = get_parsers(
... "https://github.com/foo/bar",
... ["license"]
... )
>>> [type(parser) for parser in parsers]
[<class 'gimie.parsers.license.LicenseParser'>]
"""

parsers = []

Expand All @@ -156,7 +188,17 @@ def get_parsers(

def infer_git_provider(url: str) -> str:
"""Given a git repository URL, return the corresponding git provider.
Local path or unsupported git providers will return "git"."""
Local path or unsupported git providers will return "git".
Examples
--------
>>> infer_git_provider("https://gitlab.com/foo/bar")
'gitlab'
>>> infer_git_provider("/foo/bar")
'git'
>>> infer_git_provider("https://codeberg.org/dnkl/foot")
'git'
"""
# Fall back to git if local path
if not validate_url(url):
return "git"
Expand Down
6 changes: 6 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ def test_advice_help():
"""Checks if the 'gimie advice --help' command exits successfully."""
result = runner.invoke(cli.app, ["advice", "--help"])
assert result.exit_code == 0


def test_parsers_help():
"""Checks if the 'gimie parsers --help' command exits successfully."""
result = runner.invoke(cli.app, ["parsers", "--help"])
assert result.exit_code == 0
27 changes: 27 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Test the project module."""
import pytest

from gimie.extractors import GIT_PROVIDERS
from gimie.project import get_parsers, get_extractor
from gimie.parsers import PARSERS


def test_get_parsers():

repo = "https://example.org/group/project"
for name, parser in PARSERS.items():
assert type(get_parsers(repo, [name])[0]) == parser

# Should raise error if parser not found
with pytest.raises(ValueError):
get_parsers(repo, ["bad_parser"])


def test_get_extractor():

repo = "https://example.org/group/project"
for prov, extractor in GIT_PROVIDERS.items():
assert type(get_extractor(repo, prov)) == extractor

with pytest.raises(ValueError):
get_extractor(repo, "bad_provider")

0 comments on commit cf6d2df

Please sign in to comment.