From d5460813400032794d45f3d5eb73f23157c95395 Mon Sep 17 00:00:00 2001 From: Nicolas Beltran Date: Tue, 14 Jul 2020 16:31:46 -0500 Subject: [PATCH] Solved issue pertaining to downloads and added additional testing functionality for devnull --- httpie/context.py | 15 +++++++++++++-- tests/utils.py | 14 +++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/httpie/context.py b/httpie/context.py index e5d20dbd..ad17a1ca 100644 --- a/httpie/context.py +++ b/httpie/context.py @@ -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') diff --git a/tests/utils.py b/tests/utils.py index 03c948c3..b73d1117 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -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,17 +163,20 @@ 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. 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: `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()