From 9b23a4ac9a08e3f63677b328b3838816b08d8459 Mon Sep 17 00:00:00 2001 From: Jakub Roztocil Date: Wed, 26 Oct 2016 11:53:01 +0200 Subject: [PATCH] Exit with status 130 on CTRL-C http://www.tldp.org/LDP/abs/html/exitcodes.html #531 --- CHANGELOG.rst | 1 + httpie/__init__.py | 4 ++++ httpie/__main__.py | 3 ++- httpie/core.py | 4 ++-- tests/test_exit_status.py | 18 +++++++++++++++++- tests/utils.py | 2 +- 6 files changed, 27 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 0e347207..b3d4ee44 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -11,6 +11,7 @@ This project adheres to `Semantic Versioning `_. * Added support for ``curses``-less Python installations. * Fixed ``REQUEST_ITEM`` arg incorrectly being reported as required. +* Added 130 ``REQUEST_ITEM`` arg incorrectly being reported as required. `0.9.6`_ (2016-08-13) diff --git a/httpie/__init__.py b/httpie/__init__.py index 6c7fe03b..05b4cc0e 100644 --- a/httpie/__init__.py +++ b/httpie/__init__.py @@ -11,6 +11,10 @@ class ExitStatus: """Exit status code constants.""" OK = 0 ERROR = 1 + + # 128+2 SIGINT + ERROR_CTRL_C = 130 + ERROR_TIMEOUT = 2 ERROR_TOO_MANY_REDIRECTS = 6 diff --git a/httpie/__main__.py b/httpie/__main__.py index 41913942..052d0d1d 100644 --- a/httpie/__main__.py +++ b/httpie/__main__.py @@ -10,7 +10,8 @@ def main(): from .core import main sys.exit(main()) except KeyboardInterrupt: - sys.exit(1) + from . import ExitStatus + sys.exit(ExitStatus.ERROR_CTRL_C) if __name__ == '__main__': diff --git a/httpie/core.py b/httpie/core.py index 7d7cca5e..17017502 100644 --- a/httpie/core.py +++ b/httpie/core.py @@ -212,7 +212,7 @@ def main(args=sys.argv[1:], env=Environment(), custom_log_error=None): env.stderr.write('\n') if include_traceback: raise - exit_status = ExitStatus.ERROR + exit_status = ExitStatus.ERROR_CTRL_C except SystemExit as e: if e.code != ExitStatus.OK: env.stderr.write('\n') @@ -230,7 +230,7 @@ def main(args=sys.argv[1:], env=Environment(), custom_log_error=None): env.stderr.write('\n') if include_traceback: raise - exit_status = ExitStatus.ERROR + exit_status = ExitStatus.ERROR_CTRL_C except SystemExit as e: if e.code != ExitStatus.OK: env.stderr.write('\n') diff --git a/tests/test_exit_status.py b/tests/test_exit_status.py index 9249c87b..8a73ff2b 100644 --- a/tests/test_exit_status.py +++ b/tests/test_exit_status.py @@ -1,9 +1,25 @@ +import mock + from httpie import ExitStatus from utils import TestEnvironment, http, HTTP_OK +def test_keyboard_interrupt_during_arg_parsing_exit_status(httpbin): + with mock.patch('httpie.cli.parser.parse_args', + side_effect=KeyboardInterrupt()): + r = http('GET', httpbin.url + '/status/200', error_exit_ok=True) + assert r.exit_status == ExitStatus.ERROR_CTRL_C + + +def test_keyboard_interrupt_in_program_exit_status(httpbin): + with mock.patch('httpie.core.program', + side_effect=KeyboardInterrupt()): + r = http('GET', httpbin.url + '/status/200', error_exit_ok=True) + assert r.exit_status == ExitStatus.ERROR_CTRL_C + + def test_ok_response_exits_0(httpbin): - r = http('GET', httpbin.url + '/status/200') + r = http('GET', httpbin.url + '/get') assert HTTP_OK in r assert r.exit_status == ExitStatus.OK diff --git a/tests/utils.py b/tests/utils.py index feede790..63a4288a 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -192,7 +192,7 @@ def http(*args, **kwargs): args_with_config_defaults = args + env.config.default_options add_to_args = [] if '--debug' not in args_with_config_defaults: - if '--traceback' not in args_with_config_defaults: + if not error_exit_ok and '--traceback' not in args_with_config_defaults: add_to_args.append('--traceback') if not any('--timeout' in arg for arg in args_with_config_defaults): add_to_args.append('--timeout=3')