summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNgo Quoc Cuong <cuongnq@vn.fujitsu.com>2017-07-03 02:46:01 -0400
committerNgo Quoc Cuong <cuongnq@vn.fujitsu.com>2017-07-03 02:47:10 -0400
commitc5aed4fe30a3e3d9e82ca57015668cedc1394ed1 (patch)
tree3d8593340ba570b201eee545473058c414c419a9
parent5b01c09e86cefddcee90ed31735e17e8261a81e5 (diff)
downloadoslo-vmware-c5aed4fe30a3e3d9e82ca57015668cedc1394ed1.tar.gz
Add hacking rule to prevent log translations2.22.1
Since we have removed log translations completely, we should also add hacking rule to prevent future mistakes Change-Id: I8df524e23ab072d198cbc256e8018671e5f32764
-rw-r--r--oslo_vmware/hacking/__init__.py0
-rw-r--r--oslo_vmware/hacking/checks.py52
-rw-r--r--oslo_vmware/tests/test_hacking.py29
-rw-r--r--tox.ini4
4 files changed, 83 insertions, 2 deletions
diff --git a/oslo_vmware/hacking/__init__.py b/oslo_vmware/hacking/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/oslo_vmware/hacking/__init__.py
diff --git a/oslo_vmware/hacking/checks.py b/oslo_vmware/hacking/checks.py
new file mode 100644
index 0000000..b6d3ad9
--- /dev/null
+++ b/oslo_vmware/hacking/checks.py
@@ -0,0 +1,52 @@
+# Copyright (c) 2017 OpenStack Foundation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import re
+
+
+_all_log_levels = {'critical', 'error', 'exception', 'info',
+ 'warning', 'debug'}
+
+# Since _Lx() have been removed, we just need to check _()
+_all_hints = {'_'}
+
+_log_translation_hint = re.compile(
+ r".*LOG\.(%(levels)s)\(\s*(%(hints)s)\(" % {
+ 'levels': '|'.join(_all_log_levels),
+ 'hints': '|'.join(_all_hints),
+ })
+
+
+def no_translate_logs(logical_line, filename):
+ """N537 - Don't translate logs.
+
+ Check for 'LOG.*(_('
+
+ Translators don't provide translations for log messages, and operators
+ asked not to translate them.
+
+ * This check assumes that 'LOG' is a logger.
+
+ :param logical_line: The logical line to check.
+ :param filename: The file name where the logical line exists.
+ :returns: None if the logical line passes the check, otherwise a tuple
+ is yielded that contains the offending index in logical line and a
+ message describe the check validation failure.
+ """
+ if _log_translation_hint.match(logical_line):
+ yield (0, "N537: Log messages should not be translated!")
+
+
+def factory(register):
+ register(no_translate_logs)
diff --git a/oslo_vmware/tests/test_hacking.py b/oslo_vmware/tests/test_hacking.py
new file mode 100644
index 0000000..9f5599d
--- /dev/null
+++ b/oslo_vmware/tests/test_hacking.py
@@ -0,0 +1,29 @@
+# Copyright (c) 2017 OpenStack Foundation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import itertools
+
+from oslo_vmware.hacking import checks
+from oslo_vmware.tests import base
+
+
+class HackingTestCase(base.TestCase):
+ def test_no_log_translations(self):
+ for log, hint in itertools.product(checks._all_log_levels,
+ checks._all_hints):
+ bad = 'LOG.%s(%s("Bad"))' % (log, hint)
+ self.assertEqual(1, len(list(checks.no_translate_logs(bad, 'f'))))
+ # Catch abuses when used with a variable and not a literal
+ bad = 'LOG.%s(%s(msg))' % (log, hint)
+ self.assertEqual(1, len(list(checks.no_translate_logs(bad, 'f'))))
diff --git a/tox.ini b/tox.ini
index 62212bf..ecb3790 100644
--- a/tox.ini
+++ b/tox.ini
@@ -42,9 +42,9 @@ ignore = H405
exclude=.venv,.git,.tox,dist,doc,*lib/python*,*egg,build,__init__.py
[hacking]
-import_exceptions = oslo_vmware._i18n
- oslo_vmware.tests.base
+import_exceptions = oslo_vmware.tests.base
tests.base
+local-check-factory = oslo_vmware.hacking.checks.factory
[testenv:pip-missing-reqs]
# do not install test-requirements as that will pollute the virtualenv for