From a13cc0ab17b1ef70fef34ba2456ddb1a76ba1c27 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Mon, 13 May 2019 21:10:26 -0700 Subject: [PATCH] More tests for colored logging. --- borgmatic/config/convert.py | 8 ++++---- borgmatic/logger.py | 20 +++++++++++++------- tests/unit/config/test_convert.py | 2 +- tests/unit/test_logger.py | 25 +++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 12 deletions(-) diff --git a/borgmatic/config/convert.py b/borgmatic/config/convert.py index 557dff6..23cc08b 100644 --- a/borgmatic/config/convert.py +++ b/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() diff --git a/borgmatic/logger.py b/borgmatic/logger.py index 5df5a78..8a8eda0 100644 --- a/borgmatic/logger.py +++ b/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 diff --git a/tests/unit/config/test_convert.py b/tests/unit/config/test_convert.py index 15796cf..73c3099 100644 --- a/tests/unit/config/test_convert.py +++ b/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')) diff --git a/tests/unit/test_logger.py b/tests/unit/test_logger.py index 136c568..3fd5f27 100644 --- a/tests/unit/test_logger.py +++ b/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')