From b5f76a2ce8323ae89e85b599c2aec5a89a49eabd Mon Sep 17 00:00:00 2001 From: Dolph Mathews Date: Tue, 28 Mar 2017 19:48:48 +0000 Subject: [PATCH] Unit tests to illustrate positional argument bug This patch does not provide a fix, but instead serves to illustrate several use cases where positional arguments do not behave correctly on the CLI. Change-Id: Ibdb05066b95a285f6618c861eb4d38465dbf0d02 Related-Bug: 1676989 --- oslo_config/tests/test_cfg.py | 112 +++++++++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 1 deletion(-) diff --git a/oslo_config/tests/test_cfg.py b/oslo_config/tests/test_cfg.py index 4f7549e7..db685834 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))