Skip to content

Commit

Permalink
Fix broken completions for parameters with no help and command group …
Browse files Browse the repository at this point in the history
…descriptions. (Azure#6191)

* complete parameters without help

* history

* descriptions for command-groups
  • Loading branch information
williexu authored Apr 24, 2018
1 parent 37db696 commit bac79c3
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 28 deletions.
4 changes: 3 additions & 1 deletion src/command_modules/azure-cli-interactive/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ Release History
0.3.20
++++++
* Allow interactive completers to function with positional arguments.
* More user-friendly output when users type '\'
* More user-friendly output when users type '\'.
* Fix completions for parameters with no help.
* Fix descriptions for command-groups.

0.3.19
++++++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,22 +285,23 @@ def generate_help_text(self):

rows, _ = get_window_dim()
rows = int(rows)
if not self.completer:
return param_descrip, example

param_args = self.completer.leftover_args
last_word = self.completer.unfinished_word
command = self.completer.current_command
new_command = ' '.join([command, last_word]).strip()
if not self.completer.complete_command and new_command in self.completer.command_param_info:

if not self.completer.complete_command and new_command in self.completer.command_description:
command = new_command

param = param_args[-1] if param_args else ''
param = last_word if last_word.startswith('-') else param
# get command/group help
if self.completer and command in self.completer.command_description:
self.description_docs = u'{}'.format(self.completer.command_description[command])

# get parameter help if full command
if self.completer and command in self.completer.command_param_info:
self.description_docs = u'{}'.format(
self.completer.command_description[command])
param = param_args[-1] if param_args else ''
param = last_word if last_word.startswith('-') else param

if param in self.completer.command_param_info[command] and self.completer.has_description(
command + " " + param):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(self, shell_ctx, commands, global_params=True):
self.started = False

# dictionary of command to descriptions
self.command_description = None
self.command_description = {}
# a list of all the possible parameters
self.completable_param = None
# the command tree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ def _gather_from_files(self, config):

command_params = data[command].get('parameters', {})
for param in command_params:
if command_params[param]['help'] and \
'==SUPPRESS==' not in command_params[param]['help']:
if '==SUPPRESS==' not in command_params[param]['help']:
param_aliases = set()

for par in command_params[param]['name']:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@


class CompletionTest(unittest.TestCase):
""" tests the completion generator """
def __init__(self, methodName):
super(CompletionTest, self).__init__(methodName)
with mock.patch.object(Configuration, 'get_help_files', lambda _: 'help_dump_test.json'):
with mock.patch.object(Configuration, 'get_config_dir', lambda _: TEST_DIR):
shell_ctx = AzInteractiveShell(TestCli(), None)
self.completer = shell_ctx.completer
self.shell_ctx = shell_ctx

# tests the completion generator
def verify_completions(self, generated_completions, expected_completions, start_position,
all_completions_expected=True, unexpected_completions=None):
for completion in generated_completions:
Expand All @@ -33,16 +41,7 @@ def verify_completions(self, generated_completions, expected_completions, start_
self.assertEqual(completion.start_position, start_position)
self.assertFalse(expected_completions)

def init_completer(self):
with mock.patch.object(Configuration, 'get_help_files', lambda _: 'help_dump_test.json'):
with mock.patch.object(Configuration, 'get_config_dir', lambda _: TEST_DIR):
shell_ctx = AzInteractiveShell(TestCli(), None)
self.completer = shell_ctx.completer

def test_command_completion(self):
# tests some azure commands
self.init_completer()

# initial completions
doc = Document(u' ')
gen = self.completer.get_completions(doc, None)
Expand Down Expand Up @@ -80,9 +79,6 @@ def test_command_completion(self):
self.verify_completions(gen, completions, 0)

def test_param_completion(self):
# tests some azure params
self.init_completer()

# 'az -h'
doc = Document(u'-')
gen = self.completer.get_completions(doc, None)
Expand Down Expand Up @@ -120,6 +116,12 @@ def test_param_completion(self):
completions = set(['--subnet', '--subnet-address-prefix'])
self.verify_completions(gen, completions, -8)

# test params with no help
doc = Document(u'vmss create --upgrade-policy-mo')
gen = self.completer.get_completions(doc, None)
completions = set(['--upgrade-policy-mode'])
self.verify_completions(gen, completions, -19)

# test duplicated parameters
expected = set()
doc = Document(u'vmss create --name Bob --n')
Expand All @@ -128,10 +130,10 @@ def test_param_completion(self):
self.verify_completions(gen, expected, -3, all_completions_expected=False, unexpected_completions=not_expected)

# test duplicated parameter alias
doc = Document(u'vmss create --name Bob -n')
doc = Document(u'vmss create --name Bob -')
gen = self.completer.get_completions(doc, None)
not_expected = set(['-n'])
self.verify_completions(gen, expected, -2, all_completions_expected=False, unexpected_completions=not_expected)
self.verify_completions(gen, expected, -1, all_completions_expected=False, unexpected_completions=not_expected)

# test displayed help
doc = Document(u'vm create -g')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,6 @@
parameters:
- name: --name -n
short-summary: The name of the log profile.
- name: --location -l
short-summary:
- name: --locations
short-summary: Space-separated list of regions for which Activity Log events should be stored.
- name: --categories
Expand Down

0 comments on commit bac79c3

Please sign in to comment.