diff options
| author | Jason R. Coombs <jaraco@jaraco.com> | 2022-10-04 21:23:42 -0400 |
|---|---|---|
| committer | Jason R. Coombs <jaraco@jaraco.com> | 2022-10-04 21:23:42 -0400 |
| commit | ce8692d32aaab57f2c801dfa9ccedd59fb0854c3 (patch) | |
| tree | a76fcae77ac7aad02e4b9919652ab96c783b44ce | |
| parent | e615a4d0a0002d23127db19c4febc54d6080378e (diff) | |
| download | python-setuptools-git-ce8692d32aaab57f2c801dfa9ccedd59fb0854c3.tar.gz | |
Prefer capsys to test.support.captured*
| -rw-r--r-- | distutils/tests/test_cmd.py | 21 | ||||
| -rw-r--r-- | distutils/tests/test_core.py | 21 | ||||
| -rw-r--r-- | distutils/tests/test_dist.py | 31 | ||||
| -rw-r--r-- | distutils/tests/test_filelist.py | 21 | ||||
| -rw-r--r-- | distutils/tests/test_install.py | 5 | ||||
| -rw-r--r-- | distutils/tests/test_sdist.py | 8 |
6 files changed, 37 insertions, 70 deletions
diff --git a/distutils/tests/test_cmd.py b/distutils/tests/test_cmd.py index e4d5bf3c..3aac448d 100644 --- a/distutils/tests/test_cmd.py +++ b/distutils/tests/test_cmd.py @@ -1,6 +1,5 @@ """Tests for distutils.cmd.""" import os -from test.support import captured_stdout from distutils.cmd import Command from distutils.dist import Distribution @@ -100,17 +99,9 @@ class TestCommand: with pytest.raises(DistutilsOptionError): cmd.ensure_dirname('option2') - def test_debug_print(self, cmd): - with captured_stdout() as stdout: - cmd.debug_print('xxx') - stdout.seek(0) - assert stdout.read() == '' - - debug.DEBUG = True - try: - with captured_stdout() as stdout: - cmd.debug_print('xxx') - stdout.seek(0) - assert stdout.read() == 'xxx\n' - finally: - debug.DEBUG = False + def test_debug_print(self, cmd, capsys, monkeypatch): + cmd.debug_print('xxx') + assert capsys.readouterr().out == '' + monkeypatch.setattr(debug, 'DEBUG', True) + cmd.debug_print('xxx') + assert capsys.readouterr().out == 'xxx\n' diff --git a/distutils/tests/test_core.py b/distutils/tests/test_core.py index 5fe7e958..2c11ff76 100644 --- a/distutils/tests/test_core.py +++ b/distutils/tests/test_core.py @@ -4,7 +4,6 @@ import io import distutils.core import os import sys -from test.support import captured_stdout import pytest @@ -121,20 +120,12 @@ class TestCore: distutils.core.run_commands(dist) assert 'build' in dist.have_run - def test_debug_mode(self): + def test_debug_mode(self, capsys, monkeypatch): # this covers the code called when DEBUG is set sys.argv = ['setup.py', '--name'] - with captured_stdout() as stdout: - distutils.core.setup(name='bar') - stdout.seek(0) - assert stdout.read() == 'bar\n' - - distutils.core.DEBUG = True - try: - with captured_stdout() as stdout: - distutils.core.setup(name='bar') - finally: - distutils.core.DEBUG = False - stdout.seek(0) + distutils.core.setup(name='bar') + capsys.readouterr().out == 'bar\n' + monkeypatch.setattr(distutils.core, 'DEBUG', True) + distutils.core.setup(name='bar') wanted = "options (after parsing config files):\n" - assert stdout.readlines()[0] == wanted + assert capsys.readouterr().out.startswith(wanted) diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py index 385007f2..34d75d73 100644 --- a/distutils/tests/test_dist.py +++ b/distutils/tests/test_dist.py @@ -13,7 +13,6 @@ import jaraco.path from distutils.dist import Distribution, fix_help_options from distutils.cmd import Command -from test.support import captured_stdout, captured_stderr from distutils.tests import support from distutils import log @@ -370,16 +369,15 @@ class TestMetadata(support.TempdirManager): meta = self.format_metadata(dist) assert 'Metadata-Version: 1.1' in meta - def test_classifier_invalid_type(self): + def test_classifier_invalid_type(self, capsys): attrs = { 'name': 'Boa', 'version': '3.0', 'classifiers': ('Programming Language :: Python :: 3',), } - with captured_stderr() as error: - d = Distribution(attrs) + d = Distribution(attrs) # should have warning about passing a non-list - assert 'should be a list' in error.getvalue() + assert 'should be a list' in capsys.readouterr().err # should be converted to a list assert isinstance(d.metadata.classifiers, list) assert d.metadata.classifiers == list(attrs['classifiers']) @@ -393,16 +391,15 @@ class TestMetadata(support.TempdirManager): dist = Distribution(attrs) assert dist.get_keywords() == ['spam', 'eggs', 'life of brian'] - def test_keywords_invalid_type(self): + def test_keywords_invalid_type(self, capsys): attrs = { 'name': 'Monty', 'version': '1.0', 'keywords': ('spam', 'eggs', 'life of brian'), } - with captured_stderr() as error: - d = Distribution(attrs) + d = Distribution(attrs) # should have warning about passing a non-list - assert 'should be a list' in error.getvalue() + assert 'should be a list' in capsys.readouterr().err # should be converted to a list assert isinstance(d.metadata.keywords, list) assert d.metadata.keywords == list(attrs['keywords']) @@ -416,16 +413,15 @@ class TestMetadata(support.TempdirManager): dist = Distribution(attrs) assert dist.get_platforms() == ['GNU/Linux', 'Some Evil Platform'] - def test_platforms_invalid_types(self): + def test_platforms_invalid_types(self, capsys): attrs = { 'name': 'Monty', 'version': '1.0', 'platforms': ('GNU/Linux', 'Some Evil Platform'), } - with captured_stderr() as error: - d = Distribution(attrs) + d = Distribution(attrs) # should have warning about passing a non-list - assert 'should be a list' in error.getvalue() + assert 'should be a list' in capsys.readouterr().err # should be converted to a list assert isinstance(d.metadata.platforms, list) assert d.metadata.platforms == list(attrs['platforms']) @@ -476,7 +472,7 @@ class TestMetadata(support.TempdirManager): assert fancy_options[0] == ('a', 'b', 'c') assert fancy_options[1] == (1, 2, 3) - def test_show_help(self, request): + def test_show_help(self, request, capsys): # smoke test, just makes sure some help is displayed reset_log = functools.partial(log.set_threshold, log._global_log.threshold) request.addfinalizer(reset_log) @@ -484,10 +480,11 @@ class TestMetadata(support.TempdirManager): sys.argv = [] dist.help = 1 dist.script_name = 'setup.py' - with captured_stdout() as s: - dist.parse_command_line() + dist.parse_command_line() - output = [line for line in s.getvalue().split('\n') if line.strip() != ''] + output = [ + line for line in capsys.readouterr().out.split('\n') if line.strip() != '' + ] assert output def test_read_metadata(self): diff --git a/distutils/tests/test_filelist.py b/distutils/tests/test_filelist.py index 74a2a41d..ed68df32 100644 --- a/distutils/tests/test_filelist.py +++ b/distutils/tests/test_filelist.py @@ -7,8 +7,6 @@ from distutils.errors import DistutilsTemplateError from distutils.filelist import glob_to_re, translate_pattern, FileList from distutils import filelist -from test.support import captured_stdout - import pytest import jaraco.path @@ -109,19 +107,14 @@ class TestFileList: assert file_list.files == wanted - def test_debug_print(self): + def test_debug_print(self, capsys, monkeypatch): file_list = FileList() - with captured_stdout() as stdout: - file_list.debug_print('xxx') - assert stdout.getvalue() == '' - - debug.DEBUG = True - try: - with captured_stdout() as stdout: - file_list.debug_print('xxx') - assert stdout.getvalue() == 'xxx\n' - finally: - debug.DEBUG = False + file_list.debug_print('xxx') + assert capsys.readouterr().out == '' + + monkeypatch.setattr(debug, 'DEBUG', True) + file_list.debug_print('xxx') + assert capsys.readouterr().out == 'xxx\n' def test_set_allfiles(self): file_list = FileList() diff --git a/distutils/tests/test_install.py b/distutils/tests/test_install.py index f597ad3e..e240b156 100644 --- a/distutils/tests/test_install.py +++ b/distutils/tests/test_install.py @@ -5,8 +5,6 @@ import sys import site import pathlib -from test.support import captured_stdout - import pytest from distutils import sysconfig @@ -249,6 +247,5 @@ class TestInstall( def test_debug_mode(self, logs, monkeypatch): # this covers the code called when DEBUG is set monkeypatch.setattr(install_module, 'DEBUG', True) - with captured_stdout(): - self.test_record() + self.test_record() assert logs.render(DEBUG) diff --git a/distutils/tests/test_sdist.py b/distutils/tests/test_sdist.py index da36bfcf..bc535f38 100644 --- a/distutils/tests/test_sdist.py +++ b/distutils/tests/test_sdist.py @@ -5,7 +5,6 @@ import warnings import zipfile from os.path import join from textwrap import dedent -from test.support import captured_stdout from .unix_compat import require_unix_id, require_uid_0, pwd, grp import pytest @@ -285,15 +284,14 @@ class TestSDist(BasePyPIRCCommandTestCase): cmd.check_metadata() assert len(w.warnings) == 1 - def test_show_formats(self): - with captured_stdout() as stdout: - show_formats() + def test_show_formats(self, capsys): + show_formats() # the output should be a header line + one line per format num_formats = len(ARCHIVE_FORMATS.keys()) output = [ line - for line in stdout.getvalue().split('\n') + for line in capsys.readouterr().out.split('\n') if line.strip().startswith('--formats=') ] assert len(output) == num_formats |
