Solved issue pertaining to downloads and added additional testing functionality for devnull

This commit is contained in:
Nicolas Beltran 2020-07-14 16:31:46 -05:00 committed by Jakub Roztocil
parent 6421c145d9
commit d546081340
2 changed files with 26 additions and 3 deletions

View File

@ -37,8 +37,7 @@ class Environment:
stdout_encoding: str = None
stderr: IO = sys.stderr
stderr_isatty: bool = stderr.isatty()
devnull: IO = None
devnull_isatty: bool = False
_devnull = None
colors = 256
program_name: str = 'http'
if not is_windows:
@ -113,6 +112,18 @@ class Environment:
self.log_error(e, level='warning')
return config
@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')

View File

@ -94,10 +94,12 @@ class BaseCLIResponse:
- stdout output: print(self)
- stderr output: print(self.stderr)
- devnull output: print(self.devnull)
- exit_status output: print(self.exit_status)
"""
stderr: str = None
devnull: str = None
json: dict = None
exit_status: ExitStatus = None
@ -161,6 +163,8 @@ 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.
Invoke `httpie.core.main()` with `args` and `kwargs`,
and return a `CLIResponse` subclass instance.
@ -172,6 +176,7 @@ def http(
`stdout` is represented by the instance itself (print r)
`stderr`: text written to stderr
`devnull` text written to devnull. String if possible bytes otherwise
`exit_status`: the exit status
`json`: decoded JSON (if possible) or `None`
@ -204,6 +209,7 @@ def http(
stdout = env.stdout
stderr = env.stderr
devnull = env.devnull
args = list(args)
args_with_config_defaults = args + env.config.default_options
@ -249,13 +255,18 @@ def http(
stdout.seek(0)
stderr.seek(0)
devnull.seek(0)
output = stdout.read()
devnull_output = devnull.read()
try:
output = output.decode('utf8')
except UnicodeDecodeError:
r = BytesCLIResponse(output)
else:
r = StrCLIResponse(output)
r.devnull = devnull_output
r.stderr = stderr.read()
r.exit_status = exit_status
@ -265,6 +276,7 @@ def http(
return r
finally:
devnull.close()
stdout.close()
stderr.close()
env.cleanup()