Fix of wrong cli opts unregistration

This code allows to unregister a cli option by passing to
the method any Opt object with 'dest' field and not exactly
the one that was used during creation.

Change-Id: Icebbf3ce2bdf0d2deae9b057bd70ab706aad820e
Closes-Bug: 1407950
This commit is contained in:
Mike Fedosin 2015-01-06 15:28:08 +03:00
parent 99e530ed10
commit 035636ddd6
2 changed files with 38 additions and 12 deletions

View File

@ -1883,8 +1883,15 @@ class ConfigOpts(collections.Mapping):
if self._args is not None:
raise ArgsAlreadyParsedError("reset before unregistering options")
if {'opt': opt, 'group': group} in self._cli_opts:
self._cli_opts.remove({'opt': opt, 'group': group})
remitem = None
for item in self._cli_opts:
if (item['opt'].dest == opt.dest and
(group is None or
self._get_group(group).name == item['group'].name)):
remitem = item
break
if remitem is not None:
self._cli_opts.remove(remitem)
if group is not None:
self._get_group(group)._unregister_opt(opt)

View File

@ -653,20 +653,37 @@ class PositionalTestCase(BaseTestCase):
command = cfg.StrOpt('command', positional=True)
arg1 = cfg.StrOpt('arg1', positional=True)
arg2 = cfg.StrOpt('arg2', positional=True)
cfg.CONF.register_group(cfg.OptGroup('blaa'))
self.conf.register_cli_opt(command)
self.conf.register_cli_opt(arg1)
self.conf.register_cli_opt(arg2)
self.conf.register_cli_opt(arg2, group='blaa')
self.assertEqual(3, len(self.conf._cli_opts))
self.assertEqual(1, len(self.conf._groups))
self.assertEqual('command', self.conf._cli_opts[0]['opt'].dest)
self.assertEqual('arg1', self.conf._cli_opts[1]['opt'].dest)
self.assertEqual('arg2', self.conf._cli_opts[2]['opt'].dest)
self.assertEqual('blaa', self.conf._cli_opts[2]['group'].name)
self.conf(['command', 'arg1', 'arg2'])
self.assertEqual('command', self.conf.command)
self.assertEqual('arg1', self.conf.arg1)
self.assertEqual('arg2', self.conf.arg2)
self.conf.reset()
self.conf.unregister_opt(arg1)
self.conf.unregister_opt(arg2)
new_arg1 = cfg.StrOpt('arg1', positional=True)
new_arg2 = cfg.StrOpt('arg2', positional=True)
self.conf.unregister_opt(new_arg1)
self.assertEqual(2, len(self.conf._cli_opts))
self.assertRaises(cfg.NoSuchGroupError,
self.conf.unregister_opt,
new_arg2,
group='foo')
self.conf.unregister_opt(new_arg2, group='blaa')
self.assertEqual(1, len(self.conf._cli_opts))
self.assertEqual('command',
self.conf._cli_opts[0]['opt'].dest)
arg0 = cfg.StrOpt('arg0', positional=True)
self.conf.register_cli_opt(arg0)
@ -674,9 +691,11 @@ class PositionalTestCase(BaseTestCase):
self.conf(['command', 'arg0', 'arg1'])
self.assertEqual('command', self.conf.command)
self.assertEqual('arg0', self.conf.arg0)
self.assertEqual('arg1', self.conf.arg1)
self.conf.reset()
self.assertEqual('command', self.conf._cli_opts[0]['opt'].dest)
self.assertEqual('arg0', self.conf._cli_opts[1]['opt'].dest)
self.assertEqual('arg1', self.conf._cli_opts[2]['opt'].dest)
class ConfigFileOptsTestCase(BaseTestCase):