]>
Commit | Line | Data |
---|---|---|
5c293a6b FC |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2012 Felipe Contreras | |
4 | # | |
5 | ||
5c293a6b FC |
6 | test_description='test bash completion' |
7 | ||
f8891cfa | 8 | . ./lib-bash.sh |
5c293a6b FC |
9 | |
10 | complete () | |
11 | { | |
12 | # do nothing | |
13 | return 0 | |
14 | } | |
15 | ||
16 | . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" | |
17 | ||
18 | # We don't need this function to actually join words or do anything special. | |
19 | # Also, it's cleaner to avoid touching bash's internal completion variables. | |
20 | # So let's override it with a minimal version for testing purposes. | |
21 | _get_comp_words_by_ref () | |
22 | { | |
23 | while [ $# -gt 0 ]; do | |
24 | case "$1" in | |
25 | cur) | |
26 | cur=${_words[_cword]} | |
27 | ;; | |
28 | prev) | |
29 | prev=${_words[_cword-1]} | |
30 | ;; | |
31 | words) | |
32 | words=("${_words[@]}") | |
33 | ;; | |
34 | cword) | |
35 | cword=$_cword | |
36 | ;; | |
37 | esac | |
38 | shift | |
39 | done | |
40 | } | |
41 | ||
42 | print_comp () | |
43 | { | |
44 | local IFS=$'\n' | |
45 | echo "${COMPREPLY[*]}" > out | |
46 | } | |
47 | ||
48 | run_completion () | |
49 | { | |
50 | local -a COMPREPLY _words | |
51 | local _cword | |
52 | _words=( $1 ) | |
53 | (( _cword = ${#_words[@]} - 1 )) | |
93b291e0 | 54 | __git_wrap__git_main && print_comp |
5c293a6b FC |
55 | } |
56 | ||
701ecdf1 FC |
57 | # Test high-level completion |
58 | # Arguments are: | |
59 | # 1: typed text so far (cur) | |
60 | # 2: expected completion | |
5c293a6b FC |
61 | test_completion () |
62 | { | |
63 | test $# -gt 1 && echo "$2" > expected | |
701ecdf1 | 64 | run_completion "$1" && |
5c293a6b FC |
65 | test_cmp expected out |
66 | } | |
67 | ||
49ba92b4 | 68 | # Like test_completion, but reads expectation from stdin, |
43ea0812 | 69 | # which is convenient when it is multiline. |
49ba92b4 JK |
70 | test_completion_long () |
71 | { | |
43ea0812 | 72 | sed -e 's/Z$//' >expected && |
49ba92b4 JK |
73 | test_completion "$1" |
74 | } | |
75 | ||
74a8c849 SG |
76 | newline=$'\n' |
77 | ||
78 | test_expect_success '__gitcomp - trailing space - options' ' | |
79 | sed -e "s/Z$//" >expected <<-\EOF && | |
80 | --reuse-message=Z | |
81 | --reedit-message=Z | |
82 | --reset-author Z | |
83 | EOF | |
84 | ( | |
85 | local -a COMPREPLY && | |
86 | cur="--re" && | |
87 | __gitcomp "--dry-run --reuse-message= --reedit-message= | |
88 | --reset-author" && | |
89 | IFS="$newline" && | |
90 | echo "${COMPREPLY[*]}" > out | |
91 | ) && | |
92 | test_cmp expected out | |
93 | ' | |
94 | ||
95 | test_expect_success '__gitcomp - trailing space - config keys' ' | |
96 | sed -e "s/Z$//" >expected <<-\EOF && | |
97 | branch.Z | |
98 | branch.autosetupmerge Z | |
99 | branch.autosetuprebase Z | |
100 | browser.Z | |
101 | EOF | |
102 | ( | |
103 | local -a COMPREPLY && | |
104 | cur="br" && | |
105 | __gitcomp "branch. branch.autosetupmerge | |
106 | branch.autosetuprebase browser." && | |
107 | IFS="$newline" && | |
108 | echo "${COMPREPLY[*]}" > out | |
109 | ) && | |
110 | test_cmp expected out | |
111 | ' | |
112 | ||
113 | test_expect_success '__gitcomp - option parameter' ' | |
114 | sed -e "s/Z$//" >expected <<-\EOF && | |
115 | recursive Z | |
116 | resolve Z | |
117 | EOF | |
118 | ( | |
119 | local -a COMPREPLY && | |
120 | cur="--strategy=re" && | |
121 | __gitcomp "octopus ours recursive resolve subtree | |
122 | " "" "re" && | |
123 | IFS="$newline" && | |
124 | echo "${COMPREPLY[*]}" > out | |
125 | ) && | |
126 | test_cmp expected out | |
127 | ' | |
128 | ||
129 | test_expect_success '__gitcomp - prefix' ' | |
130 | sed -e "s/Z$//" >expected <<-\EOF && | |
131 | branch.maint.merge Z | |
132 | branch.maint.mergeoptions Z | |
133 | EOF | |
134 | ( | |
135 | local -a COMPREPLY && | |
136 | cur="branch.me" && | |
137 | __gitcomp "remote merge mergeoptions rebase | |
138 | " "branch.maint." "me" && | |
139 | IFS="$newline" && | |
140 | echo "${COMPREPLY[*]}" > out | |
141 | ) && | |
142 | test_cmp expected out | |
143 | ' | |
144 | ||
145 | test_expect_success '__gitcomp - suffix' ' | |
146 | sed -e "s/Z$//" >expected <<-\EOF && | |
147 | branch.master.Z | |
148 | branch.maint.Z | |
149 | EOF | |
150 | ( | |
151 | local -a COMPREPLY && | |
152 | cur="branch.me" && | |
153 | __gitcomp "master maint next pu | |
154 | " "branch." "ma" "." && | |
155 | IFS="$newline" && | |
156 | echo "${COMPREPLY[*]}" > out | |
157 | ) && | |
158 | test_cmp expected out | |
159 | ' | |
160 | ||
5c293a6b FC |
161 | test_expect_success 'basic' ' |
162 | run_completion "git \"\"" && | |
163 | # built-in | |
164 | grep -q "^add \$" out && | |
165 | # script | |
166 | grep -q "^filter-branch \$" out && | |
167 | # plumbing | |
168 | ! grep -q "^ls-files \$" out && | |
169 | ||
170 | run_completion "git f" && | |
171 | ! grep -q -v "^f" out | |
172 | ' | |
173 | ||
174 | test_expect_success 'double dash "git" itself' ' | |
a1be444d | 175 | test_completion_long "git --" <<-\EOF |
5c293a6b FC |
176 | --paginate Z |
177 | --no-pager Z | |
178 | --git-dir= | |
179 | --bare Z | |
180 | --version Z | |
181 | --exec-path Z | |
3ffcd086 | 182 | --exec-path= |
5c293a6b | 183 | --html-path Z |
69ef3c02 | 184 | --info-path Z |
5c293a6b FC |
185 | --work-tree= |
186 | --namespace= | |
69ef3c02 | 187 | --no-replace-objects Z |
5c293a6b FC |
188 | --help Z |
189 | EOF | |
5c293a6b FC |
190 | ' |
191 | ||
192 | test_expect_success 'double dash "git checkout"' ' | |
a1be444d | 193 | test_completion_long "git checkout --" <<-\EOF |
5c293a6b FC |
194 | --quiet Z |
195 | --ours Z | |
196 | --theirs Z | |
197 | --track Z | |
198 | --no-track Z | |
199 | --merge Z | |
200 | --conflict= | |
201 | --orphan Z | |
202 | --patch Z | |
203 | EOF | |
5c293a6b FC |
204 | ' |
205 | ||
69ef3c02 FC |
206 | test_expect_success 'general options' ' |
207 | test_completion "git --ver" "--version " && | |
208 | test_completion "git --hel" "--help " && | |
a1be444d | 209 | test_completion_long "git --exe" <<-\EOF && |
3ffcd086 JN |
210 | --exec-path Z |
211 | --exec-path= | |
212 | EOF | |
69ef3c02 FC |
213 | test_completion "git --htm" "--html-path " && |
214 | test_completion "git --pag" "--paginate " && | |
215 | test_completion "git --no-p" "--no-pager " && | |
216 | test_completion "git --git" "--git-dir=" && | |
217 | test_completion "git --wor" "--work-tree=" && | |
218 | test_completion "git --nam" "--namespace=" && | |
219 | test_completion "git --bar" "--bare " && | |
220 | test_completion "git --inf" "--info-path " && | |
221 | test_completion "git --no-r" "--no-replace-objects " | |
222 | ' | |
911d5da6 SG |
223 | |
224 | test_expect_success 'general options plus command' ' | |
225 | test_completion "git --version check" "checkout " && | |
226 | test_completion "git --paginate check" "checkout " && | |
227 | test_completion "git --git-dir=foo check" "checkout " && | |
228 | test_completion "git --bare check" "checkout " && | |
229 | test_completion "git --help des" "describe " && | |
230 | test_completion "git --exec-path=foo check" "checkout " && | |
231 | test_completion "git --html-path check" "checkout " && | |
232 | test_completion "git --no-pager check" "checkout " && | |
233 | test_completion "git --work-tree=foo check" "checkout " && | |
234 | test_completion "git --namespace=foo check" "checkout " && | |
235 | test_completion "git --paginate check" "checkout " && | |
236 | test_completion "git --info-path check" "checkout " && | |
237 | test_completion "git --no-replace-objects check" "checkout " | |
238 | ' | |
239 | ||
49ba92b4 JK |
240 | test_expect_success 'setup for ref completion' ' |
241 | echo content >file1 && | |
242 | echo more >file2 && | |
243 | git add . && | |
244 | git commit -m one && | |
245 | git branch mybranch && | |
246 | git tag mytag | |
247 | ' | |
248 | ||
249 | test_expect_success 'checkout completes ref names' ' | |
250 | test_completion_long "git checkout m" <<-\EOF | |
43ea0812 FC |
251 | master Z |
252 | mybranch Z | |
253 | mytag Z | |
49ba92b4 JK |
254 | EOF |
255 | ' | |
256 | ||
257 | test_expect_success 'show completes all refs' ' | |
258 | test_completion_long "git show m" <<-\EOF | |
43ea0812 FC |
259 | master Z |
260 | mybranch Z | |
261 | mytag Z | |
49ba92b4 JK |
262 | EOF |
263 | ' | |
264 | ||
265 | test_expect_success '<ref>: completes paths' ' | |
266 | test_completion_long "git show mytag:f" <<-\EOF | |
43ea0812 FC |
267 | file1 Z |
268 | file2 Z | |
49ba92b4 JK |
269 | EOF |
270 | ' | |
271 | ||
bafed0df JK |
272 | test_expect_success 'complete tree filename with spaces' ' |
273 | echo content >"name with spaces" && | |
274 | git add . && | |
275 | git commit -m spaces && | |
276 | test_completion_long "git show HEAD:nam" <<-\EOF | |
43ea0812 | 277 | name with spaces Z |
bafed0df JK |
278 | EOF |
279 | ' | |
280 | ||
281 | test_expect_failure 'complete tree filename with metacharacters' ' | |
282 | echo content >"name with \${meta}" && | |
283 | git add . && | |
284 | git commit -m meta && | |
285 | test_completion_long "git show HEAD:nam" <<-\EOF | |
43ea0812 FC |
286 | name with ${meta} Z |
287 | name with spaces Z | |
bafed0df JK |
288 | EOF |
289 | ' | |
290 | ||
5c293a6b | 291 | test_done |