Minor clean-up (#1078)

- Remove default arguments to `open()`.
- Make use of `pytest` mechanisms for temporary folders.
This commit is contained in:
Mickaël Schoentgen 2021-05-29 12:06:06 +02:00 committed by GitHub
parent 611b278b63
commit a61f9e1114
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 38 additions and 43 deletions

View File

@ -84,7 +84,7 @@ class BaseConfigDict(dict):
def load(self):
config_type = type(self).__name__.lower()
try:
with self.path.open('rt') as f:
with self.path.open() as f:
try:
data = json.load(f)
except ValueError as e:

View File

@ -7,3 +7,5 @@ from .base import (
AuthPlugin, FormatterPlugin,
ConverterPlugin, TransportPlugin
)
__all__ = ('AuthPlugin', 'ConverterPlugin', 'FormatterPlugin', 'TransportPlugin')

View File

@ -40,4 +40,4 @@ def _httpbin_with_chunked_support_available():
def httpbin_with_chunked_support(_httpbin_with_chunked_support_available):
if _httpbin_with_chunked_support_available:
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')

View File

@ -25,8 +25,7 @@ def test_default_options(httpbin):
def test_config_file_not_valid(httpbin):
env = MockEnvironment()
env.create_temp_config_dir()
with (env.config_dir / Config.FILENAME).open('w') as f:
f.write('{invalid json}')
(env.config_dir / Config.FILENAME).write_text('{invalid json}')
r = http(httpbin + '/get', env=env)
assert HTTP_OK in r
assert 'http: warning' in r.stderr

View File

@ -6,7 +6,6 @@ import json
import os
import tempfile
import io
from tempfile import gettempdir
from urllib.request import urlopen
import pytest
@ -23,17 +22,16 @@ from .utils import COLOR, CRLF, HTTP_OK, MockEnvironment, http
@pytest.mark.parametrize('stdout_isatty', [True, False])
def test_output_option(httpbin, stdout_isatty):
output_filename = os.path.join(gettempdir(), test_output_option.__name__)
def test_output_option(tmp_path, httpbin, stdout_isatty):
output_filename = tmp_path / 'test_output_option'
url = httpbin + '/robots.txt'
r = http('--output', output_filename, url,
r = http('--output', str(output_filename), url,
env=MockEnvironment(stdout_isatty=stdout_isatty))
assert r == ''
expected_body = urlopen(url).read().decode()
with open(output_filename, 'r') as f:
actual_body = f.read()
actual_body = output_filename.read_text()
assert actual_body == expected_body
@ -103,35 +101,34 @@ class TestQuietFlag:
assert r.stderr == ''
@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'
output_path = Path('output.txt')
env = MockEnvironment()
orig_cwd = os.getcwd()
output = requests.get(url).text
extra_args = ['--download'] if with_download else []
with tempfile.TemporaryDirectory() as tmp_dirname:
os.chdir(tmp_dirname)
try:
assert os.listdir('.') == []
r = http(
'--quiet',
'--output', str(output_path),
*extra_args,
url,
env=env
)
assert os.listdir('.') == [str(output_path)]
assert r == ''
assert r.stderr == ''
assert env.stderr is env.devnull
if with_download:
assert env.stdout is env.devnull
else:
assert env.stdout is not env.devnull # --output swaps stdout.
assert output_path.read_text() == output
finally:
os.chdir(orig_cwd)
os.chdir(tmp_path)
try:
assert os.listdir('.') == []
r = http(
'--quiet',
'--output', str(output_path),
*extra_args,
url,
env=env
)
assert os.listdir('.') == [str(output_path)]
assert r == ''
assert r.stderr == ''
assert env.stderr is env.devnull
if with_download:
assert env.stdout is env.devnull
else:
assert env.stdout is not env.devnull # --output swaps stdout.
assert output_path.read_text() == output
finally:
os.chdir(orig_cwd)
class TestVerboseFlag:

View File

@ -3,7 +3,6 @@ import os
import shutil
from datetime import datetime
from unittest import mock
from tempfile import gettempdir
import pytest
@ -209,11 +208,11 @@ class TestSession(SessionTestBase):
assert HTTP_OK in r2
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
self.start_session(httpbin)
cwd = os.getcwd()
os.chdir(gettempdir())
os.chdir(tmp_path)
try:
http('--session=test', '--download',
httpbin.url + '/get', env=self.env())

View File

@ -53,7 +53,8 @@ def test_chunked_stdin(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(
'--verbose',
'--chunked',

View File

@ -19,13 +19,10 @@ class TestWindowsOnly:
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)
output_file = os.path.join(
tempfile.gettempdir(),
self.test_output_file_pretty_not_allowed_on_windows.__name__
)
r = http('--output', output_file,
output_file = tmp_path / 'test_output_file_pretty_not_allowed_on_windows'
r = http('--output', str(output_file),
'--pretty=all', 'GET', httpbin.url + '/get',
env=env, tolerate_error_exit_status=True)
assert 'Only terminal output can be colorized on Windows' in r.stderr