<s> import pprint <EOL> import difflib <EOL> import mock <EOL> from botocore . compat import OrderedDict <EOL> from awscli . clidriver import ( <EOL> CLIDriver , ServiceCommand , ServiceOperation , CLICommand ) <EOL> from awscli . arguments import BaseCLIArgument , CustomArgument <EOL> from awscli . help import ProviderHelpCommand <EOL> from awscli . completer import Completer <EOL> from awscli . testutils import unittest <EOL> from awscli . customizations . commands import BasicCommand <EOL> class BaseCompleterTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . clidriver_creator = MockCLIDriverFactory ( ) <EOL> def assert_completion ( self , completer , cmdline , expected_results , <EOL> point = None ) : <EOL> if point is None : <EOL> point = len ( cmdline ) <EOL> actual = set ( completer . complete ( cmdline , point ) ) <EOL> expected = set ( expected_results ) <EOL> if not actual == expected : <EOL> pretty_d1 = pprint . pformat ( actual , width = <NUM_LIT:1> ) . splitlines ( ) <EOL> pretty_d2 = pprint . pformat ( expected , width = <NUM_LIT:1> ) . splitlines ( ) <EOL> diff = ( '<STR_LIT:\n>' + '<STR_LIT:\n>' . join ( difflib . ndiff ( pretty_d1 , pretty_d2 ) ) ) <EOL> raise AssertionError ( "<STR_LIT>" % diff ) <EOL> self . assertEqual ( actual , expected ) <EOL> class TestCompleter ( BaseCompleterTest ) : <EOL> def test_complete_services ( self ) : <EOL> commands = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:foo>' : { } , <EOL> '<STR_LIT:bar>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { } <EOL> } <EOL> } <EOL> } , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> completer = Completer ( <EOL> self . clidriver_creator . create_clidriver ( commands ) ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ '<STR_LIT:foo>' , '<STR_LIT:bar>' ] ) <EOL> def test_complete_partial_service_name ( self ) : <EOL> commands = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } <EOL> } , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> completer = Completer ( <EOL> self . clidriver_creator . create_clidriver ( commands ) ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ ] ) <EOL> def test_complete_on_invalid_service ( self ) : <EOL> commands = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:foo>' : { } , <EOL> '<STR_LIT:bar>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { } <EOL> } <EOL> } <EOL> } , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> completer = Completer ( <EOL> self . clidriver_creator . create_clidriver ( commands ) ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ ] ) <EOL> def test_complete_top_level_args ( self ) : <EOL> commands = { <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : [ '<STR_LIT:foo>' , '<STR_LIT:bar>' ] <EOL> } <EOL> completer = Completer ( <EOL> self . clidriver_creator . create_clidriver ( commands ) ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_complete_partial_top_level_arg ( self ) : <EOL> commands = { <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : [ '<STR_LIT:foo>' , '<STR_LIT:bar>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> } <EOL> completer = Completer ( <EOL> self . clidriver_creator . create_clidriver ( commands ) ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ ] ) <EOL> def test_complete_top_level_arg_with_arg_already_used ( self ) : <EOL> commands = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { } <EOL> } , <EOL> '<STR_LIT>' : [ '<STR_LIT:foo>' , '<STR_LIT:bar>' ] <EOL> } <EOL> completer = Completer ( <EOL> self . clidriver_creator . create_clidriver ( commands ) ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ ] ) <EOL> def test_complete_service_commands ( self ) : <EOL> commands = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:foo>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:bar>' : { <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] <EOL> } , <EOL> '<STR_LIT>' : { } <EOL> } <EOL> } <EOL> } , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> completer = Completer ( <EOL> self . clidriver_creator . create_clidriver ( commands ) ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ '<STR_LIT:bar>' , '<STR_LIT>' ] ) <EOL> def test_complete_partial_service_commands ( self ) : <EOL> commands = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:foo>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] <EOL> } , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } <EOL> } <EOL> } <EOL> } , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> completer = Completer ( <EOL> self . clidriver_creator . create_clidriver ( commands ) ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ ] ) <EOL> def test_complete_service_arguments ( self ) : <EOL> commands = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:foo>' : { } <EOL> } , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> } <EOL> completer = Completer ( <EOL> self . clidriver_creator . create_clidriver ( commands ) ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_complete_partial_service_arguments ( self ) : <EOL> commands = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { } <EOL> } , <EOL> '<STR_LIT>' : [ '<STR_LIT:foo>' , '<STR_LIT:bar>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> } <EOL> completer = Completer ( <EOL> self . clidriver_creator . create_clidriver ( commands ) ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> def test_complete_service_arg_with_arg_already_used ( self ) : <EOL> commands = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { } <EOL> } , <EOL> '<STR_LIT>' : [ '<STR_LIT:foo>' , '<STR_LIT:bar>' ] <EOL> } <EOL> completer = Completer ( <EOL> self . clidriver_creator . create_clidriver ( commands ) ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ ] ) <EOL> def test_complete_operation_arguments ( self ) : <EOL> commands = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:foo>' : { '<STR_LIT>' : { <EOL> '<STR_LIT:bar>' : { '<STR_LIT>' : [ '<STR_LIT>' ] } <EOL> } } <EOL> } , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] <EOL> } <EOL> completer = Completer ( <EOL> self . clidriver_creator . create_clidriver ( commands ) ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_complete_partial_operation_arguments ( self ) : <EOL> commands = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:foo>' : { '<STR_LIT>' : { <EOL> '<STR_LIT:bar>' : { '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] } <EOL> } } <EOL> } , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] <EOL> } <EOL> completer = Completer ( <EOL> self . clidriver_creator . create_clidriver ( commands ) ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ ] ) <EOL> def test_complete_operation_arg_when_arg_already_used ( self ) : <EOL> commands = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:foo>' : { '<STR_LIT>' : { <EOL> '<STR_LIT:bar>' : { '<STR_LIT>' : [ '<STR_LIT>' ] } <EOL> } } <EOL> } , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> completer = Completer ( <EOL> self . clidriver_creator . create_clidriver ( commands ) ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ ] ) <EOL> def test_complete_positional_argument ( self ) : <EOL> commands = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:foo>' : { '<STR_LIT>' : { <EOL> '<STR_LIT:bar>' : { '<STR_LIT>' : [ <EOL> '<STR_LIT>' , <EOL> CustomArgument ( '<STR_LIT>' , positional_arg = True ) <EOL> ] } <EOL> } } <EOL> } , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> completer = Completer ( <EOL> self . clidriver_creator . create_clidriver ( commands ) ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ ] ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , <EOL> [ '<STR_LIT>' ] ) <EOL> def test_complete_undocumented_command ( self ) : <EOL> class UndocumentedCommand ( CLICommand ) : <EOL> _UNDOCUMENTED = True <EOL> commands = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:foo>' : { } , <EOL> '<STR_LIT:bar>' : UndocumentedCommand ( ) <EOL> } , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> completer = Completer ( <EOL> self . clidriver_creator . create_clidriver ( commands ) ) <EOL> self . assert_completion ( completer , '<STR_LIT>' , [ '<STR_LIT:foo>' ] ) <EOL> class TestCompleteCustomCommands ( BaseCompleterTest ) : <EOL> def setUp ( self ) : <EOL> super ( TestCompleteCustomCommands , self ) . setUp ( ) <EOL> custom_arguments = [ <EOL> { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' } <EOL> ] <EOL> custom_commands = [ <EOL> self . create_custom_command ( '<STR_LIT>' ) , <EOL> self . create_custom_command ( '<STR_LIT>' ) , <EOL> self . create_custom_command ( '<STR_LIT>' , arguments = custom_arguments ) <EOL> ] <EOL> custom_service = self . create_custom_command ( '<STR_LIT>' , custom_commands ) <EOL> clidriver = self . clidriver_creator . create_clidriver ( { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : custom_service [ '<STR_LIT>' ] ( mock . Mock ( ) ) , <EOL> '<STR_LIT:foo>' : { } <EOL> } , <EOL> '<STR_LIT>' : [ '<STR_LIT:bar>' ] <EOL> } ) <EOL> self . completer = Completer ( clidriver ) <EOL> def create_custom_command ( self , name , sub_commands = None , arguments = None ) : <EOL> arg_table = arguments <EOL> if arg_table is None : <EOL> arg_table = [ ] <EOL> subs = sub_commands <EOL> if subs is None : <EOL> subs = [ ] <EOL> class CustomCommand ( BasicCommand ) : <EOL> NAME = name <EOL> ARG_TABLE = arg_table <EOL> SUBCOMMANDS = subs <EOL> return { '<STR_LIT:name>' : name , '<STR_LIT>' : CustomCommand } <EOL> def test_complete_custom_service ( self ) : <EOL> self . assert_completion ( self . completer , '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT:foo>' ] ) <EOL> def test_complete_custom_command ( self ) : <EOL> self . assert_completion ( self . completer , '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_complete_partial_custom_command ( self ) : <EOL> self . assert_completion ( self . completer , '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_complete_custom_command_arguments ( self ) : <EOL> self . assert_completion ( self . completer , '<STR_LIT>' , [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_complete_custom_command_arguments_with_arg_already_used ( self ) : <EOL> self . assert_completion ( self . completer , '<STR_LIT>' , [ <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> class MockCLIDriverFactory ( object ) : <EOL> def create_clidriver ( self , commands = None , profiles = None ) : <EOL> session = mock . Mock ( ) <EOL> session . get_data . return_value = None <EOL> if profiles is not None and isinstance ( profiles , list ) : <EOL> session . available_profiles = profiles <EOL> else : <EOL> session . available_profiles = [ '<STR_LIT:default>' ] <EOL> clidriver = mock . Mock ( spec = CLIDriver ) <EOL> clidriver . create_help_command . return_value = self . _create_top_level_help ( commands , session ) <EOL> clidriver . session = session <EOL> return clidriver <EOL> def _create_top_level_help ( self , commands , session ) : <EOL> command_table = self . create_command_table ( <EOL> commands . get ( '<STR_LIT>' , { } ) , self . _create_service_command ) <EOL> argument_table = self . create_argument_table ( <EOL> commands . get ( '<STR_LIT>' , [ ] ) ) <EOL> return ProviderHelpCommand ( <EOL> session , command_table , argument_table , None , None , None ) <EOL> def _create_service_command ( self , name , command ) : <EOL> command_table = self . create_command_table ( <EOL> command . get ( '<STR_LIT>' , { } ) , self . _create_operation_command ) <EOL> service_command = ServiceCommand ( name , None ) <EOL> service_command . _service_model = { } <EOL> service_command . _command_table = command_table <EOL> return service_command <EOL> def _create_operation_command ( self , name , command ) : <EOL> argument_table = self . create_argument_table ( <EOL> command . get ( '<STR_LIT>' , [ ] ) ) <EOL> operation = ServiceOperation ( name , '<STR_LIT>' , None , { } , None ) <EOL> operation . _arg_table = argument_table <EOL> return operation <EOL> def create_command_table ( self , commands , command_creator ) : <EOL> if not commands : <EOL> return OrderedDict ( ) <EOL> command_table = OrderedDict ( ) <EOL> for name , command in commands . items ( ) : <EOL> if isinstance ( command , CLICommand ) : <EOL> command_table [ name ] = command <EOL> else : <EOL> command_table [ name ] = command_creator ( name , command ) <EOL> return command_table <EOL> def create_argument_table ( self , arguments ) : <EOL> if not arguments : <EOL> return OrderedDict ( ) <EOL> argument_table = OrderedDict ( ) <EOL> for arg in arguments : <EOL> if isinstance ( arg , BaseCLIArgument ) : <EOL> argument_table [ arg . name ] = arg <EOL> else : <EOL> argument_table [ arg ] = BaseCLIArgument ( arg ) <EOL> return argument_table </s>
