Skip to content

Commit

Permalink
Merge pull request #5789 from EpicWink/master
Browse files Browse the repository at this point in the history
Fix error when autocompleting after flag
  • Loading branch information
pradyunsg committed Sep 18, 2018
2 parents eb30290 + b441c15 commit 7620b37
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions news/5751.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Avoid traceback printing on autocomplete after flags in the CLI.
3 changes: 2 additions & 1 deletion src/pip/_internal/cli/autocompletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ def get_path_completion_type(cwords, cword, opts):
continue
for o in str(opt).split('/'):
if cwords[cword - 2].split('=')[0] == o:
if any(x in ('path', 'file', 'dir')
if not opt.metavar or any(
x in ('path', 'file', 'dir')
for x in opt.metavar.split('/')):
return opt.metavar

Expand Down
24 changes: 24 additions & 0 deletions tests/functional/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,30 @@ def test_completion_not_files_after_option(script, data):
)


@pytest.mark.parametrize("cl_opts", ["-U", "--user", "-h"])
def test_completion_not_files_after_nonexpecting_option(script, data, cl_opts):
"""
Test not getting completion files after options which not applicable
(e.g. ``pip install``)
"""
res, env = setup_completion(
script=script,
words=('pip install %s r' % cl_opts),
cword='2',
cwd=data.completion_paths,
)
assert not any(out in res.stdout for out in
('requirements.txt', 'readme.txt',)), (
"autocomplete function completed <file> when "
"it should not complete"
)
assert not any(os.path.join(out, '') in res.stdout
for out in ('replay', 'resources')), (
"autocomplete function completed <dir> when "
"it should not complete"
)


def test_completion_directories_after_option(script, data):
"""
Test getting completion <dir> after options in command
Expand Down

0 comments on commit 7620b37

Please sign in to comment.