diff --git a/httpie/cli/argparser.py b/httpie/cli/argparser.py index 6a62f26d..5c79a301 100644 --- a/httpie/cli/argparser.py +++ b/httpie/cli/argparser.py @@ -24,7 +24,7 @@ from httpie.cli.constants import ( ) from httpie.cli.exceptions import ParseError from httpie.cli.requestitems import RequestItems -from httpie.context import Environment +from httpie.context import Environment, DEVNULL_PATH from httpie.plugins.registry import plugin_manager from httpie.utils import ExplicitNullAuth, get_content_type @@ -135,16 +135,11 @@ class HTTPieArgumentParser(argparse.ArgumentParser): Modify `env.stdout` and `env.stdout_isatty` based on args, if needed. """ - if self.args.quiet: - self.env.stdout = self.env.devnull - self.env.stdout_isatty = self.env.devnull_isatty - self.env.stderr = self.env.devnull - self.env.stderr_isatty = self.env.devnull_isatty self.args.output_file_specified = bool(self.args.output_file) if self.args.download: # FIXME: Come up with a cleaner solution. - if not self.args.output_file and not self.env.stdout_isatty and not self.args.quiet: + if not self.args.output_file and not self.env.stdout_isatty: # Use stdout as the download output file. self.args.output_file = self.env.stdout # With `--download`, we write everything that would normally go to @@ -169,6 +164,11 @@ class HTTPieArgumentParser(argparse.ArgumentParser): self.env.stdout = self.args.output_file self.env.stdout_isatty = False + if self.args.quiet: + self.env.devnull = open(DEVNULL_PATH, 'w') + self.env.stdout = self.env.devnull + self.env.stderr = self.env.devnull + def _process_auth(self): # TODO: refactor & simplify this method. self.args.auth_plugin = None diff --git a/httpie/context.py b/httpie/context.py index 30010ebd..e5d20dbd 100644 --- a/httpie/context.py +++ b/httpie/context.py @@ -14,6 +14,8 @@ from httpie.config import DEFAULT_CONFIG_DIR, Config, ConfigFileError from httpie.utils import repr_dict +DEVNULL_PATH = os.devnull + class Environment: """ @@ -35,8 +37,8 @@ class Environment: stdout_encoding: str = None stderr: IO = sys.stderr stderr_isatty: bool = stderr.isatty() - devnull: IO = open(os.devnull, "w") - devnull_isatty: bool = devnull.isatty() + devnull: IO = None + devnull_isatty: bool = False colors = 256 program_name: str = 'http' if not is_windows: @@ -48,7 +50,7 @@ class Environment: pass else: # noinspection PyUnresolvedReferences - import colorama.initialisex + import colorama.initialise stdout = colorama.initialise.wrap_stream( stdout, convert=None, strip=None, autoreset=True, wrap=True diff --git a/tests/test_downloads.py b/tests/test_downloads.py index 572d039f..ab6da161 100644 --- a/tests/test_downloads.py +++ b/tests/test_downloads.py @@ -189,3 +189,11 @@ class TestDownloads: assert os.listdir('.') == [expected_filename] finally: os.chdir(orig_cwd) + + def test_download_quietflag(self, httpbin_both, httpbin): + robots_txt = '/robots.txt' + body = urlopen(httpbin + robots_txt).read().decode() + env = MockEnvironment(stdin_isatty=True, stdout_isatty=False) + r = http('--quiet', '--download', httpbin_both.url + robots_txt, env=env) + assert r.stderr == '' + assert body == r diff --git a/tests/test_output.py b/tests/test_output.py index 8282d2c7..e0a395c1 100644 --- a/tests/test_output.py +++ b/tests/test_output.py @@ -32,6 +32,15 @@ def test_output_option(httpbin, stdout_isatty): assert actual_body == expected_body +class TestQuietFlag: + def test_quiet(self, httpbin): + r = http('--quiet', 'GET', httpbin.url + '/get') + # check stdin + assert r.stderr == '' + # Check stdout + assert str(r) == '' + + class TestVerboseFlag: def test_verbose(self, httpbin): r = http('--verbose',