[Python-checkins] cpython: Change signature of packaging.tests.support.LoggingCatcher.get_logs.

eric.araujo python-checkins at python.org
Wed Oct 19 21:32:56 CEST 2011


http://hg.python.org/cpython/rev/c78c617a8bef
changeset:   73004:c78c617a8bef
user:        Éric Araujo <merwok at netwok.org>
date:        Wed Oct 19 08:37:22 2011 +0200
summary:
  Change signature of packaging.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:
  Lib/packaging/tests/support.py                  |  39 ++++----
  Lib/packaging/tests/test_command_check.py       |  45 ++++-----
  Lib/packaging/tests/test_command_cmd.py         |   3 +-
  Lib/packaging/tests/test_command_sdist.py       |  12 +-
  Lib/packaging/tests/test_command_test.py        |   3 +-
  Lib/packaging/tests/test_command_upload_docs.py |  10 +-
  Lib/packaging/tests/test_config.py              |   8 +-
  Lib/packaging/tests/test_dist.py                |   5 +-
  Lib/packaging/tests/test_manifest.py            |   7 +-
  Lib/packaging/tests/test_metadata.py            |   5 +-
  10 files changed, 65 insertions(+), 72 deletions(-)


diff --git a/Lib/packaging/tests/support.py b/Lib/packaging/tests/support.py
--- a/Lib/packaging/tests/support.py
+++ b/Lib/packaging/tests/support.py
@@ -82,10 +82,13 @@
     configured to record all messages logged to the 'packaging' 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):
@@ -109,25 +112,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/Lib/packaging/tests/test_command_check.py b/Lib/packaging/tests/test_command_check.py
--- a/Lib/packaging/tests/test_command_check.py
+++ b/Lib/packaging/tests/test_command_check.py
@@ -1,6 +1,5 @@
 """Tests for distutils.command.check."""
 
-import logging
 from packaging.command.check import check
 from packaging.metadata import _HAS_DOCUTILS
 from packaging.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': 'xxx', 'author': '\u00c9ric',
@@ -62,15 +61,15 @@
                     'version': '1.2',
                     'summary': 'Something about esszet \u00df',
                     'description': '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='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/Lib/packaging/tests/test_command_cmd.py b/Lib/packaging/tests/test_command_cmd.py
--- a/Lib/packaging/tests/test_command_cmd.py
+++ b/Lib/packaging/tests/test_command_cmd.py
@@ -1,5 +1,6 @@
 """Tests for distutils.cmd."""
 import os
+import logging
 
 from packaging.command.cmd import Command
 from packaging.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/Lib/packaging/tests/test_command_sdist.py b/Lib/packaging/tests/test_command_sdist.py
--- a/Lib/packaging/tests/test_command_sdist.py
+++ b/Lib/packaging/tests/test_command_sdist.py
@@ -2,7 +2,6 @@
 import os
 import zipfile
 import tarfile
-import logging
 
 from packaging.tests.support import requires_zlib
 
@@ -221,7 +220,7 @@
         # with the check subcommand
         cmd.ensure_finalized()
         cmd.run()
-        warnings = self.get_logs(logging.WARN)
+        warnings = self.get_logs()
         self.assertEqual(len(warnings), 4)
 
         # trying with a complete set of metadata
@@ -230,13 +229,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)
diff --git a/Lib/packaging/tests/test_command_test.py b/Lib/packaging/tests/test_command_test.py
--- a/Lib/packaging/tests/test_command_test.py
+++ b/Lib/packaging/tests/test_command_test.py
@@ -2,7 +2,6 @@
 import re
 import sys
 import shutil
-import logging
 import unittest as ut1
 import packaging.database
 
@@ -149,7 +148,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/Lib/packaging/tests/test_command_upload_docs.py b/Lib/packaging/tests/test_command_upload_docs.py
--- a/Lib/packaging/tests/test_command_upload_docs.py
+++ b/Lib/packaging/tests/test_command_upload_docs.py
@@ -1,6 +1,7 @@
 """Tests for packaging.command.upload_docs."""
 import os
 import shutil
+import logging
 import zipfile
 try:
     import _ssl
@@ -141,13 +142,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)
@@ -171,7 +175,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/Lib/packaging/tests/test_config.py b/Lib/packaging/tests/test_config.py
--- a/Lib/packaging/tests/test_config.py
+++ b/Lib/packaging/tests/test_config.py
@@ -1,7 +1,6 @@
 """Tests for packaging.config."""
 import os
 import sys
-import logging
 from io import StringIO
 
 from packaging import command
@@ -375,15 +374,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])
 
@@ -397,7 +395,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/Lib/packaging/tests/test_dist.py b/Lib/packaging/tests/test_dist.py
--- a/Lib/packaging/tests/test_dist.py
+++ b/Lib/packaging/tests/test_dist.py
@@ -1,7 +1,6 @@
 """Tests for packaging.dist."""
 import os
 import sys
-import logging
 import textwrap
 
 import packaging.dist
@@ -74,7 +73,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])
 
@@ -85,7 +84,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/Lib/packaging/tests/test_manifest.py b/Lib/packaging/tests/test_manifest.py
--- a/Lib/packaging/tests/test_manifest.py
+++ b/Lib/packaging/tests/test_manifest.py
@@ -1,7 +1,6 @@
 """Tests for packaging.manifest."""
 import os
 import re
-import logging
 from io import StringIO
 from packaging.errors import PackagingTemplateError
 from packaging.manifest import Manifest, _translate_pattern, _glob_to_re
@@ -37,10 +36,10 @@
         super(ManifestTestCase, self).tearDown()
 
     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()
@@ -51,7 +50,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/Lib/packaging/tests/test_metadata.py b/Lib/packaging/tests/test_metadata.py
--- a/Lib/packaging/tests/test_metadata.py
+++ b/Lib/packaging/tests/test_metadata.py
@@ -1,7 +1,6 @@
 """Tests for packaging.metadata."""
 import os
 import sys
-import logging
 from textwrap import dedent
 from io import StringIO
 
@@ -302,7 +301,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])
 
@@ -418,7 +417,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/cpython


More information about the Python-checkins mailing list