]>
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 | { | |
2fbaf813 FC |
63 | if test $# -gt 1 |
64 | then | |
65 | printf '%s\n' "$2" >expected | |
66 | else | |
67 | sed -e 's/Z$//' >expected | |
68 | fi && | |
701ecdf1 | 69 | run_completion "$1" && |
5c293a6b FC |
70 | test_cmp expected out |
71 | } | |
72 | ||
e4615238 FC |
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 () | |
49ba92b4 | 78 | { |
17393033 | 79 | local -a COMPREPLY && |
e4615238 | 80 | sed -e 's/Z$//' >expected && |
17393033 FC |
81 | cur="$1" && |
82 | shift && | |
83 | __gitcomp "$@" && | |
84 | print_comp && | |
74a8c849 | 85 | test_cmp expected out |
49ba92b4 JK |
86 | } |
87 | ||
74a8c849 | 88 | test_expect_success '__gitcomp - trailing space - options' ' |
e4615238 FC |
89 | test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message= |
90 | --reset-author" <<-EOF | |
74a8c849 SG |
91 | --reuse-message=Z |
92 | --reedit-message=Z | |
93 | --reset-author Z | |
94 | EOF | |
74a8c849 SG |
95 | ' |
96 | ||
97 | test_expect_success '__gitcomp - trailing space - config keys' ' | |
e4615238 FC |
98 | test_gitcomp "br" "branch. branch.autosetupmerge |
99 | branch.autosetuprebase browser." <<-\EOF | |
74a8c849 SG |
100 | branch.Z |
101 | branch.autosetupmerge Z | |
102 | branch.autosetuprebase Z | |
103 | browser.Z | |
104 | EOF | |
74a8c849 SG |
105 | ' |
106 | ||
107 | test_expect_success '__gitcomp - option parameter' ' | |
e4615238 FC |
108 | test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \ |
109 | "" "re" <<-\EOF | |
74a8c849 SG |
110 | recursive Z |
111 | resolve Z | |
112 | EOF | |
74a8c849 SG |
113 | ' |
114 | ||
115 | test_expect_success '__gitcomp - prefix' ' | |
e4615238 FC |
116 | test_gitcomp "branch.me" "remote merge mergeoptions rebase" \ |
117 | "branch.maint." "me" <<-\EOF | |
74a8c849 SG |
118 | branch.maint.merge Z |
119 | branch.maint.mergeoptions Z | |
120 | EOF | |
74a8c849 SG |
121 | ' |
122 | ||
123 | test_expect_success '__gitcomp - suffix' ' | |
e4615238 FC |
124 | test_gitcomp "branch.me" "master maint next pu" "branch." \ |
125 | "ma" "." <<-\EOF | |
74a8c849 SG |
126 | branch.master.Z |
127 | branch.maint.Z | |
128 | EOF | |
74a8c849 SG |
129 | ' |
130 | ||
5c293a6b FC |
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 && | |
139 | ||
140 | run_completion "git f" && | |
141 | ! grep -q -v "^f" out | |
142 | ' | |
143 | ||
144 | test_expect_success 'double dash "git" itself' ' | |
2fbaf813 | 145 | test_completion "git --" <<-\EOF |
5c293a6b FC |
146 | --paginate Z |
147 | --no-pager Z | |
148 | --git-dir= | |
149 | --bare Z | |
150 | --version Z | |
151 | --exec-path Z | |
3ffcd086 | 152 | --exec-path= |
5c293a6b | 153 | --html-path Z |
69ef3c02 | 154 | --info-path Z |
5c293a6b FC |
155 | --work-tree= |
156 | --namespace= | |
69ef3c02 | 157 | --no-replace-objects Z |
5c293a6b FC |
158 | --help Z |
159 | EOF | |
5c293a6b FC |
160 | ' |
161 | ||
162 | test_expect_success 'double dash "git checkout"' ' | |
2fbaf813 | 163 | test_completion "git checkout --" <<-\EOF |
5c293a6b FC |
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 | |
173 | EOF | |
5c293a6b FC |
174 | ' |
175 | ||
69ef3c02 FC |
176 | test_expect_success 'general options' ' |
177 | test_completion "git --ver" "--version " && | |
178 | test_completion "git --hel" "--help " && | |
2fbaf813 | 179 | test_completion "git --exe" <<-\EOF && |
3ffcd086 JN |
180 | --exec-path Z |
181 | --exec-path= | |
182 | EOF | |
69ef3c02 FC |
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 " | |
192 | ' | |
911d5da6 SG |
193 | |
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 " | |
208 | ' | |
209 | ||
49ba92b4 JK |
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 | |
217 | ' | |
218 | ||
219 | test_expect_success 'checkout completes ref names' ' | |
2fbaf813 | 220 | test_completion "git checkout m" <<-\EOF |
43ea0812 FC |
221 | master Z |
222 | mybranch Z | |
223 | mytag Z | |
49ba92b4 JK |
224 | EOF |
225 | ' | |
226 | ||
227 | test_expect_success 'show completes all refs' ' | |
2fbaf813 | 228 | test_completion "git show m" <<-\EOF |
43ea0812 FC |
229 | master Z |
230 | mybranch Z | |
231 | mytag Z | |
49ba92b4 JK |
232 | EOF |
233 | ' | |
234 | ||
235 | test_expect_success '<ref>: completes paths' ' | |
2fbaf813 | 236 | test_completion "git show mytag:f" <<-\EOF |
43ea0812 FC |
237 | file1 Z |
238 | file2 Z | |
49ba92b4 JK |
239 | EOF |
240 | ' | |
241 | ||
bafed0df JK |
242 | test_expect_success 'complete tree filename with spaces' ' |
243 | echo content >"name with spaces" && | |
244 | git add . && | |
245 | git commit -m spaces && | |
2fbaf813 | 246 | test_completion "git show HEAD:nam" <<-\EOF |
43ea0812 | 247 | name with spaces Z |
bafed0df JK |
248 | EOF |
249 | ' | |
250 | ||
251 | test_expect_failure 'complete tree filename with metacharacters' ' | |
252 | echo content >"name with \${meta}" && | |
253 | git add . && | |
254 | git commit -m meta && | |
2fbaf813 | 255 | test_completion "git show HEAD:nam" <<-\EOF |
43ea0812 FC |
256 | name with ${meta} Z |
257 | name with spaces Z | |
bafed0df JK |
258 | EOF |
259 | ' | |
260 | ||
2f65494d FC |
261 | test_expect_success 'send-email' ' |
262 | test_completion "git send-email --cov" "--cover-letter " && | |
263 | test_completion "git send-email ma" "master " | |
264 | ' | |
265 | ||
5c293a6b | 266 | test_done |