hacking: Check if correct log markers are used
Makes the check tighter and would detect mistakes like LOG.info(_LE("foo")). This would reduce reviewer loads for relevant changes. Partial-Bug: #1320867 Change-Id: I66c7ab1fd9b40beb857dc6c4b143ca47a5ebce4b
This commit is contained in:
parent
66469f4443
commit
9bba05d77f
@ -35,9 +35,24 @@ log_translation = re.compile(
|
||||
r"(.)*LOG\.(audit|error|info|warn|warning|critical|exception)\(\s*('|\")")
|
||||
author_tag_re = (re.compile("^\s*#\s*@?(a|A)uthor"),
|
||||
re.compile("^\.\.\s+moduleauthor::"))
|
||||
log_translation_hint = re.compile(
|
||||
r"(.)*LOG\.(audit|error|info|warn|warning|critical|exception)"
|
||||
"\(\s*(_\(|'|\")")
|
||||
_all_hints = set(['_', '_LI', '_LE', '_LW', '_LC'])
|
||||
_all_log_levels = {
|
||||
# NOTE(yamamoto): Following nova which uses _() for audit.
|
||||
'audit': '_',
|
||||
'error': '_LE',
|
||||
'info': '_LI',
|
||||
'warn': '_LW',
|
||||
'warning': '_LW',
|
||||
'critical': '_LC',
|
||||
'exception': '_LE',
|
||||
}
|
||||
log_translation_hints = []
|
||||
for level, hint in _all_log_levels.iteritems():
|
||||
r = "(.)*LOG\.%(level)s\(\s*((%(wrong_hints)s)\(|'|\")" % {
|
||||
'level': level,
|
||||
'wrong_hints': '|'.join(_all_hints - set([hint])),
|
||||
}
|
||||
log_translation_hints.append(re.compile(r))
|
||||
|
||||
|
||||
def _directory_to_check_translation(filename):
|
||||
@ -77,8 +92,9 @@ def validate_log_translations(logical_line, physical_line, filename):
|
||||
|
||||
if _directory_to_check_translation(filename):
|
||||
msg = "N320: Log messages require translation hints!"
|
||||
if log_translation_hint.match(logical_line):
|
||||
yield (0, msg)
|
||||
for log_translation_hint in log_translation_hints:
|
||||
if log_translation_hint.match(logical_line):
|
||||
yield (0, msg)
|
||||
|
||||
|
||||
def use_jsonutils(logical_line, filename):
|
||||
|
@ -17,9 +17,18 @@ from neutron.tests import base
|
||||
class HackingTestCase(base.BaseTestCase):
|
||||
|
||||
def test_log_translations(self):
|
||||
logs = ['audit', 'error', 'info', 'warn', 'warning', 'critical',
|
||||
'exception']
|
||||
expected_marks = {
|
||||
'audit': '_',
|
||||
'error': '_LE',
|
||||
'info': '_LI',
|
||||
'warn': '_LW',
|
||||
'warning': '_LW',
|
||||
'critical': '_LC',
|
||||
'exception': '_LE',
|
||||
}
|
||||
logs = expected_marks.keys()
|
||||
levels = ['_LI', '_LW', '_LE', '_LC']
|
||||
all_marks = levels + ['_']
|
||||
debug = "LOG.debug('OK')"
|
||||
self.assertEqual(
|
||||
0, len(list(checks.validate_log_translations(debug, debug, 'f'))))
|
||||
@ -42,11 +51,13 @@ class HackingTestCase(base.BaseTestCase):
|
||||
0, len(list(checks.validate_log_translations(ok,
|
||||
ok, 'f'))))
|
||||
filename = 'neutron/agent/f'
|
||||
bad = "LOG.%s(_('BAD - by directory'))" % log
|
||||
self.assertEqual(
|
||||
1, len(list(checks.validate_log_translations(bad,
|
||||
bad,
|
||||
filename))))
|
||||
for mark in all_marks:
|
||||
stmt = "LOG.%s(%s('test'))" % (log, mark)
|
||||
self.assertEqual(
|
||||
0 if expected_marks[log] == mark else 1,
|
||||
len(list(checks.validate_log_translations(stmt,
|
||||
stmt,
|
||||
filename))))
|
||||
|
||||
def test_use_jsonutils(self):
|
||||
def __get_msg(fun):
|
||||
|
Loading…
Reference in New Issue
Block a user