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_rm_basic() {
20 local testroot=`test_init rm_basic`
22 got checkout $testroot/repo $testroot/wt > /dev/null
23 ret="$?"
24 if [ "$ret" != "0" ]; then
25 test_done "$testroot" "$ret"
26 return 1
27 fi
29 echo 'D alpha' > $testroot/stdout.expected
30 echo 'D beta' >> $testroot/stdout.expected
31 (cd $testroot/wt && got rm alpha beta > $testroot/stdout)
33 cmp -s $testroot/stdout.expected $testroot/stdout
34 ret="$?"
35 if [ "$ret" != "0" ]; then
36 diff -u $testroot/stdout.expected $testroot/stdout
37 test_done "$testroot" "$ret"
38 return 1
39 fi
41 (cd $testroot/wt && got status > $testroot/stdout)
43 cmp -s $testroot/stdout.expected $testroot/stdout
44 ret="$?"
45 if [ "$ret" != "0" ]; then
46 diff -u $testroot/stdout.expected $testroot/stdout
47 test_done "$testroot" "$ret"
48 return 1
49 fi
51 for f in alpha beta; do
52 if [ -e $testroot/wt/$f ]; then
53 echo "removed file $f still exists on disk" >&2
54 test_done "$testroot" "1"
55 return 1
56 fi
57 done
59 test_done "$testroot" "0"
60 }
62 test_rm_with_local_mods() {
63 local testroot=`test_init rm_with_local_mods`
65 got checkout $testroot/repo $testroot/wt > /dev/null
66 ret="$?"
67 if [ "$ret" != "0" ]; then
68 test_done "$testroot" "$ret"
69 return 1
70 fi
72 echo "modified beta" > $testroot/wt/beta
73 echo 'got: beta: file contains modifications' \
74 > $testroot/stderr.expected
75 (cd $testroot/wt && got rm beta 2>$testroot/stderr)
77 cmp -s $testroot/stderr.expected $testroot/stderr
78 ret="$?"
79 if [ "$ret" != "0" ]; then
80 diff -u $testroot/stderr.expected $testroot/stderr
81 test_done "$testroot" "$ret"
82 return 1
83 fi
85 echo 'D beta' > $testroot/stdout.expected
86 (cd $testroot/wt && got rm -f beta > $testroot/stdout)
88 cmp -s $testroot/stdout.expected $testroot/stdout
89 ret="$?"
90 if [ "$ret" != "0" ]; then
91 diff -u $testroot/stdout.expected $testroot/stdout
92 fi
94 if [ -e $testroot/wt/beta ]; then
95 echo "removed file beta still exists on disk" >&2
96 test_done "$testroot" "1"
97 return 1
98 fi
100 test_done "$testroot" "$ret"
103 test_double_rm() {
104 local testroot=`test_init double_rm`
106 got checkout $testroot/repo $testroot/wt > /dev/null
107 ret="$?"
108 if [ "$ret" != "0" ]; then
109 test_done "$testroot" "$ret"
110 return 1
111 fi
113 (cd $testroot/wt && got rm beta > /dev/null)
115 for fflag in "" "-f"; do
116 echo -n > $testroot/stderr.expected
117 (cd $testroot/wt && got rm $fflag beta > $testroot/stdout \
118 2> $testroot/stderr)
119 ret="$?"
120 if [ "$ret" != "0" ]; then
121 echo "got rm command failed unexpectedly" >&2
122 diff -u $testroot/stderr.expected $testroot/stderr
123 test_done "$testroot" "$ret"
124 return 1
125 fi
126 echo -n > $testroot/stdout.expected
127 cmp -s $testroot/stdout.expected $testroot/stdout
128 ret="$?"
129 if [ "$ret" != "0" ]; then
130 diff -u $testroot/stdout.expected $testroot/stdout
131 test_done "$testroot" "$ret"
132 return 1
133 fi
134 done
135 test_done "$testroot" "0"
138 test_rm_and_add_elsewhere() {
139 local testroot=`test_init rm_and_add_elsewhere`
141 got checkout $testroot/repo $testroot/wt > /dev/null
142 ret="$?"
143 if [ "$ret" != "0" ]; then
144 test_done "$testroot" "$ret"
145 return 1
146 fi
148 (cd $testroot/wt && mv alpha epsilon/)
150 (cd $testroot/wt && got status > $testroot/stdout)
152 echo '! alpha' > $testroot/stdout.expected
153 echo '? epsilon/alpha' >> $testroot/stdout.expected
154 cmp -s $testroot/stdout.expected $testroot/stdout
155 ret="$?"
156 if [ "$ret" != "0" ]; then
157 diff -u $testroot/stdout.expected $testroot/stdout
158 test_done "$testroot" "$ret"
159 return 1
160 fi
162 echo 'D alpha' > $testroot/stdout.expected
163 (cd $testroot/wt && got rm alpha > $testroot/stdout)
165 cmp -s $testroot/stdout.expected $testroot/stdout
166 ret="$?"
167 if [ "$ret" != "0" ]; then
168 diff -u $testroot/stdout.expected $testroot/stdout
169 test_done "$testroot" "$ret"
170 return 1
171 fi
173 echo 'A epsilon/alpha' > $testroot/stdout.expected
174 (cd $testroot/wt && got add epsilon/alpha > $testroot/stdout)
176 cmp -s $testroot/stdout.expected $testroot/stdout
177 ret="$?"
178 if [ "$ret" != "0" ]; then
179 diff -u $testroot/stdout.expected $testroot/stdout
180 test_done "$testroot" "$ret"
181 return 1
182 fi
184 (cd $testroot/wt && got status > $testroot/stdout)
186 echo 'D alpha' > $testroot/stdout.expected
187 echo 'A epsilon/alpha' >> $testroot/stdout.expected
188 cmp -s $testroot/stdout.expected $testroot/stdout
189 ret="$?"
190 if [ "$ret" != "0" ]; then
191 diff -u $testroot/stdout.expected $testroot/stdout
192 fi
193 test_done "$testroot" "$ret"
196 test_rm_directory() {
197 local testroot=`test_init rm_directory`
199 got checkout $testroot/repo $testroot/wt > /dev/null
200 ret="$?"
201 if [ "$ret" != "0" ]; then
202 test_done "$testroot" "$ret"
203 return 1
204 fi
206 (cd $testroot/wt && got rm . > $testroot/stdout 2> $testroot/stderr)
207 ret="$?"
208 echo "got: removing directories requires -R option" \
209 > $testroot/stderr.expected
210 cmp -s $testroot/stderr.expected $testroot/stderr
211 ret="$?"
212 if [ "$ret" != "0" ]; then
213 diff -u $testroot/stderr.expected $testroot/stderr
214 test_done "$testroot" "$ret"
215 return 1
216 fi
218 echo -n > $testroot/stdout.expected
219 cmp -s $testroot/stdout.expected $testroot/stdout
220 ret="$?"
221 if [ "$ret" != "0" ]; then
222 diff -u $testroot/stdout.expected $testroot/stdout
223 test_done "$testroot" "$ret"
224 return 1
225 fi
227 (cd $testroot/wt && got rm -R . > $testroot/stdout)
229 echo 'D alpha' > $testroot/stdout.expected
230 echo 'D beta' >> $testroot/stdout.expected
231 echo 'D epsilon/zeta' >> $testroot/stdout.expected
232 echo 'D gamma/delta' >> $testroot/stdout.expected
234 cmp -s $testroot/stdout.expected $testroot/stdout
235 ret="$?"
236 if [ "$ret" != "0" ]; then
237 diff -u $testroot/stdout.expected $testroot/stdout
238 test_done "$testroot" "$ret"
239 return 1
240 fi
242 (cd $testroot/wt && ls -l | sed '/^total/d' > $testroot/stdout)
244 echo -n '' > $testroot/stdout.expected
246 cmp -s $testroot/stdout.expected $testroot/stdout
247 ret="$?"
248 if [ "$ret" != "0" ]; then
249 diff -u $testroot/stdout.expected $testroot/stdout
250 test_done "$testroot" "$ret"
251 return 1
252 fi
254 (cd $testroot/wt && ls -l | sed '/^total/d' > $testroot/stdout)
256 echo -n '' > $testroot/stdout.expected
258 cmp -s $testroot/stdout.expected $testroot/stdout
259 ret="$?"
260 if [ "$ret" != "0" ]; then
261 diff -u $testroot/stdout.expected $testroot/stdout
262 test_done "$testroot" "$ret"
263 return 1
264 fi
266 test_done "$testroot" "$ret"
269 test_rm_directory_keep_files() {
270 local testroot=`test_init rm_directory_keep_files`
272 got checkout $testroot/repo $testroot/wt > /dev/null
273 ret="$?"
274 if [ "$ret" != "0" ]; then
275 test_done "$testroot" "$ret"
276 return 1
277 fi
279 (cd $testroot/wt && got rm . > $testroot/stdout 2> $testroot/stderr)
280 ret="$?"
281 echo "got: removing directories requires -R option" \
282 > $testroot/stderr.expected
283 cmp -s $testroot/stderr.expected $testroot/stderr
284 ret="$?"
285 if [ "$ret" != "0" ]; then
286 diff -u $testroot/stderr.expected $testroot/stderr
287 test_done "$testroot" "$ret"
288 return 1
289 fi
291 echo -n > $testroot/stdout.expected
292 cmp -s $testroot/stdout.expected $testroot/stdout
293 ret="$?"
294 if [ "$ret" != "0" ]; then
295 diff -u $testroot/stdout.expected $testroot/stdout
296 test_done "$testroot" "$ret"
297 return 1
298 fi
300 (cd $testroot/wt && got rm -k -R . > $testroot/stdout)
302 echo 'D alpha' > $testroot/stdout.expected
303 echo 'D beta' >> $testroot/stdout.expected
304 echo 'D epsilon/zeta' >> $testroot/stdout.expected
305 echo 'D gamma/delta' >> $testroot/stdout.expected
307 cmp -s $testroot/stdout.expected $testroot/stdout
308 ret="$?"
309 if [ "$ret" != "0" ]; then
310 diff -u $testroot/stdout.expected $testroot/stdout
311 test_done "$testroot" "$ret"
312 return 1
313 fi
315 (cd $testroot/wt && got st . > $testroot/stdout)
317 echo 'D alpha' > $testroot/stdout.expected
318 echo 'D beta' >> $testroot/stdout.expected
319 echo 'D epsilon/zeta' >> $testroot/stdout.expected
320 echo 'D gamma/delta' >> $testroot/stdout.expected
322 cmp -s $testroot/stdout.expected $testroot/stdout
323 ret="$?"
324 if [ "$ret" != "0" ]; then
325 diff -u $testroot/stdout.expected $testroot/stdout
326 test_done "$testroot" "$ret"
327 return 1
328 fi
330 (cd $testroot/wt && got commit -m "keep" > /dev/null)
331 (cd $testroot/wt && got st . > $testroot/stdout)
333 echo '? alpha' > $testroot/stdout.expected
334 echo '? beta' >> $testroot/stdout.expected
335 echo '? epsilon/zeta' >> $testroot/stdout.expected
336 echo '? gamma/delta' >> $testroot/stdout.expected
338 cmp -s $testroot/stdout.expected $testroot/stdout
339 ret="$?"
340 if [ "$ret" != "0" ]; then
341 diff -u $testroot/stdout.expected $testroot/stdout
342 test_done "$testroot" "$ret"
343 return 1
344 fi
346 test_done "$testroot" "$ret"
349 test_rm_subtree() {
350 local testroot=`test_init rm_subtree`
352 got checkout $testroot/repo $testroot/wt > /dev/null
353 ret="$?"
354 if [ "$ret" != "0" ]; then
355 test_done "$testroot" "$ret"
356 return 1
357 fi
359 mkdir -p $testroot/wt/epsilon/foo/bar/baz
360 mkdir -p $testroot/wt/epsilon/foo/bar/bax
361 echo "new file" > $testroot/wt/epsilon/foo/a.o
362 echo "new file" > $testroot/wt/epsilon/foo/a.o
363 echo "new file" > $testroot/wt/epsilon/foo/bar/b.o
364 echo "new file" > $testroot/wt/epsilon/foo/bar/b.d
365 echo "new file" > $testroot/wt/epsilon/foo/bar/baz/f.o
366 echo "new file" > $testroot/wt/epsilon/foo/bar/baz/f.d
367 echo "new file" > $testroot/wt/epsilon/foo/bar/baz/c.o
368 echo "new file" > $testroot/wt/epsilon/foo/bar/baz/c.d
369 echo "new file" > $testroot/wt/epsilon/foo/bar/bax/e.o
370 echo "new file" > $testroot/wt/epsilon/foo/bar/bax/e.d
371 echo "new file" > $testroot/wt/epsilon/foo/bar/bax/x.o
372 echo "new file" > $testroot/wt/epsilon/foo/bar/bax/x.d
373 (cd $testroot/wt && got add -R epsilon >/dev/null)
374 (cd $testroot/wt && got commit -m "add subtree" >/dev/null)
376 # now delete and revert the entire subtree
377 (cd $testroot/wt && got rm -R epsilon/foo >/dev/null)
379 if [ -d $testroot/wt/epsilon/foo ]; then
380 echo "removed dir epsilon/foo still exists on disk" >&2
381 test_done "$testroot" "1"
382 return 1
383 fi
385 echo "D epsilon/foo/a.o" > $testroot/stdout.expected
386 echo "D epsilon/foo/bar/b.d" >> $testroot/stdout.expected
387 echo "D epsilon/foo/bar/b.o" >> $testroot/stdout.expected
388 echo "D epsilon/foo/bar/bax/e.d" >> $testroot/stdout.expected
389 echo "D epsilon/foo/bar/bax/e.o" >> $testroot/stdout.expected
390 echo "D epsilon/foo/bar/bax/x.d" >> $testroot/stdout.expected
391 echo "D epsilon/foo/bar/bax/x.o" >> $testroot/stdout.expected
392 echo "D epsilon/foo/bar/baz/c.d" >> $testroot/stdout.expected
393 echo "D epsilon/foo/bar/baz/c.o" >> $testroot/stdout.expected
394 echo "D epsilon/foo/bar/baz/f.d" >> $testroot/stdout.expected
395 echo "D epsilon/foo/bar/baz/f.o" >> $testroot/stdout.expected
397 (cd $testroot/wt && got status > $testroot/stdout)
399 cmp -s $testroot/stdout.expected $testroot/stdout
400 ret="$?"
401 if [ "$ret" != "0" ]; then
402 diff -u $testroot/stdout.expected $testroot/stdout
403 fi
404 test_done "$testroot" "$ret"
407 test_rm_symlink() {
408 local testroot=`test_init rm_symlink`
410 (cd $testroot/repo && ln -s alpha alpha.link)
411 (cd $testroot/repo && ln -s epsilon epsilon.link)
412 (cd $testroot/repo && ln -s /etc/passwd passwd.link)
413 (cd $testroot/repo && ln -s ../beta epsilon/beta.link)
414 (cd $testroot/repo && ln -s nonexistent nonexistent.link)
415 (cd $testroot/repo && git add .)
416 git_commit $testroot/repo -m "add symlinks"
418 got checkout $testroot/repo $testroot/wt > /dev/null
419 ret="$?"
420 if [ "$ret" != "0" ]; then
421 test_done "$testroot" "$ret"
422 return 1
423 fi
425 echo 'D alpha.link' > $testroot/stdout.expected
426 echo 'D epsilon/beta.link' >> $testroot/stdout.expected
427 echo 'D epsilon.link' >> $testroot/stdout.expected
428 echo 'D nonexistent.link' >> $testroot/stdout.expected
429 echo 'D passwd.link' >> $testroot/stdout.expected
430 (cd $testroot/wt && got rm alpha.link epsilon.link passwd.link \
431 epsilon/beta.link nonexistent.link > $testroot/stdout)
433 cmp -s $testroot/stdout.expected $testroot/stdout
434 ret="$?"
435 if [ "$ret" != "0" ]; then
436 diff -u $testroot/stdout.expected $testroot/stdout
437 fi
438 test_done "$testroot" "$ret"
441 test_rm_status_code() {
442 local testroot=`test_init rm_status_code`
444 got checkout $testroot/repo $testroot/wt > /dev/null
445 ret="$?"
446 if [ "$ret" != "0" ]; then
447 test_done "$testroot" "$ret"
448 return 1
449 fi
451 echo "modified beta" > $testroot/wt/beta
453 echo "got: invalid status code 'x'" > $testroot/stderr.expected
454 (cd $testroot/wt && got rm -s Mx beta 2>$testroot/stderr)
456 cmp -s $testroot/stderr.expected $testroot/stderr
457 ret="$?"
458 if [ "$ret" != "0" ]; then
459 diff -u $testroot/stderr.expected $testroot/stderr
460 test_done "$testroot" "$ret"
461 return 1
462 fi
464 rm $testroot/wt/epsilon/zeta # put file into 'missing' status
466 echo 'D epsilon/zeta' > $testroot/stdout.expected
467 (cd $testroot/wt && got rm -R -s '!' . >$testroot/stdout)
469 cmp -s $testroot/stdout.expected $testroot/stdout
470 ret="$?"
471 if [ "$ret" != "0" ]; then
472 diff -u $testroot/stdout.expected $testroot/stdout
473 fi
475 if [ ! -e $testroot/wt/beta ]; then
476 echo "file beta was unexpectedly removed from disk" >&2
477 test_done "$testroot" "1"
478 return 1
479 fi
481 # put file into 'missing' status again
482 (cd $testroot/wt && got revert epsilon/zeta > /dev/null)
483 rm $testroot/wt/epsilon/zeta
485 echo 'D beta' > $testroot/stdout.expected
486 echo 'D epsilon/zeta' >> $testroot/stdout.expected
487 (cd $testroot/wt && got rm -R -s 'M!' . >$testroot/stdout)
489 cmp -s $testroot/stdout.expected $testroot/stdout
490 ret="$?"
491 if [ "$ret" != "0" ]; then
492 diff -u $testroot/stdout.expected $testroot/stdout
493 test_done "$testroot" "1"
494 return 1
495 fi
497 if [ -e $testroot/wt/beta ]; then
498 echo "removed file beta still exists on disk" >&2
499 test_done "$testroot" "1"
500 return 1
501 fi
503 echo 'D beta' > $testroot/stdout.expected
504 echo 'D epsilon/zeta' >> $testroot/stdout.expected
505 (cd $testroot/wt && got status > $testroot/stdout)
506 cmp -s $testroot/stdout.expected $testroot/stdout
507 ret="$?"
508 if [ "$ret" != "0" ]; then
509 diff -u $testroot/stdout.expected $testroot/stdout
510 test_done "$testroot" "1"
511 return 1
512 fi
514 test_done "$testroot" "$ret"
518 test_parseargs "$@"
519 run_test test_rm_basic
520 run_test test_rm_with_local_mods
521 run_test test_double_rm
522 run_test test_rm_and_add_elsewhere
523 run_test test_rm_directory
524 run_test test_rm_directory_keep_files
525 run_test test_rm_subtree
526 run_test test_rm_symlink
527 run_test test_rm_status_code