diff options
| author | Tim Burke <tim.burke@gmail.com> | 2016-03-02 16:02:28 +0000 |
|---|---|---|
| committer | Tim Burke <tim.burke@gmail.com> | 2016-04-06 14:20:27 -0700 |
| commit | 17aa6c789e3c28e59be3b92e6fa65edb89077436 (patch) | |
| tree | 2795e912773b08fef590bab2b9e93bcb09198658 /tests/unit/test_shell.py | |
| parent | 985c03820926a16b9592c8857379741c496a880b (diff) | |
| download | python-swiftclient-17aa6c789e3c28e59be3b92e6fa65edb89077436.tar.gz | |
Port from optparse to argparse
Why now?
* argparse was introduced in Python 3.2 and back-ported to Python 2.7.
Until we dropped Python 2.6 support, we were stuck on optparse.
* keystoneauth.loading.cli provides register_argparse_arguments and
load_from_argparse_arguments helper methods. Now that we're moving
toward Keystone Session support, argparse seems required.
Closes-Bug: 1553030
Change-Id: I5139fb64a8631a3010680090fd04345f95c55c7b
Diffstat (limited to 'tests/unit/test_shell.py')
| -rw-r--r-- | tests/unit/test_shell.py | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/tests/unit/test_shell.py b/tests/unit/test_shell.py index 59ed17d..58c5a7c 100644 --- a/tests/unit/test_shell.py +++ b/tests/unit/test_shell.py @@ -62,16 +62,19 @@ def _make_args(cmd, opts, os_opts, separator='-', flags=None, cmd_args=None): args = [""] flags = flags or [] for k, v in opts.items(): - arg = "--" + k.replace("_", "-") - args = args + [arg, v] + args.append("--" + k.replace("_", "-")) + if v is not None: + args.append(v) for k, v in os_opts.items(): - arg = "--os" + separator + k.replace("_", separator) - args = args + [arg, v] + args.append("--os" + separator + k.replace("_", separator)) + if v is not None: + args.append(v) for flag in flags: args.append('--%s' % flag) - args = args + [cmd] + if cmd: + args.append(cmd) if cmd_args: - args = args + cmd_args + args.extend(cmd_args) return args @@ -1261,17 +1264,17 @@ class TestShell(unittest.TestCase): self.assertEqual(output.err, "segment-size should be positive\n") output.clear() with self.assertRaises(SystemExit): - argv = ["", "upload", "-S", "-40K", "container", "object"] + argv = ["", "upload", "-S=-40K", "container", "object"] swiftclient.shell.main(argv) self.assertEqual(output.err, "segment-size should be positive\n") output.clear() with self.assertRaises(SystemExit): - argv = ["", "upload", "-S", "-40M", "container", "object"] + argv = ["", "upload", "-S=-40M", "container", "object"] swiftclient.shell.main(argv) self.assertEqual(output.err, "segment-size should be positive\n") output.clear() with self.assertRaises(SystemExit): - argv = ["", "upload", "-S", "-40G", "container", "object"] + argv = ["", "upload", "-S=-40G", "container", "object"] swiftclient.shell.main(argv) self.assertEqual(output.err, "segment-size should be positive\n") output.clear() @@ -1692,17 +1695,17 @@ class TestParsing(TestBase): def test_help(self): # --help returns condensed help message - opts = {"help": ""} + opts = {"help": None} os_opts = {} - args = _make_args("stat", opts, os_opts) + args = _make_args(None, opts, os_opts) with CaptureOutput() as out: self.assertRaises(SystemExit, swiftclient.shell.main, args) self.assertTrue(out.find('[--key <api_key>]') > 0) self.assertEqual(-1, out.find('--os-username=<auth-user-name>')) # --help returns condensed help message, overrides --os-help - opts = {"help": ""} - os_opts = {"help": ""} + opts = {"help": None} + os_opts = {"help": None} args = _make_args("", opts, os_opts) with CaptureOutput() as out: self.assertRaises(SystemExit, swiftclient.shell.main, args) @@ -1711,8 +1714,8 @@ class TestParsing(TestBase): # --os-password, --os-username and --os-auth_url should be ignored # because --help overrides it - opts = {"help": ""} - os_opts = {"help": "", + opts = {"help": None} + os_opts = {"help": None, "password": "secret", "username": "user", "auth_url": "http://example.com:5000/v3"} |
