From 66e168b2afe4c9ba94275eb8649ddb8a03779679 Mon Sep 17 00:00:00 2001 From: Jakub Roztocil Date: Wed, 2 Mar 2016 13:16:41 +0800 Subject: [PATCH] Improved failed test output --- CHANGELOG.rst | 4 ++-- httpie/__init__.py | 7 +++++++ pytest.ini | 1 - tests/utils.py | 20 +++++++++++++++----- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 762deacb..4e9d46ee 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -12,12 +12,12 @@ This project adheres to `Semantic Versioning `_. * Added ``Content-Type`` of files uploaded in ``multipart/form-data`` requests * Added ``--ssl=`` to specify the desired SSL/TLS protocol version to use for HTTPS requests. +* Added JSON detection with ``--json, -j`` to work around incorrect + ``Content-Type`` * Added ``--show-redirects, -R`` to show intermediate responses with ``--follow`` * Added ``--max-redirects`` (default 30) * Added ``-A`` as short name for ``--auth-type`` * Added ``-F`` as short name for ``--follow`` -* Added JSON detection with ``--json, -j`` to work around incorrect - ``Content-Type`` * Redirected ``stdout`` doesn't trigger an error anymore when ``--output FILE`` is set. * Changed the default ``--style`` back to ``solarized`` for better support diff --git a/httpie/__init__.py b/httpie/__init__.py index c4f7b829..6c7fe03b 100644 --- a/httpie/__init__.py +++ b/httpie/__init__.py @@ -18,3 +18,10 @@ class ExitStatus: ERROR_HTTP_3XX = 3 ERROR_HTTP_4XX = 4 ERROR_HTTP_5XX = 5 + + +EXIT_STATUS_LABELS = dict( + (value, key) + for key, value in ExitStatus.__dict__.items() + if key.isupper() +) diff --git a/pytest.ini b/pytest.ini index 9735e581..f132993d 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,3 +1,2 @@ [pytest] -addopts = --tb=native norecursedirs = tests/fixtures diff --git a/tests/utils.py b/tests/utils.py index e54969e7..be2f80e2 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -6,7 +6,7 @@ import time import json import tempfile -import httpie +from httpie import ExitStatus, EXIT_STATUS_LABELS from httpie.context import Environment from httpie.core import main from httpie.compat import bytes, str @@ -135,6 +135,10 @@ class StrCLIResponse(str, BaseCLIResponse): return self._json +class ExitStatusException(Exception): + pass + + def http(*args, **kwargs): # noinspection PyUnresolvedReferences """ @@ -205,7 +209,7 @@ def http(*args, **kwargs): time.sleep(.5) except SystemExit: if error_exit_ok: - exit_status = httpie.ExitStatus.ERROR + exit_status = ExitStatus.ERROR else: dump_stderr() raise @@ -214,9 +218,15 @@ def http(*args, **kwargs): sys.stderr.write(stderr.read()) raise else: - if exit_status != httpie.ExitStatus.OK and not error_exit_ok: + if not error_exit_ok and exit_status != ExitStatus.OK: dump_stderr() - raise Exception('Unexpected exit status: %s', exit_status) + raise ExitStatusException( + 'httpie.core.main() unexpectedly returned' + ' a non-zero exit status: {0} ({1})'.format( + exit_status, + EXIT_STATUS_LABELS[exit_status] + ) + ) stdout.seek(0) stderr.seek(0) @@ -232,7 +242,7 @@ def http(*args, **kwargs): r.stderr = stderr.read() r.exit_status = exit_status - if r.exit_status != httpie.ExitStatus.OK: + if r.exit_status != ExitStatus.OK: sys.stderr.write(r.stderr) return r