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/

Closes-bug: 1541780
Change-Id: Ibdef35e1896882a8dfe3165dba989255639e61ba
This commit is contained in:
Marc Koderer 2016-02-04 13:43:33 +01:00
parent 9299e7aebe
commit 9c6ef735d3
2 changed files with 10 additions and 2 deletions

View File

@ -39,7 +39,8 @@ translated_log = re.compile(
"\(\s*_\(\s*('|\")")
string_translation = re.compile(r"(.)*_\(\s*('|\")")
vi_header_re = re.compile(r"^#\s+vim?:.+")
underscore_import_check = re.compile(r"(.)*i18n\s+import\s+_(.)*")
underscore_import_check = re.compile(r"(.)*i18n\s+import(.)* _$")
underscore_import_check_multi = re.compile(r"(.)*i18n\s+import(.)* _, (.)*")
# We need this for cases where they have created their own _ function.
custom_underscore_check = re.compile(r"(.)*_\s*=\s*(.)*")
no_audit_log = re.compile(r"(.)*LOG\.audit(.)*")
@ -165,6 +166,7 @@ def check_explicit_underscore_import(logical_line, filename):
if file in filename:
return
if (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

@ -105,7 +105,7 @@ class HackingTestCase(test.TestCase):
"msg = _('My message')",
"cinder.tests.unit/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.unit/other_files2.py"))))
self.assertEqual(0, len(list(checks.check_explicit_underscore_import(
"msg = _('My message')",
@ -120,6 +120,12 @@ class HackingTestCase(test.TestCase):
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.