def test_list_alias_key_misspell(self):
     mock_alias_table = get_config_parser()
     mock_alias_table.add_section('ac')
     mock_alias_table.set('ac', 'cmmand', 'account')
     azext_alias.custom.get_alias_table = Mock(
         return_value=mock_alias_table)
     self.assertListEqual([], list_alias())
Exemplo n.º 2
0
 def test_remove_alias_remove_non_existing_alias(self):
     mock_alias_table = get_config_parser()
     mock_alias_table.add_section('ac')
     mock_alias_table.set('ac', 'command', 'account')
     azext_alias.custom.get_alias_table = Mock(
         return_value=mock_alias_table)
     with self.assertRaises(CLIError):
         remove_alias('dns')
Exemplo n.º 3
0
 def __init__(self, **kwargs):
     self.alias_table = get_config_parser()
     self.kwargs = kwargs
     self.collided_alias = defaultdict(list)
     self.alias_config_str = ''
     self.alias_config_hash = ''
     self.load_alias_table()
     self.load_alias_hash()
Exemplo n.º 4
0
 def __init__(self, **kwargs):
     self.alias_table = get_config_parser()
     self.kwargs = kwargs
     self.collided_alias = defaultdict(list)
     self.alias_config_str = ''
     self.alias_config_hash = ''
     self.load_alias_table()
     self.load_alias_hash()
Exemplo n.º 5
0
 def test_remove_alias_remove_non_existing_alias(self):
     mock_alias_table = get_config_parser()
     mock_alias_table.add_section('ac')
     mock_alias_table.set('ac', 'command', 'account')
     azext_alias.custom.get_alias_table = Mock(return_value=mock_alias_table)
     with self.assertRaises(CLIError) as cm:
         remove_alias(['dns'])
     self.assertEqual(str(cm.exception), 'alias: "dns" alias not found')
Exemplo n.º 6
0
 def test_list_alias_multiple_alias(self):
     mock_alias_table = get_config_parser()
     mock_alias_table.add_section('ac')
     mock_alias_table.set('ac', 'command', 'account')
     mock_alias_table.add_section('dns')
     mock_alias_table.set('dns', 'command', 'network dns')
     azext_alias.custom.get_alias_table = Mock(return_value=mock_alias_table)
     self.assertListEqual([{'alias': 'ac', 'command': 'account'}, {'alias': 'dns', 'command': 'network dns'}], list_alias())
Exemplo n.º 7
0
def get_alias_completer(cmd, prefix, namespace, **kwargs):  # pylint: disable=unused-argument
    """
    An argument completer for alias name.
    """
    try:
        alias_table = get_config_parser()
        alias_table.read(GLOBAL_ALIAS_PATH)
        return alias_table.sections()
    except Exception:  # pylint: disable=broad-except
        return []
Exemplo n.º 8
0
 def load_alias_table(self):
     """
     Load (create, if not exist) the alias config file.
     """
     try:
         # w+ creates the alias config file if it does not exist
         open_mode = 'r+' if os.path.exists(GLOBAL_ALIAS_PATH) else 'w+'
         with open(GLOBAL_ALIAS_PATH, open_mode) as alias_config_file:
             self.alias_config_str = alias_config_file.read()
         self.alias_table.read(GLOBAL_ALIAS_PATH)
         telemetry.set_number_of_aliases_registered(len(self.alias_table.sections()))
     except Exception as exception:  # pylint: disable=broad-except
         logger.warning(CONFIG_PARSING_ERROR, AliasManager.process_exception_message(exception))
         self.alias_table = get_config_parser()
         telemetry.set_exception(exception)
Exemplo n.º 9
0
 def test_build_tab_completion_table(self):
     mock_alias_table = get_config_parser()
     mock_alias_table.add_section('ac')
     mock_alias_table.set('ac', 'command', 'account')
     mock_alias_table.add_section('ll')
     mock_alias_table.set('ll', 'command', 'list-locations')
     mock_alias_table.add_section('n')
     mock_alias_table.set('n', 'command', 'network')
     mock_alias_table.add_section('al')
     mock_alias_table.set('al', 'command', 'account list-locations')
     tab_completion_table = build_tab_completion_table(mock_alias_table)
     self.assertDictEqual({
         'account': ['', 'storage'],
         'list-locations': ['account'],
         'network': [''],
         'account list-locations': ['']
     }, tab_completion_table)
Exemplo n.º 10
0
 def load_alias_table(self):
     """
     Load (create, if not exist) the alias config file.
     """
     try:
         # w+ creates the alias config file if it does not exist
         open_mode = 'r+' if os.path.exists(GLOBAL_ALIAS_PATH) else 'w+'
         with open(GLOBAL_ALIAS_PATH, open_mode) as alias_config_file:
             self.alias_config_str = alias_config_file.read()
         self.alias_table.read(GLOBAL_ALIAS_PATH)
         telemetry.set_number_of_aliases_registered(
             len(self.alias_table.sections()))
     except Exception as exception:  # pylint: disable=broad-except
         logger.warning(CONFIG_PARSING_ERROR,
                        AliasManager.process_exception_message(exception))
         self.alias_table = get_config_parser()
         telemetry.set_exception(exception)
Exemplo n.º 11
0
 def test_build_tab_completion_table(self):
     mock_alias_table = get_config_parser()
     mock_alias_table.add_section('ac')
     mock_alias_table.set('ac', 'command', 'account')
     mock_alias_table.add_section('ll')
     mock_alias_table.set('ll', 'command', 'list-locations')
     mock_alias_table.add_section('n')
     mock_alias_table.set('n', 'command', 'network')
     mock_alias_table.add_section('al')
     mock_alias_table.set('al', 'command', 'account list-locations')
     tab_completion_table = build_tab_completion_table(mock_alias_table)
     self.assertDictEqual({
         'account': ['', 'storage'],
         'list-locations': ['account'],
         'network': [''],
         'account list-locations': ['']
     }, tab_completion_table)
Exemplo n.º 12
0
def _validate_alias_file_content(alias_file_path, url=''):
    """
    Make sure the alias name and alias command in the alias file is in valid format.

    Args:
        The alias file path to import aliases from.
    """
    alias_table = get_config_parser()
    try:
        alias_table.read(alias_file_path)
        for alias_name, alias_command in reduce_alias_table(alias_table):
            _validate_alias_name(alias_name)
            _validate_alias_command(alias_command)
            _validate_alias_command_level(alias_name, alias_command)
            _validate_pos_args_syntax(alias_name, alias_command)
    except Exception as exception:  # pylint: disable=broad-except
        error_msg = CONFIG_PARSING_ERROR % AliasManager.process_exception_message(exception)
        error_msg = error_msg.replace(alias_file_path, url or alias_file_path)
        raise CLIError(error_msg)
Exemplo n.º 13
0
def _validate_alias_file_content(alias_file_path, url=''):
    """
    Make sure the alias name and alias command in the alias file is in valid format.

    Args:
        The alias file path to import aliases from.
    """
    alias_table = get_config_parser()
    try:
        alias_table.read(alias_file_path)
        for alias_name, alias_command in reduce_alias_table(alias_table):
            _validate_alias_name(alias_name)
            _validate_alias_command(alias_command)
            _validate_alias_command_level(alias_name, alias_command)
            _validate_pos_args_syntax(alias_name, alias_command)
    except Exception as exception:  # pylint: disable=broad-except
        error_msg = CONFIG_PARSING_ERROR % AliasManager.process_exception_message(
            exception)
        error_msg = error_msg.replace(alias_file_path, url or alias_file_path)
        raise CLIError(error_msg)
Exemplo n.º 14
0
def remove_all_aliases():
    """
    Remove all registered aliases.
    """
    _commit_change(get_config_parser())
Exemplo n.º 15
0
def remove_all_aliases():
    """
    Remove all registered aliases.
    """
    _commit_change(get_config_parser())
Exemplo n.º 16
0
 def test_list_alias_key_misspell(self):
     mock_alias_table = get_config_parser()
     mock_alias_table.add_section('ac')
     mock_alias_table.set('ac', 'cmmand', 'account')
     azext_alias.custom.get_alias_table = Mock(return_value=mock_alias_table)
     self.assertListEqual([], list_alias())