Skip to content

Commit 6befaf9

Browse files
dkreeftKreeftjkbrztBoboTiG
authored
Added the ability to silence warnings via double -q or --quiet (#1175)
* change behavior of '--quiet' to silence errors and warnings when passed twice together with '--check-status' * Apply suggestions from code review Co-authored-by: Jakub Roztocil <[email protected]> * remove header, trailing comma, rename constant and variable * fix flags for tests * [skip ci] Update ticket number Co-authored-by: Dave <[email protected]> Co-authored-by: Jakub Roztocil <[email protected]> Co-authored-by: Mickaël Schoentgen <[email protected]>
1 parent 1b7f74c commit 6befaf9

File tree

5 files changed

+44
-15
lines changed

5 files changed

+44
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
55

66
## [2.6.0.dev0](https://github.com/httpie/httpie/compare/2.5.0...master) (unreleased)
77

8+
- Added the ability to silence warnings through using `-q` or `--quiet` twice (e.g. `-qq`) ([#1175](https://github.com/httpie/httpie/issues/1175))
89
- Added support for formatting & coloring of JSON bodies preceded by non-JSON data (e.g., an XXSI prefix). ([#1130](https://github.com/httpie/httpie/issues/1130))
910
- Added `--response-charset` to allow overriding the response encoding for terminal display purposes. ([#1168](https://github.com/httpie/httpie/issues/1168))
1011
- Added `--response-mime` to allow overriding the response mime type for coloring and formatting for the terminal. ([#1168](https://github.com/httpie/httpie/issues/1168))

docs/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,13 @@ This doesn’t affect output to a file via `--output` or `--download`.
12471247
$ http --quiet pie.dev/post enjoy='the silence'
12481248
```
12491249
1250+
If you’d like to silence warnings as well, use `-q` or `--quiet` twice:
1251+
1252+
```bash
1253+
# There will be no output, even in case of an unexpected response status code:
1254+
$ http -qq --check-status pie.dev/post enjoy='the silence without warnings'
1255+
```
1256+
12501257
### Viewing intermediary requests/responses
12511258
12521259
To see all the HTTP communication, i.e. the final request/response as well as any possible intermediary requests/responses, use the `--all` option.

httpie/cli/definition.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,12 +497,14 @@
497497

498498
output_options.add_argument(
499499
'--quiet', '-q',
500-
action='store_true',
501-
default=False,
500+
action='count',
501+
default=0,
502502
help='''
503-
Do not print to stdout or stderr.
503+
Do not print to stdout or stderr, except for errors and warnings when provided once.
504+
Provide twice to suppress warnings as well.
504505
stdout is still redirected if --output is specified.
505506
Flag doesn't affect behaviour of download beyond not printing to terminal.
507+
506508
'''
507509
)
508510

httpie/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def request_body_read_callback(chunk: bytes):
185185
final_response = message
186186
if args.check_status or downloader:
187187
exit_status = http_status_to_exit_status(http_status=message.status_code, follow=args.follow)
188-
if exit_status != ExitStatus.SUCCESS and (not env.stdout_isatty or args.quiet):
188+
if exit_status != ExitStatus.SUCCESS and (not env.stdout_isatty or args.quiet == 1):
189189
env.log_error(f'HTTP {message.raw.status} {message.raw.reason}', level='warning')
190190
write_message(requests_message=message, env=env, args=args, with_headers=with_headers,
191191
with_body=do_write_body)

tests/test_output.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,16 @@ def test_output_option(tmp_path, httpbin, stdout_isatty):
3939

4040

4141
class TestQuietFlag:
42+
QUIET_SCENARIOS = [('--quiet',), ('-q',), ('--quiet', '--quiet'), ('-qq',)]
4243

43-
@pytest.mark.parametrize('argument_name', ['--quiet', '-q'])
44-
def test_quiet(self, httpbin, argument_name):
44+
@pytest.mark.parametrize('quiet_flags', QUIET_SCENARIOS)
45+
def test_quiet(self, httpbin, quiet_flags):
4546
env = MockEnvironment(
4647
stdin_isatty=True,
4748
stdout_isatty=True,
4849
devnull=io.BytesIO()
4950
)
50-
r = http(argument_name, 'GET', httpbin.url + '/get', env=env)
51+
r = http(*quiet_flags, 'GET', httpbin.url + '/get', env=env)
5152
assert env.stdout is env.devnull
5253
assert env.stderr is env.devnull
5354
assert HTTP_OK in r.devnull
@@ -69,9 +70,25 @@ def test_quiet_with_check_status_non_zero_pipe(self, httpbin):
6970
)
7071
assert 'http: warning: HTTP 500' in r.stderr
7172

73+
def test_quiet_quiet_with_check_status_non_zero(self, httpbin):
74+
r = http(
75+
'--quiet', '--quiet', '--check-status', httpbin + '/status/500',
76+
tolerate_error_exit_status=True,
77+
)
78+
assert not r.stderr
79+
80+
def test_quiet_quiet_with_check_status_non_zero_pipe(self, httpbin):
81+
r = http(
82+
'--quiet', '--quiet', '--check-status', httpbin + '/status/500',
83+
tolerate_error_exit_status=True,
84+
env=MockEnvironment(stdout_isatty=False)
85+
)
86+
assert 'http: warning: HTTP 500' in r.stderr
87+
88+
@pytest.mark.parametrize('quiet_flags', QUIET_SCENARIOS)
7289
@mock.patch('httpie.cli.argtypes.AuthCredentials._getpass',
7390
new=lambda self, prompt: 'password')
74-
def test_quiet_with_password_prompt(self, httpbin):
91+
def test_quiet_with_password_prompt(self, httpbin, quiet_flags):
7592
"""
7693
Tests whether httpie still prompts for a password when request
7794
requires authentication and only username is provided
@@ -83,7 +100,7 @@ def test_quiet_with_password_prompt(self, httpbin):
83100
devnull=io.BytesIO()
84101
)
85102
r = http(
86-
'--quiet', '--auth', 'user', 'GET',
103+
*quiet_flags, '--auth', 'user', 'GET',
87104
httpbin.url + '/basic-auth/user/password',
88105
env=env
89106
)
@@ -93,17 +110,19 @@ def test_quiet_with_password_prompt(self, httpbin):
93110
assert r == ''
94111
assert r.stderr == ''
95112

96-
@pytest.mark.parametrize('argument_name', ['-h', '-b', '-v', '-p=hH'])
97-
def test_quiet_with_explicit_output_options(self, httpbin, argument_name):
113+
@pytest.mark.parametrize('quiet_flags', QUIET_SCENARIOS)
114+
@pytest.mark.parametrize('output_options', ['-h', '-b', '-v', '-p=hH'])
115+
def test_quiet_with_explicit_output_options(self, httpbin, quiet_flags, output_options):
98116
env = MockEnvironment(stdin_isatty=True, stdout_isatty=True)
99-
r = http('--quiet', argument_name, httpbin.url + '/get', env=env)
117+
r = http(*quiet_flags, output_options, httpbin.url + '/get', env=env)
100118
assert env.stdout is env.devnull
101119
assert env.stderr is env.devnull
102120
assert r == ''
103121
assert r.stderr == ''
104122

123+
@pytest.mark.parametrize('quiet_flags', QUIET_SCENARIOS)
105124
@pytest.mark.parametrize('with_download', [True, False])
106-
def test_quiet_with_output_redirection(self, tmp_path, httpbin, with_download):
125+
def test_quiet_with_output_redirection(self, tmp_path, httpbin, quiet_flags, with_download):
107126
url = httpbin + '/robots.txt'
108127
output_path = Path('output.txt')
109128
env = MockEnvironment()
@@ -114,7 +133,7 @@ def test_quiet_with_output_redirection(self, tmp_path, httpbin, with_download):
114133
try:
115134
assert os.listdir('.') == []
116135
r = http(
117-
'--quiet',
136+
*quiet_flags,
118137
'--output', str(output_path),
119138
*extra_args,
120139
url,
@@ -142,7 +161,7 @@ def test_verbose(self, httpbin):
142161

143162
def test_verbose_raw(self, httpbin):
144163
r = http('--verbose', '--raw', 'foo bar',
145-
'POST', httpbin.url + '/post',)
164+
'POST', httpbin.url + '/post')
146165
assert HTTP_OK in r
147166
assert 'foo bar' in r
148167

0 commit comments

Comments
 (0)