Blob


1 #!/bin/sh
2 #
3 # Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
4 #
5 # Permission to use, copy, modify, and distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8 #
9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 . ./common.sh
19 test_add_basic() {
20 local testroot=`test_init add_basic`
22 got checkout $testroot/repo $testroot/wt > /dev/null
23 ret=$?
24 if [ $ret -ne 0 ]; then
25 test_done "$testroot" "$ret"
26 return 1
27 fi
29 echo "new file" > $testroot/wt/foo
31 echo 'A foo' > $testroot/stdout.expected
32 (cd $testroot/wt && got add foo > $testroot/stdout)
34 cmp -s $testroot/stdout.expected $testroot/stdout
35 ret=$?
36 if [ $ret -ne 0 ]; then
37 diff -u $testroot/stdout.expected $testroot/stdout
38 fi
39 test_done "$testroot" "$ret"
40 }
42 test_double_add() {
43 local testroot=`test_init double_add`
45 got checkout $testroot/repo $testroot/wt > /dev/null
46 ret=$?
47 if [ $ret -ne 0 ]; then
48 test_done "$testroot" "$ret"
49 return 1
50 fi
52 echo "new file" > $testroot/wt/foo
53 (cd $testroot/wt && got add foo > /dev/null)
55 (cd $testroot/wt && got add foo > $testroot/stdout)
56 ret=$?
57 if [ $ret -ne 0 ]; then
58 echo "got add failed unexpectedly" >&2
59 test_done "$testroot" 1
60 return 1
61 fi
63 echo -n > $testroot/stdout.expected
64 cmp -s $testroot/stdout.expected $testroot/stdout
65 ret=$?
66 if [ $ret -ne 0 ]; then
67 diff -u $testroot/stdout.expected $testroot/stdout
68 test_done "$testroot" "$ret"
69 return 1
70 fi
72 echo "new file" > $testroot/wt/epsilon/zeta2
73 (cd $testroot/wt && got add epsilon/zeta* > $testroot/stdout)
74 ret=$?
75 if [ $ret -ne 0 ]; then
76 echo "got add failed unexpectedly" >&2
77 test_done "$testroot" 1
78 return 1
79 fi
81 echo 'A epsilon/zeta2' > $testroot/stdout.expected
82 cmp -s $testroot/stdout.expected $testroot/stdout
83 ret=$?
84 if [ $ret -ne 0 ]; then
85 diff -u $testroot/stdout.expected $testroot/stdout
86 fi
87 test_done "$testroot" "$ret"
88 }
90 test_add_multiple() {
91 local testroot=`test_init multiple_add`
93 got checkout $testroot/repo $testroot/wt > /dev/null
94 ret=$?
95 if [ $ret -ne 0 ]; then
96 test_done "$testroot" "$ret"
97 return 1
98 fi
100 echo "new file" > $testroot/wt/foo
101 echo "new file" > $testroot/wt/bar
102 echo "new file" > $testroot/wt/baz
103 (cd $testroot/wt && got add foo bar baz > $testroot/stdout)
104 ret=$?
105 if [ $ret -ne 0 ]; then
106 echo "got add failed unexpectedly" >&2
107 test_done "$testroot" 1
108 return 1
109 fi
111 echo "A bar" > $testroot/stdout.expected
112 echo "A baz" >> $testroot/stdout.expected
113 echo "A foo" >> $testroot/stdout.expected
115 cmp -s $testroot/stdout.expected $testroot/stdout
116 ret=$?
117 if [ $ret -ne 0 ]; then
118 diff -u $testroot/stdout.expected $testroot/stdout
119 test_done "$testroot" "$ret"
120 return 1
121 fi
123 echo "changed file" > $testroot/wt/alpha
124 echo "new file" > $testroot/wt/bax
125 (cd $testroot/wt && got add -R * > $testroot/stdout)
126 ret=$?
127 if [ $ret -ne 0 ]; then
128 echo "got add failed unexpectedly" >&2
129 test_done "$testroot" 1
130 return 1
131 fi
133 echo "A bax" > $testroot/stdout.expected
135 cmp -s $testroot/stdout.expected $testroot/stdout
136 ret=$?
137 if [ $ret -ne 0 ]; then
138 diff -u $testroot/stdout.expected $testroot/stdout
139 fi
141 test_done "$testroot" "$ret"
144 test_add_file_in_new_subdir() {
145 local testroot=`test_init add_file_in_new_subdir`
147 got checkout $testroot/repo $testroot/wt > /dev/null
148 ret=$?
149 if [ $ret -ne 0 ]; then
150 test_done "$testroot" "$ret"
151 return 1
152 fi
154 mkdir -p $testroot/wt/new
155 echo "new file" > $testroot/wt/new/foo
157 echo 'A new/foo' > $testroot/stdout.expected
158 (cd $testroot/wt && got add new/foo > $testroot/stdout)
160 cmp -s $testroot/stdout.expected $testroot/stdout
161 ret=$?
162 if [ $ret -ne 0 ]; then
163 diff -u $testroot/stdout.expected $testroot/stdout
164 fi
165 test_done "$testroot" "$ret"
168 test_add_deleted() {
169 local testroot=`test_init add_deleted`
171 got checkout $testroot/repo $testroot/wt > /dev/null
172 ret=$?
173 if [ $ret -ne 0 ]; then
174 test_done "$testroot" "$ret"
175 return 1
176 fi
178 (cd $testroot/wt && got rm beta > /dev/null)
180 echo -n > $testroot/stdout.expected
181 (cd $testroot/wt && got add beta > $testroot/stdout 2> $testroot/stderr)
182 ret=$?
183 if [ $ret -eq 0 ]; then
184 echo "got add command succeeded unexpectedly" >&2
185 diff -u $testroot/stdout.expected $testroot/stdout
186 test_done "$testroot" "1"
187 return 1
188 fi
190 echo "got: beta: file has unexpected status" > $testroot/stderr.expected
191 cmp -s $testroot/stderr.expected $testroot/stderr
192 ret=$?
193 if [ $ret -ne 0 ]; then
194 diff -u $testroot/stderr.expected $testroot/stderr
195 fi
196 test_done "$testroot" "$ret"
199 test_add_directory() {
200 local testroot=`test_init add_directory`
202 got checkout $testroot/repo $testroot/wt > /dev/null
203 ret=$?
204 if [ $ret -ne 0 ]; then
205 test_done "$testroot" "$ret"
206 return 1
207 fi
209 (cd $testroot/wt && got add . > $testroot/stdout 2> $testroot/stderr)
210 ret=$?
211 echo "got: adding directories requires -R option" \
212 > $testroot/stderr.expected
213 cmp -s $testroot/stderr.expected $testroot/stderr
214 ret=$?
215 if [ $ret -ne 0 ]; then
216 diff -u $testroot/stderr.expected $testroot/stderr
217 test_done "$testroot" "$ret"
218 return 1
219 fi
221 (cd $testroot/wt && got add -I . > $testroot/stdout 2> $testroot/stderr)
222 ret=$?
223 echo "got: adding directories requires -R option" \
224 > $testroot/stderr.expected
225 cmp -s $testroot/stderr.expected $testroot/stderr
226 ret=$?
227 if [ $ret -ne 0 ]; then
228 diff -u $testroot/stderr.expected $testroot/stderr
229 test_done "$testroot" "$ret"
230 return 1
231 fi
233 echo -n > $testroot/stdout.expected
234 cmp -s $testroot/stdout.expected $testroot/stdout
235 ret=$?
236 if [ $ret -ne 0 ]; then
237 diff -u $testroot/stdout.expected $testroot/stdout
238 test_done "$testroot" "$ret"
239 return 1
240 fi
242 mkdir -p $testroot/wt/tree1
243 mkdir -p $testroot/wt/tree2
244 echo "tree1/**" > $testroot/wt/.gitignore
245 echo "tree2/**" >> $testroot/wt/.gitignore
246 echo -n > $testroot/wt/tree1/foo
247 echo -n > $testroot/wt/tree2/foo
248 echo -n > $testroot/wt/epsilon/zeta1
249 echo -n > $testroot/wt/epsilon/zeta2
251 (cd $testroot/wt && got add -R . > $testroot/stdout)
253 echo 'A .gitignore' > $testroot/stdout.expected
254 echo 'A epsilon/zeta1' >> $testroot/stdout.expected
255 echo 'A epsilon/zeta2' >> $testroot/stdout.expected
257 cmp -s $testroot/stdout.expected $testroot/stdout
258 ret=$?
259 if [ $ret -ne 0 ]; then
260 diff -u $testroot/stdout.expected $testroot/stdout
261 test_done "$testroot" "$ret"
262 return 1
263 fi
265 (cd $testroot/wt && got add -RI tree1 > $testroot/stdout)
267 echo 'A tree1/foo' > $testroot/stdout.expected
269 cmp -s $testroot/stdout.expected $testroot/stdout
270 ret=$?
271 if [ $ret -ne 0 ]; then
272 diff -u $testroot/stdout.expected $testroot/stdout
273 test_done "$testroot" "$ret"
274 return 1
275 fi
277 (cd $testroot/wt && got add tree2/foo > $testroot/stdout)
279 echo -n '' > $testroot/stdout.expected
281 cmp -s $testroot/stdout.expected $testroot/stdout
282 ret=$?
283 if [ $ret -ne 0 ]; then
284 diff -u $testroot/stdout.expected $testroot/stdout
285 test_done "$testroot" "$ret"
286 return 1
287 fi
289 (cd $testroot/wt && got add -I tree2/foo > $testroot/stdout)
291 echo 'A tree2/foo' > $testroot/stdout.expected
293 cmp -s $testroot/stdout.expected $testroot/stdout
294 ret=$?
295 if [ $ret -ne 0 ]; then
296 diff -u $testroot/stdout.expected $testroot/stdout
297 test_done "$testroot" "$ret"
298 return 1
299 fi
300 test_done "$testroot" "$ret"
303 test_add_clashes_with_submodule() {
304 local testroot=`test_init add_clashes_with_submodule`
306 make_single_file_repo $testroot/repo2 foo
308 (cd $testroot/repo && git -c protocol.file.allow=always \
309 submodule -q add ../repo2)
310 (cd $testroot/repo && git commit -q -m 'adding submodule')
312 got checkout $testroot/repo $testroot/wt > /dev/null
314 # Atttempt to add a file clashes with a submodule
315 echo "This is a file called repo2" > $testroot/wt/repo2
316 (cd $testroot/wt && got add repo2 > /dev/null)
318 (cd $testroot/wt && got status > $testroot/stdout)
319 echo "A repo2" > $testroot/stdout.expected
320 cmp -s $testroot/stdout.expected $testroot/stdout
321 ret=$?
322 if [ $ret -ne 0 ]; then
323 diff -u $testroot/stdout.expected $testroot/stdout
324 test_done "$testroot" "$ret"
325 return 1
326 fi
328 # Update for good measure; see the error below.
329 (cd $testroot/wt && got update > /dev/null)
331 # This currently fails with "work tree must be updated"...
332 (cd $testroot/wt && got commit -m 'add file repo2' \
333 > $testroot/stdout 2> $testroot/stderr)
334 ret=$?
335 if [ $ret -eq 0 ]; then
336 echo "commit succeeded unexpectedly" >&2
337 test_done "$testroot" "1"
338 return 1
339 fi
341 echo -n "got: work tree must be updated " > $testroot/stderr.expected
342 echo "before these changes can be committed" >> $testroot/stderr.expected
343 cmp -s $testroot/stderr.expected $testroot/stderr
344 ret=$?
345 if [ $ret -ne 0 ]; then
346 diff -u $testroot/stderr.expected $testroot/stderr
347 fi
348 test_done "$testroot" "$ret"
351 test_add_symlink() {
352 local testroot=`test_init add_symlink`
354 got checkout $testroot/repo $testroot/wt > /dev/null
355 ret=$?
356 if [ $ret -ne 0 ]; then
357 test_done "$testroot" "$ret"
358 return 1
359 fi
361 (cd $testroot/wt && ln -s alpha alpha.link)
362 (cd $testroot/wt && ln -s epsilon epsilon.link)
363 (cd $testroot/wt && ln -s /etc/passwd passwd.link)
364 (cd $testroot/wt && ln -s ../beta epsilon/beta.link)
365 (cd $testroot/wt && ln -s nonexistent nonexistent.link)
367 echo "A alpha.link" > $testroot/stdout.expected
368 (cd $testroot/wt && got add alpha.link > $testroot/stdout)
369 ret=$?
370 if [ $ret -ne 0 ]; then
371 diff -u $testroot/stdout.expected $testroot/stdout
372 test_done "$testroot" "$ret"
373 return 1
374 fi
376 echo "A epsilon.link" > $testroot/stdout.expected
377 (cd $testroot/wt && got add epsilon.link > $testroot/stdout)
378 cmp -s $testroot/stdout.expected $testroot/stdout
379 ret=$?
380 if [ $ret -ne 0 ]; then
381 diff -u $testroot/stdout.expected $testroot/stdout
382 test_done "$testroot" "$ret"
383 return 1
384 fi
386 echo "A passwd.link" > $testroot/stdout.expected
387 (cd $testroot/wt && got add passwd.link > $testroot/stdout)
388 cmp -s $testroot/stdout.expected $testroot/stdout
389 ret=$?
390 if [ $ret -ne 0 ]; then
391 diff -u $testroot/stdout.expected $testroot/stdout
392 test_done "$testroot" "$ret"
393 return 1
394 fi
396 echo "A epsilon/beta.link" > $testroot/stdout.expected
397 (cd $testroot/wt && got add epsilon/beta.link > $testroot/stdout)
398 cmp -s $testroot/stdout.expected $testroot/stdout
399 ret=$?
400 if [ $ret -ne 0 ]; then
401 diff -u $testroot/stdout.expected $testroot/stdout
402 test_done "$testroot" "$ret"
403 return 1
404 fi
406 echo "A nonexistent.link" > $testroot/stdout.expected
407 (cd $testroot/wt && got add nonexistent.link > $testroot/stdout)
408 cmp -s $testroot/stdout.expected $testroot/stdout
409 ret=$?
410 if [ $ret -ne 0 ]; then
411 diff -u $testroot/stdout.expected $testroot/stdout
412 fi
413 test_done "$testroot" "$ret"
416 test_parseargs "$@"
417 run_test test_add_basic
418 run_test test_double_add
419 run_test test_add_multiple
420 run_test test_add_file_in_new_subdir
421 run_test test_add_deleted
422 run_test test_add_directory
423 run_test test_add_clashes_with_submodule
424 run_test test_add_symlink