Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invocation from a repository subdirectory #26

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading