Added tests for --quiet flag

This commit is contained in:
Nicolas Beltran 2020-06-27 11:08:44 -05:00 committed by Jakub Roztocil
parent a4a1e8d43b
commit 4bd2e622a5
4 changed files with 29 additions and 10 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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',