mirror of
https://github.com/httpie/cli.git
synced 2024-11-08 00:44:45 +01:00
Minor clean-up (#1078)
- Remove default arguments to `open()`. - Make use of `pytest` mechanisms for temporary folders.
This commit is contained in:
parent
611b278b63
commit
a61f9e1114
@ -84,7 +84,7 @@ class BaseConfigDict(dict):
|
|||||||
def load(self):
|
def load(self):
|
||||||
config_type = type(self).__name__.lower()
|
config_type = type(self).__name__.lower()
|
||||||
try:
|
try:
|
||||||
with self.path.open('rt') as f:
|
with self.path.open() as f:
|
||||||
try:
|
try:
|
||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
|
@ -7,3 +7,5 @@ from .base import (
|
|||||||
AuthPlugin, FormatterPlugin,
|
AuthPlugin, FormatterPlugin,
|
||||||
ConverterPlugin, TransportPlugin
|
ConverterPlugin, TransportPlugin
|
||||||
)
|
)
|
||||||
|
|
||||||
|
__all__ = ('AuthPlugin', 'ConverterPlugin', 'FormatterPlugin', 'TransportPlugin')
|
||||||
|
@ -40,4 +40,4 @@ def _httpbin_with_chunked_support_available():
|
|||||||
def httpbin_with_chunked_support(_httpbin_with_chunked_support_available):
|
def httpbin_with_chunked_support(_httpbin_with_chunked_support_available):
|
||||||
if _httpbin_with_chunked_support_available:
|
if _httpbin_with_chunked_support_available:
|
||||||
return HTTPBIN_WITH_CHUNKED_SUPPORT
|
return HTTPBIN_WITH_CHUNKED_SUPPORT
|
||||||
pytest.skip('{} not resolvable'.format(HTTPBIN_WITH_CHUNKED_SUPPORT_DOMAIN))
|
pytest.skip(f'{HTTPBIN_WITH_CHUNKED_SUPPORT_DOMAIN} not resolvable')
|
||||||
|
@ -25,8 +25,7 @@ def test_default_options(httpbin):
|
|||||||
def test_config_file_not_valid(httpbin):
|
def test_config_file_not_valid(httpbin):
|
||||||
env = MockEnvironment()
|
env = MockEnvironment()
|
||||||
env.create_temp_config_dir()
|
env.create_temp_config_dir()
|
||||||
with (env.config_dir / Config.FILENAME).open('w') as f:
|
(env.config_dir / Config.FILENAME).write_text('{invalid json}')
|
||||||
f.write('{invalid json}')
|
|
||||||
r = http(httpbin + '/get', env=env)
|
r = http(httpbin + '/get', env=env)
|
||||||
assert HTTP_OK in r
|
assert HTTP_OK in r
|
||||||
assert 'http: warning' in r.stderr
|
assert 'http: warning' in r.stderr
|
||||||
|
@ -6,7 +6,6 @@ import json
|
|||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
import io
|
import io
|
||||||
from tempfile import gettempdir
|
|
||||||
from urllib.request import urlopen
|
from urllib.request import urlopen
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@ -23,17 +22,16 @@ from .utils import COLOR, CRLF, HTTP_OK, MockEnvironment, http
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('stdout_isatty', [True, False])
|
@pytest.mark.parametrize('stdout_isatty', [True, False])
|
||||||
def test_output_option(httpbin, stdout_isatty):
|
def test_output_option(tmp_path, httpbin, stdout_isatty):
|
||||||
output_filename = os.path.join(gettempdir(), test_output_option.__name__)
|
output_filename = tmp_path / 'test_output_option'
|
||||||
url = httpbin + '/robots.txt'
|
url = httpbin + '/robots.txt'
|
||||||
|
|
||||||
r = http('--output', output_filename, url,
|
r = http('--output', str(output_filename), url,
|
||||||
env=MockEnvironment(stdout_isatty=stdout_isatty))
|
env=MockEnvironment(stdout_isatty=stdout_isatty))
|
||||||
assert r == ''
|
assert r == ''
|
||||||
|
|
||||||
expected_body = urlopen(url).read().decode()
|
expected_body = urlopen(url).read().decode()
|
||||||
with open(output_filename, 'r') as f:
|
actual_body = output_filename.read_text()
|
||||||
actual_body = f.read()
|
|
||||||
|
|
||||||
assert actual_body == expected_body
|
assert actual_body == expected_body
|
||||||
|
|
||||||
@ -103,35 +101,34 @@ class TestQuietFlag:
|
|||||||
assert r.stderr == ''
|
assert r.stderr == ''
|
||||||
|
|
||||||
@pytest.mark.parametrize('with_download', [True, False])
|
@pytest.mark.parametrize('with_download', [True, False])
|
||||||
def test_quiet_with_output_redirection(self, httpbin, with_download):
|
def test_quiet_with_output_redirection(self, tmp_path, httpbin, with_download):
|
||||||
url = httpbin + '/robots.txt'
|
url = httpbin + '/robots.txt'
|
||||||
output_path = Path('output.txt')
|
output_path = Path('output.txt')
|
||||||
env = MockEnvironment()
|
env = MockEnvironment()
|
||||||
orig_cwd = os.getcwd()
|
orig_cwd = os.getcwd()
|
||||||
output = requests.get(url).text
|
output = requests.get(url).text
|
||||||
extra_args = ['--download'] if with_download else []
|
extra_args = ['--download'] if with_download else []
|
||||||
with tempfile.TemporaryDirectory() as tmp_dirname:
|
os.chdir(tmp_path)
|
||||||
os.chdir(tmp_dirname)
|
try:
|
||||||
try:
|
assert os.listdir('.') == []
|
||||||
assert os.listdir('.') == []
|
r = http(
|
||||||
r = http(
|
'--quiet',
|
||||||
'--quiet',
|
'--output', str(output_path),
|
||||||
'--output', str(output_path),
|
*extra_args,
|
||||||
*extra_args,
|
url,
|
||||||
url,
|
env=env
|
||||||
env=env
|
)
|
||||||
)
|
assert os.listdir('.') == [str(output_path)]
|
||||||
assert os.listdir('.') == [str(output_path)]
|
assert r == ''
|
||||||
assert r == ''
|
assert r.stderr == ''
|
||||||
assert r.stderr == ''
|
assert env.stderr is env.devnull
|
||||||
assert env.stderr is env.devnull
|
if with_download:
|
||||||
if with_download:
|
assert env.stdout is env.devnull
|
||||||
assert env.stdout is env.devnull
|
else:
|
||||||
else:
|
assert env.stdout is not env.devnull # --output swaps stdout.
|
||||||
assert env.stdout is not env.devnull # --output swaps stdout.
|
assert output_path.read_text() == output
|
||||||
assert output_path.read_text() == output
|
finally:
|
||||||
finally:
|
os.chdir(orig_cwd)
|
||||||
os.chdir(orig_cwd)
|
|
||||||
|
|
||||||
|
|
||||||
class TestVerboseFlag:
|
class TestVerboseFlag:
|
||||||
|
@ -3,7 +3,6 @@ import os
|
|||||||
import shutil
|
import shutil
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
from tempfile import gettempdir
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@ -209,11 +208,11 @@ class TestSession(SessionTestBase):
|
|||||||
assert HTTP_OK in r2
|
assert HTTP_OK in r2
|
||||||
assert r2.json['headers']['User-Agent'] == 'custom'
|
assert r2.json['headers']['User-Agent'] == 'custom'
|
||||||
|
|
||||||
def test_download_in_session(self, httpbin):
|
def test_download_in_session(self, tmp_path, httpbin):
|
||||||
# https://github.com/httpie/httpie/issues/412
|
# https://github.com/httpie/httpie/issues/412
|
||||||
self.start_session(httpbin)
|
self.start_session(httpbin)
|
||||||
cwd = os.getcwd()
|
cwd = os.getcwd()
|
||||||
os.chdir(gettempdir())
|
os.chdir(tmp_path)
|
||||||
try:
|
try:
|
||||||
http('--session=test', '--download',
|
http('--session=test', '--download',
|
||||||
httpbin.url + '/get', env=self.env())
|
httpbin.url + '/get', env=self.env())
|
||||||
|
@ -53,7 +53,8 @@ def test_chunked_stdin(httpbin_with_chunked_support):
|
|||||||
|
|
||||||
|
|
||||||
def test_chunked_stdin_multiple_chunks(httpbin_with_chunked_support):
|
def test_chunked_stdin_multiple_chunks(httpbin_with_chunked_support):
|
||||||
stdin_bytes = FILE_PATH.read_bytes() + b'\n' + FILE_PATH.read_bytes()
|
data = FILE_PATH.read_bytes()
|
||||||
|
stdin_bytes = data + b'\n' + data
|
||||||
r = http(
|
r = http(
|
||||||
'--verbose',
|
'--verbose',
|
||||||
'--chunked',
|
'--chunked',
|
||||||
|
@ -19,13 +19,10 @@ class TestWindowsOnly:
|
|||||||
|
|
||||||
|
|
||||||
class TestFakeWindows:
|
class TestFakeWindows:
|
||||||
def test_output_file_pretty_not_allowed_on_windows(self, httpbin):
|
def test_output_file_pretty_not_allowed_on_windows(self, tmp_path, httpbin):
|
||||||
env = MockEnvironment(is_windows=True)
|
env = MockEnvironment(is_windows=True)
|
||||||
output_file = os.path.join(
|
output_file = tmp_path / 'test_output_file_pretty_not_allowed_on_windows'
|
||||||
tempfile.gettempdir(),
|
r = http('--output', str(output_file),
|
||||||
self.test_output_file_pretty_not_allowed_on_windows.__name__
|
|
||||||
)
|
|
||||||
r = http('--output', output_file,
|
|
||||||
'--pretty=all', 'GET', httpbin.url + '/get',
|
'--pretty=all', 'GET', httpbin.url + '/get',
|
||||||
env=env, tolerate_error_exit_status=True)
|
env=env, tolerate_error_exit_status=True)
|
||||||
assert 'Only terminal output can be colorized on Windows' in r.stderr
|
assert 'Only terminal output can be colorized on Windows' in r.stderr
|
||||||
|
Loading…
Reference in New Issue
Block a user