summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2022-10-05 03:27:32 -0400
committerJason R. Coombs <jaraco@jaraco.com>2022-11-13 10:03:20 -0500
commit74652cabdeaacadc76ccf126563bed8ee2ccf3ef (patch)
treee6f4b8b8d305d5e43467ca242f375eade52e5518
parentf95d384957ba4c358c6e1345c932b4445a4a38d3 (diff)
downloadpython-setuptools-git-74652cabdeaacadc76ccf126563bed8ee2ccf3ef.tar.gz
Replace bespoke logging facility with logging module, available since Python 2.3.
-rw-r--r--conftest.py53
-rw-r--r--distutils/cmd.py6
-rw-r--r--distutils/log.py79
-rw-r--r--distutils/tests/test_build_py.py4
-rw-r--r--distutils/tests/test_config.py1
-rw-r--r--distutils/tests/test_dir_util.py24
-rw-r--r--distutils/tests/test_dist.py17
-rw-r--r--distutils/tests/test_file_util.py11
-rw-r--r--distutils/tests/test_filelist.py51
-rw-r--r--distutils/tests/test_install.py7
-rw-r--r--distutils/tests/test_install_lib.py4
-rw-r--r--distutils/tests/test_log.py51
-rw-r--r--distutils/tests/test_register.py10
-rw-r--r--distutils/tests/test_sdist.py36
-rw-r--r--distutils/tests/test_upload.py15
15 files changed, 124 insertions, 245 deletions
diff --git a/conftest.py b/conftest.py
index 7427da7a..b01b3130 100644
--- a/conftest.py
+++ b/conftest.py
@@ -2,6 +2,7 @@ import os
import sys
import platform
import pathlib
+import logging
import pytest
import path
@@ -36,39 +37,20 @@ def needs_zlib():
pytest.importorskip('zlib')
-# from jaraco.collections
-class Everything:
- def __contains__(self, other):
- return True
-
-
-class SavedLogs(list):
- def render(self, *levels):
- return [
- msg % args for level, msg, args in self if level in (levels or Everything())
- ]
-
-
-@pytest.fixture
-def logs(monkeypatch):
- from distutils import log
-
- logs = SavedLogs()
- log_levels = log.DEBUG, log.INFO, log.WARN, log.ERROR, log.FATAL
-
- def _log(self, level, msg, args):
- self.logs.append((level, msg, args))
+@pytest.fixture(autouse=True)
+def log_everything():
+ """
+ For tests, set the level on the logger to log everything.
+ """
+ logging.getLogger('distutils').setLevel(0)
- def save_log(self, level, msg, args):
- if level not in log_levels:
- raise ValueError(f'invalid log level {level}')
- if not isinstance(msg, str):
- raise TypeError(f'msg should be str, not {type(msg).__name__!r}')
- logs.append((level, msg, args))
- monkeypatch.setattr(log.Log, '_log', save_log)
- monkeypatch.setattr(log._global_log, 'threshold', log.FATAL)
- return logs
+@pytest.fixture(autouse=True)
+def capture_log_at_info(caplog):
+ """
+ By default, capture logs at INFO and greater.
+ """
+ caplog.set_level(logging.INFO)
def _save_cwd():
@@ -112,15 +94,6 @@ def temp_cwd(tmp_path):
@pytest.fixture
-def threshold_warn():
- from distutils.log import set_threshold, WARN
-
- orig = set_threshold(WARN)
- yield
- set_threshold(orig)
-
-
-@pytest.fixture
def pypirc(request, save_env, distutils_managed_tempdir):
from distutils.core import PyPIRCCommand
from distutils.core import Distribution
diff --git a/distutils/cmd.py b/distutils/cmd.py
index 88a90ead..0cf7dd02 100644
--- a/distutils/cmd.py
+++ b/distutils/cmd.py
@@ -7,6 +7,7 @@ in the distutils.command package.
import sys
import os
import re
+import logging
from .errors import DistutilsOptionError
from . import util, dir_util, file_util, archive_util, dep_util, log
@@ -179,10 +180,7 @@ class Command:
"abstract method -- subclass %s must override" % self.__class__
)
- def announce(self, msg, level=1):
- """If the current verbosity level is of greater than or equal to
- 'level' print 'msg' to stdout.
- """
+ def announce(self, msg, level=logging.DEBUG):
log.log(level, msg)
def debug_print(self, msg):
diff --git a/distutils/log.py b/distutils/log.py
index be25f6ca..d0365e68 100644
--- a/distutils/log.py
+++ b/distutils/log.py
@@ -1,80 +1,37 @@
-"""A simple log mechanism styled after PEP 282."""
+"""
+A simple log mechanism styled after PEP 282.
-# The class here is styled after PEP 282 so that it could later be
-# replaced with a standard Python logging implementation.
+Retained for compatibility and should not be used.
+"""
-import sys
+import logging
-DEBUG = 1
-INFO = 2
-WARN = 3
-ERROR = 4
-FATAL = 5
+DEBUG = logging.DEBUG
+INFO = logging.INFO
+WARN = logging.WARN
+ERROR = logging.ERROR
+FATAL = logging.FATAL
-class Log:
- def __init__(self, threshold=WARN):
- self.threshold = threshold
-
- def _log(self, level, msg, args):
- if level not in (DEBUG, INFO, WARN, ERROR, FATAL):
- raise ValueError('%s wrong log level' % str(level))
-
- if level >= self.threshold:
- if args:
- msg = msg % args
- if level in (WARN, ERROR, FATAL):
- stream = sys.stderr
- else:
- stream = sys.stdout
- try:
- stream.write('%s\n' % msg)
- except UnicodeEncodeError:
- # emulate backslashreplace error handler
- encoding = stream.encoding
- msg = msg.encode(encoding, "backslashreplace").decode(encoding)
- stream.write('%s\n' % msg)
- stream.flush()
-
- def log(self, level, msg, *args):
- self._log(level, msg, args)
-
- def debug(self, msg, *args):
- self._log(DEBUG, msg, args)
-
- def info(self, msg, *args):
- self._log(INFO, msg, args)
-
- def warn(self, msg, *args):
- self._log(WARN, msg, args)
-
- def error(self, msg, *args):
- self._log(ERROR, msg, args)
-
- def fatal(self, msg, *args):
- self._log(FATAL, msg, args)
-
-
-_global_log = Log()
+_global_log = logging.getLogger('distutils')
log = _global_log.log
debug = _global_log.debug
info = _global_log.info
-warn = _global_log.warn
+warn = _global_log.warning
error = _global_log.error
fatal = _global_log.fatal
def set_threshold(level):
- # return the old threshold for use from tests
- old = _global_log.threshold
- _global_log.threshold = level
- return old
+ orig = _global_log.level
+ _global_log.setLevel(level)
+ return orig
def set_verbosity(v):
if v <= 0:
- set_threshold(WARN)
+ set_threshold(logging.WARN)
elif v == 1:
- set_threshold(INFO)
+ set_threshold(logging.INFO)
elif v >= 2:
- set_threshold(DEBUG)
+ set_threshold(logging.DEBUG)
diff --git a/distutils/tests/test_build_py.py b/distutils/tests/test_build_py.py
index e5f4320c..3bef9d79 100644
--- a/distutils/tests/test_build_py.py
+++ b/distutils/tests/test_build_py.py
@@ -150,7 +150,7 @@ class TestBuildPy(support.TempdirManager):
except DistutilsFileError:
self.fail("failed package_data when data dir includes a dir")
- def test_dont_write_bytecode(self, logs):
+ def test_dont_write_bytecode(self, caplog):
# makes sure byte_compile is not used
dist = self.create_dist()[1]
cmd = build_py(dist)
@@ -164,7 +164,7 @@ class TestBuildPy(support.TempdirManager):
finally:
sys.dont_write_bytecode = old_dont_write_bytecode
- assert 'byte-compiling is disabled' in logs.render()[0]
+ assert 'byte-compiling is disabled' in caplog.records[0].message
def test_namespace_package_does_not_warn(self, caplog):
"""
diff --git a/distutils/tests/test_config.py b/distutils/tests/test_config.py
index cdf73bb9..1ae615db 100644
--- a/distutils/tests/test_config.py
+++ b/distutils/tests/test_config.py
@@ -46,7 +46,6 @@ password:xxx
@support.combine_markers
-@pytest.mark.usefixtures('threshold_warn')
@pytest.mark.usefixtures('pypirc')
class BasePyPIRCCommandTestCase(support.TempdirManager):
pass
diff --git a/distutils/tests/test_dir_util.py b/distutils/tests/test_dir_util.py
index a48be736..0c6db4af 100644
--- a/distutils/tests/test_dir_util.py
+++ b/distutils/tests/test_dir_util.py
@@ -26,21 +26,19 @@ def stuff(request, monkeypatch, distutils_managed_tempdir):
class TestDirUtil(support.TempdirManager):
- def test_mkpath_remove_tree_verbosity(self, logs):
-
+ def test_mkpath_remove_tree_verbosity(self, caplog):
mkpath(self.target, verbose=0)
- wanted = []
- assert logs.render() == wanted
+ assert not caplog.records
remove_tree(self.root_target, verbose=0)
mkpath(self.target, verbose=1)
wanted = ['creating %s' % self.root_target, 'creating %s' % self.target]
- assert logs.render() == wanted
- logs.clear()
+ assert caplog.messages == wanted
+ caplog.clear()
remove_tree(self.root_target, verbose=1)
wanted = ["removing '%s' (and everything under it)" % self.root_target]
- assert logs.render() == wanted
+ assert caplog.messages == wanted
@pytest.mark.skipif("platform.system() == 'Windows'")
def test_mkpath_with_custom_mode(self):
@@ -52,24 +50,24 @@ class TestDirUtil(support.TempdirManager):
mkpath(self.target2, 0o555)
assert stat.S_IMODE(os.stat(self.target2).st_mode) == 0o555 & ~umask
- def test_create_tree_verbosity(self, logs):
+ def test_create_tree_verbosity(self, caplog):
create_tree(self.root_target, ['one', 'two', 'three'], verbose=0)
- assert logs.render() == []
+ assert caplog.messages == []
remove_tree(self.root_target, verbose=0)
wanted = ['creating %s' % self.root_target]
create_tree(self.root_target, ['one', 'two', 'three'], verbose=1)
- assert logs.render() == wanted
+ assert caplog.messages == wanted
remove_tree(self.root_target, verbose=0)
- def test_copy_tree_verbosity(self, logs):
+ def test_copy_tree_verbosity(self, caplog):
mkpath(self.target, verbose=0)
copy_tree(self.target, self.target2, verbose=0)
- assert logs.render() == []
+ assert caplog.messages == []
remove_tree(self.root_target, verbose=0)
@@ -80,7 +78,7 @@ class TestDirUtil(support.TempdirManager):
wanted = ['copying {} -> {}'.format(a_file, self.target2)]
copy_tree(self.target, self.target2, verbose=1)
- assert logs.render() == wanted
+ assert caplog.messages == wanted
remove_tree(self.root_target, verbose=0)
remove_tree(self.target2, verbose=0)
diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py
index 6726d506..b5e81d03 100644
--- a/distutils/tests/test_dist.py
+++ b/distutils/tests/test_dist.py
@@ -14,7 +14,6 @@ from distutils.dist import Distribution, fix_help_options
from distutils.cmd import Command
from distutils.tests import support
-from distutils import log
pydistutils_cfg = '.' * (os.name == 'posix') + 'pydistutils.cfg'
@@ -236,7 +235,7 @@ class TestDistributionBehavior(support.TempdirManager):
def test_announce(self):
# make sure the level is known
dist = Distribution()
- with pytest.raises(ValueError):
+ with pytest.raises(TypeError):
dist.announce('ok', level='ok2')
def test_find_config_files_disable(self, temp_home):
@@ -367,7 +366,7 @@ class TestMetadata(support.TempdirManager):
meta = self.format_metadata(dist)
assert 'Metadata-Version: 1.1' in meta
- def test_classifier_invalid_type(self, capsys):
+ def test_classifier_invalid_type(self, caplog):
attrs = {
'name': 'Boa',
'version': '3.0',
@@ -375,7 +374,7 @@ class TestMetadata(support.TempdirManager):
}
d = Distribution(attrs)
# should have warning about passing a non-list
- assert 'should be a list' in capsys.readouterr().err
+ assert 'should be a list' in caplog.messages[0]
# should be converted to a list
assert isinstance(d.metadata.classifiers, list)
assert d.metadata.classifiers == list(attrs['classifiers'])
@@ -389,7 +388,7 @@ class TestMetadata(support.TempdirManager):
dist = Distribution(attrs)
assert dist.get_keywords() == ['spam', 'eggs', 'life of brian']
- def test_keywords_invalid_type(self, capsys):
+ def test_keywords_invalid_type(self, caplog):
attrs = {
'name': 'Monty',
'version': '1.0',
@@ -397,7 +396,7 @@ class TestMetadata(support.TempdirManager):
}
d = Distribution(attrs)
# should have warning about passing a non-list
- assert 'should be a list' in capsys.readouterr().err
+ assert 'should be a list' in caplog.messages[0]
# should be converted to a list
assert isinstance(d.metadata.keywords, list)
assert d.metadata.keywords == list(attrs['keywords'])
@@ -411,7 +410,7 @@ class TestMetadata(support.TempdirManager):
dist = Distribution(attrs)
assert dist.get_platforms() == ['GNU/Linux', 'Some Evil Platform']
- def test_platforms_invalid_types(self, capsys):
+ def test_platforms_invalid_types(self, caplog):
attrs = {
'name': 'Monty',
'version': '1.0',
@@ -419,7 +418,7 @@ class TestMetadata(support.TempdirManager):
}
d = Distribution(attrs)
# should have warning about passing a non-list
- assert 'should be a list' in capsys.readouterr().err
+ assert 'should be a list' in caplog.messages[0]
# should be converted to a list
assert isinstance(d.metadata.platforms, list)
assert d.metadata.platforms == list(attrs['platforms'])
@@ -472,8 +471,6 @@ class TestMetadata(support.TempdirManager):
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)
dist = Distribution()
sys.argv = []
dist.help = 1
diff --git a/distutils/tests/test_file_util.py b/distutils/tests/test_file_util.py
index 8ec56c3b..9f44f91d 100644
--- a/distutils/tests/test_file_util.py
+++ b/distutils/tests/test_file_util.py
@@ -20,7 +20,7 @@ def stuff(request, monkeypatch, distutils_managed_tempdir):
class TestFileUtil(support.TempdirManager):
- def test_move_file_verbosity(self, logs):
+ def test_move_file_verbosity(self, caplog):
f = open(self.source, 'w')
try:
f.write('some content')
@@ -28,25 +28,24 @@ class TestFileUtil(support.TempdirManager):
f.close()
move_file(self.source, self.target, verbose=0)
- wanted = []
- assert logs.render() == wanted
+ assert not caplog.messages
# back to original state
move_file(self.target, self.source, verbose=0)
move_file(self.source, self.target, verbose=1)
wanted = ['moving {} -> {}'.format(self.source, self.target)]
- assert logs.render() == wanted
+ assert caplog.messages == wanted
# back to original state
move_file(self.target, self.source, verbose=0)
- logs.clear()
+ caplog.clear()
# now the target is a dir
os.mkdir(self.target_dir)
move_file(self.source, self.target_dir, verbose=1)
wanted = ['moving {} -> {}'.format(self.source, self.target_dir)]
- assert logs.render() == wanted
+ assert caplog.messages == wanted
def test_move_file_exception_unpacking_rename(self):
# see issue 22182
diff --git a/distutils/tests/test_filelist.py b/distutils/tests/test_filelist.py
index ed68df32..2cee42cd 100644
--- a/distutils/tests/test_filelist.py
+++ b/distutils/tests/test_filelist.py
@@ -1,8 +1,9 @@
"""Tests for distutils.filelist."""
import os
import re
+import logging
+
from distutils import debug
-from distutils.log import WARN
from distutils.errors import DistutilsTemplateError
from distutils.filelist import glob_to_re, translate_pattern, FileList
from distutils import filelist
@@ -35,13 +36,15 @@ def make_local_path(s):
class TestFileList:
- def assertNoWarnings(self, logs):
- assert logs.render(WARN) == []
- logs.clear()
+ def assertNoWarnings(self, caplog):
+ warnings = [rec for rec in caplog.records if rec.levelno == logging.WARNING]
+ assert not warnings
+ caplog.clear()
- def assertWarnings(self, logs):
- assert logs.render(WARN)
- logs.clear()
+ def assertWarnings(self, caplog):
+ warnings = [rec for rec in caplog.records if rec.levelno == logging.WARNING]
+ assert warnings
+ caplog.clear()
def test_glob_to_re(self):
sep = os.sep
@@ -180,7 +183,7 @@ class TestFileList:
file_list.include_pattern('*')
assert file_list.allfiles == ['a.py', 'b.txt']
- def test_process_template(self, logs):
+ def test_process_template(self, caplog):
mlp = make_local_path
# invalid lines
file_list = FileList()
@@ -204,11 +207,11 @@ class TestFileList:
file_list.process_template_line('include *.py')
assert file_list.files == ['a.py']
- self.assertNoWarnings(logs)
+ self.assertNoWarnings(caplog)
file_list.process_template_line('include *.rb')
assert file_list.files == ['a.py']
- self.assertWarnings(logs)
+ self.assertWarnings(caplog)
# exclude
file_list = FileList()
@@ -216,11 +219,11 @@ class TestFileList:
file_list.process_template_line('exclude *.py')
assert file_list.files == ['b.txt', mlp('d/c.py')]
- self.assertNoWarnings(logs)
+ self.assertNoWarnings(caplog)
file_list.process_template_line('exclude *.rb')
assert file_list.files == ['b.txt', mlp('d/c.py')]
- self.assertWarnings(logs)
+ self.assertWarnings(caplog)
# global-include
file_list = FileList()
@@ -228,11 +231,11 @@ class TestFileList:
file_list.process_template_line('global-include *.py')
assert file_list.files == ['a.py', mlp('d/c.py')]
- self.assertNoWarnings(logs)
+ self.assertNoWarnings(caplog)
file_list.process_template_line('global-include *.rb')
assert file_list.files == ['a.py', mlp('d/c.py')]
- self.assertWarnings(logs)
+ self.assertWarnings(caplog)
# global-exclude
file_list = FileList()
@@ -240,11 +243,11 @@ class TestFileList:
file_list.process_template_line('global-exclude *.py')
assert file_list.files == ['b.txt']
- self.assertNoWarnings(logs)
+ self.assertNoWarnings(caplog)
file_list.process_template_line('global-exclude *.rb')
assert file_list.files == ['b.txt']
- self.assertWarnings(logs)
+ self.assertWarnings(caplog)
# recursive-include
file_list = FileList()
@@ -252,11 +255,11 @@ class TestFileList:
file_list.process_template_line('recursive-include d *.py')
assert file_list.files == [mlp('d/b.py'), mlp('d/d/e.py')]
- self.assertNoWarnings(logs)
+ self.assertNoWarnings(caplog)
file_list.process_template_line('recursive-include e *.py')
assert file_list.files == [mlp('d/b.py'), mlp('d/d/e.py')]
- self.assertWarnings(logs)
+ self.assertWarnings(caplog)
# recursive-exclude
file_list = FileList()
@@ -264,11 +267,11 @@ class TestFileList:
file_list.process_template_line('recursive-exclude d *.py')
assert file_list.files == ['a.py', mlp('d/c.txt')]
- self.assertNoWarnings(logs)
+ self.assertNoWarnings(caplog)
file_list.process_template_line('recursive-exclude e *.py')
assert file_list.files == ['a.py', mlp('d/c.txt')]
- self.assertWarnings(logs)
+ self.assertWarnings(caplog)
# graft
file_list = FileList()
@@ -276,11 +279,11 @@ class TestFileList:
file_list.process_template_line('graft d')
assert file_list.files == [mlp('d/b.py'), mlp('d/d/e.py')]
- self.assertNoWarnings(logs)
+ self.assertNoWarnings(caplog)
file_list.process_template_line('graft e')
assert file_list.files == [mlp('d/b.py'), mlp('d/d/e.py')]
- self.assertWarnings(logs)
+ self.assertWarnings(caplog)
# prune
file_list = FileList()
@@ -288,11 +291,11 @@ class TestFileList:
file_list.process_template_line('prune d')
assert file_list.files == ['a.py', mlp('f/f.py')]
- self.assertNoWarnings(logs)
+ self.assertNoWarnings(caplog)
file_list.process_template_line('prune e')
assert file_list.files == ['a.py', mlp('f/f.py')]
- self.assertWarnings(logs)
+ self.assertWarnings(caplog)
class TestFindAll:
diff --git a/distutils/tests/test_install.py b/distutils/tests/test_install.py
index e240b156..102218bc 100644
--- a/distutils/tests/test_install.py
+++ b/distutils/tests/test_install.py
@@ -4,6 +4,7 @@ import os
import sys
import site
import pathlib
+import logging
import pytest
@@ -15,7 +16,6 @@ from distutils.command.install import INSTALL_SCHEMES
from distutils.core import Distribution
from distutils.errors import DistutilsOptionError
from distutils.extension import Extension
-from distutils.log import DEBUG
from distutils.tests import support
from test import support as test_support
@@ -244,8 +244,9 @@ class TestInstall(
]
assert found == expected
- def test_debug_mode(self, logs, monkeypatch):
+ def test_debug_mode(self, caplog, monkeypatch):
# this covers the code called when DEBUG is set
monkeypatch.setattr(install_module, 'DEBUG', True)
+ caplog.set_level(logging.DEBUG)
self.test_record()
- assert logs.render(DEBUG)
+ assert any(rec for rec in caplog.records if rec.levelno == logging.DEBUG)
diff --git a/distutils/tests/test_install_lib.py b/distutils/tests/test_install_lib.py
index cdf3fc97..0bd67cd0 100644
--- a/distutils/tests/test_install_lib.py
+++ b/distutils/tests/test_install_lib.py
@@ -93,7 +93,7 @@ class TestInstallLib(
inputs = cmd.get_inputs()
assert len(inputs) == 2, inputs
- def test_dont_write_bytecode(self, logs):
+ def test_dont_write_bytecode(self, caplog):
# makes sure byte_compile is not used
dist = self.create_dist()[1]
cmd = install_lib(dist)
@@ -107,4 +107,4 @@ class TestInstallLib(
finally:
sys.dont_write_bytecode = old_dont_write_bytecode
- assert 'byte-compiling is disabled' in logs.render()[0]
+ assert 'byte-compiling is disabled' in caplog.messages[0]
diff --git a/distutils/tests/test_log.py b/distutils/tests/test_log.py
index 7aeee405..d346d07b 100644
--- a/distutils/tests/test_log.py
+++ b/distutils/tests/test_log.py
@@ -1,52 +1,13 @@
"""Tests for distutils.log"""
-import io
-import sys
-from test.support import swap_attr
-
-import pytest
+import logging
from distutils import log
class TestLog:
- @pytest.mark.parametrize(
- 'errors',
- (
- 'strict',
- 'backslashreplace',
- 'surrogateescape',
- 'replace',
- 'ignore',
- ),
- )
- def test_non_ascii(self, errors):
- # Issues #8663, #34421: test that non-encodable text is escaped with
- # backslashreplace error handler and encodable non-ASCII text is
- # output as is.
- stdout = io.TextIOWrapper(io.BytesIO(), encoding='cp437', errors=errors)
- stderr = io.TextIOWrapper(io.BytesIO(), encoding='cp437', errors=errors)
- old_threshold = log.set_threshold(log.DEBUG)
- try:
- with swap_attr(sys, 'stdout', stdout), swap_attr(sys, 'stderr', stderr):
- log.debug('Dεbug\tMėssãge')
- log.fatal('Fαtal\tÈrrōr')
- finally:
- log.set_threshold(old_threshold)
-
- stdout.seek(0)
- assert stdout.read().rstrip() == (
- 'Dεbug\tM?ss?ge'
- if errors == 'replace'
- else 'Dεbug\tMssge'
- if errors == 'ignore'
- else 'Dεbug\tM\\u0117ss\\xe3ge'
- )
- stderr.seek(0)
- assert stderr.read().rstrip() == (
- 'Fαtal\t?rr?r'
- if errors == 'replace'
- else 'Fαtal\trrr'
- if errors == 'ignore'
- else 'Fαtal\t\\xc8rr\\u014dr'
- )
+ def test_non_ascii(self, caplog):
+ caplog.set_level(logging.DEBUG)
+ log.debug('Dεbug\tMėssãge')
+ log.fatal('Fαtal\tÈrrōr')
+ assert caplog.messages == ['Dεbug\tMėssãge', 'Fαtal\tÈrrōr']
diff --git a/distutils/tests/test_register.py b/distutils/tests/test_register.py
index d0b4cc7c..a10393b5 100644
--- a/distutils/tests/test_register.py
+++ b/distutils/tests/test_register.py
@@ -6,7 +6,6 @@ import urllib
from distutils.command import register as register_module
from distutils.command.register import register
from distutils.errors import DistutilsSetupError
-from distutils.log import INFO
from distutils.tests.test_config import BasePyPIRCCommandTestCase
import pytest
@@ -303,13 +302,13 @@ class TestRegister(BasePyPIRCCommandTestCase):
with pytest.raises(DistutilsSetupError):
cmd.run()
- def test_list_classifiers(self, logs):
+ def test_list_classifiers(self, caplog):
cmd = self._get_cmd()
cmd.list_classifiers = 1
cmd.run()
- assert logs.render(INFO) == ['running check', 'xxx']
+ assert caplog.messages == ['running check', 'xxx']
- def test_show_response(self, logs):
+ def test_show_response(self, caplog):
# test that the --show-response option return a well formatted response
cmd = self._get_cmd()
inputs = Inputs('1', 'tarek', 'y')
@@ -320,5 +319,4 @@ class TestRegister(BasePyPIRCCommandTestCase):
finally:
del register_module.input
- results = logs.render(INFO)
- assert results[3] == 75 * '-' + '\nxxx\n' + 75 * '-'
+ assert caplog.messages[3] == 75 * '-' + '\nxxx\n' + 75 * '-'
diff --git a/distutils/tests/test_sdist.py b/distutils/tests/test_sdist.py
index bc535f38..97504722 100644
--- a/distutils/tests/test_sdist.py
+++ b/distutils/tests/test_sdist.py
@@ -18,7 +18,6 @@ from distutils.core import Distribution
from distutils.tests.test_config import BasePyPIRCCommandTestCase
from distutils.errors import DistutilsOptionError
from distutils.spawn import find_executable # noqa: F401
-from distutils.log import WARN
from distutils.filelist import FileList
from distutils.archive_util import ARCHIVE_FORMATS
@@ -251,8 +250,12 @@ class TestSDist(BasePyPIRCCommandTestCase):
f.close()
assert manifest == MANIFEST % {'sep': os.sep}
+ @staticmethod
+ def warnings(messages, prefix='warning: '):
+ return [msg for msg in messages if msg.startswith(prefix)]
+
@pytest.mark.usefixtures('needs_zlib')
- def test_metadata_check_option(self, logs):
+ def test_metadata_check_option(self, caplog):
# testing the `medata-check` option
dist, cmd = self.get_cmd(metadata={})
@@ -260,21 +263,15 @@ class TestSDist(BasePyPIRCCommandTestCase):
# with the `check` subcommand
cmd.ensure_finalized()
cmd.run()
- warnings = [
- msg for msg in logs.render(WARN) if msg.startswith('warning: check:')
- ]
- assert len(warnings) == 1
+ assert len(self.warnings(caplog.messages, 'warning: check: ')) == 1
# trying with a complete set of metadata
- logs.clear()
+ caplog.clear()
dist, cmd = self.get_cmd()
cmd.ensure_finalized()
cmd.metadata_check = 0
cmd.run()
- warnings = [
- msg for msg in logs.render(WARN) if msg.startswith('warning: check:')
- ]
- assert len(warnings) == 0
+ assert len(self.warnings(caplog.messages, 'warning: check: ')) == 0
def test_check_metadata_deprecated(self):
# makes sure make_metadata is deprecated
@@ -321,28 +318,27 @@ class TestSDist(BasePyPIRCCommandTestCase):
# the following tests make sure there is a nice error message instead
# of a traceback when parsing an invalid manifest template
- def _check_template(self, content, logs):
+ def _check_template(self, content, caplog):
dist, cmd = self.get_cmd()
os.chdir(self.tmp_dir)
self.write_file('MANIFEST.in', content)
cmd.ensure_finalized()
cmd.filelist = FileList()
cmd.read_template()
- warnings = logs.render(WARN)
- assert len(warnings) == 1
+ assert len(self.warnings(caplog.messages)) == 1
- def test_invalid_template_unknown_command(self, logs):
- self._check_template('taunt knights *', logs)
+ def test_invalid_template_unknown_command(self, caplog):
+ self._check_template('taunt knights *', caplog)
- def test_invalid_template_wrong_arguments(self, logs):
+ def test_invalid_template_wrong_arguments(self, caplog):
# this manifest command takes one argument
- self._check_template('prune', logs)
+ self._check_template('prune', caplog)
@pytest.mark.skipif("platform.system() != 'Windows'")
- def test_invalid_template_wrong_path(self, logs):
+ def test_invalid_template_wrong_path(self, caplog):
# on Windows, trailing slashes are not allowed
# this used to crash instead of raising a warning: #8286
- self._check_template('include examples/', logs)
+ self._check_template('include examples/', caplog)
@pytest.mark.usefixtures('needs_zlib')
def test_get_file_list(self):
diff --git a/distutils/tests/test_upload.py b/distutils/tests/test_upload.py
index efd9e906..9685c065 100644
--- a/distutils/tests/test_upload.py
+++ b/distutils/tests/test_upload.py
@@ -8,7 +8,6 @@ from distutils.command import upload as upload_mod
from distutils.command.upload import upload
from distutils.core import Distribution
from distutils.errors import DistutilsError
-from distutils.log import ERROR, INFO
from distutils.tests.test_config import PYPIRC, BasePyPIRCCommandTestCase
import pytest
@@ -109,7 +108,7 @@ class TestUpload(BasePyPIRCCommandTestCase):
cmd.finalize_options()
assert cmd.password == 'xxx'
- def test_upload(self, logs):
+ def test_upload(self, caplog):
tmp = self.mkdtemp()
path = os.path.join(tmp, 'xxx')
self.write_file(path)
@@ -150,7 +149,7 @@ class TestUpload(BasePyPIRCCommandTestCase):
)
# The PyPI response body was echoed
- results = logs.render(INFO)
+ results = caplog.messages
assert results[-1] == 75 * '-' + '\nxyzzy\n' + 75 * '-'
# bpo-32304: archives whose last byte was b'\r' were corrupted due to
@@ -178,11 +177,11 @@ class TestUpload(BasePyPIRCCommandTestCase):
assert int(headers['Content-length']) >= 2172
assert b'long description\r' in self.last_open.req.data
- def test_upload_fails(self, logs):
+ def test_upload_fails(self, caplog):
self.next_msg = "Not Found"
self.next_code = 404
with pytest.raises(DistutilsError):
- self.test_upload(logs)
+ self.test_upload(caplog)
@pytest.mark.parametrize(
'exception,expected,raised_exception',
@@ -196,7 +195,7 @@ class TestUpload(BasePyPIRCCommandTestCase):
),
],
)
- def test_wrong_exception_order(self, exception, expected, raised_exception, logs):
+ def test_wrong_exception_order(self, exception, expected, raised_exception, caplog):
tmp = self.mkdtemp()
path = os.path.join(tmp, 'xxx')
self.write_file(path)
@@ -213,6 +212,6 @@ class TestUpload(BasePyPIRCCommandTestCase):
cmd = upload(dist)
cmd.ensure_finalized()
cmd.run()
- results = logs.render(ERROR)
+ results = caplog.messages
assert expected in results[-1]
- logs.clear()
+ caplog.clear()