mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-12 23:54:17 +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 ...``."""
|
"""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))
|
rest.extend(("--output-file", args.output_file))
|
||||||
return args, rest
|
return args, rest
|
||||||
|
|
||||||
def exit(self) -> None:
|
def exit_code(self) -> int:
|
||||||
"""Handle finalization and exiting the program.
|
"""Return the program exit code."""
|
||||||
|
|
||||||
This should be the last thing called on the application instance. It
|
|
||||||
will check certain options and exit appropriately.
|
|
||||||
"""
|
|
||||||
assert self.options is not None
|
assert self.options is not None
|
||||||
if self.options.count:
|
|
||||||
print(self.result_count)
|
|
||||||
|
|
||||||
if self.options.exit_zero:
|
if self.options.exit_zero:
|
||||||
raise SystemExit(self.catastrophic_failure)
|
return int(self.catastrophic_failure)
|
||||||
else:
|
else:
|
||||||
raise SystemExit(
|
return int((self.result_count > 0) or self.catastrophic_failure)
|
||||||
(self.result_count > 0) or self.catastrophic_failure
|
|
||||||
)
|
|
||||||
|
|
||||||
def find_plugins(self, config_finder: config.ConfigFileFinder) -> None:
|
def find_plugins(self, config_finder: config.ConfigFileFinder) -> None:
|
||||||
"""Find and load the plugins for this application.
|
"""Find and load the plugins for this application.
|
||||||
|
|
@ -193,8 +184,6 @@ class Application:
|
||||||
"future version."
|
"future version."
|
||||||
)
|
)
|
||||||
self.parsed_diff = utils.parse_unified_diff()
|
self.parsed_diff = utils.parse_unified_diff()
|
||||||
if not self.parsed_diff:
|
|
||||||
self.exit()
|
|
||||||
|
|
||||||
assert self.check_plugins is not None
|
assert self.check_plugins is not None
|
||||||
self.check_plugins.provide_options(
|
self.check_plugins.provide_options(
|
||||||
|
|
@ -268,6 +257,9 @@ class Application:
|
||||||
assert self.file_checker_manager is not None
|
assert self.file_checker_manager is not None
|
||||||
if self.running_against_diff:
|
if self.running_against_diff:
|
||||||
files = sorted(self.parsed_diff)
|
files = sorted(self.parsed_diff)
|
||||||
|
if not files:
|
||||||
|
return
|
||||||
|
|
||||||
self.file_checker_manager.start(files)
|
self.file_checker_manager.start(files)
|
||||||
try:
|
try:
|
||||||
self.file_checker_manager.run()
|
self.file_checker_manager.run()
|
||||||
|
|
@ -388,3 +380,7 @@ class Application:
|
||||||
except exceptions.EarlyQuit:
|
except exceptions.EarlyQuit:
|
||||||
self.catastrophic_failure = True
|
self.catastrophic_failure = True
|
||||||
print("... stopped while processing files")
|
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
|
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.
|
"""Execute the main bit of the application.
|
||||||
|
|
||||||
This handles the creation of an instance of :class:`Application`, runs it,
|
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 = application.Application()
|
||||||
app.run(argv)
|
app.run(argv)
|
||||||
app.exit()
|
return app.exit_code()
|
||||||
|
|
|
||||||
|
|
@ -11,9 +11,8 @@ from flake8.main import cli
|
||||||
|
|
||||||
|
|
||||||
def _call_main(argv, retv=0):
|
def _call_main(argv, retv=0):
|
||||||
with pytest.raises(SystemExit) as excinfo:
|
exit_code = cli.main(argv)
|
||||||
cli.main(argv)
|
assert exit_code == retv
|
||||||
assert excinfo.value.code == retv
|
|
||||||
|
|
||||||
|
|
||||||
def test_diff_option(tmpdir, capsys):
|
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):
|
def test_bug_report_successful(capsys):
|
||||||
"""Test that --bug-report does not crash."""
|
"""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()
|
out, err = capsys.readouterr()
|
||||||
assert json.loads(out)
|
assert json.loads(out)
|
||||||
assert err == ""
|
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):
|
def test_obtaining_args_from_sys_argv_when_not_explicity_provided(capsys):
|
||||||
"""Test that arguments are obtained from 'sys.argv'."""
|
"""Test that arguments are obtained from 'sys.argv'."""
|
||||||
with mock.patch("sys.argv", ["flake8", "--help"]):
|
with pytest.raises(SystemExit) as excinfo:
|
||||||
_call_main(None)
|
with mock.patch("sys.argv", ["flake8", "--help"]):
|
||||||
|
_call_main(None)
|
||||||
|
assert excinfo.value.args[0] == 0
|
||||||
|
|
||||||
out, err = capsys.readouterr()
|
out, err = capsys.readouterr()
|
||||||
assert out.startswith("usage: flake8 [options] file file ...\n")
|
assert out.startswith("usage: flake8 [options] file file ...\n")
|
||||||
|
|
|
||||||
|
|
@ -26,13 +26,13 @@ def application():
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"result_count, catastrophic, exit_zero, value",
|
"result_count, catastrophic, exit_zero, value",
|
||||||
[
|
[
|
||||||
(0, False, False, False),
|
(0, False, False, 0),
|
||||||
(0, True, False, True),
|
(0, True, False, 1),
|
||||||
(2, False, False, True),
|
(2, False, False, 1),
|
||||||
(2, True, False, True),
|
(2, True, False, 1),
|
||||||
(0, True, True, True),
|
(0, True, True, 1),
|
||||||
(2, False, True, False),
|
(2, False, True, 0),
|
||||||
(2, True, True, True),
|
(2, True, True, 1),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_exit_does_raise(
|
def test_exit_does_raise(
|
||||||
|
|
@ -43,10 +43,7 @@ def test_exit_does_raise(
|
||||||
application.catastrophic_failure = catastrophic
|
application.catastrophic_failure = catastrophic
|
||||||
application.options = options(exit_zero=exit_zero)
|
application.options = options(exit_zero=exit_zero)
|
||||||
|
|
||||||
with pytest.raises(SystemExit) as excinfo:
|
assert application.exit_code() == value
|
||||||
application.exit()
|
|
||||||
|
|
||||||
assert excinfo.value.args[0] is value
|
|
||||||
|
|
||||||
|
|
||||||
def test_warns_on_unknown_formatter_plugin_name(application):
|
def test_warns_on_unknown_formatter_plugin_name(application):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue