Skip to content
Open
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
43 changes: 30 additions & 13 deletions src/fixit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,27 +218,44 @@ def fix(
visited.add(result.path)
# for STDIN, we need STDOUT to equal the fixed content, so
# move everything else to STDERR
if print_result(
is_dirty = print_result(
result,
show_diff=interactive or diff,
stderr=is_stdin,
output_format=config.output_format,
output_template=config.output_template,
):
)
if is_dirty:
dirty.add(result.path)
if autofix and result.violation and result.violation.autofixable:
autofixes += 1
fixed += 1
if interactive and result.violation and result.violation.autofixable:

if result.error:
exit_code |= 2
continue

violation = result.violation
if not violation:
continue

violation_fixed = False
if violation.autofixable:
autofixes += 1
answer = click.prompt(
"Apply autofix?", default="y", type=click.Choice("ynq", False)
)
if answer == "y":
generator.respond(True) # noqa: B038
if autofix:
fixed += 1
elif answer == "q":
break
violation_fixed = True
elif interactive:
answer = click.prompt(
"Apply autofix?", default="y", type=click.Choice("ynq", False)
)
if answer == "y":
generator.respond(True) # noqa: B038
fixed += 1
violation_fixed = True
elif answer == "q":
exit_code |= 1
break

if not violation_fixed:
exit_code |= 1

splash(visited, dirty, autofixes, fixed)
ctx.exit(exit_code)
Expand Down
47 changes: 47 additions & 0 deletions src/fixit/tests/smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,53 @@ def test_directory_with_violations_and_errors(self) -> None:
self.assertIn("broken.py: EXCEPTION: Syntax Error @ 1:", result.output)
self.assertEqual(result.exit_code, 3)

def test_fix_exit_codes(self) -> None:
with self.subTest("unfixable violation"):
with TemporaryDirectory() as td:
tdp = Path(td).resolve()
dirty = tdp / "dirty.py"
dirty.write_text(
"try:\n pass\nexcept ValueError or KeyError:\n pass\n"
)

result = self.runner.invoke(
main, ["fix", "--automatic", dirty.as_posix()], catch_exceptions=False
)

self.assertIn("AvoidOrInExcept", result.output)
self.assertEqual(result.exit_code, 1)

with self.subTest("syntax error"):
with TemporaryDirectory() as td:
tdp = Path(td).resolve()
broken = tdp / "broken.py"
broken.write_text("print)\n")

result = self.runner.invoke(
main,
["fix", "--automatic", broken.as_posix()],
catch_exceptions=False,
)

self.assertIn("EXCEPTION: Syntax Error", result.output)
self.assertEqual(result.exit_code, 2)

with self.subTest("violations and errors together"):
with TemporaryDirectory() as td:
tdp = Path(td).resolve()
(tdp / "dirty.py").write_text(
"try:\n pass\nexcept ValueError or KeyError:\n pass\n"
)
(tdp / "broken.py").write_text("print)\n")

result = self.runner.invoke(
main, ["fix", "--automatic", td], catch_exceptions=False
)

self.assertIn("AvoidOrInExcept", result.output)
self.assertIn("EXCEPTION: Syntax Error", result.output)
self.assertEqual(result.exit_code, 3)

def test_directory_with_autofixes(self) -> None:
with TemporaryDirectory() as td:
tdp = Path(td).resolve()
Expand Down