當前位置: 首頁>>代碼示例>>Python>>正文


Python DummyCli.loader方法代碼示例

本文整理匯總了Python中azure.cli.core.mock.DummyCli.loader方法的典型用法代碼示例。如果您正苦於以下問題:Python DummyCli.loader方法的具體用法?Python DummyCli.loader怎麽用?Python DummyCli.loader使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在azure.cli.core.mock.DummyCli的用法示例。


在下文中一共展示了DummyCli.loader方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_register_simple_commands

# 需要導入模塊: from azure.cli.core.mock import DummyCli [as 別名]
# 或者: from azure.cli.core.mock.DummyCli import loader [as 別名]
    def test_register_simple_commands(self):
        def test_handler1():
            pass

        def test_handler2():
            pass

        cli = DummyCli()
        cli.loader = mock.MagicMock()
        cli.loader.cli_ctx = cli

        command = AzCliCommand(cli.loader, 'command the-name', test_handler1)
        command2 = AzCliCommand(cli.loader, 'sub-command the-second-name', test_handler2)
        cmd_table = {'command the-name': command, 'sub-command the-second-name': command2}
        cli.commands_loader.command_table = cmd_table

        parser = AzCliCommandParser(cli)
        parser.load_command_table(cli.commands_loader)
        args = parser.parse_args('command the-name'.split())
        self.assertIs(args.func, command)

        args = parser.parse_args('sub-command the-second-name'.split())
        self.assertIs(args.func, command2)

        AzCliCommandParser.error = VerifyError(self,)
        parser.parse_args('sub-command'.split())
        self.assertTrue(AzCliCommandParser.error.called)
開發者ID:sptramer,項目名稱:azure-cli,代碼行數:29,代碼來源:test_parser.py

示例2: test_case_insensitive_enum_choices

# 需要導入模塊: from azure.cli.core.mock import DummyCli [as 別名]
# 或者: from azure.cli.core.mock.DummyCli import loader [as 別名]
    def test_case_insensitive_enum_choices(self):
        from enum import Enum

        class TestEnum(Enum):  # pylint: disable=too-few-public-methods

            opt1 = "ALL_CAPS"
            opt2 = "camelCase"
            opt3 = "snake_case"

        def test_handler():
            pass

        cli = DummyCli()
        cli.loader = mock.MagicMock()
        cli.loader.cli_ctx = cli

        command = AzCliCommand(cli.loader, 'test command', test_handler)
        command.add_argument('opt', '--opt', required=True, **enum_choice_list(TestEnum))
        cmd_table = {'test command': command}
        cli.commands_loader.command_table = cmd_table

        parser = AzCliCommandParser(cli)
        parser.load_command_table(cli.commands_loader)

        args = parser.parse_args('test command --opt alL_cAps'.split())
        self.assertEqual(args.opt, 'ALL_CAPS')

        args = parser.parse_args('test command --opt CAMELCASE'.split())
        self.assertEqual(args.opt, 'camelCase')

        args = parser.parse_args('test command --opt sNake_CASE'.split())
        self.assertEqual(args.opt, 'snake_case')
開發者ID:sptramer,項目名稱:azure-cli,代碼行數:34,代碼來源:test_parser.py

示例3: test_parser_error_spellchecker

# 需要導入模塊: from azure.cli.core.mock import DummyCli [as 別名]
# 或者: from azure.cli.core.mock.DummyCli import loader [as 別名]
    def test_parser_error_spellchecker(self):
        cli = DummyCli()
        main_loader = MainCommandsLoader(cli)
        cli.loader = main_loader

        cli.loader.load_command_table(None)

        parser = cli.parser_cls(cli)
        parser.load_command_table(cli.loader)

        logger_msgs = []
        choice_lists = []
        original_get_close_matches = difflib.get_close_matches

        def mock_log_error(_, msg):
            logger_msgs.append(msg)

        def mock_get_close_matches(*args, **kwargs):
            choice_lists.append(original_get_close_matches(*args, **kwargs))

        # run multiple faulty commands and save error logs, as well as close matches
        with mock.patch('logging.Logger.error', mock_log_error), \
                mock.patch('difflib.get_close_matches', mock_get_close_matches):
            faulty_cmd_args = [
                'test module1 --opt enum_1',
                'test extension1 --opt enum_1',
                'test foo_bar --opt enum_3',
                'test module --opt enum_3',
                'test extension --opt enum_3'
            ]
            for text in faulty_cmd_args:
                with self.assertRaises(SystemExit):
                    parser.parse_args(text.split())
        parser.parse_args('test module --opt enum_1'.split())

        # assert the right type of error msg is logged for command vs argument parsing
        self.assertEqual(len(logger_msgs), 5)
        for msg in logger_msgs[:3]:
            self.assertIn("not in the", msg)
            self.assertIn("command group", msg)
        for msg in logger_msgs[3:]:
            self.assertIn("not a valid value for '--opt'.", msg)

        # assert the right choices are matched as "close".
        # If these don't hold, matching algorithm should be deemed flawed.
        for choices in choice_lists[:2]:
            self.assertEqual(len(choices), 1)
        self.assertEqual(len(choice_lists[2]), 0)
        for choices in choice_lists[3:]:
            self.assertEqual(len(choices), 2)
            for choice in ['enum_1', 'enum_2']:
                self.assertIn(choice, choices)
開發者ID:sptramer,項目名稱:azure-cli,代碼行數:54,代碼來源:test_parser.py

示例4: test_register_command_from_extension

# 需要導入模塊: from azure.cli.core.mock import DummyCli [as 別名]
# 或者: from azure.cli.core.mock.DummyCli import loader [as 別名]
    def test_register_command_from_extension(self):

        from azure.cli.core.commands import _load_command_loader
        cli = DummyCli()
        main_loader = MainCommandsLoader(cli)
        cli.loader = main_loader

        cmd_tbl = cli.loader.load_command_table(None)
        ext1 = cmd_tbl['hello noodle']
        ext2 = cmd_tbl['hello world']

        self.assertTrue(isinstance(ext1.command_source, ExtensionCommandSource))
        self.assertFalse(ext1.command_source.overrides_command)

        self.assertTrue(isinstance(ext2.command_source, ExtensionCommandSource))
        self.assertTrue(ext2.command_source.overrides_command)
開發者ID:johanste,項目名稱:azure-cli,代碼行數:18,代碼來源:test_command_registration.py

示例5: test_nargs_parameter

# 需要導入模塊: from azure.cli.core.mock import DummyCli [as 別名]
# 或者: from azure.cli.core.mock.DummyCli import loader [as 別名]
    def test_nargs_parameter(self):
        def test_handler():
            pass

        cli = DummyCli()
        cli.loader = mock.MagicMock()
        cli.loader.cli_ctx = cli

        command = AzCliCommand(cli.loader, 'test command', test_handler)
        command.add_argument('req', '--req', required=True, nargs=2)
        cmd_table = {'test command': command}
        cli.commands_loader.command_table = cmd_table

        parser = AzCliCommandParser(cli)
        parser.load_command_table(cli.commands_loader)

        args = parser.parse_args('test command --req yep nope'.split())
        self.assertIs(args.func, command)

        AzCliCommandParser.error = VerifyError(self)
        parser.parse_args('test command -req yep'.split())
        self.assertTrue(AzCliCommandParser.error.called)
開發者ID:sptramer,項目名稱:azure-cli,代碼行數:24,代碼來源:test_parser.py


注:本文中的azure.cli.core.mock.DummyCli.loader方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。