branch: segfault fixes and validation
[git/gitweb.git] / t / t9902-completion.sh
blob3cd53f87fb70443bc51bfec13d92a82d7f73ec7c
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 if test $# -gt 1
64 then
65 printf '%s\n' "$2" >expected
66 else
67 sed -e 's/Z$//' >expected
68 fi &&
69 run_completion "$1" &&
70 test_cmp expected out
73 # Test __gitcomp.
74 # The first argument is the typed text so far (cur); the rest are
75 # passed to __gitcomp. Expected output comes is read from the
76 # standard input, like test_completion().
77 test_gitcomp ()
79 local -a COMPREPLY &&
80 sed -e 's/Z$//' >expected &&
81 cur="$1" &&
82 shift &&
83 __gitcomp "$@" &&
84 print_comp &&
85 test_cmp expected out
88 test_expect_success '__gitcomp - trailing space - options' '
89 test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
90 --reset-author" <<-EOF
91 --reuse-message=Z
92 --reedit-message=Z
93 --reset-author Z
94 EOF
97 test_expect_success '__gitcomp - trailing space - config keys' '
98 test_gitcomp "br" "branch. branch.autosetupmerge
99 branch.autosetuprebase browser." <<-\EOF
100 branch.Z
101 branch.autosetupmerge Z
102 branch.autosetuprebase Z
103 browser.Z
107 test_expect_success '__gitcomp - option parameter' '
108 test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
109 "" "re" <<-\EOF
110 recursive Z
111 resolve Z
115 test_expect_success '__gitcomp - prefix' '
116 test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
117 "branch.maint." "me" <<-\EOF
118 branch.maint.merge Z
119 branch.maint.mergeoptions Z
123 test_expect_success '__gitcomp - suffix' '
124 test_gitcomp "branch.me" "master maint next pu" "branch." \
125 "ma" "." <<-\EOF
126 branch.master.Z
127 branch.maint.Z
131 test_expect_success 'basic' '
132 run_completion "git \"\"" &&
133 # built-in
134 grep -q "^add \$" out &&
135 # script
136 grep -q "^filter-branch \$" out &&
137 # plumbing
138 ! grep -q "^ls-files \$" out &&
140 run_completion "git f" &&
141 ! grep -q -v "^f" out
144 test_expect_success 'double dash "git" itself' '
145 test_completion "git --" <<-\EOF
146 --paginate Z
147 --no-pager Z
148 --git-dir=
149 --bare Z
150 --version Z
151 --exec-path Z
152 --exec-path=
153 --html-path Z
154 --info-path Z
155 --work-tree=
156 --namespace=
157 --no-replace-objects Z
158 --help Z
162 test_expect_success 'double dash "git checkout"' '
163 test_completion "git checkout --" <<-\EOF
164 --quiet Z
165 --ours Z
166 --theirs Z
167 --track Z
168 --no-track Z
169 --merge Z
170 --conflict=
171 --orphan Z
172 --patch Z
176 test_expect_success 'general options' '
177 test_completion "git --ver" "--version " &&
178 test_completion "git --hel" "--help " &&
179 test_completion "git --exe" <<-\EOF &&
180 --exec-path Z
181 --exec-path=
183 test_completion "git --htm" "--html-path " &&
184 test_completion "git --pag" "--paginate " &&
185 test_completion "git --no-p" "--no-pager " &&
186 test_completion "git --git" "--git-dir=" &&
187 test_completion "git --wor" "--work-tree=" &&
188 test_completion "git --nam" "--namespace=" &&
189 test_completion "git --bar" "--bare " &&
190 test_completion "git --inf" "--info-path " &&
191 test_completion "git --no-r" "--no-replace-objects "
194 test_expect_success 'general options plus command' '
195 test_completion "git --version check" "checkout " &&
196 test_completion "git --paginate check" "checkout " &&
197 test_completion "git --git-dir=foo check" "checkout " &&
198 test_completion "git --bare check" "checkout " &&
199 test_completion "git --help des" "describe " &&
200 test_completion "git --exec-path=foo check" "checkout " &&
201 test_completion "git --html-path check" "checkout " &&
202 test_completion "git --no-pager check" "checkout " &&
203 test_completion "git --work-tree=foo check" "checkout " &&
204 test_completion "git --namespace=foo check" "checkout " &&
205 test_completion "git --paginate check" "checkout " &&
206 test_completion "git --info-path check" "checkout " &&
207 test_completion "git --no-replace-objects check" "checkout "
210 test_expect_success 'setup for ref completion' '
211 echo content >file1 &&
212 echo more >file2 &&
213 git add . &&
214 git commit -m one &&
215 git branch mybranch &&
216 git tag mytag
219 test_expect_success 'checkout completes ref names' '
220 test_completion "git checkout m" <<-\EOF
221 master Z
222 mybranch Z
223 mytag Z
227 test_expect_success 'show completes all refs' '
228 test_completion "git show m" <<-\EOF
229 master Z
230 mybranch Z
231 mytag Z
235 test_expect_success '<ref>: completes paths' '
236 test_completion "git show mytag:f" <<-\EOF
237 file1 Z
238 file2 Z
242 test_expect_success 'complete tree filename with spaces' '
243 echo content >"name with spaces" &&
244 git add . &&
245 git commit -m spaces &&
246 test_completion "git show HEAD:nam" <<-\EOF
247 name with spaces Z
251 test_expect_failure 'complete tree filename with metacharacters' '
252 echo content >"name with \${meta}" &&
253 git add . &&
254 git commit -m meta &&
255 test_completion "git show HEAD:nam" <<-\EOF
256 name with ${meta} Z
257 name with spaces Z
261 test_expect_success 'send-email' '
262 test_completion "git send-email --cov" "--cover-letter " &&
263 test_completion "git send-email ma" "master "
266 test_done