Browse Source

More tests for colored logging.

end-to-end-database-tests
Dan Helfman 2 years ago
parent
commit
a13cc0ab17
  1. 8
      borgmatic/config/convert.py
  2. 20
      borgmatic/logger.py
  3. 2
      tests/unit/config/test_convert.py
  4. 25
      tests/unit/test_logger.py

8
borgmatic/config/convert.py

@ -64,9 +64,9 @@ def convert_legacy_parsed_config(source_config, source_excludes, schema):
return destination_config
class LegacyConfigurationNotUpgraded(FileNotFoundError):
class Legacy_configuration_not_upgraded(FileNotFoundError):
def __init__(self):
super(LegacyConfigurationNotUpgraded, self).__init__(
super(Legacy_configuration_not_upgraded, self).__init__(
'''borgmatic changed its configuration file format in version 1.1.0 from INI-style
to YAML. This better supports validation, and has a more natural way to express
lists of values. To upgrade your existing configuration, run:
@ -83,7 +83,7 @@ instead of the old one.'''
def guard_configuration_upgraded(source_config_filename, destination_config_filenames):
'''
If legacy source configuration exists but no destination upgraded configs do, raise
LegacyConfigurationNotUpgraded.
Legacy_configuration_not_upgraded.
The idea is that we want to alert the user about upgrading their config if they haven't already.
'''
@ -92,4 +92,4 @@ def guard_configuration_upgraded(source_config_filename, destination_config_file
)
if os.path.exists(source_config_filename) and not destination_config_exists:
raise LegacyConfigurationNotUpgraded()
raise Legacy_configuration_not_upgraded()

20
borgmatic/logger.py

@ -38,32 +38,38 @@ def should_do_markup(no_color):
LOG_LEVEL_TO_COLOR = {
logging.CRITICAL: colorama.Fore.RED,
logging.ERROR: colorama.Fore.RED,
logging.WARN: colorama.Fore.YELLOW,
logging.INFO: colorama.Fore.GREEN,
logging.DEBUG: colorama.Fore.CYAN,
}
class BorgmaticLogger(logging.Logger):
class Borgmatic_logger(logging.Logger):
def critical(self, msg, *args, **kwargs):
color = LOG_LEVEL_TO_COLOR.get(logging.CRITICAL)
return super(BorgmaticLogger, self).critical(color_text(color, msg), *args, **kwargs)
return super(Borgmatic_logger, self).critical(color_text(color, msg), *args, **kwargs)
def error(self, msg, *args, **kwargs):
color = LOG_LEVEL_TO_COLOR.get(logging.ERROR)
return super(Borgmatic_logger, self).error(color_text(color, msg), *args, **kwargs)
def warn(self, msg, *args, **kwargs):
color = LOG_LEVEL_TO_COLOR.get(logging.WARN)
return super(BorgmaticLogger, self).warn(color_text(color, msg), *args, **kwargs)
return super(Borgmatic_logger, self).warn(color_text(color, msg), *args, **kwargs)
def info(self, msg, *args, **kwargs):
color = LOG_LEVEL_TO_COLOR.get(logging.INFO)
return super(BorgmaticLogger, self).info(color_text(color, msg), *args, **kwargs)
return super(Borgmatic_logger, self).info(color_text(color, msg), *args, **kwargs)
def debug(self, msg, *args, **kwargs):
color = LOG_LEVEL_TO_COLOR.get(logging.DEBUG)
return super(BorgmaticLogger, self).debug(color_text(color, msg), *args, **kwargs)
return super(Borgmatic_logger, self).debug(color_text(color, msg), *args, **kwargs)
def handle(self, record):
color = LOG_LEVEL_TO_COLOR.get(record.levelno)
@ -71,14 +77,14 @@ class BorgmaticLogger(logging.Logger):
dict(levelno=record.levelno, msg=color_text(color, record.msg))
)
return super(BorgmaticLogger, self).handle(colored_record)
return super(Borgmatic_logger, self).handle(colored_record)
def get_logger(name=None):
'''
Build a logger with the given name.
'''
logging.setLoggerClass(BorgmaticLogger)
logging.setLoggerClass(Borgmatic_logger)
logger = logging.getLogger(name)
logger.propagate = False
return logger

2
tests/unit/config/test_convert.py

@ -91,7 +91,7 @@ def test_guard_configuration_upgraded_raises_when_only_source_config_present():
flexmock(os.path).should_receive('exists').with_args('config.yaml').and_return(False)
flexmock(os.path).should_receive('exists').with_args('other.yaml').and_return(False)
with pytest.raises(module.LegacyConfigurationNotUpgraded):
with pytest.raises(module.Legacy_configuration_not_upgraded):
module.guard_configuration_upgraded('config', ('config.yaml', 'other.yaml'))

25
tests/unit/test_logger.py

@ -47,3 +47,28 @@ def test_should_do_markup_prefers_PY_COLORS_to_stdout_tty_value():
flexmock(module).should_receive('to_bool').and_return(True)
assert module.should_do_markup(no_color=False) is True
@pytest.mark.parametrize('method_name', ('critical', 'error', 'warn', 'info', 'debug'))
def test_borgmatic_logger_log_method_does_not_raise(method_name):
flexmock(module).should_receive('color_text')
flexmock(module.logging.Logger).should_receive(method_name)
getattr(module.Borgmatic_logger('test'), method_name)(msg='hi')
def test_borgmatic_logger_handle_does_not_raise():
flexmock(module).should_receive('color_text')
flexmock(module.logging.Logger).should_receive('handle')
module.Borgmatic_logger('test').handle(
module.logging.makeLogRecord(dict(levelno=module.logging.CRITICAL, msg='hi'))
)
def test_color_text_does_not_raise():
module.color_text(module.colorama.Fore.RED, 'hi')
def test_color_text_without_color_does_not_raise():
module.color_text(None, 'hi')

Loading…
Cancel
Save