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

@ -94,10 +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)
- exit_status output: print(self.exit_status) - exit_status output: print(self.exit_status)
""" """
stderr: str = None stderr: str = None
devnull: str = None
json: dict = None json: dict = None
exit_status: ExitStatus = None exit_status: ExitStatus = None
@ -161,17 +163,20 @@ 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
env.devnull in env does not correspond to os.devnull file.
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.
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
`exit_status`: the exit status `exit_status`: the exit status
`json`: decoded JSON (if possible) or `None` `json`: decoded JSON (if possible) or `None`
@ -204,6 +209,7 @@ def http(
stdout = env.stdout stdout = env.stdout
stderr = env.stderr stderr = env.stderr
devnull = env.devnull
args = list(args) args = list(args)
args_with_config_defaults = args + env.config.default_options args_with_config_defaults = args + env.config.default_options
@ -249,13 +255,18 @@ def http(
stdout.seek(0) stdout.seek(0)
stderr.seek(0) stderr.seek(0)
devnull.seek(0)
output = stdout.read() output = stdout.read()
devnull_output = devnull.read()
try: try:
output = output.decode('utf8') output = output.decode('utf8')
except UnicodeDecodeError: except UnicodeDecodeError:
r = BytesCLIResponse(output) r = BytesCLIResponse(output)
else: else:
r = StrCLIResponse(output) r = StrCLIResponse(output)
r.devnull = devnull_output
r.stderr = stderr.read() r.stderr = stderr.read()
r.exit_status = exit_status r.exit_status = exit_status
@ -265,6 +276,7 @@ def http(
return r return r
finally: finally:
devnull.close()
stdout.close() stdout.close()
stderr.close() stderr.close()
env.cleanup() env.cleanup()