1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00
pip/tests/functional/test_completion.py

117 lines
3.3 KiB
Python
Raw Normal View History

import os
def test_completion_for_bash(script):
"""
Test getting completion for bash shell
"""
bash_completion = """\
_pip_completion()
{
COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]}" \\
COMP_CWORD=$COMP_CWORD \\
PIP_AUTO_COMPLETE=1 $1 ) )
}
complete -o default -F _pip_completion pip"""
result = script.pip('completion', '--bash')
2010-04-15 21:08:45 +02:00
assert bash_completion in result.stdout, 'bash completion is wrong'
def test_completion_for_zsh(script):
"""
Test getting completion for zsh shell
"""
zsh_completion = """\
function _pip_completion {
local words cword
read -Ac words
read -cn cword
reply=( $( COMP_WORDS="$words[*]" \\
COMP_CWORD=$(( cword-1 )) \\
PIP_AUTO_COMPLETE=1 $words[1] ) )
}
compctl -K _pip_completion pip"""
result = script.pip('completion', '--zsh')
2010-04-15 21:08:45 +02:00
assert zsh_completion in result.stdout, 'zsh completion is wrong'
def test_completion_for_fish(script):
"""
Test getting completion for fish shell
"""
fish_completion = """\
function __fish_complete_pip
set -lx COMP_WORDS (commandline -o) ""
set -lx COMP_CWORD (math (contains -i -- (commandline -t) $COMP_WORDS)-1)
set -lx PIP_AUTO_COMPLETE 1
string split \ -- (eval $COMP_WORDS[1])
end
complete -fa "(__fish_complete_pip)" -c pip"""
result = script.pip('completion', '--fish')
assert fish_completion in result.stdout, 'fish completion is wrong'
def test_completion_for_unknown_shell(script):
2010-04-15 21:08:45 +02:00
"""
Test getting completion for an unknown shell
"""
2012-09-01 21:21:30 +02:00
error_msg = 'no such option: --myfooshell'
result = script.pip('completion', '--myfooshell', expect_error=True)
2010-04-15 21:08:45 +02:00
assert error_msg in result.stderr, 'tests for an unknown shell failed'
def test_completion_alone(script):
"""
Test getting completion for none shell, just pip completion
"""
result = script.pip('completion', expect_error=True)
2012-12-14 14:53:02 +01:00
assert 'ERROR: You must pass --bash or --zsh' in result.stderr, \
'completion alone failed -- ' + result.stderr
def setup_completion(script, words, cword):
script.environ = os.environ.copy()
script.environ['PIP_AUTO_COMPLETE'] = '1'
script.environ['COMP_WORDS'] = words
script.environ['COMP_CWORD'] = cword
2012-12-14 14:53:02 +01:00
# expect_error is True because autocomplete exists with 1 status code
result = script.run(
'python', '-c', 'import pip;pip.autocomplete()',
expect_error=True,
)
2012-12-14 14:53:02 +01:00
return result, script
2012-12-14 14:53:02 +01:00
def test_completion_for_un_snippet(script):
2012-12-14 14:53:02 +01:00
"""
Test getting completion for ``un`` should return uninstall
2012-12-14 14:53:02 +01:00
"""
res, env = setup_completion(script, 'pip un', '1')
assert res.stdout.strip().split() == ['uninstall'], res.stdout
def test_completion_for_default_parameters(script):
"""
Test getting completion for ``--`` should contain --help
"""
2012-12-14 14:53:02 +01:00
res, env = setup_completion(script, 'pip --', '1')
2012-12-14 14:53:02 +01:00
assert '--help' in res.stdout,\
"autocomplete function could not complete ``--``"
def test_completion_option_for_command(script):
"""
Test getting completion for ``--`` in command (eg. pip search --)
"""
res, env = setup_completion(script, 'pip search --', '2')
assert '--help' in res.stdout,\
"autocomplete function could not complete ``--``"