Aesthetic changes

This commit is contained in:
Nicolas Beltran 2020-07-14 17:21:57 -05:00 committed by Jakub Roztocil
parent 7e38f9ccf0
commit 69e1067a2c
4 changed files with 20 additions and 17 deletions

View File

@ -112,18 +112,16 @@ class Environment:
self.log_error(e, level='warning') self.log_error(e, level='warning')
return config return config
@property
@property
def devnull(self) -> IO: def devnull(self) -> IO:
if self._devnull is None: if self._devnull is None:
self._devnull = open(os.devnull, 'w+') self._devnull = open(os.devnull, 'w+')
return self._devnull return self._devnull
#For ease of testing
@devnull.setter @devnull.setter
def devnull(self, value): def devnull(self, value):
self._devnull = value self._devnull = value
def log_error(self, msg, level='error'): def log_error(self, msg, level='error'):
assert level in ['error', 'warning'] assert level in ['error', 'warning']
self.stderr.write(f'\n{self.program_name}: {level}: {msg}\n\n') self.stderr.write(f'\n{self.program_name}: {level}: {msg}\n\n')

View File

@ -199,4 +199,3 @@ class TestDownloads:
assert r.stderr == '' assert r.stderr == ''
assert env.devnull == env.stderr assert env.devnull == env.stderr
assert env.devnull == env.stdout assert env.devnull == env.stdout

View File

@ -2,7 +2,7 @@ import argparse
import mock import mock
import json import json
import os import os
import io import io
from tempfile import gettempdir from tempfile import gettempdir
from urllib.request import urlopen from urllib.request import urlopen
@ -54,18 +54,18 @@ class TestQuietFlag:
assert env.stdout == env.devnull assert env.stdout == env.devnull
assert env.stderr == env.devnull assert env.stderr == env.devnull
assert r.stderr == '' assert r.stderr == ''
assert HTTP_OK in r.devnull.decode() assert HTTP_OK in r.devnull
assert str(r) == '' assert str(r) == ''
@mock.patch('httpie.cli.argtypes.AuthCredentials._getpass', @mock.patch('httpie.cli.argtypes.AuthCredentials._getpass',
new=lambda self, prompt:'password') new=lambda self, prompt: 'password')
def test_quiet_password_prompt(self, httpbin): def test_quiet_password_prompt(self, httpbin):
""" Tests whether httpie still prompts for password when request """ Tests whether httpie still prompts for password when request
requires authetication and only username is provided""" requires authetication and only username is provided"""
env = MockEnvironment(stdin_isatty=True, stdout_isatty=True) env = MockEnvironment(stdin_isatty=True, stdout_isatty=True)
env.devnull = io.BytesIO() env.devnull = io.BytesIO()
r = http('--quiet', '--auth', 'user','GET', httpbin.url + '/basic-auth/user/password', env=env) r = http('--quiet', '--auth', 'user', 'GET', httpbin.url + '/basic-auth/user/password', env=env)
assert HTTP_OK in r.devnull.decode() assert HTTP_OK in r.devnull
assert env.stdout == env.devnull assert env.stdout == env.devnull
assert env.stderr == env.devnull assert env.stderr == env.devnull
assert str(r) == '' assert str(r) == ''
@ -80,6 +80,7 @@ class TestQuietFlag:
assert str(r) == '' assert str(r) == ''
assert r.stderr == '' assert r.stderr == ''
class TestVerboseFlag: class TestVerboseFlag:
def test_verbose(self, httpbin): def test_verbose(self, httpbin):
r = http('--verbose', r = http('--verbose',

View File

@ -94,12 +94,12 @@ class BaseCLIResponse:
- stdout output: print(self) - stdout output: print(self)
- stderr output: print(self.stderr) - stderr output: print(self.stderr)
- devnull output: print(self.devnull) - devnull output: print(self.devnull)
- exit_status output: print(self.exit_status) - exit_status output: print(self.exit_status)
""" """
stderr: str = None stderr: str = None
devnull: str = None devnull: str = None
json: dict = None json: dict = None
exit_status: ExitStatus = None exit_status: ExitStatus = None
@ -163,20 +163,21 @@ def http(
# noinspection PyUnresolvedReferences # noinspection PyUnresolvedReferences
""" """
Run HTTPie and capture stderr/out and exit status. Run HTTPie and capture stderr/out and exit status.
Content writtent to devnull will be captured if Content writtent to devnull will be captured only if
env.devnull in env does not correspond to os.devnull file. env.devnull is set manually.
Invoke `httpie.core.main()` with `args` and `kwargs`, Invoke `httpie.core.main()` with `args` and `kwargs`,
and return a `CLIResponse` subclass instance. and return a `CLIResponse` subclass instance.
The return value is either a `StrCLIResponse`, or `BytesCLIResponse` 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: The response has the following attributes:
`stdout` is represented by the instance itself (print r) `stdout` is represented by the instance itself (print r)
`stderr`: text written to stderr `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 `exit_status`: the exit status
`json`: decoded JSON (if possible) or `None` `json`: decoded JSON (if possible) or `None`
@ -264,7 +265,11 @@ def http(
r = BytesCLIResponse(output) r = BytesCLIResponse(output)
else: else:
r = StrCLIResponse(output) r = StrCLIResponse(output)
try:
devnull_output = devnull_output.decode('utf8')
except Exception:
pass
r.devnull = devnull_output r.devnull = devnull_output
r.stderr = stderr.read() r.stderr = stderr.read()