From 656cf91f898ff1a20986f358bedc64f410f5ee4a Mon Sep 17 00:00:00 2001 From: Johannes Erdfelt Date: Thu, 20 Mar 2014 13:29:10 -0700 Subject: [PATCH] Fix error when run with no arguments on Python 3 Python 3 changed the map built-in to return an iterable instead of a list. When tested in a boolean context, this always returns True, even if it would not return anything when iterated. Instead of the usage being printed, this error was printed: ERROR: 'Namespace' object has no attribute 'func' Use list comprehension instead to ensure that an iterable isn't returned Change-Id: Ie15f2fa8ee93ab26490e371133fa0f944430737b Closes-bug: 1295356 --- novaclient/shell.py | 3 ++- novaclient/tests/test_shell.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/novaclient/shell.py b/novaclient/shell.py index 63715d939..770f5bb48 100644 --- a/novaclient/shell.py +++ b/novaclient/shell.py @@ -785,7 +785,8 @@ class OpenStackHelpFormatter(argparse.HelpFormatter): def main(): try: - OpenStackComputeShell().main(map(strutils.safe_decode, sys.argv[1:])) + argv = [strutils.safe_decode(a) for a in sys.argv[1:]] + OpenStackComputeShell().main(argv) except Exception as e: logger.debug(e, exc_info=1) diff --git a/novaclient/tests/test_shell.py b/novaclient/tests/test_shell.py index 8994cb34b..a9f060b34 100644 --- a/novaclient/tests/test_shell.py +++ b/novaclient/tests/test_shell.py @@ -257,3 +257,17 @@ class ShellTest(utils.TestCase): @mock.patch('novaclient.client.Client') def test_v_unknown_service_type(self, mock_client): self._test_service_type('unknown', 'compute', mock_client) + + @mock.patch('sys.argv', ['nova']) + @mock.patch('sys.stdout', six.StringIO()) + @mock.patch('sys.stderr', six.StringIO()) + def test_main_noargs(self): + # Ensure that main works with no command-line arguments + try: + novaclient.shell.main() + except SystemExit as exc: + self.fail('Unexpected SystemExit') + + # We expect the normal usage as a result + self.assertIn('Command-line interface to the OpenStack Nova API', + sys.stdout.getvalue())