[Python-checkins] distutils2: Change signature of tests.support.LoggingCatcher.get_logs.
eric.araujo
python-checkins at python.org
Mon Nov 14 15:24:07 CET 2011
http://hg.python.org/distutils2/rev/be248fc83a60
changeset: 1238:be248fc83a60
user: Éric Araujo <merwok at netwok.org>
date: Sat Nov 12 03:18:15 2011 +0100
summary:
Change signature of tests.support.LoggingCatcher.get_logs.
I need this for some tests, and it makes code clearer. This commit also
changes some assertEqual calls to use (actual, expected) order and fix
some pyflakes warnings.
files:
CHANGES.txt | 2 +
distutils2/tests/support.py | 39 ++++----
distutils2/tests/test_command_check.py | 45 ++++-----
distutils2/tests/test_command_cmd.py | 3 +-
distutils2/tests/test_command_sdist.py | 21 ++--
distutils2/tests/test_command_test.py | 3 +-
distutils2/tests/test_command_upload_docs.py | 10 +-
distutils2/tests/test_config.py | 8 +-
distutils2/tests/test_dist.py | 6 +-
distutils2/tests/test_manifest.py | 7 +-
distutils2/tests/test_metadata.py | 5 +-
11 files changed, 72 insertions(+), 77 deletions(-)
diff --git a/CHANGES.txt b/CHANGES.txt
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -151,6 +151,8 @@
- #11751: Improve test coverage for manifest [justin]
- Byte compilation is now isolated from the calling Python -B or -O options
[éric]
+- The signature of tests.support.LoggingCatcher.get_logs changed, see
+ docstring [éric]
1.0a3 - 2010-10-08
diff --git a/distutils2/tests/support.py b/distutils2/tests/support.py
--- a/distutils2/tests/support.py
+++ b/distutils2/tests/support.py
@@ -90,10 +90,13 @@
configured to record all messages logged to the 'distutils2' logger.
Use get_logs to retrieve messages and self.loghandler.flush to discard
- them. get_logs automatically flushes the logs; if you test code that
- generates logging messages but don't use get_logs, you have to flush
- manually before doing other checks on logging message, otherwise you
- will get irrelevant results. See example in test_command_check.
+ them. get_logs automatically flushes the logs, unless you pass
+ *flush=False*, for example to make multiple calls to the method with
+ different level arguments. If your test calls some code that generates
+ logging message and then you don't call get_logs, you will need to flush
+ manually before testing other code in the same test_* method, otherwise
+ get_logs in the next lines will see messages from the previous lines.
+ See example in test_command_check.
"""
def setUp(self):
@@ -117,25 +120,23 @@
logger2to3.setLevel(self._old_levels[1])
super(LoggingCatcher, self).tearDown()
- def get_logs(self, *levels):
- """Return all log messages with level in *levels*.
+ def get_logs(self, level=logging.WARNING, flush=True):
+ """Return all log messages with given level.
- Without explicit levels given, returns all messages. *levels* defaults
- to all levels. For log calls with arguments (i.e.
- logger.info('bla bla %r', arg)), the messages will be formatted before
- being returned (e.g. "bla bla 'thing'").
+ *level* defaults to logging.WARNING.
+
+ For log calls with arguments (i.e. logger.info('bla bla %r', arg)),
+ the messages will be formatted before being returned (e.g. "bla bla
+ 'thing'").
Returns a list. Automatically flushes the loghandler after being
- called.
-
- Example: self.get_logs(logging.WARN, logging.DEBUG).
+ called, unless *flush* is False (this is useful to get e.g. all
+ warnings then all info messages).
"""
- if not levels:
- messages = [log.getMessage() for log in self.loghandler.buffer]
- else:
- messages = [log.getMessage() for log in self.loghandler.buffer
- if log.levelno in levels]
- self.loghandler.flush()
+ messages = [log.getMessage() for log in self.loghandler.buffer
+ if log.levelno == level]
+ if flush:
+ self.loghandler.flush()
return messages
diff --git a/distutils2/tests/test_command_check.py b/distutils2/tests/test_command_check.py
--- a/distutils2/tests/test_command_check.py
+++ b/distutils2/tests/test_command_check.py
@@ -1,6 +1,5 @@
"""Tests for distutils.command.check."""
-import logging
from distutils2.command.check import check
from distutils2.metadata import _HAS_DOCUTILS
from distutils2.errors import PackagingSetupError, MetadataMissingError
@@ -27,11 +26,11 @@
# let's run the command with no metadata at all
# by default, check is checking the metadata
# should have some warnings
- cmd = self._run()
+ self._run()
# trick: using assertNotEqual with an empty list will give us a more
# useful error message than assertGreater(.., 0) when the code change
# and the test fails
- self.assertNotEqual([], self.get_logs(logging.WARNING))
+ self.assertNotEqual(self.get_logs(), [])
# now let's add the required fields
# and run it again, to make sure we don't get
@@ -40,8 +39,8 @@
'author_email': 'xxx',
'name': 'xxx', 'version': '4.2',
}
- cmd = self._run(metadata)
- self.assertEqual([], self.get_logs(logging.WARNING))
+ self._run(metadata)
+ self.assertEqual(self.get_logs(), [])
# now with the strict mode, we should
# get an error if there are missing metadata
@@ -53,8 +52,8 @@
self.loghandler.flush()
# and of course, no error when all metadata fields are present
- cmd = self._run(metadata, strict=True)
- self.assertEqual([], self.get_logs(logging.WARNING))
+ self._run(metadata, strict=True)
+ self.assertEqual(self.get_logs(), [])
# now a test with non-ASCII characters
metadata = {'home_page': u'xxx', 'author': u'\u00c9ric',
@@ -62,15 +61,15 @@
'version': u'1.2',
'summary': u'Something about esszet \u00df',
'description': u'More things about esszet \u00df'}
- cmd = self._run(metadata)
- self.assertEqual([], self.get_logs(logging.WARNING))
+ self._run(metadata)
+ self.assertEqual(self.get_logs(), [])
def test_check_metadata_1_2(self):
# let's run the command with no metadata at all
# by default, check is checking the metadata
# should have some warnings
- cmd = self._run()
- self.assertNotEqual([], self.get_logs(logging.WARNING))
+ self._run()
+ self.assertNotEqual(self.get_logs(), [])
# now let's add the required fields and run it again, to make sure we
# don't get any warning anymore let's use requires_python as a marker
@@ -80,8 +79,8 @@
'name': 'xxx', 'version': '4.2',
'requires_python': '2.4',
}
- cmd = self._run(metadata)
- self.assertEqual([], self.get_logs(logging.WARNING))
+ self._run(metadata)
+ self.assertEqual(self.get_logs(), [])
# now with the strict mode, we should
# get an error if there are missing metadata
@@ -99,8 +98,8 @@
# now with correct version format again
metadata['version'] = '4.2'
- cmd = self._run(metadata, strict=True)
- self.assertEqual([], self.get_logs(logging.WARNING))
+ self._run(metadata, strict=True)
+ self.assertEqual(self.get_logs(), [])
@unittest.skipUnless(_HAS_DOCUTILS, "requires docutils")
def test_check_restructuredtext(self):
@@ -109,9 +108,7 @@
pkg_info, dist = self.create_dist(description=broken_rest)
cmd = check(dist)
cmd.check_restructuredtext()
- self.assertEqual(len(self.get_logs(logging.WARNING)), 1)
- # clear warnings from the previous call
- self.loghandler.flush()
+ self.assertEqual(len(self.get_logs()), 1)
# let's see if we have an error with strict=1
metadata = {'home_page': 'xxx', 'author': 'xxx',
@@ -126,7 +123,7 @@
dist = self.create_dist(description=u'title\n=====\n\ntest \u00df')[1]
cmd = check(dist)
cmd.check_restructuredtext()
- self.assertEqual([], self.get_logs(logging.WARNING))
+ self.assertEqual(self.get_logs(), [])
def test_check_all(self):
self.assertRaises(PackagingSetupError, self._run,
@@ -143,18 +140,18 @@
}
cmd = check(dist)
cmd.check_hooks_resolvable()
- self.assertEqual(len(self.get_logs(logging.WARNING)), 1)
+ self.assertEqual(len(self.get_logs()), 1)
def test_warn(self):
_, dist = self.create_dist()
cmd = check(dist)
- self.assertEqual([], self.get_logs())
+ self.assertEqual(self.get_logs(), [])
cmd.warn('hello')
- self.assertEqual(['check: hello'], self.get_logs())
+ self.assertEqual(self.get_logs(), ['check: hello'])
cmd.warn('hello %s', 'world')
- self.assertEqual(['check: hello world'], self.get_logs())
+ self.assertEqual(self.get_logs(), ['check: hello world'])
cmd.warn('hello %s %s', 'beautiful', 'world')
- self.assertEqual(['check: hello beautiful world'], self.get_logs())
+ self.assertEqual(self.get_logs(), ['check: hello beautiful world'])
def test_suite():
diff --git a/distutils2/tests/test_command_cmd.py b/distutils2/tests/test_command_cmd.py
--- a/distutils2/tests/test_command_cmd.py
+++ b/distutils2/tests/test_command_cmd.py
@@ -1,5 +1,6 @@
"""Tests for distutils.cmd."""
import os
+import logging
from distutils2.command.cmd import Command
from distutils2.dist import Distribution
@@ -43,7 +44,7 @@
wanted = ["command options for 'MyCmd':", ' option1 = 1',
' option2 = 1']
- msgs = self.get_logs()
+ msgs = self.get_logs(logging.INFO)
self.assertEqual(msgs, wanted)
def test_ensure_string(self):
diff --git a/distutils2/tests/test_command_sdist.py b/distutils2/tests/test_command_sdist.py
--- a/distutils2/tests/test_command_sdist.py
+++ b/distutils2/tests/test_command_sdist.py
@@ -1,7 +1,6 @@
"""Tests for distutils2.command.sdist."""
import os
import zipfile
-import logging
from distutils2.tests.support import requires_zlib
@@ -226,12 +225,14 @@
# testing the `check-metadata` option
dist, cmd = self.get_cmd(metadata={'name': 'xxx', 'version': 'xxx'})
- # this should raise some warnings
- # with the check subcommand
+ # this should cause the check subcommand to log two warnings:
+ # version is invalid, home-page and author are missing
cmd.ensure_finalized()
cmd.run()
- warnings = self.get_logs(logging.WARN)
- self.assertEqual(len(warnings), 4)
+ warnings = self.get_logs()
+ check_warnings = [msg for msg in warnings if
+ not msg.startswith('sdist:')]
+ self.assertEqual(len(check_warnings), 2, warnings)
# trying with a complete set of metadata
self.loghandler.flush()
@@ -239,13 +240,10 @@
cmd.ensure_finalized()
cmd.metadata_check = False
cmd.run()
- warnings = self.get_logs(logging.WARN)
- # removing manifest generated warnings
- warnings = [warn for warn in warnings if
- not warn.endswith('-- skipping')]
- # the remaining warnings are about the use of the default file list and
- # the absence of setup.cfg
+ warnings = self.get_logs()
self.assertEqual(len(warnings), 2)
+ self.assertIn('using default file list', warnings[0])
+ self.assertIn("'setup.cfg' file not found", warnings[1])
def test_show_formats(self):
__, stdout = captured_stdout(show_formats)
@@ -257,7 +255,6 @@
self.assertEqual(len(output), num_formats)
def test_finalize_options(self):
-
dist, cmd = self.get_cmd()
cmd.finalize_options()
diff --git a/distutils2/tests/test_command_test.py b/distutils2/tests/test_command_test.py
--- a/distutils2/tests/test_command_test.py
+++ b/distutils2/tests/test_command_test.py
@@ -2,7 +2,6 @@
import re
import sys
import shutil
-import logging
import unittest as ut1
import distutils2.database
@@ -150,7 +149,7 @@
phony_project = 'ohno_ohno-impossible_1234-name_stop-that!'
cmd.tests_require = [phony_project]
cmd.ensure_finalized()
- logs = self.get_logs(logging.WARNING)
+ logs = self.get_logs()
self.assertIn(phony_project, logs[-1])
def prepare_a_module(self):
diff --git a/distutils2/tests/test_command_upload_docs.py b/distutils2/tests/test_command_upload_docs.py
--- a/distutils2/tests/test_command_upload_docs.py
+++ b/distutils2/tests/test_command_upload_docs.py
@@ -1,6 +1,7 @@
"""Tests for distutils2.command.upload_docs."""
import os
import shutil
+import logging
import zipfile
try:
import _ssl
@@ -140,13 +141,16 @@
self.pypi.default_response_status = '403 Forbidden'
self.prepare_command()
self.cmd.run()
- self.assertIn('Upload failed (403): Forbidden', self.get_logs()[-1])
+ errors = self.get_logs(logging.ERROR)
+ self.assertEqual(len(errors), 1)
+ self.assertIn('Upload failed (403): Forbidden', errors[0])
self.pypi.default_response_status = '301 Moved Permanently'
self.pypi.default_response_headers.append(
("Location", "brand_new_location"))
self.cmd.run()
- self.assertIn('brand_new_location', self.get_logs()[-1])
+ lastlog = self.get_logs(logging.INFO)[-1]
+ self.assertIn('brand_new_location', lastlog)
def test_reads_pypirc_data(self):
self.write_file(self.rc, PYPIRC % self.pypi.full_address)
@@ -170,7 +174,7 @@
self.prepare_command()
self.cmd.show_response = True
self.cmd.run()
- record = self.get_logs()[-1]
+ record = self.get_logs(logging.INFO)[-1]
self.assertTrue(record, "should report the response")
self.assertIn(self.pypi.default_response_data, record)
diff --git a/distutils2/tests/test_config.py b/distutils2/tests/test_config.py
--- a/distutils2/tests/test_config.py
+++ b/distutils2/tests/test_config.py
@@ -2,7 +2,6 @@
"""Tests for distutils2.config."""
import os
import sys
-import logging
from StringIO import StringIO
from distutils2 import command
@@ -376,15 +375,14 @@
self.write_file('README', 'yeah')
self.write_file('hooks.py', HOOKS_MODULE)
self.get_dist()
- logs = self.get_logs(logging.WARNING)
- self.assertEqual(['logging_hook called'], logs)
+ self.assertEqual(['logging_hook called'], self.get_logs())
self.assertIn('hooks', sys.modules)
def test_missing_setup_hook_warns(self):
self.write_setup({'setup-hooks': 'this.does._not.exist'})
self.write_file('README', 'yeah')
self.get_dist()
- logs = self.get_logs(logging.WARNING)
+ logs = self.get_logs()
self.assertEqual(1, len(logs))
self.assertIn('cannot find setup hook', logs[0])
@@ -398,7 +396,7 @@
dist = self.get_dist()
self.assertEqual(['haven', 'first', 'third'], dist.py_modules)
- logs = self.get_logs(logging.WARNING)
+ logs = self.get_logs()
self.assertEqual(1, len(logs))
self.assertIn('cannot find setup hook', logs[0])
diff --git a/distutils2/tests/test_dist.py b/distutils2/tests/test_dist.py
--- a/distutils2/tests/test_dist.py
+++ b/distutils2/tests/test_dist.py
@@ -1,8 +1,6 @@
"""Tests for distutils2.dist."""
import os
import sys
-import codecs
-import logging
import textwrap
import distutils2.dist
@@ -78,7 +76,7 @@
'version': '1.2',
'home_page': 'xxxx',
'badoptname': 'xxx'})
- logs = self.get_logs(logging.WARNING)
+ logs = self.get_logs()
self.assertEqual(len(logs), 1)
self.assertIn('unknown argument', logs[0])
@@ -89,7 +87,7 @@
'version': '1.2', 'home_page': 'xxxx',
'options': {}})
- self.assertEqual([], self.get_logs(logging.WARNING))
+ self.assertEqual(self.get_logs(), [])
self.assertNotIn('options', dir(dist))
def test_non_empty_options(self):
diff --git a/distutils2/tests/test_manifest.py b/distutils2/tests/test_manifest.py
--- a/distutils2/tests/test_manifest.py
+++ b/distutils2/tests/test_manifest.py
@@ -1,7 +1,6 @@
"""Tests for distutils2.manifest."""
import os
import re
-import logging
from StringIO import StringIO
from distutils2.errors import PackagingTemplateError
from distutils2.manifest import Manifest, _translate_pattern, _glob_to_re
@@ -29,10 +28,10 @@
unittest.TestCase):
def assertNoWarnings(self):
- self.assertEqual(self.get_logs(logging.WARNING), [])
+ self.assertEqual(self.get_logs(), [])
def assertWarnings(self):
- self.assertGreater(len(self.get_logs(logging.WARNING)), 0)
+ self.assertNotEqual(self.get_logs(), [])
def test_manifest_reader(self):
tmpdir = self.mkdtemp()
@@ -46,7 +45,7 @@
manifest = Manifest()
manifest.read_template(MANIFEST)
- warnings = self.get_logs(logging.WARNING)
+ warnings = self.get_logs()
# the manifest should have been read and 3 warnings issued
# (we didn't provide the files)
self.assertEqual(3, len(warnings))
diff --git a/distutils2/tests/test_metadata.py b/distutils2/tests/test_metadata.py
--- a/distutils2/tests/test_metadata.py
+++ b/distutils2/tests/test_metadata.py
@@ -3,7 +3,6 @@
import os
import sys
import codecs
-import logging
from textwrap import dedent
from StringIO import StringIO
@@ -318,7 +317,7 @@
'name': 'xxx',
'version': 'xxx',
'home_page': 'xxxx'})
- logs = self.get_logs(logging.WARNING)
+ logs = self.get_logs()
self.assertEqual(1, len(logs))
self.assertIn('not a valid version', logs[0])
@@ -440,7 +439,7 @@
# XXX check PEP and see if 3 == 3.0
metadata['Requires-Python'] = '>=2.6, <3.0'
metadata['Requires-Dist'] = ['Foo (>=2.6, <3.0)']
- self.assertEqual([], self.get_logs(logging.WARNING))
+ self.assertEqual(self.get_logs(), [])
@unittest.skip('needs to be implemented')
def test_requires_illegal(self):
--
Repository URL: http://hg.python.org/distutils2
More information about the Python-checkins
mailing list