-
Notifications
You must be signed in to change notification settings - Fork 409
Expand file tree
/
Copy pathtest_unit_compgen.py
More file actions
236 lines (205 loc) · 8.11 KB
/
test_unit_compgen.py
File metadata and controls
236 lines (205 loc) · 8.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import re
import pytest
from conftest import assert_bash_exec, assert_complete, bash_env_saved
@pytest.mark.bashcomp(cmd=None)
class TestUtilCompgen:
@pytest.fixture
def functions(self, bash):
assert_bash_exec(
bash,
"_comp__test_dump() { ((${#arr[@]})) && printf '<%s>' \"${arr[@]}\"; echo; }",
)
assert_bash_exec(
bash,
'_comp__test_compgen() { local -a arr=(00); _comp_compgen -v arr "$@"; _comp__test_dump; }',
)
assert_bash_exec(
bash,
'_comp__test_words() { local -a input=("${@:1:$#-1}"); _comp__test_compgen -c "${@:$#}" -- -W \'${input[@]+"${input[@]}"}\'; }',
)
assert_bash_exec(
bash,
'_comp__test_words_ifs() { local input=$2; _comp__test_compgen -F "$1" -c "${@:$#}" -- -W \'$input\'; }',
)
assert_bash_exec(
bash,
'_comp_cmd_fb() { _comp_compgen -c "$(_get_cword)" -C _filedir -- -f; }; '
"complete -F _comp_cmd_fb fb",
)
assert_bash_exec(
bash,
'_comp_cmd_fc() { _comp_compgen -c "$(_get_cword)" -C _filedir filedir; }; '
"complete -F _comp_cmd_fc fc; "
"complete -F _comp_cmd_fc -o filenames fc2",
)
assert_bash_exec(
bash,
'_comp_cmd_fcd() { _comp_compgen -c "$(_get_cword)" -C _filedir filedir -d; }; '
"complete -F _comp_cmd_fcd fcd",
)
# test_8_option_U
assert_bash_exec(
bash,
"_comp_compgen_gen8() { local -a arr=(x y z); _comp_compgen -U arr -- -W '\"${arr[@]}\"'; }",
)
# test_9_inherit_a
assert_bash_exec(
bash,
'_comp_compgen_gen9sub() { local -a gen=(00); _comp_compgen -v gen -- -W 11; _comp_compgen_set "${gen[@]}"; }; '
"_comp_compgen_gen9() { _comp_compgen_gen9sub; _comp_compgen -a gen9sub; }",
)
def test_1_basic(self, bash, functions):
output = assert_bash_exec(
bash, "_comp__test_words 12 34 56 ''", want_output=True
)
assert output.strip() == "<12><34><56>"
def test_2_space(self, bash, functions):
output = assert_bash_exec(
bash,
"_comp__test_words $'a b' $'c d\\t' ' e ' $'\\tf\\t' ''",
want_output=True,
)
assert output.strip() == "<a b><c d\t>< e ><\tf\t>"
def test_2_IFS(self, bash, functions):
with bash_env_saved(bash) as bash_env:
bash_env.write_variable("IFS", "34")
output = assert_bash_exec(
bash, "_comp__test_words 12 34 56 ''", want_output=True
)
assert output.strip() == "<12><34><56>"
def test_3_glob(self, bash, functions):
output = assert_bash_exec(
bash,
"_comp__test_words '*' '[a-z]*' '[a][b][c]' ''",
want_output=True,
)
assert output.strip() == "<*><[a-z]*><[a][b][c]>"
def test_3_failglob(self, bash, functions):
with bash_env_saved(bash) as bash_env:
bash_env.shopt("failglob", True)
output = assert_bash_exec(
bash,
"_comp__test_words '*' '[a-z]*' '[a][b][c]' ''",
want_output=True,
)
assert output.strip() == "<*><[a-z]*><[a][b][c]>"
def test_3_nullglob(self, bash, functions):
with bash_env_saved(bash) as bash_env:
bash_env.shopt("nullglob", True)
output = assert_bash_exec(
bash,
"_comp__test_words '*' '[a-z]*' '[a][b][c]' ''",
want_output=True,
)
assert output.strip() == "<*><[a-z]*><[a][b][c]>"
def test_4_empty(self, bash, functions):
output = assert_bash_exec(
bash, "_comp__test_words ''", want_output=True
)
assert output.strip() == ""
def test_5_option_F(self, bash, functions):
output = assert_bash_exec(
bash,
"_comp__test_words_ifs '25' ' 123 456 555 ' ''",
want_output=True,
)
assert output.strip() == "< 1><3 4><6 >< >"
def test_6_option_C_1(self, bash, functions):
output = assert_bash_exec(
bash,
"_comp__test_compgen -c a -C _filedir filedir",
want_output=True,
)
set1 = set(re.findall(r"<[^<>]*>", output.strip()))
assert set1 == {
"<a b/>",
"<a$b/>",
"<a&b/>",
"<a'b/>",
"<ab/>",
"<aé/>",
}
def test_6_option_C_2(self, bash, functions):
output = assert_bash_exec(
bash,
"_comp__test_compgen -c b -C _filedir -- -d",
want_output=True,
)
assert output.strip() == "<brackets>"
@pytest.mark.parametrize("funcname", "fc fc2".split())
def test_6_option_C_3(self, bash, functions, funcname):
completion = assert_complete(bash, "%s _filedir ab/" % funcname)
assert completion == "e"
@pytest.mark.complete(r"fcd a\ ")
def test_6_option_C_4(self, functions, completion):
# Note: we are not in the original directory that "b" exists, so Bash
# will not suffix a slash to the directory name.
assert completion == "b/"
@pytest.mark.complete(r"fb nonexistent")
def test_6_option_C_5(self, bash, functions, completion):
assert not completion
def test_7_icmd(self, bash, functions):
with bash_env_saved(bash) as bash_env:
bash_env.write_variable(
"BASH_COMPLETION_USER_DIR", "$PWD/_comp_compgen", quote=False
)
completions = assert_complete(bash, "compgen-cmd1 '")
assert completions == ["012", "123", "234", "5abc", "6def", "7ghi"]
def test_7_xcmd(self, bash, functions):
with bash_env_saved(bash) as bash_env:
bash_env.write_variable(
"BASH_COMPLETION_USER_DIR", "$PWD/_comp_compgen", quote=False
)
completions = assert_complete(bash, "compgen-cmd2 '")
assert completions == ["012", "123", "234", "5foo", "6bar", "7baz"]
def test_8_option_U(self, bash, functions):
output = assert_bash_exec(
bash, "_comp__test_compgen gen8", want_output=True
)
assert output.strip() == "<x><y><z>"
def test_9_inherit_a(self, bash, functions):
output = assert_bash_exec(
bash, "_comp__test_compgen gen9", want_output=True
)
assert output.strip() == "<11><11>"
def test_10_option_P_unmatching_prefix(self, bash, functions):
output = assert_bash_exec(
bash,
"_comp__test_compgen -c 'x' -P 'prefix,' -- -W 'alpha apple beta lemon'",
want_output=True,
)
assert output.strip() == ""
def test_10_option_P_incomplete_prefix(self, bash, functions):
output = assert_bash_exec(
bash,
"_comp__test_compgen -c 'pre' -P 'prefix,' -- -W 'alpha apple beta lemon'",
want_output=True,
)
assert (
output.strip()
== "<prefix,alpha><prefix,apple><prefix,beta><prefix,lemon>"
)
def test_10_option_P_exact_prefix(self, bash, functions):
output = assert_bash_exec(
bash,
"_comp__test_compgen -c 'prefix,' -P 'prefix,' -- -W 'alpha apple beta lemon'",
want_output=True,
)
assert (
output.strip()
== "<prefix,alpha><prefix,apple><prefix,beta><prefix,lemon>"
)
def test_10_option_P_starts_with_prefix(self, bash, functions):
output = assert_bash_exec(
bash,
"_comp__test_compgen -c 'prefix,a' -P 'prefix,' -- -W 'alpha apple beta lemon'",
want_output=True,
)
assert output.strip() == "<prefix,alpha><prefix,apple>"
def test_10_option_P_no_match(self, bash, functions):
output = assert_bash_exec(
bash,
"_comp__test_compgen -c 'prefix,x' -P 'prefix,' -- -W 'alpha apple beta lemon'",
want_output=True,
)
assert output.strip() == ""