From 69e1067a2c84000d218e432c104a68d5f8f1d4fc Mon Sep 17 00:00:00 2001 From: Nicolas Beltran Date: Tue, 14 Jul 2020 17:21:57 -0500 Subject: [PATCH] Aesthetic changes --- httpie/context.py | 6 ++---- tests/test_downloads.py | 1 - tests/test_output.py | 11 ++++++----- tests/utils.py | 19 ++++++++++++------- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/httpie/context.py b/httpie/context.py index ad17a1ca..945b3c15 100644 --- a/httpie/context.py +++ b/httpie/context.py @@ -112,18 +112,16 @@ class Environment: self.log_error(e, level='warning') return config - - @property + @property def devnull(self) -> IO: if self._devnull is None: self._devnull = open(os.devnull, 'w+') return self._devnull - #For ease of testing @devnull.setter def devnull(self, value): self._devnull = value - + def log_error(self, msg, level='error'): assert level in ['error', 'warning'] self.stderr.write(f'\n{self.program_name}: {level}: {msg}\n\n') diff --git a/tests/test_downloads.py b/tests/test_downloads.py index 48180180..21623b56 100644 --- a/tests/test_downloads.py +++ b/tests/test_downloads.py @@ -199,4 +199,3 @@ class TestDownloads: assert r.stderr == '' assert env.devnull == env.stderr assert env.devnull == env.stdout - diff --git a/tests/test_output.py b/tests/test_output.py index 4de77196..41ef5d3f 100644 --- a/tests/test_output.py +++ b/tests/test_output.py @@ -2,7 +2,7 @@ import argparse import mock import json import os -import io +import io from tempfile import gettempdir from urllib.request import urlopen @@ -54,18 +54,18 @@ class TestQuietFlag: assert env.stdout == env.devnull assert env.stderr == env.devnull assert r.stderr == '' - assert HTTP_OK in r.devnull.decode() + assert HTTP_OK in r.devnull assert str(r) == '' @mock.patch('httpie.cli.argtypes.AuthCredentials._getpass', - new=lambda self, prompt:'password') + new=lambda self, prompt: 'password') def test_quiet_password_prompt(self, httpbin): """ Tests whether httpie still prompts for password when request requires authetication and only username is provided""" env = MockEnvironment(stdin_isatty=True, stdout_isatty=True) env.devnull = io.BytesIO() - r = http('--quiet', '--auth', 'user','GET', httpbin.url + '/basic-auth/user/password', env=env) - assert HTTP_OK in r.devnull.decode() + r = http('--quiet', '--auth', 'user', 'GET', httpbin.url + '/basic-auth/user/password', env=env) + assert HTTP_OK in r.devnull assert env.stdout == env.devnull assert env.stderr == env.devnull assert str(r) == '' @@ -80,6 +80,7 @@ class TestQuietFlag: assert str(r) == '' assert r.stderr == '' + class TestVerboseFlag: def test_verbose(self, httpbin): r = http('--verbose', diff --git a/tests/utils.py b/tests/utils.py index b73d1117..829e9f27 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -94,12 +94,12 @@ class BaseCLIResponse: - stdout output: print(self) - stderr output: print(self.stderr) - - devnull output: print(self.devnull) + - devnull output: print(self.devnull) - exit_status output: print(self.exit_status) """ stderr: str = None - devnull: str = None + devnull: str = None json: dict = None exit_status: ExitStatus = None @@ -163,20 +163,21 @@ def http( # noinspection PyUnresolvedReferences """ Run HTTPie and capture stderr/out and exit status. - Content writtent to devnull will be captured if - env.devnull in env does not correspond to os.devnull file. + Content writtent to devnull will be captured only if + env.devnull is set manually. Invoke `httpie.core.main()` with `args` and `kwargs`, and return a `CLIResponse` subclass instance. The return value is either a `StrCLIResponse`, or `BytesCLIResponse` - if unable to decode the output. + if unable to decode the output. Devnull is string when possible, + bytes otherwise. The response has the following attributes: `stdout` is represented by the instance itself (print r) `stderr`: text written to stderr - `devnull` text written to devnull. String if possible bytes otherwise + `devnull` text written to devnull. `exit_status`: the exit status `json`: decoded JSON (if possible) or `None` @@ -264,7 +265,11 @@ def http( r = BytesCLIResponse(output) else: r = StrCLIResponse(output) - + + try: + devnull_output = devnull_output.decode('utf8') + except Exception: + pass r.devnull = devnull_output r.stderr = stderr.read()