3 # Copyright (c) 2012 Felipe Contreras
6 test_description='test bash completion'
16 # Be careful when updating this list:
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
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.
33 GIT_TESTING_COMMAND_COMPLETION='add checkout check-attr filter-branch ls-files'
35 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
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 ()
42 while [ $# -gt 0 ]; do
48 prev=${_words[_cword-1]}
51 words=("${_words[@]}")
64 echo "${COMPREPLY[*]}" > out
69 local -a COMPREPLY _words
72 test "${1: -1}" == ' ' && _words+=('')
73 (( _cword = ${#_words[@]} - 1 ))
74 __git_wrap__git_main && print_comp
77 # Test high-level completion
79 # 1: typed text so far (cur)
80 # 2: expected completion
85 printf '%s\n' "$2" >expected
87 sed -e 's/Z$//' >expected
89 run_completion "$1" &&
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().
100 sed -e 's/Z$//' >expected &&
105 test_cmp expected out
110 # 1: current word (cur)
111 # -: the rest are passed to __gitcomp_nl
114 local -a COMPREPLY &&
115 sed -e 's/Z$//' >expected &&
120 test_cmp expected out
123 invalid_variable_name='${foo.bar}'
125 test_expect_success '__gitcomp - trailing space - options' '
126 test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
127 --reset-author" <<-EOF
134 test_expect_success '__gitcomp - trailing space - config keys' '
135 test_gitcomp "br" "branch. branch.autosetupmerge
136 branch.autosetuprebase browser." <<-\EOF
138 branch.autosetupmerge Z
139 branch.autosetuprebase Z
144 test_expect_success '__gitcomp - option parameter' '
145 test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
152 test_expect_success '__gitcomp - prefix' '
153 test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
154 "branch.maint." "me" <<-\EOF
156 branch.maint.mergeoptions Z
160 test_expect_success '__gitcomp - suffix' '
161 test_gitcomp "branch.me" "master maint next pu" "branch." \
168 test_expect_success '__gitcomp - doesnt fail because of invalid variable name' '
169 __gitcomp "$invalid_variable_name"
172 read -r -d "" refs <<-\EOF
179 test_expect_success '__gitcomp_nl - trailing space' '
180 test_gitcomp_nl "m" "$refs" <<-EOF
186 test_expect_success '__gitcomp_nl - prefix' '
187 test_gitcomp_nl "--fixup=m" "$refs" "--fixup=" "m" <<-EOF
193 test_expect_success '__gitcomp_nl - suffix' '
194 test_gitcomp_nl "branch.ma" "$refs" "branch." "ma" "." <<-\EOF
200 test_expect_success '__gitcomp_nl - no suffix' '
201 test_gitcomp_nl "ma" "$refs" "" "ma" "" <<-\EOF
207 test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name' '
208 __gitcomp_nl "$invalid_variable_name"
211 test_expect_success 'basic' '
212 run_completion "git " &&
214 grep -q "^add \$" out &&
216 grep -q "^filter-branch \$" out &&
218 ! grep -q "^ls-files \$" out &&
220 run_completion "git f" &&
221 ! grep -q -v "^f" out
224 test_expect_success 'double dash "git" itself' '
225 test_completion "git --" <<-\EOF
237 --no-replace-objects Z
242 test_expect_success 'double dash "git checkout"' '
243 test_completion "git checkout --" <<-\EOF
256 test_expect_success 'general options' '
257 test_completion "git --ver" "--version " &&
258 test_completion "git --hel" "--help " &&
259 test_completion "git --exe" <<-\EOF &&
263 test_completion "git --htm" "--html-path " &&
264 test_completion "git --pag" "--paginate " &&
265 test_completion "git --no-p" "--no-pager " &&
266 test_completion "git --git" "--git-dir=" &&
267 test_completion "git --wor" "--work-tree=" &&
268 test_completion "git --nam" "--namespace=" &&
269 test_completion "git --bar" "--bare " &&
270 test_completion "git --inf" "--info-path " &&
271 test_completion "git --no-r" "--no-replace-objects "
274 test_expect_success 'general options plus command' '
275 test_completion "git --version check" "checkout " &&
276 test_completion "git --paginate check" "checkout " &&
277 test_completion "git --git-dir=foo check" "checkout " &&
278 test_completion "git --bare check" "checkout " &&
279 test_completion "git --exec-path=foo check" "checkout " &&
280 test_completion "git --html-path check" "checkout " &&
281 test_completion "git --no-pager check" "checkout " &&
282 test_completion "git --work-tree=foo check" "checkout " &&
283 test_completion "git --namespace=foo check" "checkout " &&
284 test_completion "git --paginate check" "checkout " &&
285 test_completion "git --info-path check" "checkout " &&
286 test_completion "git --no-replace-objects check" "checkout "
289 test_expect_success 'git --help completion' '
290 test_completion "git --help ad" "add " &&
291 test_completion "git --help core" "core-tutorial "
294 test_expect_success 'setup for ref completion' '
295 echo content >file1 &&
299 git branch mybranch &&
303 test_expect_success 'checkout completes ref names' '
304 test_completion "git checkout m" <<-\EOF
311 test_expect_success 'show completes all refs' '
312 test_completion "git show m" <<-\EOF
319 test_expect_success '<ref>: completes paths' '
320 test_completion "git show mytag:f" <<-\EOF
326 test_expect_success 'complete tree filename with spaces' '
327 echo content >"name with spaces" &&
329 git commit -m spaces &&
330 test_completion "git show HEAD:nam" <<-\EOF
335 test_expect_success 'complete tree filename with metacharacters' '
336 echo content >"name with \${meta}" &&
338 git commit -m meta &&
339 test_completion "git show HEAD:nam" <<-\EOF
345 test_expect_success 'send-email' '
346 test_completion "git send-email --cov" "--cover-letter " &&
347 test_completion "git send-email ma" "master "