Skip to content

Commit 0d46ee3

Browse files
committed
Handle case where autocompleting empty string crashes
Fixes awslabs#39.
1 parent 2ef8977 commit 0d46ee3

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

awsshell/autocomplete.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ def autocomplete(self, line):
4949
# The user has hit backspace. We'll need to check
5050
# the current words.
5151
return self._handle_backspace()
52+
elif not line:
53+
return []
5254
elif current_length != self._last_position + 1:
5355
return self._complete_from_full_parse()
5456

5557
# This position is important. We only update the _last_position
5658
# after we've checked the special cases above where that value
5759
# matters.
5860
self._last_position = len(line)
59-
if not line:
60-
return []
6161
if line and not line.strip():
6262
# Special case, the user hits a space on a new line so
6363
# we autocomplete all the top level commands.

tests/unit/test_autocomplete.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,20 @@ def test_last_option_is_updated_on_global_options(index_data):
337337
assert completer.last_option == '--resources'
338338
completer.autocomplete('ec2 create-tags --resources f --no-sign-request ')
339339
assert completer.last_option == '--no-sign-request'
340+
341+
342+
def test_can_handle_autocompleting_same_string_twice(index_data):
343+
index_data['aws']['commands'] = ['first', 'second']
344+
completer = AWSCLIModelCompleter(index_data)
345+
completer.autocomplete('f')
346+
assert completer.autocomplete('f') == ['first']
347+
348+
349+
def test_can_handle_autocomplete_empty_string_twice(index_data):
350+
# Sometimes prompt_toolkit will try to autocomplete
351+
# the empty string multiple times. We need to handle this
352+
# gracefully.
353+
index_data['aws']['commands'] = ['first', 'second']
354+
completer = AWSCLIModelCompleter(index_data)
355+
assert completer.autocomplete('') == []
356+
assert completer.autocomplete('') == []

0 commit comments

Comments
 (0)