mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-03 11:56:52 +00:00
have application return exit code for easier testing
This commit is contained in:
parent
5a85dd8ddb
commit
81a4110338
5 changed files with 33 additions and 36 deletions
|
|
@ -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())
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue