Skip to content

Commit

Permalink
Fix invocation from a repository subdirectory (#26)
Browse files Browse the repository at this point in the history
This should mean that you can invoke the tool from, for example, the
'rosdep' subdirectory.
  • Loading branch information
cottsay authored Oct 4, 2024
1 parent d0c0600 commit 96b7a2e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
21 changes: 18 additions & 3 deletions rosdistro_reviewer/verb/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@
from rosdistro_reviewer.submitter import submit_review


def _find_repo_root():
# We delay this import until after the GitPython
# logger has already been configured to avoid DEBUG
# messages on the console at import time
from git import Repo
from git import InvalidGitRepositoryError
try:
with Repo(Path.cwd(), search_parent_directories=True) as repo:
return Path(repo.working_tree_dir)
except InvalidGitRepositoryError as e:
raise RuntimeError(
'This tool must be run from within a git repository') from e


class ReviewVerb(VerbExtensionPoint):
"""Generate a review for rosdistro changes."""

Expand All @@ -36,13 +50,14 @@ def add_arguments(self, *, parser): # noqa: D102
add_review_submitter_arguments(parser)

def main(self, *, context): # noqa: D102
root = _find_repo_root()
review = analyze(
Path.cwd(),
root,
target_ref=context.args.target_ref,
head_ref=context.args.head_ref)
if review:
root = Path.cwd() if context.args.head_ref is None else None
print('\n' + review.to_text(root=root) + '\n')
text_root = root if context.args.head_ref is None else None
print('\n' + review.to_text(root=text_root) + '\n')

submit_review(context.args, review)
return 0
16 changes: 16 additions & 0 deletions test/test_verb_review.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,19 @@ def test_verb_review(empty_repo):
return_value=Path(empty_repo.working_tree_dir),
):
assert 0 == extension.main(context=context)


def test_verb_review_no_repo(tmp_path):
extension = ReviewVerb()
extension.add_arguments(parser=Mock())

context = CommandContext(
command_name='rosdistro-reviewer',
args=Mock())

with patch(
'rosdistro_reviewer.verb.review.Path.cwd',
return_value=tmp_path,
):
with pytest.raises(RuntimeError):
extension.main(context=context)

0 comments on commit 96b7a2e

Please sign in to comment.