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

Handle not found commit hash #125

Merged
merged 6 commits into from
Nov 5, 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
23 changes: 15 additions & 8 deletions ondivi/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import click
from git import Repo
from git.exc import GitCommandError

from ondivi._internal.define_changed_lines import define_changed_lines
from ondivi._internal.filter_out_violations import filter_out_violations
Expand Down Expand Up @@ -72,6 +73,13 @@ def controller(
)


def _linter_output_from_file(file_path: FromFilePathStr) -> list[str]:
if not Path(file_path).exists():
sys.stdout.write('File with violations "{0}" not found\n'.format(file_path))
sys.exit(1)
return Path(file_path).read_text(encoding='utf-8').strip().splitlines()


def cli(
baseline: BaselineStr,
fromfile: FromFilePathStr | None,
Expand All @@ -85,15 +93,14 @@ def cli(
:param violation_format: ViolationFormatStr
:param only_violations: bool
"""
if fromfile:
if not Path(fromfile).exists():
sys.stdout.write('File with violations "{0}" not found\n'.format(fromfile))
sys.exit(1)
linter_output = Path(fromfile).read_text(encoding='utf-8').strip().splitlines()
else:
linter_output = sys.stdin.read().strip().splitlines()
linter_output = _linter_output_from_file(fromfile) if fromfile else sys.stdin.read().strip().splitlines()
try:
diff = Repo('.').git.diff('--unified=0', baseline)
except GitCommandError:
sys.stdout.write('Revision "{0}" not found'.format(baseline))
sys.exit(1)
filtered_lines, violation_found = controller(
Repo('.').git.diff('--unified=0', baseline),
diff,
linter_output,
violation_format,
only_violations,
Expand Down
9 changes: 9 additions & 0 deletions tests/it/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,12 @@ def test_fromfile_not_found_via_cli_runner() -> None:

assert got.stdout == 'File with violations "undefined.txt" not found\n'
assert got.exit_code == 1


@pytest.mark.usefixtures('_test_repo')
def test_commit_not_found() -> None:
"""Test commit not found."""
got = CliRunner().invoke(main, ['--baseline', 'fakeHash'], input='')

assert got.stdout == 'Revision "fakeHash" not found'
assert got.exit_code == 1