From ac69b3b5c73db290a712f92255bcdb0d8514a1b5 Mon Sep 17 00:00:00 2001 From: Niels Thykier Date: Sat, 24 Mar 2018 09:11:03 +0000 Subject: [PATCH] Migrate hint parser to fully use logging Signed-off-by: Niels Thykier --- britney.py | 2 +- britney2/hints.py | 24 ++++++++++++------------ tests/test_hint_parser.py | 25 ++++++++++--------------- tests/test_policy.py | 2 +- 4 files changed, 24 insertions(+), 29 deletions(-) diff --git a/britney.py b/britney.py index a0ac5df..fc27864 100755 --- a/britney.py +++ b/britney.py @@ -282,7 +282,7 @@ class Britney(object): # parse the command line arguments self.policies = [] - self._hint_parser = HintParser(self) + self._hint_parser = HintParser() self.suite_info = {} self.__parse_arguments() MigrationItem.set_architectures(self.options.architectures) diff --git a/britney2/hints.py b/britney2/hints.py index aa9a7e7..5fb39b1 100644 --- a/britney2/hints.py +++ b/britney2/hints.py @@ -12,6 +12,8 @@ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. +import logging + from itertools import chain from britney2.migrationitem import MigrationItem @@ -131,8 +133,9 @@ def single_hint_taking_list_of_packages(hints, who, hint_type, *args): class HintParser(object): - def __init__(self, britney): - self._britney = britney + def __init__(self): + logger_name = ".".join((self.__class__.__module__, self.__class__.__name__)) + self.logger = logging.getLogger(logger_name) self.hints = HintCollection() self._hint_table = { 'remark': (0, lambda *x: None), @@ -215,24 +218,21 @@ class HintParser(object): if hint_name == 'finished': break if hint_name not in hint_table: - self.log("Unknown hint found in %s (line %d): '%s'" % (filename, line_no, line), type="W") + self.logger.warning("Unknown hint found in %s (line %d): '%s'" % (filename, line_no, line)) continue if hint_name not in permitted_hints and 'ALL' not in permitted_hints: reason = 'The hint is not a part of the permitted hints for ' + who - self.log("Ignoring \"%s\" hint from %s found in %s (line %d): %s" % ( - hint_name, who, filename, line_no, reason), type="I") + self.logger.info("Ignoring \"%s\" hint from %s found in %s (line %d): %s" % ( + hint_name, who, filename, line_no, reason)) continue min_args, hint_parser_impl = hint_table[hint_name] if len(l) - 1 < min_args: - self.log("Malformed hint found in %s (line %d): Needs at least %d argument(s), got %d" % ( - filename, line_no, min_args, len(l) - 1), type="W") + self.logger.warning("Malformed hint found in %s (line %d): Needs at least %d argument(s), got %d" % ( + filename, line_no, min_args, len(l) - 1)) continue try: hint_parser_impl(hints, who, *l) except MalformedHintException as e: - self.log("Malformed hint found in %s (line %d): \"%s\"" % ( - filename, line_no, e.args[0]), type="W") + self.logger.warning("Malformed hint found in %s (line %d): \"%s\"" % ( + filename, line_no, e.args[0])) continue - - def log(self, msg, type="I"): - self._britney.log(msg, type=type) diff --git a/tests/test_hint_parser.py b/tests/test_hint_parser.py index 0105db0..834c179 100644 --- a/tests/test_hint_parser.py +++ b/tests/test_hint_parser.py @@ -1,18 +1,13 @@ +import logging import unittest from britney2.hints import HintParser, single_hint_taking_list_of_packages -from . import MockObject, HINTS_ALL, TEST_HINTER +from . import HINTS_ALL, TEST_HINTER -def new_hint_parser(logger=None): - if logger is None: - def empty_logger(x, type='I'): - pass - logger = empty_logger - fake_britney = MockObject(log=logger) - hint_parser = HintParser(fake_britney) - return hint_parser +def new_hint_parser(): + return HintParser() def parse_should_not_call_this_function(*args, **kwargs): @@ -22,8 +17,7 @@ def parse_should_not_call_this_function(*args, **kwargs): class HintParsing(unittest.TestCase): def test_parse_invalid_hints(self): - hint_log = [] - hint_parser = new_hint_parser(lambda x, type='I': hint_log.append(x)) + hint_parser = new_hint_parser() hint_parser.register_hint_type('min-10-arg', parse_should_not_call_this_function, min_args=10) hint_parser.register_hint_type('simple-hint', parse_should_not_call_this_function) @@ -47,11 +41,12 @@ class HintParsing(unittest.TestCase): ] for test in tests: - hint_parser.parse_hints(TEST_HINTER, test['permissions'], 'test-parse-hint', [test['hint_text']]) - assert len(hint_log) == 1 - assert test['error_message_contains'] in hint_log[0] + with self.assertLogs() as cm: + hint_parser.parse_hints(TEST_HINTER, test['permissions'], 'test-parse-hint', [test['hint_text']]) + + assert len(cm.output) == 1 + assert test['error_message_contains'] in cm.output[0] assert hint_parser.hints.is_empty - hint_log.clear() def test_alias(self): hint_parser = new_hint_parser() diff --git a/tests/test_policy.py b/tests/test_policy.py index c85f4fa..efc6b04 100644 --- a/tests/test_policy.py +++ b/tests/test_policy.py @@ -24,7 +24,7 @@ def initialize_policy(test_name, policy_class, *args, **kwargs): } policy = policy_class(options, suite_info, *args) fake_britney = MockObject(log=lambda x, y='I': None) - hint_parser = HintParser(fake_britney) + hint_parser = HintParser() policy.initialise(fake_britney) policy.register_hints(hint_parser) hint_parser.parse_hints(TEST_HINTER, HINTS_ALL, 'test-%s' % test_name, hints)