summaryrefslogtreecommitdiffstats
path: root/tests/unit/test_cfg.py
diff options
context:
space:
mode:
authorMark McLoughlin <markmc@redhat.com>2012-11-12 16:25:59 -0500
committerMark McLoughlin <markmc@redhat.com>2012-11-15 15:01:14 -0500
commit5b9cb4148c2c16c910cb7eae9e7875a9fc06d3da (patch)
tree5ec357d1099d30148605d40be449eedd91479be6 /tests/unit/test_cfg.py
parentdbc72a6ef9a784c1bf5820a9750061d76529d571 (diff)
downloadoslo-5b9cb4148c2c16c910cb7eae9e7875a9fc06d3da.tar.gz
oslo-5b9cb4148c2c16c910cb7eae9e7875a9fc06d3da.tar.xz
oslo-5b9cb4148c2c16c910cb7eae9e7875a9fc06d3da.zip
Add support for positional arguments
argparse makes it awkward to implement the current cfg API where we simply return the leftover arguments to the caller. Very few cfg users actually rely on this functionality and for those cases it probably makes more sense for them to explicitly register positional arguments or sub-parsers. Add support for positional arguments via a 'required' Opt attribute: opt = StrOpt('foo', positional=True) conf.register_cli_opt(opt) conf(['bar']) conf.foo == 'bar' Change-Id: Iea746d710237e1ea26c1ef4871643941d1df09bd
Diffstat (limited to 'tests/unit/test_cfg.py')
-rw-r--r--tests/unit/test_cfg.py77
1 files changed, 65 insertions, 12 deletions
diff --git a/tests/unit/test_cfg.py b/tests/unit/test_cfg.py
index 4289c7d..384a57a 100644
--- a/tests/unit/test_cfg.py
+++ b/tests/unit/test_cfg.py
@@ -137,17 +137,6 @@ class HelpTestCase(BaseTestCase):
self.assertTrue('-h, --help' in f.getvalue())
-class LeftoversTestCase(BaseTestCase):
-
- def test_leftovers(self):
- self.conf.register_cli_opts([StrOpt('foo'), StrOpt('bar')])
-
- leftovers = self.conf(['those', '--foo', 'this',
- 'thems', '--bar', 'that', 'these'])
-
- self.assertEquals(leftovers, ['those', 'thems', 'these'])
-
-
class FindConfigFilesTestCase(BaseTestCase):
def test_find_config_files(self):
@@ -268,6 +257,70 @@ class CliOptsTestCase(BaseTestCase):
self.assertEquals(self.conf.config_file, paths)
+class PositionalTestCase(BaseTestCase):
+
+ def _do_pos_test(self, opt_class, default, cli_args, value):
+ self.conf.register_cli_opt(opt_class('foo',
+ default=default,
+ positional=True))
+
+ self.conf(cli_args)
+
+ self.assertTrue(hasattr(self.conf, 'foo'))
+ self.assertEquals(self.conf.foo, value)
+
+ def test_positional_str_default(self):
+ self._do_pos_test(StrOpt, None, [], None)
+
+ def test_positional_str_arg(self):
+ self._do_pos_test(StrOpt, None, ['bar'], 'bar')
+
+ def test_positional_int_default(self):
+ self._do_pos_test(IntOpt, 10, [], 10)
+
+ def test_positional_int_arg(self):
+ self._do_pos_test(IntOpt, None, ['20'], 20)
+
+ def test_positional_float_default(self):
+ self._do_pos_test(FloatOpt, 1.0, [], 1.0)
+
+ def test_positional_float_arg(self):
+ self._do_pos_test(FloatOpt, None, ['2.0'], 2.0)
+
+ def test_positional_list_default(self):
+ self._do_pos_test(ListOpt, ['bar'], [], ['bar'])
+
+ def test_positional_list_arg(self):
+ self._do_pos_test(ListOpt, None,
+ ['blaa,bar'], ['blaa', 'bar'])
+
+ def test_positional_multistr_default(self):
+ self._do_pos_test(MultiStrOpt, ['bar'], [], ['bar'])
+
+ def test_positional_multistr_arg(self):
+ self._do_pos_test(MultiStrOpt, None,
+ ['blaa', 'bar'], ['blaa', 'bar'])
+
+ def test_positional_bool(self):
+ self.assertRaises(ValueError, BoolOpt, 'foo', positional=True)
+
+ def test_required_positional_opt(self):
+ self.conf.register_cli_opt(StrOpt('foo',
+ required=True,
+ positional=True))
+
+ self.conf(['bar'])
+
+ self.assertTrue(hasattr(self.conf, 'foo'))
+ self.assertEquals(self.conf.foo, 'bar')
+
+ def test_missing_required_cli_opt(self):
+ self.conf.register_cli_opt(StrOpt('foo',
+ required=True,
+ positional=True))
+ self.assertRaises(RequiredOptError, self.conf, [])
+
+
class ConfigFileOptsTestCase(BaseTestCase):
def _do_deprecated_test_use(self, opt_class, value, result):
@@ -387,7 +440,7 @@ class ConfigFileOptsTestCase(BaseTestCase):
'[DEFAULT]\n'
'foo = yes\n')])
- self.conf(['--foo', 'bar',
+ self.conf(['--foo',
'--config-file', paths[0],
'--config-file', paths[1]])