def test_register_cli_argument(self): command_table.clear() cli_command(None, 'test register sample-vm-get', '{}#Test_command_registration.sample_vm_get'.format(__name__)) register_cli_argument('test register sample-vm-get', 'vm_name', CliArgumentType( options_list=('--wonky-name', '-n'), metavar='VMNAME', help='Completely WONKY name...', required=False )) command_table['test register sample-vm-get'].load_arguments() _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_command(self): command_table.clear() cli_command(None, 'test command sample-vm-get', '{}#Test_command_registration.sample_vm_get'.format(__name__), None) self.assertEqual(len(command_table), 1, 'We expect exactly one command in the command table') command_table['test command sample-vm-get'].load_arguments() command_metadata = command_table['test command 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, help='The name of the resource group.'), 'vm_name': CliArgumentType(dest='vm_name', required=True, help='The name of the virtual machine.'), 'opt_param': CliArgumentType(required=False, help='Used to verify reflection correctly identifies optional params.'), # pylint: disable=line-too-long 'expand': CliArgumentType(required=False, help='The expand expression to apply on the operation.') } 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['resource_group_name'].options_list, ['--resource-group-name'])
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() setattr(sys.modules[__name__], sample_sdk_method_with_weird_docstring.__name__, sample_sdk_method_with_weird_docstring) #pylint: disable=line-too-long cli_command(None, 'test command foo', '{}#{}'.format(__name__, sample_sdk_method_with_weird_docstring.__name__), None) #pylint: disable=line-too-long command_table['test command foo'].load_arguments() _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_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() setattr(sys.modules[__name__], sample_sdk_method.__name__, sample_sdk_method) cli_command(None, 'override_using_register_cli_argument foo', '{}#{}'.format(__name__, sample_sdk_method.__name__), 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) command_table['override_using_register_cli_argument foo'].load_arguments() _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_cli_argument(self): command_table.clear() cli_command( None, 'test register sample-vm-get', '{}#Test_command_registration.sample_vm_get'.format(__name__)) register_cli_argument( 'test register sample-vm-get', 'vm_name', CliArgumentType(options_list=('--wonky-name', '-n'), metavar='VMNAME', help='Completely WONKY name...', required=False)) command_table['test register sample-vm-get'].load_arguments() _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(None, 'test command sample-vm-get', '{}#Test_command_registration.sample_vm_get'.format(__name__), 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 ) command_table['test command sample-vm-get'].load_arguments() _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_register_extra_cli_argument(self): command_table.clear() cli_command( None, 'test command sample-vm-get', '{}#Test_command_registration.sample_vm_get'.format(__name__), 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) command_table['test command sample-vm-get'].load_arguments() _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_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() setattr(sys.modules[__name__], sample_sdk_method.__name__, sample_sdk_method) cli_command(None, 'override_using_register_cli_argument foo', '{}#{}'.format(__name__, sample_sdk_method.__name__), 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) command_table[ 'override_using_register_cli_argument foo'].load_arguments() _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_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()
def test_register_command_from_extension(self): command_table.clear() # A standard command cli_command(None, 'hello world', 'dummy_operation', None) self.assertEqual(len(command_table), 1) self.assertEqual(command_table['hello world'].command_source, None) command_table.clear() # A command from an extension cli_command('{}myextension'.format(EXTENSIONS_MOD_PREFIX), 'hello world', 'dummy_operation', None) self.assertEqual(len(command_table), 1) cmd_source = command_table['hello world'].command_source self.assertTrue(isinstance(cmd_source, ExtensionCommandSource)) self.assertFalse(cmd_source.overrides_command) command_table.clear() # A command from an extension that overrides the original command cli_command(None, 'hello world', 'dummy_operation', None) cli_command('{}myextension'.format(EXTENSIONS_MOD_PREFIX), 'hello world', 'dummy_operation', None) self.assertEqual(len(command_table), 1) cmd_source = command_table['hello world'].command_source self.assertTrue(isinstance(cmd_source, ExtensionCommandSource)) self.assertTrue(cmd_source.overrides_command) command_table.clear()
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( None, 'test vm-get', '{}#Test_command_registration.sample_vm_get'.format(__name__), None) cli_command( None, 'test command vm-get-1', '{}#Test_command_registration.sample_vm_get'.format(__name__), None) cli_command( None, 'test command vm-get-2', '{}#Test_command_registration.sample_vm_get'.format(__name__), 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') command_table['test vm-get'].load_arguments() command_table['test command vm-get-1'].load_arguments() command_table['test command vm-get-2'].load_arguments() _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()
def set_up_command_table(self, required_arg=False): command_table.clear() module_name = __name__ + '.' + self._testMethodName cli_command(module_name, 'test sample-vm-list', '{}#TestCommandWithConfiguredDefaults.sample_vm_list'.format(__name__)) register_cli_argument('test sample-vm-list', 'resource_group_name', CliArgumentType(options_list=('--resource-group-name', '-g'), configured_default='group', required=required_arg)) command_table['test sample-vm-list'].load_arguments() _update_command_definitions(command_table) self.argv = 'az test sample-vm-list'.split() config = Configuration() config.get_command_table = lambda argv: command_table self.application = Application(config)
def set_up_command_table(self, required_arg=False): command_table.clear() module_name = __name__ + '.' + self._testMethodName cli_command(module_name, 'test sample-vm-list', '{}#TestCommandWithConfiguredDefaults.sample_vm_list'.format(__name__)) register_cli_argument('test sample-vm-list', 'resource_group_name', CliArgumentType(options_list=('--resource-group-name', '-g'), configured_default='group', required=required_arg)) command_table['test sample-vm-list'].load_arguments() _update_command_definitions(command_table) self.argv = 'az test sample-vm-list'.split() config = Configuration(self.argv) config.get_command_table = lambda: command_table self.application = Application(config)