From a61f9e1114e2ffd2be50f3f7c3a03e40d57c10ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Schoentgen?= Date: Sat, 29 May 2021 12:06:06 +0200 Subject: [PATCH] Minor clean-up (#1078) - Remove default arguments to `open()`. - Make use of `pytest` mechanisms for temporary folders. --- httpie/config.py | 2 +- httpie/plugins/__init__.py | 2 ++ tests/conftest.py | 2 +- tests/test_config.py | 3 +-- tests/test_output.py | 55 ++++++++++++++++++-------------------- tests/test_sessions.py | 5 ++-- tests/test_uploads.py | 3 ++- tests/test_windows.py | 9 +++---- 8 files changed, 38 insertions(+), 43 deletions(-) diff --git a/httpie/config.py b/httpie/config.py index 65a10eb0..5960a6bb 100644 --- a/httpie/config.py +++ b/httpie/config.py @@ -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: diff --git a/httpie/plugins/__init__.py b/httpie/plugins/__init__.py index a3ee528c..301abf1b 100644 --- a/httpie/plugins/__init__.py +++ b/httpie/plugins/__init__.py @@ -7,3 +7,5 @@ from .base import ( AuthPlugin, FormatterPlugin, ConverterPlugin, TransportPlugin ) + +__all__ = ('AuthPlugin', 'ConverterPlugin', 'FormatterPlugin', 'TransportPlugin') diff --git a/tests/conftest.py b/tests/conftest.py index 282d231c..ff79ed17 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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') diff --git a/tests/test_config.py b/tests/test_config.py index 5848106a..7b990edb 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -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 diff --git a/tests/test_output.py b/tests/test_output.py index b602eeff..a9edbf4c 100644 --- a/tests/test_output.py +++ b/tests/test_output.py @@ -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: diff --git a/tests/test_sessions.py b/tests/test_sessions.py index 5718832e..eaa923ef 100644 --- a/tests/test_sessions.py +++ b/tests/test_sessions.py @@ -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()) diff --git a/tests/test_uploads.py b/tests/test_uploads.py index 3b97fccf..ca424020 100644 --- a/tests/test_uploads.py +++ b/tests/test_uploads.py @@ -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', diff --git a/tests/test_windows.py b/tests/test_windows.py index 0282fa98..9354cb83 100644 --- a/tests/test_windows.py +++ b/tests/test_windows.py @@ -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