123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- import os
- import sys
- import pytest
- 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')
- 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')
- 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):
- """
- Test getting completion for an unknown shell
- """
- error_msg = 'no such option: --myfooshell'
- result = script.pip('completion', '--myfooshell', expect_error=True)
- 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)
- assert 'ERROR: You must pass --bash or --fish or --zsh' in result.stderr, \
- 'completion alone failed -- ' + result.stderr
- def setup_completion(script, words, cword, cwd=None):
- script.environ = os.environ.copy()
- script.environ['PIP_AUTO_COMPLETE'] = '1'
- script.environ['COMP_WORDS'] = words
- script.environ['COMP_CWORD'] = cword
- # expect_error is True because autocomplete exists with 1 status code
- result = script.run(
- 'python', '-c', 'import pip._internal;pip._internal.autocomplete()',
- expect_error=True,
- cwd=cwd,
- )
- return result, script
- def test_completion_for_un_snippet(script):
- """
- Test getting completion for ``un`` should return uninstall
- """
- 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
- """
- res, env = setup_completion(script, 'pip --', '1')
- assert '--help' in res.stdout,\
- "autocomplete function could not complete ``--``"
- def test_completion_option_for_command(script):
- """
- Test getting completion for ``--`` in command (e.g. ``pip search --``)
- """
- res, env = setup_completion(script, 'pip search --', '2')
- assert '--help' in res.stdout,\
- "autocomplete function could not complete ``--``"
- def test_completion_short_option(script):
- """
- Test getting completion for short options after ``-`` (eg. pip -)
- """
- res, env = setup_completion(script, 'pip -', '1')
- assert '-h' in res.stdout.split(),\
- "autocomplete function could not complete short options after ``-``"
- def test_completion_short_option_for_command(script):
- """
- Test getting completion for short options after ``-`` in command
- (eg. pip search -)
- """
- res, env = setup_completion(script, 'pip search -', '2')
- assert '-h' in res.stdout.split(),\
- "autocomplete function could not complete short options after ``-``"
- def test_completion_files_after_option(script, data):
- """
- Test getting completion for <file> or <dir> after options in command
- (e.g. ``pip install -r``)
- """
- res, env = setup_completion(
- script=script,
- words=('pip install -r r'),
- cword='3',
- cwd=data.completion_paths,
- )
- assert 'requirements.txt' in res.stdout, (
- "autocomplete function could not complete <file> "
- "after options in command"
- )
- assert os.path.join('resources', '') in res.stdout, (
- "autocomplete function could not complete <dir> "
- "after options in command"
- )
- assert not any(out in res.stdout for out in
- (os.path.join('REPLAY', ''), 'README.txt')), (
- "autocomplete function completed <file> or <dir> that "
- "should not be completed"
- )
- if sys.platform != 'win32':
- return
- assert 'readme.txt' in res.stdout, (
- "autocomplete function could not complete <file> "
- "after options in command"
- )
- assert os.path.join('replay', '') in res.stdout, (
- "autocomplete function could not complete <dir> "
- "after options in command"
- )
- def test_completion_not_files_after_option(script, data):
- """
- Test not getting completion files after options which not applicable
- (e.g. ``pip install``)
- """
- res, env = setup_completion(
- script=script,
- words=('pip install r'),
- 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
- (e.g. ``pip --cache-dir``)
- """
- res, env = setup_completion(
- script=script,
- words=('pip --cache-dir r'),
- cword='2',
- cwd=data.completion_paths,
- )
- assert os.path.join('resources', '') in res.stdout, (
- "autocomplete function could not complete <dir> after options"
- )
- assert not any(out in res.stdout for out in (
- 'requirements.txt', 'README.txt', os.path.join('REPLAY', ''))), (
- "autocomplete function completed <dir> when "
- "it should not complete"
- )
- if sys.platform == 'win32':
- assert os.path.join('replay', '') in res.stdout, (
- "autocomplete function could not complete <dir> after options"
- )
- def test_completion_subdirectories_after_option(script, data):
- """
- Test getting completion <dir> after options in command
- given path of a directory
- """
- res, env = setup_completion(
- script=script,
- words=('pip --cache-dir ' + os.path.join('resources', '')),
- cword='2',
- cwd=data.completion_paths,
- )
- assert os.path.join('resources',
- os.path.join('images', '')) in res.stdout, (
- "autocomplete function could not complete <dir> "
- "given path of a directory after options"
- )
- def test_completion_path_after_option(script, data):
- """
- Test getting completion <path> after options in command
- given absolute path
- """
- res, env = setup_completion(
- script=script,
- words=('pip install -e ' + os.path.join(data.completion_paths, 'R')),
- cword='3',
- )
- assert all(os.path.normcase(os.path.join(data.completion_paths, out))
- in res.stdout for out in (
- 'README.txt', os.path.join('REPLAY', ''))), (
- "autocomplete function could not complete <path> "
- "after options in command given absolute path"
- )
- @pytest.mark.parametrize('flag', ['--bash', '--zsh', '--fish'])
- def test_completion_uses_same_executable_name(script, flag):
- expect_stderr = sys.version_info[:2] == (3, 3)
- executable_name = 'pip{}'.format(sys.version_info[0])
- result = script.run(
- executable_name, 'completion', flag, expect_stderr=expect_stderr
- )
- assert executable_name in result.stdout
|