From f39c28e44c1890cc9ac1a6af46d6e64c171f4c32 Mon Sep 17 00:00:00 2001 From: wangxiyuan Date: Fri, 21 Sep 2018 09:46:05 +0800 Subject: [PATCH] Update log translation hacking check Make sure the message in LOG must not be translated. Closes-bug: #1793421 Change-Id: I86457db5dd0e9296d4130c3801c3257600571561 --- keystone/tests/hacking/checks.py | 44 +---------------------- keystone/tests/unit/ksfixtures/hacking.py | 17 ++++++--- 2 files changed, 13 insertions(+), 48 deletions(-) diff --git a/keystone/tests/hacking/checks.py b/keystone/tests/hacking/checks.py index ea9a16af4a..3590aa264e 100644 --- a/keystone/tests/hacking/checks.py +++ b/keystone/tests/hacking/checks.py @@ -295,51 +295,9 @@ class CheckForTranslationIssues(BaseASTChecker): # if the first arg is a reference to a i18n call elif (isinstance(msg, ast.Name) - and msg.id in self.assignments - and not self._is_raised_later(node, msg.id)): + and msg.id in self.assignments): self.add_error(msg, message=self.LOGGING_CHECK_DESC) - def _is_raised_later(self, node, name): - - def find_peers(node): - node_for_line = node._parent - for _field, value in ast.iter_fields(node._parent._parent): - if isinstance(value, list) and node_for_line in value: - return value[value.index(node_for_line) + 1:] - continue - return [] - - def is_in_args(node, name): - if (len(node.args) > 0 and isinstance(node.args[0], ast.Name) - and name in (a.id for a in node.args)): - return True - return False - - def is_in_kwargs(node, name): - for keyword in node.keywords: - if (isinstance(keyword.value, ast.Name) - and keyword.value.id == name): - return True - return False - - peers = find_peers(node) - for peer in peers: - if isinstance(peer, ast.Raise): - if six.PY3: - exc = peer.exc - else: - exc = peer.type - if isinstance(exc, ast.Call): - if is_in_args(exc, name): - return True - elif is_in_kwargs(exc, name): - return True - - return False - elif isinstance(peer, ast.Assign): - if name in (t.id for t in peer.targets if hasattr(t, 'id')): - return False - def dict_constructor_with_sequence_copy(logical_line): """Should use a dict comprehension instead of a dict constructor. diff --git a/keystone/tests/unit/ksfixtures/hacking.py b/keystone/tests/unit/ksfixtures/hacking.py index 79d2e4744c..379a14ef24 100644 --- a/keystone/tests/unit/ksfixtures/hacking.py +++ b/keystone/tests/unit/ksfixtures/hacking.py @@ -220,24 +220,28 @@ class HackingTranslations(fixtures.Fixture): }, { 'code': """ - # this should not be an error + # this should be an error even if it'll be raised later. L = log.getLogger(__name__) msg = _('text') L.warning(msg) raise Exception(msg) """, - 'expected_errors': [], + 'expected_errors': [ + (4, 10, 'K005'), + ], }, { 'code': """ L = log.getLogger(__name__) def f(): msg = _('text') - L2.warning(msg) + L.warning(msg) something = True # add an extra statement here raise Exception(msg) """, - 'expected_errors': [], + 'expected_errors': [ + (4, 14, 'K005'), + ], }, { 'code': """ @@ -262,6 +266,7 @@ class HackingTranslations(fixtures.Fixture): raise Exception(msg) """, 'expected_errors': [ + (6, 12, 'K005'), ], }, { @@ -320,6 +325,8 @@ class HackingTranslations(fixtures.Fixture): LOG.warning(msg) raise exception.Unauthorized(message=msg) """, - 'expected_errors': [], + 'expected_errors': [ + (7, 16, 'K005'), + ], }, ]