diff options
author | Zuul <zuul@review.openstack.org> | 2018-10-12 17:54:08 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2018-10-12 17:54:08 +0000 |
commit | d2ee0bf4069aea13c36599572bb38d0ed10966bb (patch) | |
tree | b60870a80879dce1175c2c2ad95c5dd44708f3c1 | |
parent | 83d2881b432e2ea7f9c3468383a926ee85057d8c (diff) | |
parent | b5f76a2ce8323ae89e85b599c2aec5a89a49eabd (diff) | |
download | oslo-config-6.6.1.tar.gz |
Merge "Unit tests to illustrate positional argument bug"6.6.1
-rw-r--r-- | oslo_config/tests/test_cfg.py | 112 |
1 files changed, 111 insertions, 1 deletions
diff --git a/oslo_config/tests/test_cfg.py b/oslo_config/tests/test_cfg.py index 4f7549e..db68583 100644 --- a/oslo_config/tests/test_cfg.py +++ b/oslo_config/tests/test_cfg.py @@ -893,15 +893,125 @@ class PositionalTestCase(BaseTestCase): def test_positional_bool(self): self.assertRaises(ValueError, cfg.BoolOpt, 'foo', positional=True) - def test_required_positional_opt(self): + def test_required_positional_opt_defined(self): self.conf.register_cli_opt( cfg.StrOpt('foo', required=True, positional=True)) + self.useFixture(fixtures.MonkeyPatch('sys.stdout', moves.StringIO())) + self.assertRaises(SystemExit, self.conf, ['--help']) + self.assertIn(' foo\n', sys.stdout.getvalue()) + + self.conf(['bar']) + + self.assertTrue(hasattr(self.conf, 'foo')) + self.assertEqual('bar', self.conf.foo) + + def test_required_positional_opt_undefined(self): + self.conf.register_cli_opt( + cfg.StrOpt('foo', required=True, positional=True)) + + self.useFixture(fixtures.MonkeyPatch('sys.stdout', moves.StringIO())) + self.assertRaises(SystemExit, self.conf, ['--help']) + self.assertIn(' foo\n', sys.stdout.getvalue()) + + self.assertRaises(cfg.RequiredOptError, self.conf, []) + + def test_optional_positional_opt_defined(self): + self.conf.register_cli_opt( + cfg.StrOpt('foo', required=False, positional=True)) + + self.useFixture(fixtures.MonkeyPatch('sys.stdout', moves.StringIO())) + self.assertRaises(SystemExit, self.conf, ['--help']) + # FIXME(dolphm): Due to bug 1676989, this argument appears as a + # required argument in the CLI help. Instead, the following + # commented-out code should work: + # self.assertIn(' [foo]\n', sys.stdout.getvalue()) + self.assertIn(' foo\n', sys.stdout.getvalue()) + self.conf(['bar']) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) + def test_optional_positional_opt_undefined(self): + self.conf.register_cli_opt( + cfg.StrOpt('foo', required=False, positional=True)) + + self.useFixture(fixtures.MonkeyPatch('sys.stdout', moves.StringIO())) + self.assertRaises(SystemExit, self.conf, ['--help']) + # FIXME(dolphm): Due to bug 1676989, this argument appears as a + # required argument in the CLI help. Instead, the following + # commented-out code should work: + # self.assertIn(' [foo]\n', sys.stdout.getvalue()) + self.assertIn(' foo\n', sys.stdout.getvalue()) + + self.conf([]) + + self.assertTrue(hasattr(self.conf, 'foo')) + self.assertIsNone(self.conf.foo) + + def test_optional_positional_hyphenated_opt_defined(self): + self.conf.register_cli_opt( + cfg.StrOpt('foo-bar', required=False, positional=True)) + + self.useFixture(fixtures.MonkeyPatch('sys.stdout', moves.StringIO())) + self.assertRaises(SystemExit, self.conf, ['--help']) + # FIXME(dolphm): Due to bug 1676989, this argument appears as a + # required argument in the CLI help. Instead, the following + # commented-out code should work: + # self.assertIn(' [foo-bar]\n', sys.stdout.getvalue()) + self.assertIn(' foo-bar\n', sys.stdout.getvalue()) + + self.conf(['baz']) + self.assertTrue(hasattr(self.conf, 'foo_bar')) + # FIXME(dolphm): Due to bug 1676989, this argument cannot be retrieved + # by oslo_config.cfg. Instead, the following commented-out code should + # work: + # self.assertEqual('baz', self.conf.foo_bar) + self.assertIsNone(self.conf.foo_bar) + + def test_optional_positional_hyphenated_opt_undefined(self): + self.conf.register_cli_opt( + cfg.StrOpt('foo-bar', required=False, positional=True)) + + self.useFixture(fixtures.MonkeyPatch('sys.stdout', moves.StringIO())) + self.assertRaises(SystemExit, self.conf, ['--help']) + # FIXME(dolphm): Due to bug 1676989, this argument appears as a + # required argument in the CLI help. Instead, the following + # commented-out code should work: + # self.assertIn(' [foo-bar]\n', sys.stdout.getvalue()) + self.assertIn(' foo-bar\n', sys.stdout.getvalue()) + + self.conf([]) + self.assertTrue(hasattr(self.conf, 'foo_bar')) + self.assertIsNone(self.conf.foo_bar) + + def test_required_positional_hyphenated_opt_defined(self): + self.conf.register_cli_opt( + cfg.StrOpt('foo-bar', required=True, positional=True)) + + self.useFixture(fixtures.MonkeyPatch('sys.stdout', moves.StringIO())) + self.assertRaises(SystemExit, self.conf, ['--help']) + self.assertIn(' foo-bar\n', sys.stdout.getvalue()) + + # FIXME(dolphm): Due to bug 1676989, this mistakenly raises an + # exception, even though the option is clearly defined. Instead, the + # following commented out lines should work: + # self.conf(['baz']) + # self.assertTrue(hasattr(self.conf, 'foo_bar')) + # self.assertEqual('baz', self.conf.foo_bar) + self.assertRaises(cfg.RequiredOptError, self.conf, ['baz']) + + def test_required_positional_hyphenated_opt_undefined(self): + self.conf.register_cli_opt( + cfg.StrOpt('foo-bar', required=True, positional=True)) + + self.useFixture(fixtures.MonkeyPatch('sys.stdout', moves.StringIO())) + self.assertRaises(SystemExit, self.conf, ['--help']) + self.assertIn(' foo-bar\n', sys.stdout.getvalue()) + + self.assertRaises(cfg.RequiredOptError, self.conf, []) + def test_missing_required_cli_opt(self): self.conf.register_cli_opt( cfg.StrOpt('foo', required=True, positional=True)) |