summaryrefslogtreecommitdiff
path: root/tests/test_cfg.py
diff options
context:
space:
mode:
authorMark McLoughlin <markmc@redhat.com>2013-09-25 20:07:03 +0100
committerMark McLoughlin <markmc@redhat.com>2013-09-25 20:17:49 +0100
commitbf705193595d34a70abefc4b3867ea550c0c15db (patch)
treeb4837beb83181d80f52a1ff9a1416e247149c367 /tests/test_cfg.py
parent9dabbd0ff744ae9fde993861aaeae576c2e19597 (diff)
downloadoslo-config-bf705193595d34a70abefc4b3867ea550c0c15db.tar.gz
Fix subparsers add_parser() regression
With SubCommandOpt, the handler() callback gets invoked with a subparsers object which has a add_parser() method. The arguments to this method match the arguments to the ArgumentParser constructor and the arguments are passed to the class for our ArgumentParser instance. However, we added a ArgumentParser subclass (_CachedArgumentParser) so that we could sort the output of --help ... but the subclass doesn't support all the arguments of the parent class. glance-control is the only known example of a SubCommandOpt user which passes unusual arguments to subparsers.add_parser(). It uses the parent arg and blows up at startup with: File "./bin/glance-control", line 276, in add_command_parsers parser = subparsers.add_parser(server, parents=[cmd_parser]) File "/usr/lib64/python2.7/argparse.py", line 1064, in add_parser parser = self._parser_class(**kwargs) TypeError: __init__() got an unexpected keyword argument 'parents' It appears we broke glance-control back in June (commit 2951391), but nobody noticed it until 1.2.0 was released and broke stable/grizzly because we stopped using glance-control in the Havana glance unit tests before this regression. Closes-Bug: #1230416 Change-Id: I8d1b52e5390295726eb49af32eb7cab14c29592c
Diffstat (limited to 'tests/test_cfg.py')
-rw-r--r--tests/test_cfg.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/tests/test_cfg.py b/tests/test_cfg.py
index 3f2fafc..b5a8c60 100644
--- a/tests/test_cfg.py
+++ b/tests/test_cfg.py
@@ -14,6 +14,7 @@
# License for the specific language governing permissions and limitations
# under the License.
+import argparse
import os
import shutil
import sys
@@ -2627,6 +2628,21 @@ class SubCommandTestCase(BaseTestCase):
self.assertEqual(self.conf.cmd.name, 'a')
self.assertEqual(self.conf.cmd.bar, 10)
+ def test_sub_command_with_parent(self):
+ def add_parsers(subparsers):
+ parent = argparse.ArgumentParser(add_help=False)
+ parent.add_argument('bar', type=int)
+ subparsers.add_parser('a', parents=[parent])
+
+ self.conf.register_cli_opt(
+ cfg.SubCommandOpt('cmd', handler=add_parsers))
+ self.assertTrue(hasattr(self.conf, 'cmd'))
+ self.conf(['a', '10'])
+ self.assertTrue(hasattr(self.conf.cmd, 'name'))
+ self.assertTrue(hasattr(self.conf.cmd, 'bar'))
+ self.assertEqual(self.conf.cmd.name, 'a')
+ self.assertEqual(self.conf.cmd.bar, 10)
+
def test_sub_command_with_dest(self):
def add_parsers(subparsers):
subparsers.add_parser('a')