]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Fixes nits in check_no_contextlib_nested
authorChangBo Guo(gcb) <eric.guo@easystack.cn>
Wed, 18 Mar 2015 12:18:52 +0000 (20:18 +0800)
committerChangBo Guo(gcb) <eric.guo@easystack.cn>
Thu, 19 Mar 2015 11:32:08 +0000 (19:32 +0800)
Currently, check will not work if nested is imported like below:

from contextlib import nested

with nested(......

This commit adds the case and add unit test for it.

Change-Id: I60aa280054d9950b1d2edc9e23505c3395f96fb0

cinder/hacking/checks.py
cinder/tests/test_hacking.py

index 972748658e6f53d2ace9a8e02a980bed52a6259d..762d7a09921c5ed8385aa5cafb2a481b420d522a 100644 (file)
@@ -46,6 +46,7 @@ no_audit_log = re.compile(r"(.)*LOG\.audit(.)*")
 # imports, we will need to add them to the regex below.
 oslo_namespace_imports = re.compile(r"from[\s]*oslo[.](concurrency|db"
                                     "|config|utils|serialization|log)")
+no_contextlib_nested = re.compile(r"\s*with (contextlib\.)?nested\(")
 
 
 def no_vi_headers(physical_line, line_number, lines):
@@ -143,7 +144,7 @@ def check_no_contextlib_nested(logical_line):
            "the with-statement supports multiple nested objects. See https://"
            "docs.python.org/2/library/contextlib.html#contextlib.nested "
            "for more information.")
-    if "with contextlib.nested" in logical_line:
+    if no_contextlib_nested.match(logical_line):
         yield(0, msg)
 
 
index 365152b319d2e51dfb832d25ed3b0e427b30f7cd..32e57e2a5686b304207fb1563e63884df4471ef0 100644 (file)
@@ -167,5 +167,9 @@ class HackingTestCase(test.TestCase):
     def test_no_contextlib_nested(self):
         self.assertEqual(1, len(list(checks.check_no_contextlib_nested(
             "with contextlib.nested("))))
+        self.assertEqual(1, len(list(checks.check_no_contextlib_nested(
+            "  with nested("))))
+        self.assertEqual(0, len(list(checks.check_no_contextlib_nested(
+            "with my.nested("))))
         self.assertEqual(0, len(list(checks.check_no_contextlib_nested(
             "with foo as bar"))))