Merge branch 'maint-1.7.11' into maint
[git.git] / t / t9902-completion.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2012 Felipe Contreras
4 #
5
6 test_description='test bash completion'
7
8 . ./lib-bash.sh
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 ))
54         __git_wrap__git_main && print_comp
55 }
56
57 test_completion ()
58 {
59         test $# -gt 1 && echo "$2" > expected
60         run_completion "$@" &&
61         test_cmp expected out
62 }
63
64 newline=$'\n'
65
66 test_expect_success '__gitcomp - trailing space - options' '
67         sed -e "s/Z$//" >expected <<-\EOF &&
68         --reuse-message=Z
69         --reedit-message=Z
70         --reset-author Z
71         EOF
72         (
73                 local -a COMPREPLY &&
74                 cur="--re" &&
75                 __gitcomp "--dry-run --reuse-message= --reedit-message=
76                                 --reset-author" &&
77                 IFS="$newline" &&
78                 echo "${COMPREPLY[*]}" > out
79         ) &&
80         test_cmp expected out
81 '
82
83 test_expect_success '__gitcomp - trailing space - config keys' '
84         sed -e "s/Z$//" >expected <<-\EOF &&
85         branch.Z
86         branch.autosetupmerge Z
87         branch.autosetuprebase Z
88         browser.Z
89         EOF
90         (
91                 local -a COMPREPLY &&
92                 cur="br" &&
93                 __gitcomp "branch. branch.autosetupmerge
94                                 branch.autosetuprebase browser." &&
95                 IFS="$newline" &&
96                 echo "${COMPREPLY[*]}" > out
97         ) &&
98         test_cmp expected out
99 '
100
101 test_expect_success '__gitcomp - option parameter' '
102         sed -e "s/Z$//" >expected <<-\EOF &&
103         recursive Z
104         resolve Z
105         EOF
106         (
107                 local -a COMPREPLY &&
108                 cur="--strategy=re" &&
109                 __gitcomp "octopus ours recursive resolve subtree
110                         " "" "re" &&
111                 IFS="$newline" &&
112                 echo "${COMPREPLY[*]}" > out
113         ) &&
114         test_cmp expected out
115 '
116
117 test_expect_success '__gitcomp - prefix' '
118         sed -e "s/Z$//" >expected <<-\EOF &&
119         branch.maint.merge Z
120         branch.maint.mergeoptions Z
121         EOF
122         (
123                 local -a COMPREPLY &&
124                 cur="branch.me" &&
125                 __gitcomp "remote merge mergeoptions rebase
126                         " "branch.maint." "me" &&
127                 IFS="$newline" &&
128                 echo "${COMPREPLY[*]}" > out
129         ) &&
130         test_cmp expected out
131 '
132
133 test_expect_success '__gitcomp - suffix' '
134         sed -e "s/Z$//" >expected <<-\EOF &&
135         branch.master.Z
136         branch.maint.Z
137         EOF
138         (
139                 local -a COMPREPLY &&
140                 cur="branch.me" &&
141                 __gitcomp "master maint next pu
142                         " "branch." "ma" "." &&
143                 IFS="$newline" &&
144                 echo "${COMPREPLY[*]}" > out
145         ) &&
146         test_cmp expected out
147 '
148
149 test_expect_success 'basic' '
150         run_completion "git \"\"" &&
151         # built-in
152         grep -q "^add \$" out &&
153         # script
154         grep -q "^filter-branch \$" out &&
155         # plumbing
156         ! grep -q "^ls-files \$" out &&
157
158         run_completion "git f" &&
159         ! grep -q -v "^f" out
160 '
161
162 test_expect_success 'double dash "git" itself' '
163         sed -e "s/Z$//" >expected <<-\EOF &&
164         --paginate Z
165         --no-pager Z
166         --git-dir=
167         --bare Z
168         --version Z
169         --exec-path Z
170         --exec-path=
171         --html-path Z
172         --info-path Z
173         --work-tree=
174         --namespace=
175         --no-replace-objects Z
176         --help Z
177         EOF
178         test_completion "git --"
179 '
180
181 test_expect_success 'double dash "git checkout"' '
182         sed -e "s/Z$//" >expected <<-\EOF &&
183         --quiet Z
184         --ours Z
185         --theirs Z
186         --track Z
187         --no-track Z
188         --merge Z
189         --conflict=
190         --orphan Z
191         --patch Z
192         EOF
193         test_completion "git checkout --"
194 '
195
196 test_expect_success 'general options' '
197         test_completion "git --ver" "--version " &&
198         test_completion "git --hel" "--help " &&
199         sed -e "s/Z$//" >expected <<-\EOF &&
200         --exec-path Z
201         --exec-path=
202         EOF
203         test_completion "git --exe" &&
204         test_completion "git --htm" "--html-path " &&
205         test_completion "git --pag" "--paginate " &&
206         test_completion "git --no-p" "--no-pager " &&
207         test_completion "git --git" "--git-dir=" &&
208         test_completion "git --wor" "--work-tree=" &&
209         test_completion "git --nam" "--namespace=" &&
210         test_completion "git --bar" "--bare " &&
211         test_completion "git --inf" "--info-path " &&
212         test_completion "git --no-r" "--no-replace-objects "
213 '
214
215 test_expect_success 'general options plus command' '
216         test_completion "git --version check" "checkout " &&
217         test_completion "git --paginate check" "checkout " &&
218         test_completion "git --git-dir=foo check" "checkout " &&
219         test_completion "git --bare check" "checkout " &&
220         test_completion "git --help des" "describe " &&
221         test_completion "git --exec-path=foo check" "checkout " &&
222         test_completion "git --html-path check" "checkout " &&
223         test_completion "git --no-pager check" "checkout " &&
224         test_completion "git --work-tree=foo check" "checkout " &&
225         test_completion "git --namespace=foo check" "checkout " &&
226         test_completion "git --paginate check" "checkout " &&
227         test_completion "git --info-path check" "checkout " &&
228         test_completion "git --no-replace-objects check" "checkout "
229 '
230
231 test_done