Skip to content

Commit 02ad0f3

Browse files
authored
Merge pull request #12401 from orf/handle-missing-completion-env
2 parents 4f3a184 + 082a6c6 commit 02ad0f3

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

news/12401.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix exception with completions when COMP_CWORD is not set

src/pip/_internal/cli/autocompletion.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ def autocomplete() -> None:
1717
# Don't complete if user hasn't sourced bash_completion file.
1818
if "PIP_AUTO_COMPLETE" not in os.environ:
1919
return
20+
# Don't complete if autocompletion environment variables
21+
# are not present
22+
if not os.environ.get("COMP_WORDS") or not os.environ.get("COMP_CWORD"):
23+
return
2024
cwords = os.environ["COMP_WORDS"].split()[1:]
2125
cword = int(os.environ["COMP_CWORD"])
2226
try:

tests/functional/test_completion.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,12 @@ def autocomplete_script(
120120

121121
class DoAutocomplete(Protocol):
122122
def __call__(
123-
self, words: str, cword: str, cwd: Union[Path, str, None] = None
123+
self,
124+
words: str,
125+
cword: str,
126+
cwd: Union[Path, str, None] = None,
127+
include_env: bool = True,
128+
expect_error: bool = True,
124129
) -> Tuple[TestPipResult, PipTestEnvironment]:
125130
...
126131

@@ -133,16 +138,21 @@ def autocomplete(
133138
autocomplete_script.environ["PIP_AUTO_COMPLETE"] = "1"
134139

135140
def do_autocomplete(
136-
words: str, cword: str, cwd: Union[Path, str, None] = None
141+
words: str,
142+
cword: str,
143+
cwd: Union[Path, str, None] = None,
144+
include_env: bool = True,
145+
expect_error: bool = True,
137146
) -> Tuple[TestPipResult, PipTestEnvironment]:
138-
autocomplete_script.environ["COMP_WORDS"] = words
139-
autocomplete_script.environ["COMP_CWORD"] = cword
147+
if include_env:
148+
autocomplete_script.environ["COMP_WORDS"] = words
149+
autocomplete_script.environ["COMP_CWORD"] = cword
140150
result = autocomplete_script.run(
141151
"python",
142152
"-c",
143153
"from pip._internal.cli.autocompletion import autocomplete;"
144154
"autocomplete()",
145-
expect_error=True,
155+
expect_error=expect_error,
146156
cwd=cwd,
147157
)
148158

@@ -160,6 +170,17 @@ def test_completion_for_unknown_shell(autocomplete_script: PipTestEnvironment) -
160170
assert error_msg in result.stderr, "tests for an unknown shell failed"
161171

162172

173+
def test_completion_without_env_vars(autocomplete: DoAutocomplete) -> None:
174+
"""
175+
Test getting completion <path> after options in command
176+
given absolute path
177+
"""
178+
res, env = autocomplete(
179+
words="pip install ", cword="", include_env=False, expect_error=False
180+
)
181+
assert res.stdout == "", "autocomplete function did not complete"
182+
183+
163184
def test_completion_alone(autocomplete_script: PipTestEnvironment) -> None:
164185
"""
165186
Test getting completion for none shell, just pip completion

0 commit comments

Comments
 (0)