def test_returns_all_results_when_current_fragment_empty(self): parsed = self.parser.parse('aws ') expected = [ # The -1 is because we need to replace the string starting # 1 character back (the last fragment is the string 'e'). CompletionResult('ec2', starting_index=0), CompletionResult('ecs', starting_index=0), CompletionResult('s3', starting_index=0), ] self.assertEqual(self.completer.complete(parsed), expected)
def test_delegates_to_autocompleters(self): mock_complete = mock.Mock(spec=completer.BaseCompleter) expected = [CompletionResult('ec2', -1), CompletionResult('ecs', -1)] mock_complete.complete.return_value = expected auto_complete = completer.AutoCompleter(self.parser, completers=[mock_complete]) results = auto_complete.autocomplete('aws e') self.assertEqual(results, expected) self.parser.parse.assert_called_with('aws e', None) mock_complete.complete.assert_called_with(self.parsed_result)
def test_can_prefix_match_results(self): self.mock_client.list_policies.return_value = { 'Policies': [{'PolicyName': 'a1'}, {'PolicyName': 'b1'}, {'PolicyName': 'a2'}, {'PolicyName': 'b2'}], } parsed = self.parser.parse( 'aws iam delete-user-policy --policy-name a') results = self.completer.complete(parsed) self.assertEqual( results, [CompletionResult('a1', -1), CompletionResult('a2', -1)] )
def test_returns_all_results_when_current_fragment_empty(self): self.mock_client.list_policies.return_value = { 'Policies': [{'PolicyName': 'a1'}, {'PolicyName': 'b1'}, {'PolicyName': 'a2'}, {'PolicyName': 'b2'}], } parsed = self.parser.parse('aws iam delete-user-policy --policy-name ') results = self.completer.complete(parsed) self.assertEqual( results, [CompletionResult('a1', 0), CompletionResult('b1', 0), CompletionResult('a2', 0), CompletionResult('b2', 0)], )
def test_first_result_wins(self): first = mock.Mock(spec=completer.BaseCompleter) second = mock.Mock(spec=completer.BaseCompleter) first.complete.return_value = [CompletionResult('ec2', -1)] second.complete.return_value = [CompletionResult('ecs', -1)] auto_complete = completer.AutoCompleter(self.parser, completers=[first, second]) self.assertEqual(auto_complete.autocomplete('aws e'), [CompletionResult('ec2', -1)]) first.complete.assert_called_with(self.parsed_result) self.assertFalse(second.complete.called)
def test_complete_table_name_with_put(self): self.mock_client.list_tables.return_value = { 'TableNames': [ 'tablename', 'mytable' ] } parsed = self.parser.parse('aws ddb put ') results = self.completer.complete(parsed) self.assertEqual( results, [CompletionResult('tablename', 0), CompletionResult('mytable', 0)] )
def test_can_prefix_match_services(self): parsed = parser.ParsedResult( current_command='aws', lineage=[], current_fragment='e', ) parsed = self.parser.parse('aws e') expected = [ # The -1 is because we need to replace the string starting # 1 character back (the last fragment is the string 'e'). CompletionResult('ec2', starting_index=-1), CompletionResult('ecs', starting_index=-1), ] self.assertEqual(self.completer.complete(parsed), expected)
def test_stops_processing_when_list_returned(self): first = mock.Mock(spec=completer.BaseCompleter) second = mock.Mock(spec=completer.BaseCompleter) first.complete.return_value = None expected = [CompletionResult('ec2', -1), CompletionResult('ecs', -1)] second.complete.return_value = expected auto_complete = completer.AutoCompleter(self.parser, completers=[first, second]) self.assertEqual(auto_complete.autocomplete('aws e'), expected) first.complete.assert_called_with(self.parsed_result) second.complete.assert_called_with(self.parsed_result)
def test_complete_group_name(self): self.mock_client.describe_log_groups.return_value = { 'logGroups': [ { 'logGroupName': 'group' }, { 'logGroupName': 'mygroup' }, ] } parsed = self.parser.parse('aws logs tail ') results = self.completer.complete(parsed) self.assertEqual( results, [CompletionResult('group', 0), CompletionResult('mygroup', 0)])
def _complete_command(self, parsed): lineage = parsed.lineage + [parsed.current_command] offset = -len(parsed.current_fragment) result = [ CompletionResult(name, starting_index=offset) for name in self._index.command_names(lineage) if name.startswith(parsed.current_fragment) ] return result
def complete(self, parsed): if not self._is_value_for_param(parsed): return remote_results = self._get_remote_results(parsed) completion_results = [] for remote_result in remote_results: if remote_result.startswith(parsed.current_fragment): completion_results.append( CompletionResult( remote_result, -len(parsed.current_fragment)) ) return completion_results
def _inject_global_params_if_needed(self, parsed, results, fragment): is_in_global_scope = (parsed.lineage == [] and parsed.current_command == 'aws') if not is_in_global_scope: offset = -len(parsed.current_fragment) global_params = self._index.arg_names(lineage=[], command_name='aws') global_param_completions = [ CompletionResult('--%s' % arg_name, starting_index=offset) for arg_name in global_params if arg_name.startswith(fragment) ] results.extend(global_param_completions)
def test_complete_group_name_filters_startswith(self): self.mock_client.list_tables.return_value = { 'TableNames': [ 'tablename', 'mytable' ] } parsed = self.parser.parse('aws ddb select my') results = self.completer.complete(parsed) self.assertEqual( results, [CompletionResult('mytable', -2)] )
def _complete_options(self, parsed): # '--endpoint' -> 'endpoint' offset = -len(parsed.current_fragment) fragment = parsed.current_fragment[2:] arg_names = self._index.arg_names(lineage=parsed.lineage, command_name=parsed.current_command) results = [ CompletionResult('--%s' % arg_name, starting_index=offset) for arg_name in arg_names if arg_name.startswith(fragment) ] # Global params apply to any scope, so if we're not # in the global scope, we need to add completions for # global params self._inject_global_params_if_needed(parsed, results, fragment) return results
def _convert_to_completion_data(self, raw_results, parsed): index = -len(parsed.current_fragment) return [CompletionResult(r, index) for r in raw_results if r.startswith(parsed.current_fragment)]
def test_can_combine_global_and_command_params(self): parsed = self.parser.parse('aws ec2 describe-instances --r') self.assertEqual(self.completer.complete(parsed), [ CompletionResult('--reserve', -3), CompletionResult('--region', -3) ])
def test_can_autocomplete_global_param(self): parsed = self.parser.parse('aws --re') self.assertEqual(self.completer.complete(parsed), [CompletionResult('--region', -4)])