From fdf64d0a8d287d05fc3eb035d8341a2cb980538a Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Apr 22 2021 07:58:08 +0000 Subject: [PATCH 1/2] cli: list-api method Limit output to given method Fixes: https://pagure.io/koji/issue/2687 --- diff --git a/cli/koji_cli/commands.py b/cli/koji_cli/commands.py index ba871d6..4bc724a 100644 --- a/cli/koji_cli/commands.py +++ b/cli/koji_cli/commands.py @@ -2459,29 +2459,34 @@ def anon_handle_latest_build(goptions, session, args): def anon_handle_list_api(goptions, session, args): "[info] Print the list of XML-RPC APIs" - usage = _("usage: %prog list-api [options]") + usage = _("usage: %prog list-api [options] [method_name ...]") parser = OptionParser(usage=get_usage_str(usage)) (options, args) = parser.parse_args(args) - if len(args) != 0: - parser.error(_("This command takes no arguments")) ensure_connection(session, goptions) - for x in sorted(session._listapi(), key=lambda x: x['name']): - if 'argdesc' in x: - args = x['argdesc'] - elif x['args']: - # older servers may not provide argdesc - expanded = [] - for arg in x['args']: - if isinstance(arg, str): - expanded.append(arg) - else: - expanded.append('%s=%s' % (arg[0], arg[1])) - args = "(%s)" % ", ".join(expanded) - else: - args = "()" - print('%s%s' % (x['name'], args)) - if x['doc']: - print(" description: %s" % x['doc']) + if args: + for method in args: + help = session.system.methodHelp(method) + if not help: + parser.error(_("Unknown method: %s") % method) + print(help) + else: + for x in sorted(session._listapi(), key=lambda x: x['name']): + if 'argdesc' in x: + args = x['argdesc'] + elif x['args']: + # older servers may not provide argdesc + expanded = [] + for arg in x['args']: + if isinstance(arg, str): + expanded.append(arg) + else: + expanded.append('%s=%s' % (arg[0], arg[1])) + args = "(%s)" % ", ".join(expanded) + else: + args = "()" + print('%s%s' % (x['name'], args)) + if x['doc']: + print(" description: %s" % x['doc']) def anon_handle_list_tagged(goptions, session, args): diff --git a/tests/test_cli/test_list_api.py b/tests/test_cli/test_list_api.py index deed4fc..862b631 100644 --- a/tests/test_cli/test_list_api.py +++ b/tests/test_cli/test_list_api.py @@ -13,7 +13,7 @@ class TestListApi(utils.CliTestCase): maxDiff = None def setUp(self): - self.error_format = """Usage: %s list-api [options] + self.error_format = """Usage: %s list-api [options] [method_name] (Specify the --help global option for a list of other help options) %s: error: {message} @@ -29,18 +29,7 @@ class TestListApi(utils.CliTestCase): session = mock.MagicMock() options = mock.MagicMock() - # Case 1. argument error - expected = self.format_error_message( - "This command takes no arguments") - self.assert_system_exit( - anon_handle_list_api, - options, - session, - ['arg'], - stderr=expected, - activate_session=None) - - # Case 2. + # Case 1. list all methods session._listapi.return_value = [ { 'argdesc': '(tagInfo, **kwargs)', @@ -70,10 +59,29 @@ class TestListApi(utils.CliTestCase): anon_handle_list_api(options, session, []) self.assert_console_message(stdout, expected) + # Case 2. unknown method + session.system.methodHelp.return_value = None + expected = self.format_error_message("Unknown method: unknown method") + self.assert_system_exit( + anon_handle_list_api, + options, + session, + ['unknown method'], + stderr=expected, + activate_session=None) + + # Case 3. known method + session.system.methodHelp.return_value = "editTag2(tagInfo, **kwargs)\n" \ + " description: Edit information for an existing tag." + anon_handle_list_api(options, session, ['editTag2']) + expected = "editTag2(tagInfo, **kwargs)\n" + expected += " description: Edit information for an existing tag.\n" + self.assert_console_message(stdout, expected) + def test_anon_handle_list_api_help(self): self.assert_help( anon_handle_list_api, - """Usage: %s list-api [options] + """Usage: %s list-api [options] [method_name] (Specify the --help global option for a list of other help options) Options: From 897b7fe81aa3eb90ad08f524aa39d784ef9d1a16 Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Apr 22 2021 07:58:08 +0000 Subject: [PATCH 2/2] more informative test string --- diff --git a/tests/test_cli/test_list_api.py b/tests/test_cli/test_list_api.py index 862b631..e150104 100644 --- a/tests/test_cli/test_list_api.py +++ b/tests/test_cli/test_list_api.py @@ -59,14 +59,14 @@ class TestListApi(utils.CliTestCase): anon_handle_list_api(options, session, []) self.assert_console_message(stdout, expected) - # Case 2. unknown method + # Case 2. non-existent fake method session.system.methodHelp.return_value = None - expected = self.format_error_message("Unknown method: unknown method") + expected = self.format_error_message("Unknown method: non-existent-fake-method") self.assert_system_exit( anon_handle_list_api, options, session, - ['unknown method'], + ['non-existent-fake-method'], stderr=expected, activate_session=None)