This commit is contained in:
Tom Forbes 2023-11-14 09:57:18 -05:00 committed by GitHub
commit 929d47c78a
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. # Don't complete if user hasn't sourced bash_completion file.
if "PIP_AUTO_COMPLETE" not in os.environ: if "PIP_AUTO_COMPLETE" not in os.environ:
return 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:] cwords = os.environ["COMP_WORDS"].split()[1:]
cword = int(os.environ["COMP_CWORD"]) cword = int(os.environ["COMP_CWORD"])
try: try:

View File

@ -14,7 +14,6 @@ else:
# dropping support for Python 3.7. # dropping support for Python 3.7.
Protocol = object Protocol = object
COMPLETION_FOR_SUPPORTED_SHELLS_TESTS = ( COMPLETION_FOR_SUPPORTED_SHELLS_TESTS = (
( (
"bash", "bash",
@ -128,7 +127,12 @@ def autocomplete_script(
class DoAutocomplete(Protocol): class DoAutocomplete(Protocol):
def __call__( 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]: ) -> Tuple[TestPipResult, PipTestEnvironment]:
... ...
@ -141,16 +145,21 @@ def autocomplete(
autocomplete_script.environ["PIP_AUTO_COMPLETE"] = "1" autocomplete_script.environ["PIP_AUTO_COMPLETE"] = "1"
def do_autocomplete( 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]: ) -> Tuple[TestPipResult, PipTestEnvironment]:
autocomplete_script.environ["COMP_WORDS"] = words if include_env:
autocomplete_script.environ["COMP_CWORD"] = cword autocomplete_script.environ["COMP_WORDS"] = words
autocomplete_script.environ["COMP_CWORD"] = cword
result = autocomplete_script.run( result = autocomplete_script.run(
"python", "python",
"-c", "-c",
"from pip._internal.cli.autocompletion import autocomplete;" "from pip._internal.cli.autocompletion import autocomplete;"
"autocomplete()", "autocomplete()",
expect_error=True, expect_error=expect_error,
cwd=cwd, 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" 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: def test_completion_alone(autocomplete_script: PipTestEnvironment) -> None:
""" """
Test getting completion for none shell, just pip completion Test getting completion for none shell, just pip completion