From 81a4110338496714c0bf2bb0e8dd929461d9d492 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 14 Nov 2021 16:24:59 -0800 Subject: [PATCH] have application return exit code for easier testing --- src/flake8/__main__.py | 5 +++-- src/flake8/main/application.py | 26 +++++++++++--------------- src/flake8/main/cli.py | 4 ++-- tests/integration/test_main.py | 15 +++++++++------ tests/unit/test_application.py | 19 ++++++++----------- 5 files changed, 33 insertions(+), 36 deletions(-) diff --git a/src/flake8/__main__.py b/src/flake8/__main__.py index 42bc428..de240dc 100644 --- a/src/flake8/__main__.py +++ b/src/flake8/__main__.py @@ -1,4 +1,5 @@ """Module allowing for ``python -m flake8 ...``.""" -from flake8.main import cli +from flake8.main.cli import main -cli.main() +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/src/flake8/main/application.py b/src/flake8/main/application.py index c85172c..61e22a8 100644 --- a/src/flake8/main/application.py +++ b/src/flake8/main/application.py @@ -121,22 +121,13 @@ class Application: rest.extend(("--output-file", args.output_file)) return args, rest - def exit(self) -> None: - """Handle finalization and exiting the program. - - This should be the last thing called on the application instance. It - will check certain options and exit appropriately. - """ + def exit_code(self) -> int: + """Return the program exit code.""" assert self.options is not None - if self.options.count: - print(self.result_count) - if self.options.exit_zero: - raise SystemExit(self.catastrophic_failure) + return int(self.catastrophic_failure) else: - raise SystemExit( - (self.result_count > 0) or self.catastrophic_failure - ) + return int((self.result_count > 0) or self.catastrophic_failure) def find_plugins(self, config_finder: config.ConfigFileFinder) -> None: """Find and load the plugins for this application. @@ -193,8 +184,6 @@ class Application: "future version." ) self.parsed_diff = utils.parse_unified_diff() - if not self.parsed_diff: - self.exit() assert self.check_plugins is not None self.check_plugins.provide_options( @@ -268,6 +257,9 @@ class Application: assert self.file_checker_manager is not None if self.running_against_diff: files = sorted(self.parsed_diff) + if not files: + return + self.file_checker_manager.start(files) try: self.file_checker_manager.run() @@ -388,3 +380,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) diff --git a/src/flake8/main/cli.py b/src/flake8/main/cli.py index ddbc7c0..6c0b1ad 100644 --- a/src/flake8/main/cli.py +++ b/src/flake8/main/cli.py @@ -6,7 +6,7 @@ from typing import Optional from flake8.main import application -def main(argv: Optional[List[str]] = None) -> None: +def main(argv: Optional[List[str]] = None) -> int: """Execute the main bit of the application. This handles the creation of an instance of :class:`Application`, runs it, @@ -20,4 +20,4 @@ def main(argv: Optional[List[str]] = None) -> None: app = application.Application() app.run(argv) - app.exit() + return app.exit_code() diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py index 6c92c4a..bc78c8b 100644 --- a/tests/integration/test_main.py +++ b/tests/integration/test_main.py @@ -11,9 +11,8 @@ from flake8.main import cli def _call_main(argv, retv=0): - with pytest.raises(SystemExit) as excinfo: - cli.main(argv) - assert excinfo.value.code == retv + exit_code = cli.main(argv) + assert exit_code == retv def test_diff_option(tmpdir, capsys): @@ -221,7 +220,9 @@ def test_tokenization_error_is_a_syntax_error(tmpdir, capsys): def test_bug_report_successful(capsys): """Test that --bug-report does not crash.""" - _call_main(["--bug-report"]) + with pytest.raises(SystemExit) as excinfo: + _call_main(["--bug-report"]) + assert excinfo.value.args[0] == 0 out, err = capsys.readouterr() assert json.loads(out) assert err == "" @@ -316,8 +317,10 @@ t.py:1:9: W292 no newline at end of file def test_obtaining_args_from_sys_argv_when_not_explicity_provided(capsys): """Test that arguments are obtained from 'sys.argv'.""" - with mock.patch("sys.argv", ["flake8", "--help"]): - _call_main(None) + with pytest.raises(SystemExit) as excinfo: + with mock.patch("sys.argv", ["flake8", "--help"]): + _call_main(None) + assert excinfo.value.args[0] == 0 out, err = capsys.readouterr() assert out.startswith("usage: flake8 [options] file file ...\n") diff --git a/tests/unit/test_application.py b/tests/unit/test_application.py index b95e383..2d0e849 100644 --- a/tests/unit/test_application.py +++ b/tests/unit/test_application.py @@ -26,13 +26,13 @@ def application(): @pytest.mark.parametrize( "result_count, catastrophic, exit_zero, value", [ - (0, False, False, False), - (0, True, False, True), - (2, False, False, True), - (2, True, False, True), - (0, True, True, True), - (2, False, True, False), - (2, True, True, True), + (0, False, False, 0), + (0, True, False, 1), + (2, False, False, 1), + (2, True, False, 1), + (0, True, True, 1), + (2, False, True, 0), + (2, True, True, 1), ], ) def test_exit_does_raise( @@ -43,10 +43,7 @@ def test_exit_does_raise( application.catastrophic_failure = catastrophic application.options = options(exit_zero=exit_zero) - with pytest.raises(SystemExit) as excinfo: - application.exit() - - assert excinfo.value.args[0] is value + assert application.exit_code() == value def test_warns_on_unknown_formatter_plugin_name(application):