summaryrefslogtreecommitdiff
path: root/oslo_config
diff options
context:
space:
mode:
authorDolph Mathews <dolph.mathews@gmail.com>2017-03-30 17:56:13 +0000
committerStephen Finucane <sfinucan@redhat.com>2017-03-30 17:56:13 +0000
commitec84eeda526adc20dcabdf6e704958cc06773dd3 (patch)
tree68a234c25cf27f44218f9f67fa8b3a923145506f /oslo_config
parente3e2ba55eeeb86a9bc0624bb2592e46583e839e7 (diff)
downloadoslo-config-ec84eeda526adc20dcabdf6e704958cc06773dd3.tar.gz
Refactor unit tests for CLI usage
Prior to this change, CLI usage was hardcoded into the config opt fixture used for all tests. That CLI usage described two required positional arguments that were not actually part of the argument parser (FOO and BAR), but were instead just made up to have a valid-looking usage description. The trouble is that you can't then extend that argument parser with additional real arguments, and then test the configuration of those arguments by inspecting the *real* usage and --help output from argparse. For example, a unit test could not assert whether argparse was configured correctly by oslo.config, because there's simply no way to know when the usage was just statically set to the arbitrary values of "FOO BAR." This patch moves that specific unit test coverage (overriding usage with something arbitrary) into a dedicated unit test, while removing the arbitrary usage from all other unit tests. Instead, those unit tests now make assertions against the argparser's real configuration, hence options like --config-dir and --config-file now appear in those tests, because those options are included in every instance of cfg.ConfigOpts(). Change-Id: I54ba989768d074a5f24897299c85bd35fa1cbd1a Related-Bug: 1676989
Diffstat (limited to 'oslo_config')
-rw-r--r--oslo_config/tests/test_cfg.py42
1 files changed, 34 insertions, 8 deletions
diff --git a/oslo_config/tests/test_cfg.py b/oslo_config/tests/test_cfg.py
index db68583..a7d7767 100644
--- a/oslo_config/tests/test_cfg.py
+++ b/oslo_config/tests/test_cfg.py
@@ -94,13 +94,13 @@ class BaseTestCase(base.BaseTestCase):
class TestConfigOpts(cfg.ConfigOpts):
def __call__(self, args=None, default_config_files=[],
- default_config_dirs=[]):
+ default_config_dirs=[], usage=None):
return cfg.ConfigOpts.__call__(
self,
args=args,
prog='test',
version='1.0',
- usage='%(prog)s FOO BAR',
+ usage=usage,
description='somedesc',
epilog='tepilog',
default_config_files=default_config_files,
@@ -142,6 +142,21 @@ class UsageTestCase(BaseTestCase):
f = moves.StringIO()
self.conf([])
self.conf.print_usage(file=f)
+ self.assertIn(
+ 'usage: test [-h] [--config-dir DIR] [--config-file PATH] '
+ '[--version]',
+ f.getvalue())
+ self.assertNotIn('somedesc', f.getvalue())
+ self.assertNotIn('tepilog', f.getvalue())
+ self.assertNotIn('optional:', f.getvalue())
+
+ def test_print_custom_usage(self):
+ conf = self.TestConfigOpts()
+
+ self.tempdirs = []
+ f = moves.StringIO()
+ conf([], usage='%(prog)s FOO BAR')
+ conf.print_usage(file=f)
self.assertIn('usage: test FOO BAR', f.getvalue())
self.assertNotIn('somedesc', f.getvalue())
self.assertNotIn('tepilog', f.getvalue())
@@ -151,7 +166,10 @@ class UsageTestCase(BaseTestCase):
f = moves.StringIO()
self.conf([])
self.conf.print_help(file=f)
- self.assertIn('usage: test FOO BAR', f.getvalue())
+ self.assertIn(
+ 'usage: test [-h] [--config-dir DIR] [--config-file PATH] '
+ '[--version]',
+ f.getvalue())
self.assertIn('somedesc', f.getvalue())
self.assertIn('tepilog', f.getvalue())
self.assertNotIn('optional:', f.getvalue())
@@ -163,7 +181,10 @@ class HelpTestCase(BaseTestCase):
f = moves.StringIO()
self.conf([])
self.conf.print_help(file=f)
- self.assertIn('usage: test FOO BAR', f.getvalue())
+ self.assertIn(
+ 'usage: test [-h] [--config-dir DIR] [--config-file PATH] '
+ '[--version]',
+ f.getvalue())
self.assertIn('optional', f.getvalue())
self.assertIn('-h, --help', f.getvalue())
@@ -183,7 +204,10 @@ class HelpTestCase(BaseTestCase):
self.conf.register_cli_opts(cli_opts)
self.conf([])
self.conf.print_help(file=f)
- self.assertIn('usage: test FOO BAR', f.getvalue())
+ self.assertIn(
+ 'usage: test [-h] [--aa AA] [--bb BB] [--cc CC] [--config-dir DIR]'
+ '\n [--config-file PATH] [--version]',
+ f.getvalue())
self.assertIn('optional', f.getvalue())
self.assertIn('-h, --help', f.getvalue())
self.assertIn('StrOpt with choices. Allowed values: xx, yy, zz',
@@ -756,10 +780,12 @@ class CliSpecialOptsTestCase(BaseTestCase):
def test_help(self):
self.useFixture(fixtures.MonkeyPatch('sys.stdout', moves.StringIO()))
self.assertRaises(SystemExit, self.conf, ['--help'])
- self.assertIn('FOO BAR', sys.stdout.getvalue())
- self.assertIn('--version', sys.stdout.getvalue())
+ self.assertIn('usage: test', sys.stdout.getvalue())
+ self.assertIn('[--version]', sys.stdout.getvalue())
+ self.assertIn('[-h]', sys.stdout.getvalue())
self.assertIn('--help', sys.stdout.getvalue())
- self.assertIn('--config-file', sys.stdout.getvalue())
+ self.assertIn('[--config-dir DIR]', sys.stdout.getvalue())
+ self.assertIn('[--config-file PATH]', sys.stdout.getvalue())
def test_version(self):
# In Python 3.4+, argparse prints the version on stdout; before 3.4, it