diff options
| author | Mark McLoughlin <markmc@redhat.com> | 2012-05-29 08:27:05 +0100 |
|---|---|---|
| committer | Mark McLoughlin <markmc@redhat.com> | 2012-05-29 08:27:05 +0100 |
| commit | e42276a50a3a1a2b4eb0af871154bf802cda1f00 (patch) | |
| tree | 3005932bb499cc64550d9997d990942ad7b15ada /tests | |
| parent | f96acd8a9f0cef80b6e0eb2f9cf980702bba4043 (diff) | |
cfg: move constructor args to __call__() args
In order to effectively use a global ConfigOpts object, you
need to be able to initialize the global object with none of
the information we currently require at construction.
By moving those constructor args to the __call__() method,
we enable the global object usage model but also make the API
generally more flexible. For example, you can now reset the
object and re-use it for parsing a different set of config
files with the same options.
There are a couple of other minor behavior changes as a
result:
- print_usage() and print_help() no longer works before
the object has been called to parse options
- registration of duplicate short options are no longer
detected until the options are parsed
- the --config-file and --config-dir options aren't
registered until just before parsing the options since
the default set of config files can be specified at
that time
- find_file() can't be used until after the options have
been parsed, again because of the late registration
of --config-file and --config-dir
Finally, an unregister_opt() method is added to support
the re-registeration of the --config-file and --config-dir
options.
Change-Id: I650d8e299e92cbc5d10da47a7ce1b73ca8066bd0
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/unit/test_cfg.py | 66 |
1 files changed, 58 insertions, 8 deletions
diff --git a/tests/unit/test_cfg.py b/tests/unit/test_cfg.py index 896cc1e..b1eb41e 100644 --- a/tests/unit/test_cfg.py +++ b/tests/unit/test_cfg.py @@ -75,11 +75,18 @@ class ExceptionsTestCase(unittest.TestCase): class BaseTestCase(unittest.TestCase): + class TestConfigOpts(ConfigOpts): + def __call__(self, args=None): + return ConfigOpts.__call__(self, + args=args, + prog='test', + version='1.0', + usage='%prog FOO BAR', + default_config_files=[]) + def setUp(self): - self.conf = ConfigOpts(prog='test', - version='1.0', - usage='%prog FOO BAR', - default_config_files=[]) + self.conf = self.TestConfigOpts() + self.tempfiles = [] self.tempdirs = [] self.stubs = stubout.StubOutForTesting() @@ -113,6 +120,7 @@ class UsageTestCase(BaseTestCase): def test_print_usage(self): f = StringIO.StringIO() + self.conf([]) self.conf.print_usage(file=f) self.assertTrue('Usage: test FOO BAR' in f.getvalue()) @@ -121,6 +129,7 @@ class HelpTestCase(BaseTestCase): def test_print_help(self): f = StringIO.StringIO() + self.conf([]) self.conf.print_help(file=f) self.assertTrue('Usage: test FOO BAR' in f.getvalue()) self.assertTrue('Options:' in f.getvalue()) @@ -923,6 +932,45 @@ class ResetAndClearTestCase(BaseTestCase): self.assertEquals(self.conf.blaa.bar, None) +class UnregisterOptTestCase(BaseTestCase): + + def test_unregister_opt(self): + opts = [StrOpt('foo'), StrOpt('bar')] + + self.conf.register_opts(opts) + + self.assertTrue(hasattr(self.conf, 'foo')) + self.assertTrue(hasattr(self.conf, 'bar')) + + self.conf.unregister_opt(opts[0]) + + self.assertFalse(hasattr(self.conf, 'foo')) + self.assertTrue(hasattr(self.conf, 'bar')) + + self.conf([]) + + self.assertRaises(ArgsAlreadyParsedError, + self.conf.unregister_opt, opts[1]) + + self.conf.clear() + + self.assertTrue(hasattr(self.conf, 'bar')) + + self.conf.unregister_opts(opts) + + def test_unregister_opt_from_group(self): + opt = StrOpt('foo') + + self.conf.register_opt(opt, group='blaa') + + self.assertTrue(hasattr(self.conf, 'blaa')) + self.assertTrue(hasattr(self.conf.blaa, 'foo')) + + self.conf.unregister_opt(opt, group='blaa') + + self.assertFalse(hasattr(self.conf.blaa, 'foo')) + + class RequiredOptsTestCase(BaseTestCase): def setUp(self): @@ -1046,13 +1094,13 @@ class SadPathTestCase(BaseTestCase): def test_error_duplicate_with_different_dest(self): self.conf.register_cli_opt(StrOpt('foo', dest='f')) - self.assertRaises(DuplicateOptError, - self.conf.register_cli_opt, StrOpt('foo')) + self.conf.register_cli_opt(StrOpt('foo')) + self.assertRaises(DuplicateOptError, self.conf, []) def test_error_duplicate_short(self): self.conf.register_cli_opt(StrOpt('foo', short='f')) - self.assertRaises(DuplicateOptError, - self.conf.register_cli_opt, StrOpt('bar', short='f')) + self.conf.register_cli_opt(StrOpt('bar', short='f')) + self.assertRaises(DuplicateOptError, self.conf, []) def test_no_such_group(self): group = OptGroup('blaa') @@ -1159,6 +1207,8 @@ class FindFileTestCase(BaseTestCase): self.stubs.Set(os.path, 'exists', lambda p: p == policy_file) + self.conf([]) + self.assertEquals(self.conf.find_file('foo.json'), None) self.assertEquals(self.conf.find_file('policy.json'), policy_file) |
