Skip to content

Commit 740665c

Browse files
authored
Added 'expand environmental variables in path' feature (#96)
* Update _completer.py Added expand vars for Path-type -tab-completions * Added test cases for expand vars in Path type args * Update test_command_collection.py Fixed failing test cases * Update _completer.py Fixes failing tests * Update test_path_type.py Removed failing windows test
1 parent a068e82 commit 740665c

File tree

3 files changed

+36
-33
lines changed

3 files changed

+36
-33
lines changed

click_repl/_completer.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import unicode_literals
22

3-
import ntpath
43
import os
54
from glob import iglob
65

@@ -79,7 +78,7 @@ def _get_completion_from_autocompletion_functions(
7978
return param_choices
8079

8180
def _get_completion_from_choices_click_le_7(self, param, incomplete):
82-
if not getattr(param.type, "case_sensitive"):
81+
if not getattr(param.type, "case_sensitive", True):
8382
incomplete = incomplete.lower()
8483
return [
8584
Completion(
@@ -103,16 +102,15 @@ def _get_completion_from_choices_click_le_7(self, param, incomplete):
103102
]
104103

105104
def _get_completion_for_Path_types(self, param, args, incomplete):
106-
choices = []
107-
search_pattern = incomplete.strip("'\"\t\n\r\v ").replace("\\\\", "\\")
108-
109105
if "*" in incomplete:
110106
return []
111107

112-
search_pattern += "*"
108+
choices = []
109+
_incomplete = os.path.expandvars(incomplete)
110+
search_pattern = _incomplete.strip("'\"\t\n\r\v ").replace("\\\\", "\\") + "*"
113111
quote = ""
114112

115-
if " " in incomplete:
113+
if " " in _incomplete:
116114
for i in incomplete:
117115
if i in ("'", '"'):
118116
quote = i
@@ -133,7 +131,7 @@ def _get_completion_for_Path_types(self, param, args, incomplete):
133131
Completion(
134132
text_type(path),
135133
-len(incomplete),
136-
display=text_type(ntpath.basename(path.strip("'\""))),
134+
display=text_type(os.path.basename(path.strip("'\""))),
137135
)
138136
)
139137

@@ -267,6 +265,12 @@ def get_completions(self, document, complete_event=None):
267265
return
268266

269267
try:
268+
choices.extend(
269+
self._get_completion_for_cmd_args(
270+
ctx_command, incomplete, autocomplete_ctx, args
271+
)
272+
)
273+
270274
if isinstance(ctx_command, click.MultiCommand):
271275
incomplete_lower = incomplete.lower()
272276

@@ -284,13 +288,6 @@ def get_completions(self, document, complete_event=None):
284288
)
285289
)
286290

287-
else:
288-
choices.extend(
289-
self._get_completion_for_cmd_args(
290-
ctx_command, incomplete, autocomplete_ctx, args
291-
)
292-
)
293-
294291
except Exception as e:
295292
click.echo("{}: {}".format(type(e).__name__, str(e)))
296293

tests/test_command_collection.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ def c1(user):
4545
c = ClickCompleter(cli)
4646

4747

48-
@pytest.mark.parametrize("test_input,expected", [(" ", {"c1"}), ("c1 ", {"--user"})])
48+
@pytest.mark.parametrize("test_input,expected", [
49+
(" ", {'--user', 'c1'}),
50+
("c1 ", {"--user"})
51+
])
4952
def test_subcommand_invocation_from_group(test_input, expected):
5053
completions = list(c.get_completions(Document(test_input)))
5154
assert {x.text for x in completions} == expected
Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import click
22
from click_repl import ClickCompleter
33
from prompt_toolkit.document import Document
4+
import os
45
import glob
5-
import ntpath
66
import pytest
77

88

@@ -14,26 +14,29 @@ def root_command():
1414
c = ClickCompleter(root_command)
1515

1616

17-
@pytest.mark.parametrize(
18-
"test_input,expected",
19-
[
20-
("path-type-arg ", glob.glob("*")),
21-
("path-type-arg tests/", glob.glob("tests/*")),
22-
("path-type-arg click_repl/*", []),
23-
("path-type-arg click_repl/**", []),
24-
(
25-
"path-type-arg tests/testdir/",
26-
glob.glob("tests/testdir/*"),
27-
),
28-
],
29-
)
17+
@pytest.mark.parametrize("test_input,expected", [
18+
("path-type-arg ", glob.glob("*")),
19+
("path-type-arg tests/", glob.glob('tests/*')),
20+
("path-type-arg src/*", []),
21+
("path-type-arg src/**", []),
22+
(
23+
"path-type-arg tests/testdir/",
24+
glob.glob("tests/testdir/*"),
25+
)
26+
])
3027
def test_path_type_arg(test_input, expected):
3128
@root_command.command()
3229
@click.argument("path", type=click.Path())
3330
def path_type_arg(path):
3431
pass
3532

3633
completions = list(c.get_completions(Document(test_input)))
37-
assert {x.display[0][1] for x in completions} == set(
38-
ntpath.basename(i) for i in expected
39-
)
34+
assert {x.display[0][1] for x in completions} == {
35+
os.path.basename(i) for i in expected
36+
}
37+
38+
39+
# @pytest.mark.skipif(os.name != 'nt', reason='This is a test for Windows OS')
40+
# def test_win_path_env_expanders():
41+
# completions = list(c.get_completions(Document('path-type-arg %LocalAppData%')))
42+
# assert {x.display[0][1] for x in completions} == {'Local', 'LocalLow'}

0 commit comments

Comments
 (0)