Skip to content

Commit

Permalink
Merge "Refactor unit tests for CLI usage"
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuul authored and openstack-gerrit committed Dec 20, 2019
2 parents 6011b9c + ec84eed commit 062b98a
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions oslo_config/tests/test_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -142,6 +142,21 @@ def test_print_usage(self):
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())
Expand All @@ -151,7 +166,10 @@ def test_print_help(self):
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())
Expand All @@ -163,7 +181,10 @@ def test_print_help(self):
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())

Expand All @@ -183,7 +204,10 @@ def test_print_strOpt_with_choices_help(self):
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',
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 062b98a

Please sign in to comment.