summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDolph Mathews <dolph.mathews@gmail.com>2017-03-28 19:48:48 +0000
committerStephen Finucane <sfinucan@redhat.com>2018-10-08 11:17:49 +0100
commitb5f76a2ce8323ae89e85b599c2aec5a89a49eabd (patch)
tree4ef7d0d57ac9a0eb31ebc0114ee64238e4870e9a
parent7625e9531cd2350d730f24c00028de1be8a52353 (diff)
downloadoslo-config-b5f76a2ce8323ae89e85b599c2aec5a89a49eabd.tar.gz
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
-rw-r--r--oslo_config/tests/test_cfg.py112
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))