Skip to content

Commit

Permalink
fix: Handle Git errors when checking for API breaking changes
Browse files Browse the repository at this point in the history
Issue #144: #144
  • Loading branch information
pawamoy committed Jul 2, 2023
1 parent 45332ba commit f9e8ba3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/griffe/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from griffe.diff import ExplanationStyle, find_breaking_changes
from griffe.docstrings.parsers import Parser
from griffe.encoders import JSONEncoder
from griffe.exceptions import ExtensionError
from griffe.exceptions import ExtensionError, GitError
from griffe.extensions.base import load_extensions
from griffe.git import _get_latest_tag, _get_repo_root, load_git
from griffe.loader import GriffeLoader, load
Expand Down Expand Up @@ -364,7 +364,11 @@ def check(

search_paths = list(search_paths) if search_paths else []

against = against or _get_latest_tag(package)
try:
against = against or _get_latest_tag(package)
except GitError as error:
print(f"griffe: error: {error}", file=sys.stderr)
return 2
against_path = against_path or package
repository = _get_repo_root(against_path)

Expand Down
4 changes: 4 additions & 0 deletions src/griffe/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,7 @@ class ExtensionError(GriffeError):

class ExtensionNotLoadedError(ExtensionError):
"""Exception raised when an extension could not be loaded."""


class GitError(GriffeError):
"""Exception raised for errors related to Git."""
10 changes: 8 additions & 2 deletions src/griffe/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from typing import TYPE_CHECKING, Any, Iterator, Sequence

from griffe import loader
from griffe.exceptions import GitError

if TYPE_CHECKING:
from griffe.collections import LinesCollection, ModulesCollection
Expand Down Expand Up @@ -47,12 +48,17 @@ def _get_latest_tag(path: str | Path) -> str:
path = Path(path)
if not path.is_dir():
path = path.parent
output = subprocess.check_output(
process = subprocess.run(
["git", "tag", "-l", "--sort=-committerdate"],
cwd=path,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
return output.strip().split("\n", 1)[0]
output = process.stdout.strip()
if process.returncode != 0 or not output:
raise GitError(f"Cannot list Git tags in {path}: {output or 'no tags'}")
return output.split("\n", 1)[0]


def _get_repo_root(path: str | Path) -> str:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import pytest

from griffe.cli import check
from griffe.dataclasses import Module
from griffe.git import load_git
from tests import FIXTURES_DIR
Expand Down Expand Up @@ -98,3 +99,8 @@ def test_load_git_errors(git_repo: Path) -> None:

with pytest.raises(ModuleNotFoundError, match="No module named 'not_a_real_module'"):
load_git("not_a_real_module", ref="v0.2.0", repo=git_repo)


def test_git_failures(tmp_path: Path) -> None:
"""Test failures to use Git."""
assert check(tmp_path) == 2

0 comments on commit f9e8ba3

Please sign in to comment.