Merge "hacking: also catch 'import oslo.*' imports"

This commit is contained in:
Jenkins 2015-02-13 00:13:04 +00:00 committed by Gerrit Code Review
commit 1714ee07a7
2 changed files with 28 additions and 6 deletions

View File

@ -52,9 +52,9 @@ log_translation_hint = re.compile(
'|'.join('(?:%s)' % _regex_for_level(level, hint)
for level, hint in _all_log_levels.iteritems()))
oslo_namespace_imports_dot = re.compile(r"from[\s]*oslo[.]")
oslo_namespace_imports_root = re.compile(r"from[\s]*oslo[\s]*import[\s]*")
oslo_namespace_imports_dot = re.compile(r"import[\s]+oslo[.][^\s]+")
oslo_namespace_imports_from_dot = re.compile(r"from[\s]+oslo[.]")
oslo_namespace_imports_from_root = re.compile(r"from[\s]+oslo[\s]+import[\s]+")
def validate_log_translations(logical_line, physical_line, filename):
@ -118,17 +118,22 @@ def check_assert_called_once_with(logical_line, filename):
yield (0, msg)
def check_oslo_namespace_imports(logical_line, blank_before, filename):
if re.match(oslo_namespace_imports_dot, logical_line):
def check_oslo_namespace_imports(logical_line):
if re.match(oslo_namespace_imports_from_dot, logical_line):
msg = ("N323: '%s' must be used instead of '%s'.") % (
logical_line.replace('oslo.', 'oslo_'),
logical_line)
yield(0, msg)
elif re.match(oslo_namespace_imports_root, logical_line):
elif re.match(oslo_namespace_imports_from_root, logical_line):
msg = ("N323: '%s' must be used instead of '%s'.") % (
logical_line.replace('from oslo import ', 'import oslo_'),
logical_line)
yield(0, msg)
elif re.match(oslo_namespace_imports_dot, logical_line):
msg = ("N323: '%s' must be used instead of '%s'.") % (
logical_line.replace('import', 'from').replace('.', ' import '),
logical_line)
yield(0, msg)
def factory(register):

View File

@ -10,6 +10,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import testtools
from neutron.hacking import checks
from neutron.tests import base
@ -105,3 +107,18 @@ class HackingTestCase(base.BaseTestCase):
self.assertEqual(
0, len(list(checks.check_assert_called_once_with(pass_code,
"neutron/tests/test_assert.py"))))
def test_check_oslo_namespace_imports(self):
def check(s, fail=True):
func = checks.check_oslo_namespace_imports
if fail:
self.assertIsInstance(next(func(s)), tuple)
else:
with testtools.ExpectedException(StopIteration):
next(func(s))
check('from oslo_utils import importutils', fail=False)
check('import oslo_messaging', fail=False)
check('from oslo.utils import importutils')
check('from oslo import messaging')
check('import oslo.messaging')