Fix issue in hacking with underscore imports

Hacking rule for underscore imports _() are not working correctly.
It also matches _.* imports like "import _LE"

See review: https://review.openstack.org/#/c/270754/

Change-Id: Ibdef35e1896882a8dfe3165dba989255639e61ba
This commit is contained in:
Marc Koderer 2016-02-04 12:00:33 +01:00
parent 5abf1a230d
commit 068d6ee34b
2 changed files with 14 additions and 2 deletions

View File

@ -49,7 +49,8 @@ translated_log = re.compile(
r"(.)*LOG\.(audit|error|info|warn|warning|critical|exception)"
"\(\s*_\(\s*('|\")")
string_translation = re.compile(r"[^_]*_\(\s*('|\")")
underscore_import_check = re.compile(r"(.)*import _(.)*")
underscore_import_check = re.compile(r"(.)*import _$")
underscore_import_check_multi = re.compile(r"(.)*import (.)*_, (.)*")
# We need this for cases where they have created their own _ function.
custom_underscore_check = re.compile(r"(.)*_\s*=\s*(.)*")
oslo_namespace_imports = re.compile(r"from[\s]*oslo[.](.*)")
@ -158,6 +159,7 @@ def check_explicit_underscore_import(logical_line, filename):
if filename in UNDERSCORE_IMPORT_FILES:
pass
elif (underscore_import_check.match(logical_line) or
underscore_import_check_multi.match(logical_line) or
custom_underscore_check.match(logical_line)):
UNDERSCORE_IMPORT_FILES.append(filename)
elif (translated_log.match(logical_line) or

View File

@ -84,7 +84,7 @@ class HackingTestCase(test.TestCase):
"msg = _('My message')",
"cinder/tests/other_files.py"))))
self.assertEqual(0, len(list(checks.check_explicit_underscore_import(
"from cinder.i18n import _, _LW",
"from cinder.i18n import _LE, _, _LW",
"cinder/tests/other_files2.py"))))
self.assertEqual(0, len(list(checks.check_explicit_underscore_import(
"msg = _('My message')",
@ -95,6 +95,16 @@ class HackingTestCase(test.TestCase):
self.assertEqual(0, len(list(checks.check_explicit_underscore_import(
"msg = _('My message')",
"cinder/tests/other_files3.py"))))
# Complete code coverage by falling through all checks
self.assertEqual(0, len(list(checks.check_explicit_underscore_import(
"LOG.info('My info message')",
"cinder.tests.unit/other_files4.py"))))
self.assertEqual(0, len(list(checks.check_explicit_underscore_import(
"from cinder.i18n import _LW",
"cinder.tests.unit/other_files5.py"))))
self.assertEqual(1, len(list(checks.check_explicit_underscore_import(
"msg = _('My message')",
"cinder.tests.unit/other_files5.py"))))
# We are patching pep8 so that only the check under test is actually
# installed.