completion: add comment for test_completion()
[git/gitweb.git] / t / t9902-completion.sh
blob5c06709ea0601a26485c8503b4a91e645ec18226
1 #!/bin/sh
3 # Copyright (c) 2012 Felipe Contreras
6 test_description='test bash completion'
8 . ./lib-bash.sh
10 complete ()
12 # do nothing
13 return 0
16 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
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 ()
23 while [ $# -gt 0 ]; do
24 case "$1" in
25 cur)
26 cur=${_words[_cword]}
28 prev)
29 prev=${_words[_cword-1]}
31 words)
32 words=("${_words[@]}")
34 cword)
35 cword=$_cword
37 esac
38 shift
39 done
42 print_comp ()
44 local IFS=$'\n'
45 echo "${COMPREPLY[*]}" > out
48 run_completion ()
50 local -a COMPREPLY _words
51 local _cword
52 _words=( $1 )
53 (( _cword = ${#_words[@]} - 1 ))
54 __git_wrap__git_main && print_comp
57 # Test high-level completion
58 # Arguments are:
59 # 1: typed text so far (cur)
60 # 2: expected completion
61 test_completion ()
63 test $# -gt 1 && echo "$2" > expected
64 run_completion "$1" &&
65 test_cmp expected out
68 # Like test_completion, but reads expectation from stdin,
69 # which is convenient when it is multiline. We also process "_" into
70 # spaces to make test vectors more readable.
71 test_completion_long ()
73 tr _ " " >expected &&
74 test_completion "$1"
77 newline=$'\n'
79 test_expect_success '__gitcomp - trailing space - options' '
80 sed -e "s/Z$//" >expected <<-\EOF &&
81 --reuse-message=Z
82 --reedit-message=Z
83 --reset-author Z
84 EOF
86 local -a COMPREPLY &&
87 cur="--re" &&
88 __gitcomp "--dry-run --reuse-message= --reedit-message=
89 --reset-author" &&
90 IFS="$newline" &&
91 echo "${COMPREPLY[*]}" > out
92 ) &&
93 test_cmp expected out
96 test_expect_success '__gitcomp - trailing space - config keys' '
97 sed -e "s/Z$//" >expected <<-\EOF &&
98 branch.Z
99 branch.autosetupmerge Z
100 branch.autosetuprebase Z
101 browser.Z
104 local -a COMPREPLY &&
105 cur="br" &&
106 __gitcomp "branch. branch.autosetupmerge
107 branch.autosetuprebase browser." &&
108 IFS="$newline" &&
109 echo "${COMPREPLY[*]}" > out
110 ) &&
111 test_cmp expected out
114 test_expect_success '__gitcomp - option parameter' '
115 sed -e "s/Z$//" >expected <<-\EOF &&
116 recursive Z
117 resolve Z
120 local -a COMPREPLY &&
121 cur="--strategy=re" &&
122 __gitcomp "octopus ours recursive resolve subtree
123 " "" "re" &&
124 IFS="$newline" &&
125 echo "${COMPREPLY[*]}" > out
126 ) &&
127 test_cmp expected out
130 test_expect_success '__gitcomp - prefix' '
131 sed -e "s/Z$//" >expected <<-\EOF &&
132 branch.maint.merge Z
133 branch.maint.mergeoptions Z
136 local -a COMPREPLY &&
137 cur="branch.me" &&
138 __gitcomp "remote merge mergeoptions rebase
139 " "branch.maint." "me" &&
140 IFS="$newline" &&
141 echo "${COMPREPLY[*]}" > out
142 ) &&
143 test_cmp expected out
146 test_expect_success '__gitcomp - suffix' '
147 sed -e "s/Z$//" >expected <<-\EOF &&
148 branch.master.Z
149 branch.maint.Z
152 local -a COMPREPLY &&
153 cur="branch.me" &&
154 __gitcomp "master maint next pu
155 " "branch." "ma" "." &&
156 IFS="$newline" &&
157 echo "${COMPREPLY[*]}" > out
158 ) &&
159 test_cmp expected out
162 test_expect_success 'basic' '
163 run_completion "git \"\"" &&
164 # built-in
165 grep -q "^add \$" out &&
166 # script
167 grep -q "^filter-branch \$" out &&
168 # plumbing
169 ! grep -q "^ls-files \$" out &&
171 run_completion "git f" &&
172 ! grep -q -v "^f" out
175 test_expect_success 'double dash "git" itself' '
176 sed -e "s/Z$//" >expected <<-\EOF &&
177 --paginate Z
178 --no-pager Z
179 --git-dir=
180 --bare Z
181 --version Z
182 --exec-path Z
183 --exec-path=
184 --html-path Z
185 --info-path Z
186 --work-tree=
187 --namespace=
188 --no-replace-objects Z
189 --help Z
191 test_completion "git --"
194 test_expect_success 'double dash "git checkout"' '
195 sed -e "s/Z$//" >expected <<-\EOF &&
196 --quiet Z
197 --ours Z
198 --theirs Z
199 --track Z
200 --no-track Z
201 --merge Z
202 --conflict=
203 --orphan Z
204 --patch Z
206 test_completion "git checkout --"
209 test_expect_success 'general options' '
210 test_completion "git --ver" "--version " &&
211 test_completion "git --hel" "--help " &&
212 sed -e "s/Z$//" >expected <<-\EOF &&
213 --exec-path Z
214 --exec-path=
216 test_completion "git --exe" &&
217 test_completion "git --htm" "--html-path " &&
218 test_completion "git --pag" "--paginate " &&
219 test_completion "git --no-p" "--no-pager " &&
220 test_completion "git --git" "--git-dir=" &&
221 test_completion "git --wor" "--work-tree=" &&
222 test_completion "git --nam" "--namespace=" &&
223 test_completion "git --bar" "--bare " &&
224 test_completion "git --inf" "--info-path " &&
225 test_completion "git --no-r" "--no-replace-objects "
228 test_expect_success 'general options plus command' '
229 test_completion "git --version check" "checkout " &&
230 test_completion "git --paginate check" "checkout " &&
231 test_completion "git --git-dir=foo check" "checkout " &&
232 test_completion "git --bare check" "checkout " &&
233 test_completion "git --help des" "describe " &&
234 test_completion "git --exec-path=foo check" "checkout " &&
235 test_completion "git --html-path check" "checkout " &&
236 test_completion "git --no-pager check" "checkout " &&
237 test_completion "git --work-tree=foo check" "checkout " &&
238 test_completion "git --namespace=foo check" "checkout " &&
239 test_completion "git --paginate check" "checkout " &&
240 test_completion "git --info-path check" "checkout " &&
241 test_completion "git --no-replace-objects check" "checkout "
244 test_expect_success 'setup for ref completion' '
245 echo content >file1 &&
246 echo more >file2 &&
247 git add . &&
248 git commit -m one &&
249 git branch mybranch &&
250 git tag mytag
253 test_expect_success 'checkout completes ref names' '
254 test_completion_long "git checkout m" <<-\EOF
255 master_
256 mybranch_
257 mytag_
261 test_expect_success 'show completes all refs' '
262 test_completion_long "git show m" <<-\EOF
263 master_
264 mybranch_
265 mytag_
269 test_expect_success '<ref>: completes paths' '
270 test_completion_long "git show mytag:f" <<-\EOF
271 file1_
272 file2_
276 test_expect_success 'complete tree filename with spaces' '
277 echo content >"name with spaces" &&
278 git add . &&
279 git commit -m spaces &&
280 test_completion_long "git show HEAD:nam" <<-\EOF
281 name with spaces_
285 test_expect_failure 'complete tree filename with metacharacters' '
286 echo content >"name with \${meta}" &&
287 git add . &&
288 git commit -m meta &&
289 test_completion_long "git show HEAD:nam" <<-\EOF
290 name with ${meta}_
291 name with spaces_
295 test_done