]>
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 | ||
50478223 JH |
16 | # Be careful when updating this list: |
17 | # | |
18 | # (1) The build tree may have build artifact from different branch, or | |
19 | # the user's $PATH may have a random executable that may begin | |
20 | # with "git-check" that are not part of the subcommands this build | |
21 | # will ship, e.g. "check-ignore". The tests for completion for | |
22 | # subcommand names tests how "check" is expanded; we limit the | |
23 | # possible candidates to "checkout" and "check-attr" to make sure | |
24 | # "check-attr", which is known by the filter function as a | |
25 | # subcommand to be thrown out, while excluding other random files | |
26 | # that happen to begin with "check" to avoid letting them get in | |
27 | # the way. | |
28 | # | |
29 | # (2) A test makes sure that common subcommands are included in the | |
30 | # completion for "git <TAB>", and a plumbing is excluded. "add", | |
31 | # "filter-branch" and "ls-files" are listed for this. | |
32 | ||
33 | GIT_TESTING_COMMAND_COMPLETION='add checkout check-attr filter-branch ls-files' | |
34 | ||
5c293a6b FC |
35 | . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" |
36 | ||
37 | # We don't need this function to actually join words or do anything special. | |
38 | # Also, it's cleaner to avoid touching bash's internal completion variables. | |
39 | # So let's override it with a minimal version for testing purposes. | |
40 | _get_comp_words_by_ref () | |
41 | { | |
42 | while [ $# -gt 0 ]; do | |
43 | case "$1" in | |
44 | cur) | |
45 | cur=${_words[_cword]} | |
46 | ;; | |
47 | prev) | |
48 | prev=${_words[_cword-1]} | |
49 | ;; | |
50 | words) | |
51 | words=("${_words[@]}") | |
52 | ;; | |
53 | cword) | |
54 | cword=$_cword | |
55 | ;; | |
56 | esac | |
57 | shift | |
58 | done | |
59 | } | |
60 | ||
61 | print_comp () | |
62 | { | |
63 | local IFS=$'\n' | |
64 | echo "${COMPREPLY[*]}" > out | |
65 | } | |
66 | ||
67 | run_completion () | |
68 | { | |
69 | local -a COMPREPLY _words | |
70 | local _cword | |
71 | _words=( $1 ) | |
0ef09702 | 72 | test "${1: -1}" = ' ' && _words[${#_words[@]}+1]='' |
5c293a6b | 73 | (( _cword = ${#_words[@]} - 1 )) |
93b291e0 | 74 | __git_wrap__git_main && print_comp |
5c293a6b FC |
75 | } |
76 | ||
701ecdf1 FC |
77 | # Test high-level completion |
78 | # Arguments are: | |
79 | # 1: typed text so far (cur) | |
80 | # 2: expected completion | |
5c293a6b FC |
81 | test_completion () |
82 | { | |
2fbaf813 FC |
83 | if test $# -gt 1 |
84 | then | |
85 | printf '%s\n' "$2" >expected | |
86 | else | |
87 | sed -e 's/Z$//' >expected | |
88 | fi && | |
701ecdf1 | 89 | run_completion "$1" && |
5c293a6b FC |
90 | test_cmp expected out |
91 | } | |
92 | ||
e4615238 FC |
93 | # Test __gitcomp. |
94 | # The first argument is the typed text so far (cur); the rest are | |
95 | # passed to __gitcomp. Expected output comes is read from the | |
96 | # standard input, like test_completion(). | |
97 | test_gitcomp () | |
49ba92b4 | 98 | { |
17393033 | 99 | local -a COMPREPLY && |
e4615238 | 100 | sed -e 's/Z$//' >expected && |
17393033 FC |
101 | cur="$1" && |
102 | shift && | |
103 | __gitcomp "$@" && | |
104 | print_comp && | |
74a8c849 | 105 | test_cmp expected out |
49ba92b4 JK |
106 | } |
107 | ||
43369a22 FC |
108 | # Test __gitcomp_nl |
109 | # Arguments are: | |
110 | # 1: current word (cur) | |
111 | # -: the rest are passed to __gitcomp_nl | |
112 | test_gitcomp_nl () | |
113 | { | |
114 | local -a COMPREPLY && | |
115 | sed -e 's/Z$//' >expected && | |
116 | cur="$1" && | |
117 | shift && | |
118 | __gitcomp_nl "$@" && | |
119 | print_comp && | |
120 | test_cmp expected out | |
121 | } | |
122 | ||
123 | invalid_variable_name='${foo.bar}' | |
124 | ||
c9a102e8 SG |
125 | actual="$TRASH_DIRECTORY/actual" |
126 | ||
127 | test_expect_success 'setup for __gitdir tests' ' | |
128 | mkdir -p subdir/subsubdir && | |
129 | git init otherrepo | |
130 | ' | |
131 | ||
132 | test_expect_success '__gitdir - from command line (through $__git_dir)' ' | |
133 | echo "$TRASH_DIRECTORY/otherrepo/.git" >expected && | |
134 | ( | |
135 | __git_dir="$TRASH_DIRECTORY/otherrepo/.git" && | |
136 | __gitdir >"$actual" | |
137 | ) && | |
138 | test_cmp expected "$actual" | |
139 | ' | |
140 | ||
141 | test_expect_success '__gitdir - repo as argument' ' | |
142 | echo "otherrepo/.git" >expected && | |
143 | __gitdir "otherrepo" >"$actual" && | |
144 | test_cmp expected "$actual" | |
145 | ' | |
146 | ||
147 | test_expect_success '__gitdir - remote as argument' ' | |
148 | echo "remote" >expected && | |
149 | __gitdir "remote" >"$actual" && | |
150 | test_cmp expected "$actual" | |
151 | ' | |
152 | ||
153 | test_expect_success '__gitdir - .git directory in cwd' ' | |
154 | echo ".git" >expected && | |
155 | __gitdir >"$actual" && | |
156 | test_cmp expected "$actual" | |
157 | ' | |
158 | ||
159 | test_expect_success '__gitdir - .git directory in parent' ' | |
160 | echo "$(pwd -P)/.git" >expected && | |
161 | ( | |
162 | cd subdir/subsubdir && | |
163 | __gitdir >"$actual" | |
164 | ) && | |
165 | test_cmp expected "$actual" | |
166 | ' | |
167 | ||
168 | test_expect_success '__gitdir - cwd is a .git directory' ' | |
169 | echo "." >expected && | |
170 | ( | |
171 | cd .git && | |
172 | __gitdir >"$actual" | |
173 | ) && | |
174 | test_cmp expected "$actual" | |
175 | ' | |
176 | ||
177 | test_expect_success '__gitdir - parent is a .git directory' ' | |
178 | echo "$(pwd -P)/.git" >expected && | |
179 | ( | |
180 | cd .git/refs/heads && | |
181 | __gitdir >"$actual" | |
182 | ) && | |
183 | test_cmp expected "$actual" | |
184 | ' | |
185 | ||
186 | test_expect_success '__gitdir - $GIT_DIR set while .git directory in cwd' ' | |
187 | echo "$TRASH_DIRECTORY/otherrepo/.git" >expected && | |
188 | ( | |
189 | GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" && | |
190 | export GIT_DIR && | |
191 | __gitdir >"$actual" | |
192 | ) && | |
193 | test_cmp expected "$actual" | |
194 | ' | |
195 | ||
196 | test_expect_success '__gitdir - $GIT_DIR set while .git directory in parent' ' | |
197 | echo "$TRASH_DIRECTORY/otherrepo/.git" >expected && | |
198 | ( | |
199 | GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" && | |
200 | export GIT_DIR && | |
201 | cd subdir && | |
202 | __gitdir >"$actual" | |
203 | ) && | |
204 | test_cmp expected "$actual" | |
205 | ' | |
206 | ||
207 | test_expect_success '__gitdir - non-existing $GIT_DIR' ' | |
208 | ( | |
209 | GIT_DIR="$TRASH_DIRECTORY/non-existing" && | |
210 | export GIT_DIR && | |
211 | test_must_fail __gitdir | |
212 | ) | |
213 | ' | |
214 | ||
44cf1c0e PT |
215 | function pwd_P_W () { |
216 | if test_have_prereq MINGW | |
217 | then | |
218 | pwd -W | |
219 | else | |
220 | pwd -P | |
221 | fi | |
222 | } | |
223 | ||
c9a102e8 | 224 | test_expect_success '__gitdir - gitfile in cwd' ' |
44cf1c0e PT |
225 | echo "$(pwd_P_W)/otherrepo/.git" >expected && |
226 | echo "gitdir: $(pwd_P_W)/otherrepo/.git" >subdir/.git && | |
c9a102e8 SG |
227 | test_when_finished "rm -f subdir/.git" && |
228 | ( | |
229 | cd subdir && | |
230 | __gitdir >"$actual" | |
231 | ) && | |
232 | test_cmp expected "$actual" | |
233 | ' | |
234 | ||
235 | test_expect_success '__gitdir - gitfile in parent' ' | |
44cf1c0e PT |
236 | echo "$(pwd_P_W)/otherrepo/.git" >expected && |
237 | echo "gitdir: $(pwd_P_W)/otherrepo/.git" >subdir/.git && | |
c9a102e8 SG |
238 | test_when_finished "rm -f subdir/.git" && |
239 | ( | |
240 | cd subdir/subsubdir && | |
241 | __gitdir >"$actual" | |
242 | ) && | |
243 | test_cmp expected "$actual" | |
244 | ' | |
245 | ||
246 | test_expect_success SYMLINKS '__gitdir - resulting path avoids symlinks' ' | |
247 | echo "$(pwd -P)/otherrepo/.git" >expected && | |
248 | mkdir otherrepo/dir && | |
249 | test_when_finished "rm -rf otherrepo/dir" && | |
250 | ln -s otherrepo/dir link && | |
251 | test_when_finished "rm -f link" && | |
252 | ( | |
253 | cd link && | |
254 | __gitdir >"$actual" | |
255 | ) && | |
256 | test_cmp expected "$actual" | |
257 | ' | |
258 | ||
259 | test_expect_success '__gitdir - not a git repository' ' | |
260 | ( | |
261 | cd subdir/subsubdir && | |
262 | GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY" && | |
263 | export GIT_CEILING_DIRECTORIES && | |
264 | test_must_fail __gitdir | |
265 | ) | |
266 | ' | |
267 | ||
74a8c849 | 268 | test_expect_success '__gitcomp - trailing space - options' ' |
e4615238 FC |
269 | test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message= |
270 | --reset-author" <<-EOF | |
74a8c849 SG |
271 | --reuse-message=Z |
272 | --reedit-message=Z | |
273 | --reset-author Z | |
274 | EOF | |
74a8c849 SG |
275 | ' |
276 | ||
277 | test_expect_success '__gitcomp - trailing space - config keys' ' | |
e4615238 FC |
278 | test_gitcomp "br" "branch. branch.autosetupmerge |
279 | branch.autosetuprebase browser." <<-\EOF | |
74a8c849 SG |
280 | branch.Z |
281 | branch.autosetupmerge Z | |
282 | branch.autosetuprebase Z | |
283 | browser.Z | |
284 | EOF | |
74a8c849 SG |
285 | ' |
286 | ||
287 | test_expect_success '__gitcomp - option parameter' ' | |
e4615238 FC |
288 | test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \ |
289 | "" "re" <<-\EOF | |
74a8c849 SG |
290 | recursive Z |
291 | resolve Z | |
292 | EOF | |
74a8c849 SG |
293 | ' |
294 | ||
295 | test_expect_success '__gitcomp - prefix' ' | |
e4615238 FC |
296 | test_gitcomp "branch.me" "remote merge mergeoptions rebase" \ |
297 | "branch.maint." "me" <<-\EOF | |
74a8c849 SG |
298 | branch.maint.merge Z |
299 | branch.maint.mergeoptions Z | |
300 | EOF | |
74a8c849 SG |
301 | ' |
302 | ||
303 | test_expect_success '__gitcomp - suffix' ' | |
e4615238 FC |
304 | test_gitcomp "branch.me" "master maint next pu" "branch." \ |
305 | "ma" "." <<-\EOF | |
74a8c849 SG |
306 | branch.master.Z |
307 | branch.maint.Z | |
308 | EOF | |
74a8c849 SG |
309 | ' |
310 | ||
7d13e0a3 | 311 | test_expect_success '__gitcomp - doesnt fail because of invalid variable name' ' |
43369a22 FC |
312 | __gitcomp "$invalid_variable_name" |
313 | ' | |
314 | ||
315 | read -r -d "" refs <<-\EOF | |
316 | maint | |
317 | master | |
318 | next | |
319 | pu | |
320 | EOF | |
321 | ||
322 | test_expect_success '__gitcomp_nl - trailing space' ' | |
323 | test_gitcomp_nl "m" "$refs" <<-EOF | |
324 | maint Z | |
325 | master Z | |
326 | EOF | |
327 | ' | |
328 | ||
329 | test_expect_success '__gitcomp_nl - prefix' ' | |
330 | test_gitcomp_nl "--fixup=m" "$refs" "--fixup=" "m" <<-EOF | |
331 | --fixup=maint Z | |
332 | --fixup=master Z | |
333 | EOF | |
334 | ' | |
335 | ||
336 | test_expect_success '__gitcomp_nl - suffix' ' | |
337 | test_gitcomp_nl "branch.ma" "$refs" "branch." "ma" "." <<-\EOF | |
338 | branch.maint.Z | |
339 | branch.master.Z | |
340 | EOF | |
341 | ' | |
342 | ||
343 | test_expect_success '__gitcomp_nl - no suffix' ' | |
344 | test_gitcomp_nl "ma" "$refs" "" "ma" "" <<-\EOF | |
345 | maintZ | |
346 | masterZ | |
347 | EOF | |
348 | ' | |
349 | ||
7d13e0a3 | 350 | test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name' ' |
43369a22 FC |
351 | __gitcomp_nl "$invalid_variable_name" |
352 | ' | |
353 | ||
5c293a6b | 354 | test_expect_success 'basic' ' |
cdbff7d6 | 355 | run_completion "git " && |
5c293a6b FC |
356 | # built-in |
357 | grep -q "^add \$" out && | |
358 | # script | |
359 | grep -q "^filter-branch \$" out && | |
360 | # plumbing | |
361 | ! grep -q "^ls-files \$" out && | |
362 | ||
363 | run_completion "git f" && | |
364 | ! grep -q -v "^f" out | |
365 | ' | |
366 | ||
367 | test_expect_success 'double dash "git" itself' ' | |
2fbaf813 | 368 | test_completion "git --" <<-\EOF |
5c293a6b FC |
369 | --paginate Z |
370 | --no-pager Z | |
371 | --git-dir= | |
372 | --bare Z | |
373 | --version Z | |
374 | --exec-path Z | |
3ffcd086 | 375 | --exec-path= |
5c293a6b | 376 | --html-path Z |
66fb37d0 | 377 | --man-path Z |
69ef3c02 | 378 | --info-path Z |
5c293a6b FC |
379 | --work-tree= |
380 | --namespace= | |
69ef3c02 | 381 | --no-replace-objects Z |
5c293a6b FC |
382 | --help Z |
383 | EOF | |
5c293a6b FC |
384 | ' |
385 | ||
386 | test_expect_success 'double dash "git checkout"' ' | |
2fbaf813 | 387 | test_completion "git checkout --" <<-\EOF |
5c293a6b FC |
388 | --quiet Z |
389 | --ours Z | |
390 | --theirs Z | |
391 | --track Z | |
392 | --no-track Z | |
393 | --merge Z | |
394 | --conflict= | |
395 | --orphan Z | |
396 | --patch Z | |
397 | EOF | |
5c293a6b FC |
398 | ' |
399 | ||
69ef3c02 FC |
400 | test_expect_success 'general options' ' |
401 | test_completion "git --ver" "--version " && | |
402 | test_completion "git --hel" "--help " && | |
2fbaf813 | 403 | test_completion "git --exe" <<-\EOF && |
3ffcd086 JN |
404 | --exec-path Z |
405 | --exec-path= | |
406 | EOF | |
69ef3c02 FC |
407 | test_completion "git --htm" "--html-path " && |
408 | test_completion "git --pag" "--paginate " && | |
409 | test_completion "git --no-p" "--no-pager " && | |
410 | test_completion "git --git" "--git-dir=" && | |
411 | test_completion "git --wor" "--work-tree=" && | |
412 | test_completion "git --nam" "--namespace=" && | |
413 | test_completion "git --bar" "--bare " && | |
414 | test_completion "git --inf" "--info-path " && | |
415 | test_completion "git --no-r" "--no-replace-objects " | |
416 | ' | |
911d5da6 SG |
417 | |
418 | test_expect_success 'general options plus command' ' | |
419 | test_completion "git --version check" "checkout " && | |
420 | test_completion "git --paginate check" "checkout " && | |
421 | test_completion "git --git-dir=foo check" "checkout " && | |
422 | test_completion "git --bare check" "checkout " && | |
911d5da6 SG |
423 | test_completion "git --exec-path=foo check" "checkout " && |
424 | test_completion "git --html-path check" "checkout " && | |
425 | test_completion "git --no-pager check" "checkout " && | |
426 | test_completion "git --work-tree=foo check" "checkout " && | |
427 | test_completion "git --namespace=foo check" "checkout " && | |
428 | test_completion "git --paginate check" "checkout " && | |
429 | test_completion "git --info-path check" "checkout " && | |
430 | test_completion "git --no-replace-objects check" "checkout " | |
431 | ' | |
432 | ||
50478223 JH |
433 | test_expect_success 'git --help completion' ' |
434 | test_completion "git --help ad" "add " && | |
435 | test_completion "git --help core" "core-tutorial " | |
436 | ' | |
437 | ||
49ba92b4 JK |
438 | test_expect_success 'setup for ref completion' ' |
439 | echo content >file1 && | |
440 | echo more >file2 && | |
441 | git add . && | |
442 | git commit -m one && | |
443 | git branch mybranch && | |
444 | git tag mytag | |
445 | ' | |
446 | ||
447 | test_expect_success 'checkout completes ref names' ' | |
2fbaf813 | 448 | test_completion "git checkout m" <<-\EOF |
43ea0812 FC |
449 | master Z |
450 | mybranch Z | |
451 | mytag Z | |
49ba92b4 JK |
452 | EOF |
453 | ' | |
454 | ||
455 | test_expect_success 'show completes all refs' ' | |
2fbaf813 | 456 | test_completion "git show m" <<-\EOF |
43ea0812 FC |
457 | master Z |
458 | mybranch Z | |
459 | mytag Z | |
49ba92b4 JK |
460 | EOF |
461 | ' | |
462 | ||
463 | test_expect_success '<ref>: completes paths' ' | |
2fbaf813 | 464 | test_completion "git show mytag:f" <<-\EOF |
43ea0812 FC |
465 | file1 Z |
466 | file2 Z | |
49ba92b4 JK |
467 | EOF |
468 | ' | |
469 | ||
bafed0df JK |
470 | test_expect_success 'complete tree filename with spaces' ' |
471 | echo content >"name with spaces" && | |
472 | git add . && | |
473 | git commit -m spaces && | |
2fbaf813 | 474 | test_completion "git show HEAD:nam" <<-\EOF |
43ea0812 | 475 | name with spaces Z |
bafed0df JK |
476 | EOF |
477 | ' | |
478 | ||
7d13e0a3 | 479 | test_expect_success 'complete tree filename with metacharacters' ' |
bafed0df JK |
480 | echo content >"name with \${meta}" && |
481 | git add . && | |
482 | git commit -m meta && | |
2fbaf813 | 483 | test_completion "git show HEAD:nam" <<-\EOF |
43ea0812 FC |
484 | name with ${meta} Z |
485 | name with spaces Z | |
bafed0df JK |
486 | EOF |
487 | ' | |
488 | ||
2f65494d FC |
489 | test_expect_success 'send-email' ' |
490 | test_completion "git send-email --cov" "--cover-letter " && | |
491 | test_completion "git send-email ma" "master " | |
492 | ' | |
493 | ||
ddf07bdd FC |
494 | test_expect_success 'complete files' ' |
495 | git init tmp && cd tmp && | |
496 | test_when_finished "cd .. && rm -rf tmp" && | |
497 | ||
498 | echo "expected" > .gitignore && | |
499 | echo "out" >> .gitignore && | |
500 | ||
501 | git add .gitignore && | |
502 | test_completion "git commit " ".gitignore" && | |
503 | ||
504 | git commit -m ignore && | |
505 | ||
506 | touch new && | |
507 | test_completion "git add " "new" && | |
508 | ||
509 | git add new && | |
510 | git commit -a -m new && | |
511 | test_completion "git add " "" && | |
512 | ||
513 | git mv new modified && | |
514 | echo modify > modified && | |
515 | test_completion "git add " "modified" && | |
516 | ||
517 | touch untracked && | |
518 | ||
519 | : TODO .gitignore should not be here && | |
520 | test_completion "git rm " <<-\EOF && | |
521 | .gitignore | |
522 | modified | |
523 | EOF | |
524 | ||
525 | test_completion "git clean " "untracked" && | |
526 | ||
527 | : TODO .gitignore should not be here && | |
528 | test_completion "git mv " <<-\EOF && | |
529 | .gitignore | |
530 | modified | |
531 | EOF | |
532 | ||
533 | mkdir dir && | |
534 | touch dir/file-in-dir && | |
535 | git add dir/file-in-dir && | |
536 | git commit -m dir && | |
537 | ||
538 | mkdir untracked-dir && | |
539 | ||
540 | : TODO .gitignore should not be here && | |
541 | test_completion "git mv modified " <<-\EOF && | |
542 | .gitignore | |
543 | dir | |
544 | modified | |
545 | untracked | |
546 | untracked-dir | |
547 | EOF | |
548 | ||
549 | test_completion "git commit " "modified" && | |
550 | ||
551 | : TODO .gitignore should not be here && | |
552 | test_completion "git ls-files " <<-\EOF | |
553 | .gitignore | |
554 | dir | |
555 | modified | |
556 | EOF | |
557 | ||
558 | touch momified && | |
559 | test_completion "git add mom" "momified" | |
560 | ' | |
561 | ||
56f24e80 SP |
562 | test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" ' |
563 | test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" && | |
564 | test_completion "git co m" <<-\EOF | |
565 | master Z | |
566 | mybranch Z | |
567 | mytag Z | |
568 | EOF | |
569 | ' | |
570 | ||
571 | test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' ' | |
572 | test_config alias.co "!f () { VAR=val git checkout ... ; } f" && | |
573 | test_completion "git co m" <<-\EOF | |
574 | master Z | |
575 | mybranch Z | |
576 | mytag Z | |
577 | EOF | |
578 | ' | |
579 | ||
580 | test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' ' | |
581 | test_config alias.co "!f() { : git checkout ; if ... } f" && | |
582 | test_completion "git co m" <<-\EOF | |
583 | master Z | |
584 | mybranch Z | |
585 | mytag Z | |
586 | EOF | |
587 | ' | |
588 | ||
f03efba4 FC |
589 | test_expect_failure 'complete with tilde expansion' ' |
590 | git init tmp && cd tmp && | |
591 | test_when_finished "cd .. && rm -rf tmp" && | |
592 | ||
593 | touch ~/tmp/file && | |
594 | ||
595 | test_completion "git add ~/tmp/" "~/tmp/file" | |
596 | ' | |
597 | ||
5c293a6b | 598 | test_done |