From b9019422311f24b02ebf3e3dc0d02ba6a4d551ed Mon Sep 17 00:00:00 2001 From: Masaki Matsushita Date: Tue, 29 Sep 2015 16:32:50 +0900 Subject: [PATCH] Add item_type and bounds to ListOpt This change adds keyword parameter "item_type" and "bounds" to ListOpt. It can be helpful for users to validate items in lists. DocImpact Closes-Bug: #1500737 Change-Id: Ib224ac4b654fa9997d4ec044db2f1fde5a48e5f5 --- oslo_config/cfg.py | 7 +++++-- oslo_config/tests/test_cfg.py | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/oslo_config/cfg.py b/oslo_config/cfg.py index 26a8a14e..3ff85eeb 100644 --- a/oslo_config/cfg.py +++ b/oslo_config/cfg.py @@ -1076,8 +1076,11 @@ class ListOpt(Opt): `Kept for backward-compatibility with options not using Opt directly`. """ - def __init__(self, name, **kwargs): - super(ListOpt, self).__init__(name, type=types.List(), **kwargs) + def __init__(self, name, item_type=None, bounds=None, **kwargs): + super(ListOpt, self).__init__(name, + type=types.List(item_type=item_type, + bounds=bounds), + **kwargs) class DictOpt(Opt): diff --git a/oslo_config/tests/test_cfg.py b/oslo_config/tests/test_cfg.py index ce27a1bc..2f48666a 100644 --- a/oslo_config/tests/test_cfg.py +++ b/oslo_config/tests/test_cfg.py @@ -1127,6 +1127,29 @@ class ConfigFileOptsTestCase(BaseTestCase): self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(self.conf.foo, ['b', 'a', 'r']) + def test_conf_file_list_item_type(self): + self.conf.register_cli_opt(cfg.ListOpt('foo', + item_type=types.Integer())) + + paths = self.create_tempfiles([('1', + '[DEFAULT]\n' + 'foo = 1,2\n')]) + + self.conf(['--config-file', paths[0]]) + + self.assertTrue(hasattr(self.conf, 'foo')) + self.assertEqual([1, 2], self.conf.foo) + + @mock.patch.object(cfg, 'LOG') + def test_conf_file_list_item_wrong_type(self, mock_log): + cfg.ListOpt('foo', default="bar", item_type=types.Integer()) + self.assertEqual(1, mock_log.debug.call_count) + + @mock.patch.object(cfg, 'LOG') + def test_conf_file_list_bounds(self, mock_log): + cfg.ListOpt('foo', default="1,2", bounds=True) + self.assertEqual(1, mock_log.debug.call_count) + def test_conf_file_list_use_dname(self): self._do_dname_test_use(cfg.ListOpt, 'a,b,c', ['a', 'b', 'c'])