Skip to content

Commit

Permalink
Return non-zero exit codes for violations/errors
Browse files Browse the repository at this point in the history
This tracks exit codes based on the existence of violations and/or
errors when linting files.

- 0: everything clean
- 1: violations only
- 2: errors only
- 3: both violations and errors

Fixes #258

[ghstack-poisoned]
  • Loading branch information
amyreese committed Mar 15, 2023
1 parent 8e7aca3 commit 614d7ea
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/fixit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ def print_result(result: Result, debug: bool = False) -> None:
# An exception occurred while processing a file
error, tb = result.error
if debug:
click.secho(f"{error}: {tb}", fg="red")
click.secho(f"{path}: {error}: {tb}", fg="red")
else:
click.secho(f"{error}", fg="red")
click.secho(f"{path}: {error}", fg="red")


def _make_result(path: Path, violations: Iterable[LintViolation]) -> Iterable[Result]:
Expand Down
33 changes: 33 additions & 0 deletions src/fixit/tests/smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# LICENSE file in the root directory of this source tree.

from pathlib import Path
from tempfile import TemporaryDirectory
from unittest import TestCase

from click.testing import CliRunner
Expand All @@ -30,3 +31,35 @@ def test_this_project_is_clean(self) -> None:
result = self.runner.invoke(main, ["lint", project_dir], catch_exceptions=False)
self.assertEqual(result.output, "")
self.assertEqual(result.exit_code, 0)

def test_directory_with_violations(self) -> None:
with TemporaryDirectory() as td:
tdp = Path(td).resolve()
(tdp / "clean.py").write_text("name = 'Kirby'\nprint(f'hello {name}')")
(tdp / "dirty.py").write_text("name = 'Kirby'\nprint('hello %s' % name)\n")

result = self.runner.invoke(main, ["lint", td])
self.assertIn("dirty.py@2:6 UseFstringRule:", result.output)
self.assertEqual(result.exit_code, 1)

def test_directory_with_errors(self) -> None:
with TemporaryDirectory() as td:
tdp = Path(td).resolve()
(tdp / "clean.py").write_text("name = 'Kirby'\nprint(f'hello {name}')")
(tdp / "broken.py").write_text("print)\n")

result = self.runner.invoke(main, ["lint", td])
self.assertIn("broken.py: Syntax Error @ 1:6.", result.output)
self.assertEqual(result.exit_code, 2)

def test_directory_with_violations_and_errors(self) -> None:
with TemporaryDirectory() as td:
tdp = Path(td).resolve()
(tdp / "clean.py").write_text("name = 'Kirby'\nprint(f'hello {name}')")
(tdp / "dirty.py").write_text("name = 'Kirby'\nprint('hello %s' % name)\n")
(tdp / "broken.py").write_text("print)\n")

result = self.runner.invoke(main, ["lint", td])
self.assertIn("dirty.py@2:6 UseFstringRule:", result.output)
self.assertIn("broken.py: Syntax Error @ 1:6.", result.output)
self.assertEqual(result.exit_code, 3)

0 comments on commit 614d7ea

Please sign in to comment.