/
test_completer.py
164 lines (136 loc) · 6.16 KB
/
test_completer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from awscli.testutils import unittest, mock
from awscli.autocomplete import completer, parser
from awscli.autocomplete.local import basic
from awscli.autocomplete.completer import CompletionResult
from tests.unit.autocomplete import InMemoryIndex
class TestAutoCompleter(unittest.TestCase):
def setUp(self):
self.parser = mock.Mock(spec=parser.CLIParser)
self.parsed_result = parser.ParsedResult()
self.parser.parse.return_value = self.parsed_result
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_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_returns_empty_list_if_no_completers_have_results(self):
first = mock.Mock(spec=completer.BaseCompleter)
second = mock.Mock(spec=completer.BaseCompleter)
first.complete.return_value = None
second.complete.return_value = None
auto_complete = completer.AutoCompleter(
self.parser, completers=[first, second])
self.assertEqual(auto_complete.autocomplete('aws e'), [])
first.complete.assert_called_with(self.parsed_result)
second.complete.assert_called_with(self.parsed_result)
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)
class TestModelIndexCompleter(unittest.TestCase):
def setUp(self):
self.index = InMemoryIndex({
'command_names': {
'': ['aws'],
'aws': ['ec2', 'ecs', 's3'],
'aws.ec2': ['describe-instances'],
},
'arg_names': {
'': {
'aws': ['region', 'endpoint-url'],
},
'aws.ec2': {
'describe-instances': ['instance-ids', 'reserve'],
}
},
})
self.parser = parser.CLIParser(self.index)
self.completer = basic.ModelIndexCompleter(self.index)
def test_does_not_complete_if_unparsed_items(self):
parsed = self.parser.parse('aws foo ')
self.assertIsNone(self.completer.complete(parsed))
def test_does_complete_if_current_fragment_is_none(self):
parsed = self.parser.parse('aws')
self.assertIsNone(self.completer.complete(parsed))
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_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_can_autocomplete_global_param(self):
parsed = self.parser.parse('aws --re')
self.assertEqual(
self.completer.complete(parsed),
[CompletionResult('--region', -4)]
)
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_no_autocompletions_if_nothing_matches(self):
parsed = self.parser.parse('aws --foo')
self.assertEqual(self.completer.complete(parsed), [])