have application return exit code for easier testing

This commit is contained in:
Anthony Sottile 2021-11-14 16:24:59 -08:00
parent 5a85dd8ddb
commit 81a4110338
5 changed files with 33 additions and 36 deletions

View file

@ -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")

View file

@ -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):