Skip to content

Commit

Permalink
Handle not found commit hash (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
blablatdinov authored Nov 5, 2024
1 parent a8311a6 commit 9df7ac7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
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

0 comments on commit 9df7ac7

Please sign in to comment.