]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
hacking: Check if correct log markers are used
authorYAMAMOTO Takashi <yamamoto@valinux.co.jp>
Tue, 25 Nov 2014 04:53:02 +0000 (13:53 +0900)
committerGary Kotton <gkotton@vmware.com>
Sun, 30 Nov 2014 08:24:50 +0000 (00:24 -0800)
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

neutron/hacking/checks.py
neutron/tests/unit/test_hacking.py

index 8474ee533bb4a98d9d5fcca69f7056eafe6da0c4..9a7599dbce33c19e5cf8c0c988b47408ab2f8bca 100644 (file)
@@ -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):
index 0a8fa0de625eb201bc36b679c96d05e2d71daaa0..d82f5febe999f21ba0f8905f06802fce668b6309 100644 (file)
@@ -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):