qutebrowser/tests/utils/test_log.py

253 lines
9.1 KiB
Python
Raw Normal View History

2014-06-19 09:04:37 +02:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2015-01-03 15:51:31 +01:00
# Copyright 2014-2015 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
2014-05-27 16:24:26 +02:00
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
2014-08-04 03:47:09 +02:00
# pylint: disable=protected-access
2014-05-27 16:24:26 +02:00
"""Tests for qutebrowser.utils.log."""
import logging
import unittest
import argparse
import sys
2015-03-05 23:41:46 +01:00
from unittest import mock
2014-05-27 16:24:26 +02:00
2014-08-26 19:10:14 +02:00
from qutebrowser.utils import log
2014-05-27 16:24:26 +02:00
from PyQt5.QtCore import qWarning
2014-05-27 16:24:26 +02:00
2014-06-23 19:44:21 +02:00
class BaseTest(unittest.TestCase):
2014-06-20 08:49:48 +02:00
"""Base class for logging tests.
Based on CPython's Lib/test/test_logging.py.
"""
def setUp(self):
"""Save the old logging configuration."""
logger_dict = logging.getLogger().manager.loggerDict
logging._acquireLock()
try:
self.saved_handlers = logging._handlers.copy()
self.saved_handler_list = logging._handlerList[:]
self.saved_loggers = saved_loggers = logger_dict.copy()
2015-03-05 23:41:46 +01:00
self.saved_name_to_level = logging._nameToLevel.copy()
self.saved_level_to_name = logging._levelToName.copy()
2014-06-20 08:49:48 +02:00
self.logger_states = {}
for name in saved_loggers:
self.logger_states[name] = getattr(saved_loggers[name],
'disabled', None)
finally:
logging._releaseLock()
self.root_logger = logging.getLogger("")
2015-03-05 23:41:46 +01:00
self.root_handlers = self.root_logger.handlers[:]
2014-06-20 08:49:48 +02:00
self.original_logging_level = self.root_logger.getEffectiveLevel()
def tearDown(self):
"""Restore the original logging configuration."""
while self.root_logger.handlers:
h = self.root_logger.handlers[0]
self.root_logger.removeHandler(h)
h.close()
self.root_logger.setLevel(self.original_logging_level)
2015-03-05 23:41:46 +01:00
for h in self.root_handlers:
self.root_logger.addHandler(h)
2014-06-20 08:49:48 +02:00
logging._acquireLock()
try:
2015-03-05 23:41:46 +01:00
logging._levelToName.clear()
logging._levelToName.update(self.saved_level_to_name)
logging._nameToLevel.clear()
logging._nameToLevel.update(self.saved_name_to_level)
2014-06-20 08:49:48 +02:00
logging._handlers.clear()
logging._handlers.update(self.saved_handlers)
logging._handlerList[:] = self.saved_handler_list
2014-06-23 19:44:21 +02:00
logger_dict = logging.getLogger().manager.loggerDict
logger_dict.clear()
logger_dict.update(self.saved_loggers)
2014-06-20 08:49:48 +02:00
logger_states = self.logger_states
for name in self.logger_states:
if logger_states[name] is not None:
self.saved_loggers[name].disabled = logger_states[name]
finally:
logging._releaseLock()
2014-06-23 19:44:21 +02:00
class LogFilterTests(unittest.TestCase):
2014-05-27 16:24:26 +02:00
"""Tests for LogFilter.
Attributes:
logger: The logger we use to create records.
"""
def setUp(self):
self.logger = logging.getLogger("foo")
def _make_record(self, name, level=logging.DEBUG):
2014-05-27 16:24:26 +02:00
"""Create a bogus logging record with the supplied logger name."""
2014-06-06 15:48:42 +02:00
return self.logger.makeRecord(name, level=level, fn=None, lno=0,
msg="", args=None, exc_info=None)
2014-05-27 16:24:26 +02:00
def test_empty(self):
"""Test if an empty filter lets all messages through."""
logfilter = log.LogFilter(None)
record = self._make_record("eggs.bacon.spam")
self.assertTrue(logfilter.filter(record))
record = self._make_record("eggs")
self.assertTrue(logfilter.filter(record))
def test_matching(self):
2015-03-31 20:49:29 +02:00
"""Test if a filter lets an exactly matching log record through."""
2014-05-27 16:24:26 +02:00
logfilter = log.LogFilter(["eggs", "bacon"])
record = self._make_record("eggs")
self.assertTrue(logfilter.filter(record))
record = self._make_record("bacon")
self.assertTrue(logfilter.filter(record))
record = self._make_record("spam")
self.assertFalse(logfilter.filter(record))
logfilter = log.LogFilter(["eggs.bacon"])
record = self._make_record("eggs.bacon")
self.assertTrue(logfilter.filter(record))
def test_equal_start(self):
"""Test if a filter blocks a logger which looks equal but isn't."""
logfilter = log.LogFilter(["eggs"])
record = self._make_record("eggsauce")
self.assertFalse(logfilter.filter(record))
logfilter = log.LogFilter("eggs.bacon")
record = self._make_record("eggs.baconstrips")
self.assertFalse(logfilter.filter(record))
def test_child(self):
"""Test if a filter lets through a logger which is a child."""
logfilter = log.LogFilter(["eggs.bacon", "spam.ham"])
record = self._make_record("eggs.bacon.spam")
self.assertTrue(logfilter.filter(record))
record = self._make_record("spam.ham.salami")
self.assertTrue(logfilter.filter(record))
2014-06-06 15:48:42 +02:00
def test_debug(self):
2014-06-06 16:51:24 +02:00
"""Test if messages more important than debug are never filtered."""
2014-06-06 15:48:42 +02:00
logfilter = log.LogFilter(["eggs"])
2014-06-06 16:51:24 +02:00
# First check if the filter works as intended with debug messages
record = self._make_record("eggs")
2014-06-06 15:48:42 +02:00
self.assertTrue(logfilter.filter(record))
record = self._make_record("bacon")
2014-06-06 16:51:24 +02:00
self.assertFalse(logfilter.filter(record))
# Then check if info is not filtered
record = self._make_record("eggs", level=logging.INFO)
self.assertTrue(logfilter.filter(record))
record = self._make_record("bacon", level=logging.INFO)
2014-06-06 15:48:42 +02:00
self.assertTrue(logfilter.filter(record))
2014-05-27 16:24:26 +02:00
2014-06-20 08:49:48 +02:00
class RAMHandlerTests(BaseTest):
2014-05-27 16:24:26 +02:00
"""Tests for RAMHandler.
Attributes:
logger: The logger we use to log to the handler.
handler: The RAMHandler we're testing.
old_level: The level the root logger had before executing the test.
old_handlers: The handlers the root logger had before executing the
test.
"""
def setUp(self):
2014-06-20 08:49:48 +02:00
super().setUp()
2014-05-27 16:24:26 +02:00
self.logger = logging.getLogger()
self.logger.handlers = []
self.logger.setLevel(logging.NOTSET)
self.handler = log.RAMHandler(capacity=2)
self.handler.setLevel(logging.NOTSET)
self.logger.addHandler(self.handler)
def test_filled(self):
"""Test handler with exactly as much records as it can hold."""
self.logger.debug("One")
self.logger.debug("Two")
self.assertEqual(len(self.handler._data), 2)
self.assertEqual(self.handler._data[0].msg, "One")
self.assertEqual(self.handler._data[1].msg, "Two")
2014-05-27 16:24:26 +02:00
def test_overflow(self):
"""Test handler with more records as it can hold."""
self.logger.debug("One")
self.logger.debug("Two")
self.logger.debug("Three")
self.assertEqual(len(self.handler._data), 2)
self.assertEqual(self.handler._data[0].msg, "Two")
self.assertEqual(self.handler._data[1].msg, "Three")
2014-05-27 16:24:26 +02:00
def test_dump_log(self):
"""Test dump_log()."""
self.logger.debug("One")
self.logger.debug("Two")
self.logger.debug("Three")
self.assertEqual(self.handler.dump_log(), "Two\nThree")
2015-03-05 23:41:46 +01:00
@mock.patch('qutebrowser.utils.log.qInstallMessageHandler', autospec=True)
class InitLogTests(BaseTest):
"""Tests for init_log."""
def setUp(self):
super().setUp()
self.args = argparse.Namespace(debug=True, loglevel=logging.DEBUG,
color=True, loglines=10, logfilter="")
2015-03-05 23:41:46 +01:00
def test_stderr_none(self, _mock):
"""Test init_log with sys.stderr = None."""
old_stderr = sys.stderr
sys.stderr = None
log.init_log(self.args)
sys.stderr = old_stderr
2014-05-27 16:24:26 +02:00
class HideQtWarningTests(BaseTest):
"""Tests for hide_qt_warning/QtWarningFilter."""
def test_unfiltered(self):
"""Test a message which is not filtered."""
with log.hide_qt_warning("World", logger='qt-tests'):
with self.assertLogs('qt-tests', logging.WARNING):
qWarning("Hello World")
def test_filtered_exact(self):
"""Test a message which is filtered (exact match)."""
with log.hide_qt_warning("Hello", logger='qt-tests'):
qWarning("Hello")
def test_filtered_start(self):
"""Test a message which is filtered (match at line start)."""
with log.hide_qt_warning("Hello", logger='qt-tests'):
qWarning("Hello World")
def test_filtered_whitespace(self):
"""Test a message which is filtered (match with whitespace)."""
with log.hide_qt_warning("Hello", logger='qt-tests'):
qWarning(" Hello World ")
2014-05-27 16:24:26 +02:00
if __name__ == '__main__':
unittest.main()