From 4ff22defe4037a8a27fa69baa8d0c622292dc83e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Schoentgen?= Date: Fri, 6 Aug 2021 16:57:19 +0200 Subject: [PATCH] Rework `__main__.py` to follow best practices (#1124) It also simplifies how the `main()` function could be tested. --- httpie/__main__.py | 8 ++++---- tests/test_httpie.py | 8 ++------ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/httpie/__main__.py b/httpie/__main__.py index 319c6e24..7b5042b8 100644 --- a/httpie/__main__.py +++ b/httpie/__main__.py @@ -1,7 +1,6 @@ """The main entry point. Invoke as `http' or `python -m httpie'. """ -import sys def main(): @@ -12,8 +11,9 @@ def main(): from httpie.status import ExitStatus exit_status = ExitStatus.ERROR_CTRL_C - sys.exit(exit_status.value) + return exit_status.value -if __name__ == '__main__': - main() +if __name__ == '__main__': # pragma: nocover + import sys + sys.exit(main()) diff --git a/tests/test_httpie.py b/tests/test_httpie.py index e45e0d29..3ed1bb2c 100644 --- a/tests/test_httpie.py +++ b/tests/test_httpie.py @@ -17,18 +17,14 @@ from .utils import HTTP_OK, MockEnvironment, StdinBytesIO, http def test_main_entry_point(): # Patch stdin to bypass pytest capture with mock.patch.object(Environment, 'stdin', io.StringIO()): - with pytest.raises(SystemExit) as e: - httpie.__main__.main() - assert e.value.code == ExitStatus.ERROR + assert httpie.__main__.main() == ExitStatus.ERROR.value @mock.patch('httpie.core.main') def test_main_entry_point_keyboard_interrupt(main): main.side_effect = KeyboardInterrupt() with mock.patch.object(Environment, 'stdin', io.StringIO()): - with pytest.raises(SystemExit) as e: - httpie.__main__.main() - assert e.value.code == ExitStatus.ERROR_CTRL_C + assert httpie.__main__.main() == ExitStatus.ERROR_CTRL_C.value def test_debug():