From a7be5e798b06ef24d4c61f57a4ba65437576911e Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Fri, 24 Dec 2021 16:38:17 -0500 Subject: [PATCH] fix AttributeError when catatstrophic failure is triggered --- src/flake8/main/application.py | 14 ++++++++------ tests/integration/test_main.py | 11 +++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/flake8/main/application.py b/src/flake8/main/application.py index bf43826..1894881 100644 --- a/src/flake8/main/application.py +++ b/src/flake8/main/application.py @@ -115,11 +115,13 @@ class Application: def exit_code(self) -> int: """Return the program exit code.""" + if self.catastrophic_failure: + return 1 assert self.options is not None if self.options.exit_zero: - return int(self.catastrophic_failure) + return 0 else: - return int((self.result_count > 0) or self.catastrophic_failure) + return int(self.result_count > 0) def find_plugins( self, @@ -391,7 +393,7 @@ class Application: except exceptions.EarlyQuit: self.catastrophic_failure = True print("... stopped while processing files") - - assert self.options is not None - if self.options.count: - print(self.result_count) + else: + assert self.options is not None + if self.options.count: + print(self.result_count) diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py index 27acab5..b351e9d 100644 --- a/tests/integration/test_main.py +++ b/tests/integration/test_main.py @@ -8,6 +8,7 @@ import pytest from flake8 import utils from flake8.main import cli +from flake8.options import config def test_diff_option(tmpdir, capsys): @@ -375,3 +376,13 @@ def test_output_file(tmpdir, capsys): expected = "t.py:1:1: F401 'os' imported but unused\n" assert tmpdir.join("a/b/f").read() == expected + + +def test_early_keyboard_interrupt_does_not_crash(capsys): + with mock.patch.object( + config, "load_config", side_effect=KeyboardInterrupt + ): + assert cli.main(["does-not-exist"]) == 1 + out, err = capsys.readouterr() + assert out == "... stopped\n" + assert err == ""