unittests: Set up logging properly.
This commit is contained in:
parent
1c48440797
commit
7ed8f3d4ac
@ -23,6 +23,8 @@ import atexit
|
|||||||
|
|
||||||
from PyQt5.QtWidgets import QApplication
|
from PyQt5.QtWidgets import QApplication
|
||||||
|
|
||||||
|
from qutebrowser.test import log
|
||||||
|
|
||||||
# We create a singleton QApplication here.
|
# We create a singleton QApplication here.
|
||||||
|
|
||||||
qApp = QApplication([])
|
qApp = QApplication([])
|
||||||
@ -30,3 +32,4 @@ qApp.setApplicationName('qutebrowser')
|
|||||||
qApp.processEvents()
|
qApp.processEvents()
|
||||||
atexit.register(qApp.processEvents)
|
atexit.register(qApp.processEvents)
|
||||||
atexit.register(qApp.quit)
|
atexit.register(qApp.quit)
|
||||||
|
log.init()
|
||||||
|
@ -25,6 +25,7 @@ import logging
|
|||||||
import unittest
|
import unittest
|
||||||
import argparse
|
import argparse
|
||||||
import sys
|
import sys
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
from qutebrowser.utils import log
|
from qutebrowser.utils import log
|
||||||
|
|
||||||
@ -44,6 +45,8 @@ class BaseTest(unittest.TestCase):
|
|||||||
self.saved_handlers = logging._handlers.copy()
|
self.saved_handlers = logging._handlers.copy()
|
||||||
self.saved_handler_list = logging._handlerList[:]
|
self.saved_handler_list = logging._handlerList[:]
|
||||||
self.saved_loggers = saved_loggers = logger_dict.copy()
|
self.saved_loggers = saved_loggers = logger_dict.copy()
|
||||||
|
self.saved_name_to_level = logging._nameToLevel.copy()
|
||||||
|
self.saved_level_to_name = logging._levelToName.copy()
|
||||||
self.logger_states = {}
|
self.logger_states = {}
|
||||||
for name in saved_loggers:
|
for name in saved_loggers:
|
||||||
self.logger_states[name] = getattr(saved_loggers[name],
|
self.logger_states[name] = getattr(saved_loggers[name],
|
||||||
@ -52,6 +55,7 @@ class BaseTest(unittest.TestCase):
|
|||||||
logging._releaseLock()
|
logging._releaseLock()
|
||||||
|
|
||||||
self.root_logger = logging.getLogger("")
|
self.root_logger = logging.getLogger("")
|
||||||
|
self.root_handlers = self.root_logger.handlers[:]
|
||||||
self.original_logging_level = self.root_logger.getEffectiveLevel()
|
self.original_logging_level = self.root_logger.getEffectiveLevel()
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
@ -61,8 +65,14 @@ class BaseTest(unittest.TestCase):
|
|||||||
self.root_logger.removeHandler(h)
|
self.root_logger.removeHandler(h)
|
||||||
h.close()
|
h.close()
|
||||||
self.root_logger.setLevel(self.original_logging_level)
|
self.root_logger.setLevel(self.original_logging_level)
|
||||||
|
for h in self.root_handlers:
|
||||||
|
self.root_logger.addHandler(h)
|
||||||
logging._acquireLock()
|
logging._acquireLock()
|
||||||
try:
|
try:
|
||||||
|
logging._levelToName.clear()
|
||||||
|
logging._levelToName.update(self.saved_level_to_name)
|
||||||
|
logging._nameToLevel.clear()
|
||||||
|
logging._nameToLevel.update(self.saved_name_to_level)
|
||||||
logging._handlers.clear()
|
logging._handlers.clear()
|
||||||
logging._handlers.update(self.saved_handlers)
|
logging._handlers.update(self.saved_handlers)
|
||||||
logging._handlerList[:] = self.saved_handler_list
|
logging._handlerList[:] = self.saved_handler_list
|
||||||
@ -192,6 +202,7 @@ class RAMHandlerTests(BaseTest):
|
|||||||
self.assertEqual(self.handler.dump_log(), "Two\nThree")
|
self.assertEqual(self.handler.dump_log(), "Two\nThree")
|
||||||
|
|
||||||
|
|
||||||
|
@mock.patch('qutebrowser.utils.log.qInstallMessageHandler', autospec=True)
|
||||||
class InitLogTests(BaseTest):
|
class InitLogTests(BaseTest):
|
||||||
|
|
||||||
"""Tests for init_log."""
|
"""Tests for init_log."""
|
||||||
@ -201,7 +212,7 @@ class InitLogTests(BaseTest):
|
|||||||
self.args = argparse.Namespace(debug=True, loglevel=logging.DEBUG,
|
self.args = argparse.Namespace(debug=True, loglevel=logging.DEBUG,
|
||||||
color=True, loglines=10, logfilter="")
|
color=True, loglines=10, logfilter="")
|
||||||
|
|
||||||
def test_stderr_none(self):
|
def test_stderr_none(self, _mock):
|
||||||
"""Test init_log with sys.stderr = None."""
|
"""Test init_log with sys.stderr = None."""
|
||||||
old_stderr = sys.stderr
|
old_stderr = sys.stderr
|
||||||
sys.stderr = None
|
sys.stderr = None
|
||||||
|
Loading…
Reference in New Issue
Block a user