From e50abb20b3c6debd9fc35da83b637422437c7e7c Mon Sep 17 00:00:00 2001 From: derwinlu Date: Mon, 19 Oct 2015 22:38:23 +0200 Subject: [PATCH] Add logging for warnings during test suite run The default configuration for the warnings module changed some time ago so we lost the ability to detect deprecation warnings and such early. This commit sets up the test suite to redirect all warnings through our logging system. Additionally we enable DeprecationWarnings as a default and throw exceptions on all warnings related to pelican modules. This enables output of warnings related to dependencies, while letting tests only fail if the warnings are originating from pelican's source. Also adding a test to detect if warnings cause an Exception as expected. --- pelican/log.py | 24 +++++++++++++++++------- pelican/tests/__init__.py | 13 +++++++++++++ pelican/tests/test_testsuite.py | 16 ++++++++++++++++ 3 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 pelican/tests/test_testsuite.py diff --git a/pelican/log.py b/pelican/log.py index 0f4b795b..ddeeb6d0 100644 --- a/pelican/log.py +++ b/pelican/log.py @@ -172,21 +172,31 @@ class LimitLogger(SafeLogger): logging.setLoggerClass(LimitLogger) -def init(level=None, handler=logging.StreamHandler()): - - logger = logging.getLogger() - +def get_formatter(): if os.isatty(sys.stdout.fileno()) and not sys.platform.startswith('win'): - fmt = ANSIFormatter() + return ANSIFormatter() else: - fmt = TextFormatter() - handler.setFormatter(fmt) + return TextFormatter() + + +def init(level=None, handler=logging.StreamHandler(), name=None): + + logger = logging.getLogger(name) + + handler.setFormatter(get_formatter()) logger.addHandler(handler) if level: logger.setLevel(level) +def log_warnings(): + import warnings + logging.captureWarnings(True) + warnings.simplefilter("default", DeprecationWarning) + init(logging.DEBUG, name='py.warnings') + + if __name__ == '__main__': init(level=logging.DEBUG) diff --git a/pelican/tests/__init__.py b/pelican/tests/__init__.py index 32353ea2..4605a02b 100644 --- a/pelican/tests/__init__.py +++ b/pelican/tests/__init__.py @@ -1,2 +1,15 @@ import logging +import warnings + +from pelican.log import log_warnings + +# redirect warnings modulole to use logging instead +log_warnings() + +# setup warnings to log DeprecationWarning's and error on +# warnings in pelican's codebase +warnings.simplefilter("default", DeprecationWarning) +warnings.filterwarnings("error", ".*", Warning, "pelican") + +# Add a NullHandler to silence warning about no available handlers logging.getLogger().addHandler(logging.NullHandler()) diff --git a/pelican/tests/test_testsuite.py b/pelican/tests/test_testsuite.py new file mode 100644 index 00000000..5dc92bb1 --- /dev/null +++ b/pelican/tests/test_testsuite.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +from __future__ import print_function, unicode_literals + +import sys +import warnings + +from pelican.tests.support import unittest + + +class TestSuiteTest(unittest.TestCase): + + @unittest.skipIf(sys.version_info[:2] == (3, 3), + "does not throw an exception on python 3.3") + def test_error_on_warning(self): + with self.assertRaises(UserWarning): + warnings.warn('test warning')