3 # Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
20 local testroot=`test_init rm_basic`
22 got checkout $testroot/repo $testroot/wt > /dev/null
24 if [ "$ret" != "0" ]; then
25 test_done "$testroot" "$ret"
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
35 if [ "$ret" != "0" ]; then
36 diff -u $testroot/stdout.expected $testroot/stdout
37 test_done "$testroot" "$ret"
41 (cd $testroot/wt && got status > $testroot/stdout)
43 cmp -s $testroot/stdout.expected $testroot/stdout
45 if [ "$ret" != "0" ]; then
46 diff -u $testroot/stdout.expected $testroot/stdout
47 test_done "$testroot" "$ret"
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"
59 test_done "$testroot" "0"
62 test_rm_with_local_mods() {
63 local testroot=`test_init rm_with_local_mods`
65 got checkout $testroot/repo $testroot/wt > /dev/null
67 if [ "$ret" != "0" ]; then
68 test_done "$testroot" "$ret"
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
79 if [ "$ret" != "0" ]; then
80 diff -u $testroot/stderr.expected $testroot/stderr
81 test_done "$testroot" "$ret"
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
90 if [ "$ret" != "0" ]; then
91 diff -u $testroot/stdout.expected $testroot/stdout
94 if [ -e $testroot/wt/beta ]; then
95 echo "removed file beta still exists on disk" >&2
96 test_done "$testroot" "1"
100 test_done "$testroot" "$ret"
104 local testroot=`test_init double_rm`
106 got checkout $testroot/repo $testroot/wt > /dev/null
108 if [ "$ret" != "0" ]; then
109 test_done "$testroot" "$ret"
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 \
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"
126 echo -n > $testroot/stdout.expected
127 cmp -s $testroot/stdout.expected $testroot/stdout
129 if [ "$ret" != "0" ]; then
130 diff -u $testroot/stdout.expected $testroot/stdout
131 test_done "$testroot" "$ret"
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
143 if [ "$ret" != "0" ]; then
144 test_done "$testroot" "$ret"
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
156 if [ "$ret" != "0" ]; then
157 diff -u $testroot/stdout.expected $testroot/stdout
158 test_done "$testroot" "$ret"
162 (cd $testroot/wt && got rm alpha > $testroot/stdout 2> $testroot/stderr)
164 if [ "$ret" == "0" ]; then
165 echo "got rm command succeeded unexpectedly" >&2
166 diff -u $testroot/stderr.expected $testroot/stderr
167 test_done "$testroot" "1"
171 echo -n '' > $testroot/stdout.expected
172 cmp -s $testroot/stdout.expected $testroot/stdout
174 if [ "$ret" != "0" ]; then
175 diff -u $testroot/stdout.expected $testroot/stdout
176 test_done "$testroot" "$ret"
180 echo "got: alpha: No such file or directory" \
181 > $testroot/stderr.expected
182 cmp -s $testroot/stderr.expected $testroot/stderr
184 if [ "$ret" != "0" ]; then
185 diff -u $testroot/stderr.expected $testroot/stderr
186 test_done "$testroot" "$ret"
190 (cd $testroot/wt && got rm -f alpha > $testroot/stdout)
192 if [ "$ret" != "0" ]; then
193 echo "got rm command failed unexpectedly" >&2
194 diff -u $testroot/stderr.expected $testroot/stderr
195 test_done "$testroot" "$ret"
199 echo 'D alpha' > $testroot/stdout.expected
200 cmp -s $testroot/stdout.expected $testroot/stdout
202 if [ "$ret" != "0" ]; then
203 diff -u $testroot/stdout.expected $testroot/stdout
204 test_done "$testroot" "$ret"
208 cmp -s $testroot/stdout.expected $testroot/stdout
210 if [ "$ret" != "0" ]; then
211 diff -u $testroot/stdout.expected $testroot/stdout
212 test_done "$testroot" "$ret"
216 # While here, test behaviour of rm on files in unversioned status.
217 (cd $testroot/wt && got rm epsilon/alpha > $testroot/stdout \
220 if [ "$ret" == "0" ]; then
221 echo "got rm command succeeded unexpectedly" >&2
222 diff -u $testroot/stderr.expected $testroot/stderr
223 test_done "$testroot" "1"
227 echo -n '' > $testroot/stdout.expected
228 cmp -s $testroot/stdout.expected $testroot/stdout
230 if [ "$ret" != "0" ]; then
231 diff -u $testroot/stdout.expected $testroot/stdout
232 test_done "$testroot" "$ret"
236 echo "got: epsilon/alpha: file has unexpected status" \
237 > $testroot/stderr.expected
238 cmp -s $testroot/stderr.expected $testroot/stderr
240 if [ "$ret" != "0" ]; then
241 diff -u $testroot/stderr.expected $testroot/stderr
242 test_done "$testroot" "$ret"
246 # And test the same case with -f.
247 (cd $testroot/wt && got rm -f epsilon/alpha > $testroot/stdout \
250 if [ "$ret" == "0" ]; then
251 echo "got rm command succeeded unexpectedly" >&2
252 diff -u $testroot/stderr.expected $testroot/stderr
253 test_done "$testroot" "1"
257 echo -n '' > $testroot/stdout.expected
258 cmp -s $testroot/stdout.expected $testroot/stdout
260 if [ "$ret" != "0" ]; then
261 diff -u $testroot/stdout.expected $testroot/stdout
262 test_done "$testroot" "$ret"
266 echo "got: epsilon/alpha: file has unexpected status" \
267 > $testroot/stderr.expected
268 cmp -s $testroot/stderr.expected $testroot/stderr
270 if [ "$ret" != "0" ]; then
271 diff -u $testroot/stderr.expected $testroot/stderr
272 test_done "$testroot" "$ret"
276 echo 'A epsilon/alpha' > $testroot/stdout.expected
277 (cd $testroot/wt && got add epsilon/alpha > $testroot/stdout)
279 cmp -s $testroot/stdout.expected $testroot/stdout
281 if [ "$ret" != "0" ]; then
282 diff -u $testroot/stdout.expected $testroot/stdout
283 test_done "$testroot" "$ret"
287 (cd $testroot/wt && got status > $testroot/stdout)
289 echo 'D alpha' > $testroot/stdout.expected
290 echo 'A epsilon/alpha' >> $testroot/stdout.expected
291 cmp -s $testroot/stdout.expected $testroot/stdout
293 if [ "$ret" != "0" ]; then
294 diff -u $testroot/stdout.expected $testroot/stdout
296 test_done "$testroot" "$ret"
299 test_rm_directory() {
300 local testroot=`test_init rm_directory`
302 got checkout $testroot/repo $testroot/wt > /dev/null
304 if [ "$ret" != "0" ]; then
305 test_done "$testroot" "$ret"
309 (cd $testroot/wt && got rm . > $testroot/stdout 2> $testroot/stderr)
311 echo "got: removing directories requires -R option" \
312 > $testroot/stderr.expected
313 cmp -s $testroot/stderr.expected $testroot/stderr
315 if [ "$ret" != "0" ]; then
316 diff -u $testroot/stderr.expected $testroot/stderr
317 test_done "$testroot" "$ret"
321 echo -n > $testroot/stdout.expected
322 cmp -s $testroot/stdout.expected $testroot/stdout
324 if [ "$ret" != "0" ]; then
325 diff -u $testroot/stdout.expected $testroot/stdout
326 test_done "$testroot" "$ret"
330 (cd $testroot/wt && got rm -R . > $testroot/stdout)
332 echo 'D alpha' > $testroot/stdout.expected
333 echo 'D beta' >> $testroot/stdout.expected
334 echo 'D epsilon/zeta' >> $testroot/stdout.expected
335 echo 'D gamma/delta' >> $testroot/stdout.expected
337 cmp -s $testroot/stdout.expected $testroot/stdout
339 if [ "$ret" != "0" ]; then
340 diff -u $testroot/stdout.expected $testroot/stdout
341 test_done "$testroot" "$ret"
345 (cd $testroot/wt && ls -l | sed '/^total/d' > $testroot/stdout)
347 echo -n '' > $testroot/stdout.expected
349 cmp -s $testroot/stdout.expected $testroot/stdout
351 if [ "$ret" != "0" ]; then
352 diff -u $testroot/stdout.expected $testroot/stdout
353 test_done "$testroot" "$ret"
357 (cd $testroot/wt && ls -l | sed '/^total/d' > $testroot/stdout)
359 echo -n '' > $testroot/stdout.expected
361 cmp -s $testroot/stdout.expected $testroot/stdout
363 if [ "$ret" != "0" ]; then
364 diff -u $testroot/stdout.expected $testroot/stdout
365 test_done "$testroot" "$ret"
369 test_done "$testroot" "$ret"
372 test_rm_directory_keep_files() {
373 local testroot=`test_init rm_directory_keep_files`
375 got checkout $testroot/repo $testroot/wt > /dev/null
377 if [ "$ret" != "0" ]; then
378 test_done "$testroot" "$ret"
382 (cd $testroot/wt && got rm . > $testroot/stdout 2> $testroot/stderr)
384 echo "got: removing directories requires -R option" \
385 > $testroot/stderr.expected
386 cmp -s $testroot/stderr.expected $testroot/stderr
388 if [ "$ret" != "0" ]; then
389 diff -u $testroot/stderr.expected $testroot/stderr
390 test_done "$testroot" "$ret"
394 echo -n > $testroot/stdout.expected
395 cmp -s $testroot/stdout.expected $testroot/stdout
397 if [ "$ret" != "0" ]; then
398 diff -u $testroot/stdout.expected $testroot/stdout
399 test_done "$testroot" "$ret"
403 (cd $testroot/wt && got rm -k -R . > $testroot/stdout)
405 echo 'D alpha' > $testroot/stdout.expected
406 echo 'D beta' >> $testroot/stdout.expected
407 echo 'D epsilon/zeta' >> $testroot/stdout.expected
408 echo 'D gamma/delta' >> $testroot/stdout.expected
410 cmp -s $testroot/stdout.expected $testroot/stdout
412 if [ "$ret" != "0" ]; then
413 diff -u $testroot/stdout.expected $testroot/stdout
414 test_done "$testroot" "$ret"
418 (cd $testroot/wt && got st . > $testroot/stdout)
420 echo 'D alpha' > $testroot/stdout.expected
421 echo 'D beta' >> $testroot/stdout.expected
422 echo 'D epsilon/zeta' >> $testroot/stdout.expected
423 echo 'D gamma/delta' >> $testroot/stdout.expected
425 cmp -s $testroot/stdout.expected $testroot/stdout
427 if [ "$ret" != "0" ]; then
428 diff -u $testroot/stdout.expected $testroot/stdout
429 test_done "$testroot" "$ret"
433 (cd $testroot/wt && got commit -m "keep" > /dev/null)
434 (cd $testroot/wt && got st . > $testroot/stdout)
436 echo '? alpha' > $testroot/stdout.expected
437 echo '? beta' >> $testroot/stdout.expected
438 echo '? epsilon/zeta' >> $testroot/stdout.expected
439 echo '? gamma/delta' >> $testroot/stdout.expected
441 cmp -s $testroot/stdout.expected $testroot/stdout
443 if [ "$ret" != "0" ]; then
444 diff -u $testroot/stdout.expected $testroot/stdout
445 test_done "$testroot" "$ret"
449 test_done "$testroot" "$ret"
453 local testroot=`test_init rm_subtree`
455 got checkout $testroot/repo $testroot/wt > /dev/null
457 if [ "$ret" != "0" ]; then
458 test_done "$testroot" "$ret"
462 mkdir -p $testroot/wt/epsilon/foo/bar/baz
463 mkdir -p $testroot/wt/epsilon/foo/bar/bax
464 echo "new file" > $testroot/wt/epsilon/foo/a.o
465 echo "new file" > $testroot/wt/epsilon/foo/a.o
466 echo "new file" > $testroot/wt/epsilon/foo/bar/b.o
467 echo "new file" > $testroot/wt/epsilon/foo/bar/b.d
468 echo "new file" > $testroot/wt/epsilon/foo/bar/baz/f.o
469 echo "new file" > $testroot/wt/epsilon/foo/bar/baz/f.d
470 echo "new file" > $testroot/wt/epsilon/foo/bar/baz/c.o
471 echo "new file" > $testroot/wt/epsilon/foo/bar/baz/c.d
472 echo "new file" > $testroot/wt/epsilon/foo/bar/bax/e.o
473 echo "new file" > $testroot/wt/epsilon/foo/bar/bax/e.d
474 echo "new file" > $testroot/wt/epsilon/foo/bar/bax/x.o
475 echo "new file" > $testroot/wt/epsilon/foo/bar/bax/x.d
476 (cd $testroot/wt && got add -R epsilon >/dev/null)
477 (cd $testroot/wt && got commit -m "add subtree" >/dev/null)
479 # now delete and revert the entire subtree
480 (cd $testroot/wt && got rm -R epsilon/foo >/dev/null)
482 if [ -d $testroot/wt/epsilon/foo ]; then
483 echo "removed dir epsilon/foo still exists on disk" >&2
484 test_done "$testroot" "1"
488 echo "D epsilon/foo/a.o" > $testroot/stdout.expected
489 echo "D epsilon/foo/bar/b.d" >> $testroot/stdout.expected
490 echo "D epsilon/foo/bar/b.o" >> $testroot/stdout.expected
491 echo "D epsilon/foo/bar/bax/e.d" >> $testroot/stdout.expected
492 echo "D epsilon/foo/bar/bax/e.o" >> $testroot/stdout.expected
493 echo "D epsilon/foo/bar/bax/x.d" >> $testroot/stdout.expected
494 echo "D epsilon/foo/bar/bax/x.o" >> $testroot/stdout.expected
495 echo "D epsilon/foo/bar/baz/c.d" >> $testroot/stdout.expected
496 echo "D epsilon/foo/bar/baz/c.o" >> $testroot/stdout.expected
497 echo "D epsilon/foo/bar/baz/f.d" >> $testroot/stdout.expected
498 echo "D epsilon/foo/bar/baz/f.o" >> $testroot/stdout.expected
500 (cd $testroot/wt && got status > $testroot/stdout)
502 cmp -s $testroot/stdout.expected $testroot/stdout
504 if [ "$ret" != "0" ]; then
505 diff -u $testroot/stdout.expected $testroot/stdout
507 test_done "$testroot" "$ret"
511 local testroot=`test_init rm_symlink`
513 (cd $testroot/repo && ln -s alpha alpha.link)
514 (cd $testroot/repo && ln -s epsilon epsilon.link)
515 (cd $testroot/repo && ln -s /etc/passwd passwd.link)
516 (cd $testroot/repo && ln -s ../beta epsilon/beta.link)
517 (cd $testroot/repo && ln -s nonexistent nonexistent.link)
518 (cd $testroot/repo && git add .)
519 git_commit $testroot/repo -m "add symlinks"
521 got checkout $testroot/repo $testroot/wt > /dev/null
523 if [ "$ret" != "0" ]; then
524 test_done "$testroot" "$ret"
528 echo 'D alpha.link' > $testroot/stdout.expected
529 echo 'D epsilon/beta.link' >> $testroot/stdout.expected
530 echo 'D epsilon.link' >> $testroot/stdout.expected
531 echo 'D nonexistent.link' >> $testroot/stdout.expected
532 echo 'D passwd.link' >> $testroot/stdout.expected
533 (cd $testroot/wt && got rm alpha.link epsilon.link passwd.link \
534 epsilon/beta.link nonexistent.link > $testroot/stdout)
536 cmp -s $testroot/stdout.expected $testroot/stdout
538 if [ "$ret" != "0" ]; then
539 diff -u $testroot/stdout.expected $testroot/stdout
541 test_done "$testroot" "$ret"
544 test_rm_status_code() {
545 local testroot=`test_init rm_status_code`
547 got checkout $testroot/repo $testroot/wt > /dev/null
549 if [ "$ret" != "0" ]; then
550 test_done "$testroot" "$ret"
554 echo "modified beta" > $testroot/wt/beta
556 echo "got: invalid status code 'x'" > $testroot/stderr.expected
557 (cd $testroot/wt && got rm -s Mx beta 2>$testroot/stderr)
559 cmp -s $testroot/stderr.expected $testroot/stderr
561 if [ "$ret" != "0" ]; then
562 diff -u $testroot/stderr.expected $testroot/stderr
563 test_done "$testroot" "$ret"
567 rm $testroot/wt/epsilon/zeta # put file into 'missing' status
569 echo 'D epsilon/zeta' > $testroot/stdout.expected
570 (cd $testroot/wt && got rm -R -s '!' . >$testroot/stdout)
572 cmp -s $testroot/stdout.expected $testroot/stdout
574 if [ "$ret" != "0" ]; then
575 diff -u $testroot/stdout.expected $testroot/stdout
578 if [ ! -e $testroot/wt/beta ]; then
579 echo "file beta was unexpectedly removed from disk" >&2
580 test_done "$testroot" "1"
584 # put file into 'missing' status again
585 (cd $testroot/wt && got revert epsilon/zeta > /dev/null)
586 rm $testroot/wt/epsilon/zeta
588 echo 'D beta' > $testroot/stdout.expected
589 echo 'D epsilon/zeta' >> $testroot/stdout.expected
590 (cd $testroot/wt && got rm -R -s 'M!' . >$testroot/stdout)
592 cmp -s $testroot/stdout.expected $testroot/stdout
594 if [ "$ret" != "0" ]; then
595 diff -u $testroot/stdout.expected $testroot/stdout
596 test_done "$testroot" "1"
600 if [ -e $testroot/wt/beta ]; then
601 echo "removed file beta still exists on disk" >&2
602 test_done "$testroot" "1"
606 echo 'D beta' > $testroot/stdout.expected
607 echo 'D epsilon/zeta' >> $testroot/stdout.expected
608 (cd $testroot/wt && got status > $testroot/stdout)
609 cmp -s $testroot/stdout.expected $testroot/stdout
611 if [ "$ret" != "0" ]; then
612 diff -u $testroot/stdout.expected $testroot/stdout
613 test_done "$testroot" "1"
617 test_done "$testroot" "$ret"
620 test_rm_nonexistent_directory() {
621 local testroot=`test_init rm_nonexistent_directory`
623 got checkout $testroot/repo $testroot/wt > /dev/null
625 if [ "$ret" != "0" ]; then
626 test_done "$testroot" "$ret"
630 rm -r $testroot/wt/epsilon
632 (cd $testroot/wt && got rm epsilon > $testroot/stdout \
635 if [ "$ret" == "0" ]; then
636 echo "got rm command succeeded unexpectedly" >&2
637 diff -u $testroot/stderr.expected $testroot/stderr
638 test_done "$testroot" "1"
642 echo -n '' > $testroot/stdout.expected
643 cmp -s $testroot/stdout.expected $testroot/stdout
645 if [ "$ret" != "0" ]; then
646 diff -u $testroot/stdout.expected $testroot/stdout
647 test_done "$testroot" "$ret"
651 echo "got: epsilon: No such file or directory" \
652 > $testroot/stderr.expected
653 cmp -s $testroot/stderr.expected $testroot/stderr
655 if [ "$ret" != "0" ]; then
656 diff -u $testroot/stderr.expected $testroot/stderr
657 test_done "$testroot" "$ret"
661 (cd $testroot/wt && got rm -f epsilon > $testroot/stdout \
664 if [ "$ret" != "0" ]; then
665 echo "got rm command failed unexpectedly" >&2
666 diff -u $testroot/stderr.expected $testroot/stderr
667 test_done "$testroot" "$ret"
671 echo -n '' > $testroot/stdout.expected
672 cmp -s $testroot/stdout.expected $testroot/stdout
674 if [ "$ret" != "0" ]; then
675 diff -u $testroot/stdout.expected $testroot/stdout
676 test_done "$testroot" "$ret"
680 echo -n '' > $testroot/stderr.expected
681 cmp -s $testroot/stderr.expected $testroot/stderr
683 if [ "$ret" != "0" ]; then
684 diff -u $testroot/stderr.expected $testroot/stderr
685 test_done "$testroot" "$ret"
689 test_done "$testroot" "$ret"
694 run_test test_rm_basic
695 run_test test_rm_with_local_mods
696 run_test test_double_rm
697 run_test test_rm_and_add_elsewhere
698 run_test test_rm_directory
699 run_test test_rm_directory_keep_files
700 run_test test_rm_subtree
701 run_test test_rm_symlink
702 run_test test_rm_status_code
703 run_test test_rm_nonexistent_directory