def test_completions(): completer = Completer() for cmdline, point, expected_results in COMPLETIONS: if point == -1: point = len(cmdline) results = set(completer.complete(cmdline, point)) yield check_completer, cmdline, results, expected_results
def test_complete_top_level_args(self): commands = { 'subcommands': {}, 'arguments': ['foo', 'bar'] } completer = Completer( self.clidriver_creator.create_clidriver(commands)) self.assert_completion(completer, 'aws --', ['--foo', '--bar'])
def test_complete_service_arg_with_arg_already_used(self): commands = { 'subcommands': { 'baz': {} }, 'arguments': ['foo', 'bar'] } completer = Completer( self.clidriver_creator.create_clidriver(commands)) self.assert_completion(completer, 'aws baz --foo --f', [])
def test_complete_service_arguments(self): commands = { 'subcommands': { 'foo': {} }, 'arguments': ['baz', 'bin'] } completer = Completer( self.clidriver_creator.create_clidriver(commands)) self.assert_completion(completer, 'aws foo --', ['--baz', '--bin'])
def test_complete_operation_arg_when_arg_already_used(self): commands = { 'subcommands': { 'foo': {'subcommands': { 'bar': {'arguments': ['baz']} }} }, 'arguments': [] } completer = Completer( self.clidriver_creator.create_clidriver(commands)) self.assert_completion(completer, 'aws foo bar --baz --b', [])
def test_complete_undocumented_command(self): class UndocumentedCommand(CLICommand): _UNDOCUMENTED = True commands = { 'subcommands': { 'foo': {}, 'bar': UndocumentedCommand() }, 'arguments': [] } completer = Completer( self.clidriver_creator.create_clidriver(commands)) self.assert_completion(completer, 'aws ', ['foo'])
def test_complete_partial_service_arguments(self): commands = { 'subcommands': { 'biz': {} }, 'arguments': ['foo', 'bar', 'foobar', 'fubar'] } completer = Completer( self.clidriver_creator.create_clidriver(commands)) self.assert_completion(completer, 'aws biz --f', [ '--foo', '--fubar', '--foobar']) self.assert_completion(completer, 'aws biz --fo', [ '--foo', '--foobar']) self.assert_completion(completer, 'aws biz --foob', ['--foobar'])
def test_completions(): environ = { 'AWS_DATA_PATH': os.environ['AWS_DATA_PATH'], 'AWS_DEFAULT_REGION': 'us-east-1', 'AWS_ACCESS_KEY_ID': 'access_key', 'AWS_SECRET_ACCESS_KEY': 'secret_key', 'AWS_CONFIG_FILE': '', } with mock.patch('os.environ', environ): completer = Completer() for cmdline, point, expected_results in COMPLETIONS: if point == -1: point = len(cmdline) results = set(completer.complete(cmdline, point)) yield check_completer, cmdline, results, expected_results
def test_complete_on_invalid_service(self): commands = { 'subcommands': { 'foo': {}, 'bar': { 'subcommands': { 'baz': {} } } }, 'arguments': [] } completer = Completer( self.clidriver_creator.create_clidriver(commands)) self.assert_completion(completer, 'aws bin', [])
def test_complete_positional_argument(self): commands = { 'subcommands': { 'foo': {'subcommands': { 'bar': {'arguments': [ 'baz', CustomArgument('bin', positional_arg=True) ]} }} }, 'arguments': [] } completer = Completer( self.clidriver_creator.create_clidriver(commands)) self.assert_completion(completer, 'aws foo bar --bin ', []) self.assert_completion(completer, 'aws foo bar --bin blah --', ['--baz'])
def test_complete_partial_operation_arguments(self): commands = { 'subcommands': { 'foo': {'subcommands': { 'bar': {'arguments': ['base', 'baz', 'air']} }} }, 'arguments': ['bin'] } completer = Completer( self.clidriver_creator.create_clidriver(commands)) self.assert_completion(completer, 'aws foo bar --b', [ '--base', '--baz', '--bin']) self.assert_completion(completer, 'aws foo bar --ba', [ '--base', '--baz']) self.assert_completion(completer, 'aws foo bar --bas', ['--base']) self.assert_completion(completer, 'aws foo bar --base', [])
def setUp(self): super(TestCompleteCustomCommands, self).setUp() custom_arguments = [{'name': 'recursive'}, {'name': 'sse'}] custom_commands = [ self.create_custom_command('mb'), self.create_custom_command('mv'), self.create_custom_command('cp', arguments=custom_arguments) ] custom_service = self.create_custom_command('s3', custom_commands) clidriver = self.clidriver_creator.create_clidriver({ 'subcommands': { 's3': custom_service['command_class'](mock.Mock()), 'foo': {} }, 'arguments': ['bar'] }) self.completer = Completer(clidriver)
def test_complete_partial_service_name(self): commands = { 'subcommands': { 'cloudfront': {}, 'cloudformation': {}, 'cloudhsm': {}, 'sts': {} }, 'arguments': [] } completer = Completer( self.clidriver_creator.create_clidriver(commands)) self.assert_completion(completer, 'aws cloud', [ 'cloudfront', 'cloudformation', 'cloudhsm']) self.assert_completion(completer, 'aws cloudf', [ 'cloudfront', 'cloudformation']) self.assert_completion(completer, 'aws cloudfr', ['cloudfront']) self.assert_completion(completer, 'aws cloudfront', [])
def start_server(ns): clients = [] sock = socket() try: sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) sock.bind((ns.address, ns.port)) sock.listen(5) completer = Completer() ns.logfile = None if ns.log: ns.logfile = open(ns.log, 'a') while True: c, a = sock.accept() log(ns, f'New client from {a}') clients.append(c) ClientThread(c, a, completer, ns).start() except: if ns.logfile: try: traceback.print_exc(file=ns.logfile) traceback.print_stack(file=ns.logfile) except: traceback.print_exc() traceback.print_stack() finally: for c in clients: try: c.shutdown(SHUT_RDWR) c.close() except: pass try: sock.shutdown(SHUT_RDWR) sock.close() except: pass if ns.log: try: ns.logfile.close() except: pass
def test_complete_partial_service_commands(self): commands = { 'subcommands': { 'foo': { 'subcommands': { 'barb': { 'arguments': ['nil'] }, 'baz': {}, 'biz': {}, 'foobar': {} } } }, 'arguments': [] } completer = Completer( self.clidriver_creator.create_clidriver(commands)) self.assert_completion(completer, 'aws foo b', ['barb', 'baz', 'biz']) self.assert_completion(completer, 'aws foo ba', ['barb', 'baz']) self.assert_completion(completer, 'aws foo bar', ['barb']) self.assert_completion(completer, 'aws foo barb', [])