]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
hacking: also catch 'import oslo.*' imports
authorIhar Hrachyshka <ihrachys@redhat.com>
Wed, 11 Feb 2015 13:00:53 +0000 (14:00 +0100)
committerIhar Hrachyshka <ihrachys@redhat.com>
Wed, 11 Feb 2015 13:06:42 +0000 (14:06 +0100)
Enhanced N323 hacking check to catch 'import oslo.*' form of obsolete
imports.

Added missing unit tests.

Change-Id: I92ae3818fbfd304dfc5e77596483c7caf6775e69

neutron/hacking/checks.py
neutron/tests/unit/hacking/test_checks.py

index ffce620e2f98458afbfe4a14fe41d6d298ed561e..d19c71430bbcfdd2759073439e6927e49526bc4d 100644 (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):
index 642ba6ab17eaba5abf517c7679dff11700dd1e0f..4d14416c692241203081f6e06996ba8502f43a34 100644 (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')