def test_register_cli_argument_with_overrides(self):
        command_table.clear()

        global_vm_name_type = CliArgumentType(
            options_list=('--foo', '-f'), metavar='FOO', help='foo help'
        )
        derived_vm_name_type = CliArgumentType(base_type=global_vm_name_type,
                                               help='first modification')

        cli_command('test vm-get', Test_command_registration.sample_vm_get, None)
        cli_command('test command vm-get-1', Test_command_registration.sample_vm_get, None)
        cli_command('test command vm-get-2', Test_command_registration.sample_vm_get, None)

        register_cli_argument('test', 'vm_name', global_vm_name_type)
        register_cli_argument('test command', 'vm_name', derived_vm_name_type)
        register_cli_argument('test command vm-get-2', 'vm_name', derived_vm_name_type,
                              help='second modification')

        _update_command_definitions(command_table)

        self.assertEqual(len(command_table), 3,
                         'We expect exactly three commands in the command table')
        command1 = command_table['test vm-get'].arguments['vm_name']
        command2 = command_table['test command vm-get-1'].arguments['vm_name']
        command3 = command_table['test command vm-get-2'].arguments['vm_name']

        self.assertTrue(command1.options['help'] == 'foo help')
        self.assertTrue(command2.options['help'] == 'first modification')
        self.assertTrue(command3.options['help'] == 'second modification')
    def test_override_using_register_cli_argument(self):
        def sample_sdk_method(param_a): # pylint: disable=unused-argument
            pass

        def test_validator_completer():
            pass

        command_table.clear()
        cli_command('override_using_register_cli_argument foo',
                    sample_sdk_method,
                    None)
        register_cli_argument('override_using_register_cli_argument',
                              'param_a',
                              options_list=('--overridden', '-r'),
                              validator=test_validator_completer,
                              completer=test_validator_completer,
                              required=False)

        _update_command_definitions(command_table)

        command_metadata = command_table['override_using_register_cli_argument foo']
        self.assertEqual(len(command_metadata.arguments), 1, 'We expected exactly 1 arguments')

        actual_arg = command_metadata.arguments['param_a']
        self.assertEqual(actual_arg.options_list, ('--overridden', '-r'))
        self.assertEqual(actual_arg.validator, test_validator_completer)
        self.assertEqual(actual_arg.completer, test_validator_completer)
        self.assertFalse(actual_arg.options['required'])
    def test_register_cli_argument(self):
        command_table.clear()
        cli_command('test register sample-vm-get',
                    Test_command_registration.sample_vm_get)
        register_cli_argument(
            'test register sample-vm-get', 'vm_name',
            CliArgumentType(options_list=('--wonky-name', '-n'),
                            metavar='VMNAME',
                            help='Completely WONKY name...',
                            required=False))

        _update_command_definitions(command_table)

        self.assertEqual(len(command_table), 1,
                         'We expect exactly one command in the command table')
        command_metadata = command_table['test register sample-vm-get']
        self.assertEqual(len(command_metadata.arguments), 4,
                         'We expected exactly 4 arguments')
        some_expected_arguments = {
            'resource_group_name':
            CliArgumentType(dest='resource_group_name', required=True),
            'vm_name':
            CliArgumentType(dest='vm_name', required=False),
        }

        for probe in some_expected_arguments:
            existing = next(arg for arg in command_metadata.arguments
                            if arg == probe)
            self.assertDictContainsSubset(
                some_expected_arguments[existing].settings,
                command_metadata.arguments[existing].options)
        self.assertEqual(command_metadata.arguments['vm_name'].options_list,
                         ('--wonky-name', '-n'))
    def test_command_build_argument_help_text(self):
        def sample_sdk_method_with_weird_docstring(param_a, param_b, param_c): # pylint: disable=unused-argument
            """
            An operation with nothing good.

            :param dict param_a:
            :param param_b: The name
            of
            nothing.
            :param param_c: The name
            of

            nothing2.
            """
        command_table.clear()
        cli_command('test command foo', sample_sdk_method_with_weird_docstring, None)

        _update_command_definitions(command_table)

        command_metadata = command_table['test command foo']
        self.assertEqual(len(command_metadata.arguments), 3, 'We expected exactly 3 arguments')
        some_expected_arguments = {
            'param_a': CliArgumentType(dest='param_a', required=True, help=''),
            'param_b': CliArgumentType(dest='param_b', required=True, help='The name of nothing.'),
            'param_c': CliArgumentType(dest='param_c', required=True, help='The name of nothing2.')
        }

        for probe in some_expected_arguments:
            existing = next(arg for arg in command_metadata.arguments if arg == probe)
            self.assertDictContainsSubset(some_expected_arguments[existing].settings,
                                          command_metadata.arguments[existing].options)
    def test_override_using_register_cli_argument(self):
        def sample_sdk_method(param_a):  # pylint: disable=unused-argument
            pass

        def test_validator_completer():
            pass

        command_table.clear()
        cli_command('override_using_register_cli_argument foo',
                    sample_sdk_method, None)
        register_cli_argument('override_using_register_cli_argument',
                              'param_a',
                              options_list=('--overridden', '-r'),
                              validator=test_validator_completer,
                              completer=test_validator_completer,
                              required=False)

        _update_command_definitions(command_table)

        command_metadata = command_table[
            'override_using_register_cli_argument foo']
        self.assertEqual(len(command_metadata.arguments), 1,
                         'We expected exactly 1 arguments')

        actual_arg = command_metadata.arguments['param_a']
        self.assertEqual(actual_arg.options_list, ('--overridden', '-r'))
        self.assertEqual(actual_arg.validator, test_validator_completer)
        self.assertEqual(actual_arg.completer, test_validator_completer)
        self.assertFalse(actual_arg.options['required'])
        command_table.clear()
    def test_register_extra_cli_argument(self):
        command_table.clear()

        cli_command('test command sample-vm-get',
                    Test_command_registration.sample_vm_get, None)
        register_extra_cli_argument('test command sample-vm-get',
                                    'added_param',
                                    options_list=('--added-param', ),
                                    metavar='ADDED',
                                    help='Just added this right now!',
                                    required=True)

        _update_command_definitions(command_table)

        self.assertEqual(len(command_table), 1,
                         'We expect exactly one command in the command table')
        command_metadata = command_table['test command sample-vm-get']
        self.assertEqual(len(command_metadata.arguments), 5,
                         'We expected exactly 5 arguments')

        some_expected_arguments = {
            'added_param': CliArgumentType(dest='added_param', required=True)
        }

        for probe in some_expected_arguments:
            existing = next(arg for arg in command_metadata.arguments
                            if arg == probe)
            self.assertDictContainsSubset(
                some_expected_arguments[existing].settings,
                command_metadata.arguments[existing].options)

        command_table.clear()
    def test_command_build_argument_help_text(self):
        def sample_sdk_method_with_weird_docstring(param_a, param_b, param_c):  # pylint: disable=unused-argument
            """
            An operation with nothing good.

            :param dict param_a:
            :param param_b: The name
            of
            nothing.
            :param param_c: The name
            of

            nothing2.
            """

        command_table.clear()
        cli_command('test command foo', sample_sdk_method_with_weird_docstring,
                    None)

        _update_command_definitions(command_table)

        command_metadata = command_table['test command foo']
        self.assertEqual(len(command_metadata.arguments), 3,
                         'We expected exactly 3 arguments')
        some_expected_arguments = {
            'param_a':
            CliArgumentType(dest='param_a', required=True, help=''),
            'param_b':
            CliArgumentType(dest='param_b',
                            required=True,
                            help='The name of nothing.'),
            'param_c':
            CliArgumentType(dest='param_c',
                            required=True,
                            help='The name of nothing2.')
        }

        for probe in some_expected_arguments:
            existing = next(arg for arg in command_metadata.arguments
                            if arg == probe)
            self.assertDictContainsSubset(
                some_expected_arguments[existing].settings,
                command_metadata.arguments[existing].options)
        command_table.clear()
    def test_register_cli_argument(self):
        command_table.clear()
        cli_command('test register sample-vm-get', Test_command_registration.sample_vm_get)
        register_cli_argument('test register sample-vm-get', 'vm_name', CliArgumentType(
            options_list=('--wonky-name', '-n'), metavar='VMNAME', help='Completely WONKY name...',
            required=False
        ))

        _update_command_definitions(command_table)

        self.assertEqual(len(command_table), 1,
                         'We expect exactly one command in the command table')
        command_metadata = command_table['test register sample-vm-get']
        self.assertEqual(len(command_metadata.arguments), 4, 'We expected exactly 4 arguments')
        some_expected_arguments = {
            'resource_group_name': CliArgumentType(dest='resource_group_name', required=True),
            'vm_name': CliArgumentType(dest='vm_name', required=False),
        }

        for probe in some_expected_arguments:
            existing = next(arg for arg in command_metadata.arguments if arg == probe)
            self.assertDictContainsSubset(some_expected_arguments[existing].settings,
                                          command_metadata.arguments[existing].options)
        self.assertEqual(command_metadata.arguments['vm_name'].options_list, ('--wonky-name', '-n'))
    def test_register_extra_cli_argument(self):
        command_table.clear()

        cli_command('test command sample-vm-get', Test_command_registration.sample_vm_get, None)
        register_extra_cli_argument(
            'test command sample-vm-get', 'added_param', options_list=('--added-param',),
            metavar='ADDED', help='Just added this right now!', required=True
        )

        _update_command_definitions(command_table)

        self.assertEqual(len(command_table), 1,
                         'We expect exactly one command in the command table')
        command_metadata = command_table['test command sample-vm-get']
        self.assertEqual(len(command_metadata.arguments), 5, 'We expected exactly 5 arguments')

        some_expected_arguments = {
            'added_param': CliArgumentType(dest='added_param', required=True)
        }

        for probe in some_expected_arguments:
            existing = next(arg for arg in command_metadata.arguments if arg == probe)
            self.assertDictContainsSubset(some_expected_arguments[existing].settings,
                                          command_metadata.arguments[existing].options)
    def test_register_cli_argument_with_overrides(self):
        command_table.clear()

        global_vm_name_type = CliArgumentType(options_list=('--foo', '-f'),
                                              metavar='FOO',
                                              help='foo help')
        derived_vm_name_type = CliArgumentType(base_type=global_vm_name_type,
                                               help='first modification')

        cli_command('test vm-get', Test_command_registration.sample_vm_get,
                    None)
        cli_command('test command vm-get-1',
                    Test_command_registration.sample_vm_get, None)
        cli_command('test command vm-get-2',
                    Test_command_registration.sample_vm_get, None)

        register_cli_argument('test', 'vm_name', global_vm_name_type)
        register_cli_argument('test command', 'vm_name', derived_vm_name_type)
        register_cli_argument('test command vm-get-2',
                              'vm_name',
                              derived_vm_name_type,
                              help='second modification')

        _update_command_definitions(command_table)

        self.assertEqual(
            len(command_table), 3,
            'We expect exactly three commands in the command table')
        command1 = command_table['test vm-get'].arguments['vm_name']
        command2 = command_table['test command vm-get-1'].arguments['vm_name']
        command3 = command_table['test command vm-get-2'].arguments['vm_name']

        self.assertTrue(command1.options['help'] == 'foo help')
        self.assertTrue(command2.options['help'] == 'first modification')
        self.assertTrue(command3.options['help'] == 'second modification')
        command_table.clear()