From: Ihar Hrachyshka Date: Wed, 11 Feb 2015 13:00:53 +0000 (+0100) Subject: hacking: also catch 'import oslo.*' imports X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=3b6a2902f0622b0fe86c1ca54f774888a957fb0f;p=openstack-build%2Fneutron-build.git hacking: also catch 'import oslo.*' imports Enhanced N323 hacking check to catch 'import oslo.*' form of obsolete imports. Added missing unit tests. Change-Id: I92ae3818fbfd304dfc5e77596483c7caf6775e69 --- diff --git a/neutron/hacking/checks.py b/neutron/hacking/checks.py index ffce620e2..d19c71430 100644 --- a/neutron/hacking/checks.py +++ b/neutron/hacking/checks.py @@ -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): diff --git a/neutron/tests/unit/hacking/test_checks.py b/neutron/tests/unit/hacking/test_checks.py index 642ba6ab1..4d14416c6 100644 --- a/neutron/tests/unit/hacking/test_checks.py +++ b/neutron/tests/unit/hacking/test_checks.py @@ -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')