Test new logging approach, both debug.logger and resetLogging

This commit is contained in:
Dmitri Bogomolov 2019-08-30 14:56:46 +03:00
parent bbdbca253b
commit a48b51721d
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13
2 changed files with 73 additions and 2 deletions

View File

@ -68,7 +68,8 @@ def configureLogging():
fail_msg = '' fail_msg = ''
try: try:
logging_config = os.path.join(state.appdata, 'logging.dat') logging_config = os.path.join(state.appdata, 'logging.dat')
logging.config.fileConfig(logging_config) logging.config.fileConfig(
logging_config, disable_existing_loggers=False)
return ( return (
False, False,
'Loaded logger configuration from %s' % logging_config 'Loaded logger configuration from %s' % logging_config
@ -80,7 +81,8 @@ def configureLogging():
' logging config\n%s' % \ ' logging config\n%s' % \
(logging_config, sys.exc_info()) (logging_config, sys.exc_info())
else: else:
# no need to confuse the user if the logger config is missing entirely # no need to confuse the user if the logger config
# is missing entirely
fail_msg = 'Using default logger configuration' fail_msg = 'Using default logger configuration'
logging_config = { logging_config = {

69
src/tests/test_logger.py Normal file
View File

@ -0,0 +1,69 @@
"""
Testing the logger configuration
"""
import logging
import os
import tempfile
import unittest
class TestLogger(unittest.TestCase):
"""A test case for bmconfigparser"""
conf_template = '''
[loggers]
keys=root
[handlers]
keys=default
[formatters]
keys=default
[formatter_default]
format=%(asctime)s {1} %(message)s
[handler_default]
class=FileHandler
level=NOTSET
formatter=default
args=('{0}', 'w')
[logger_root]
level=DEBUG
handlers=default
'''
def test_fileConfig(self):
"""Put logging.dat with special pattern and check it was used"""
tmp = os.environ['BITMESSAGE_HOME'] = tempfile.gettempdir()
log_config = os.path.join(tmp, 'logging.dat')
log_file = os.path.join(tmp, 'debug.log')
def gen_log_config(pattern):
"""A small closure to generate logging.dat with custom pattern"""
with open(log_config, 'wb') as dst:
dst.write(self.conf_template.format(log_file, pattern))
pattern = r' o_0 '
gen_log_config(pattern)
try:
from pybitmessage.debug import logger, resetLogging
if not os.path.isfile(log_file): # second pass
pattern = r' <===> '
gen_log_config(pattern)
resetLogging()
except ImportError:
self.fail('There is no package pybitmessage. Things gone wrong.')
finally:
os.remove(log_config)
logger_ = logging.getLogger('default')
self.assertEqual(logger, logger_)
logger_.info('Testing the logger...')
self.assertRegexpMatches(open(log_file).read(), pattern)