This commit is contained in:
Tom Forbes 2023-11-29 08:33:59 +01:00 committed by GitHub
commit 3cc37f142a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 6 deletions

1
news/12401.bugfix.rst Normal file
View File

@ -0,0 +1 @@
Fix exception with completions when COMP_CWORD is not set

View File

@ -17,6 +17,10 @@ def autocomplete() -> None:
# Don't complete if user hasn't sourced bash_completion file.
if "PIP_AUTO_COMPLETE" not in os.environ:
return
# Don't complete if autocompletion environment variables
# are not present
if not os.environ.get("COMP_WORDS") or not os.environ.get("COMP_CWORD"):
return
cwords = os.environ["COMP_WORDS"].split()[1:]
cword = int(os.environ["COMP_CWORD"])
try:

View File

@ -14,7 +14,6 @@ else:
# dropping support for Python 3.7.
Protocol = object
COMPLETION_FOR_SUPPORTED_SHELLS_TESTS = (
(
"bash",
@ -128,7 +127,12 @@ def autocomplete_script(
class DoAutocomplete(Protocol):
def __call__(
self, words: str, cword: str, cwd: Union[Path, str, None] = None
self,
words: str,
cword: str,
cwd: Union[Path, str, None] = None,
include_env: bool = True,
expect_error: bool = True,
) -> Tuple[TestPipResult, PipTestEnvironment]:
...
@ -141,16 +145,21 @@ def autocomplete(
autocomplete_script.environ["PIP_AUTO_COMPLETE"] = "1"
def do_autocomplete(
words: str, cword: str, cwd: Union[Path, str, None] = None
words: str,
cword: str,
cwd: Union[Path, str, None] = None,
include_env: bool = True,
expect_error: bool = True,
) -> Tuple[TestPipResult, PipTestEnvironment]:
autocomplete_script.environ["COMP_WORDS"] = words
autocomplete_script.environ["COMP_CWORD"] = cword
if include_env:
autocomplete_script.environ["COMP_WORDS"] = words
autocomplete_script.environ["COMP_CWORD"] = cword
result = autocomplete_script.run(
"python",
"-c",
"from pip._internal.cli.autocompletion import autocomplete;"
"autocomplete()",
expect_error=True,
expect_error=expect_error,
cwd=cwd,
)
@ -168,6 +177,17 @@ def test_completion_for_unknown_shell(autocomplete_script: PipTestEnvironment) -
assert error_msg in result.stderr, "tests for an unknown shell failed"
def test_completion_without_env_vars(autocomplete: DoAutocomplete) -> None:
"""
Test getting completion <path> after options in command
given absolute path
"""
res, env = autocomplete(
words="pip install ", cword="", include_env=False, expect_error=False
)
assert res.stdout == "", "autocomplete function did not complete"
def test_completion_alone(autocomplete_script: PipTestEnvironment) -> None:
"""
Test getting completion for none shell, just pip completion