]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Update register_opts hacking check to allow tuples
authorRyan McNair <rdmcnair@us.ibm.com>
Tue, 27 Oct 2015 22:46:46 +0000 (22:46 +0000)
committerRyan McNair <rdmcnair@us.ibm.com>
Wed, 28 Oct 2015 16:17:51 +0000 (16:17 +0000)
Update the hacking check for register_opts to allow tuples to be
passed in directly as an argument. Previously tuples were getting
flagged as invalid due to a missing pair of parenthesis in the
hacking check.

Change-Id: Ida2ae47eae4ebc6188696dc25943226acd67b5de
Closes-Bug: 1511010

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

index a50f17f367bf8d272d868632ea7ddc902b658d1d..8324a9706cecf8c4cc9ba58f529663d7307300dc 100644 (file)
@@ -304,6 +304,9 @@ class CheckOptRegistrationArgs(BaseASTChecker):
         else:  # could be Subscript, Call or many more
             return None
 
+    def _is_list_or_tuple(self, obj):
+        return isinstance(obj, ast.List) or isinstance(obj, ast.Tuple)
+
     def visit_Call(self, node):
         """Look for the register_opt/register_opts calls."""
         # extract the obj_name and method_name
@@ -319,7 +322,6 @@ class CheckOptRegistrationArgs(BaseASTChecker):
                 return (super(CheckOptRegistrationArgs,
                               self).generic_visit(node))
 
-            # argument should be a list with register_opts()
             if len(node.args) > 0:
                 argument_name = self._find_name(node.args[0])
                 if argument_name:
@@ -335,12 +337,10 @@ class CheckOptRegistrationArgs(BaseASTChecker):
                     # passing in a variable referencing the options being
                     # registered.
                     if (method_name == self.singular_method and
-                            (isinstance(node.args[0], ast.List)) or
-                            isinstance(node.args[0], ast.Tuple)):
+                            self._is_list_or_tuple(node.args[0])):
                         self.add_error(node.args[0])
-                    elif (method_name == self.plural_method and
-                            (not isinstance(node.args[0], ast.List)) or
-                            isinstance(node.args[0], ast.Tuple)):
+                    elif (method_name == self.plural_method and not
+                            self._is_list_or_tuple(node.args[0])):
                         self.add_error(node.args[0])
 
         return super(CheckOptRegistrationArgs, self).generic_visit(node)
index 5502f949b66e1c312021717acd7770818da26916..6e19716b18f7f16fbc7819b67fe9c725bcec39bd 100644 (file)
@@ -179,6 +179,7 @@ class HackingTestCase(test.TestCase):
         checker = checks.CheckOptRegistrationArgs
         code = """
                CONF.register_opts([opt1, opt2, opt3])
+               CONF.register_opts((opt4, opt5))
                CONF.register_opt(lonely_opt)
                CONF.register_opts([OPT1, OPT2], group="group_of_opts")
                CONF.register_opt(single_opt, group=blah)
@@ -187,14 +188,15 @@ class HackingTestCase(test.TestCase):
 
         code = """
                CONF.register_opt([opt4, opt5, opt6])
+               CONF.register_opt((opt7, opt8))
                CONF.register_opts(lonely_opt)
                CONF.register_opt((an_opt, another_opt))
                """
-        for method in checker.register_methods:
-            self._assert_has_errors(code.format(method), checker,
-                                    expected_errors=[(1, 18, 'C311'),
-                                                     (2, 19, 'C311'),
-                                                     (3, 19, 'C311')])
+        self._assert_has_errors(code, checker,
+                                expected_errors=[(1, 18, 'C311'),
+                                                 (2, 19, 'C311'),
+                                                 (3, 19, 'C311'),
+                                                 (4, 19, 'C311')])
 
         code = """
                CONF.register_opt(single_opt)