3 # Copyright (c) 2022 Omar Polo <op@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.
19 test_patch_simple_add_file() {
20 local testroot=`test_init patch_simple_add_file`
22 got checkout $testroot/repo $testroot/wt > /dev/null
24 if [ $ret -ne 0 ]; then
25 test_done $testroot $ret
29 cat <<EOF > $testroot/wt/patch
36 (cd $testroot/wt && got patch patch) > $testroot/stdout
38 if [ $ret -ne 0 ]; then
39 test_done $testroot $ret
43 echo "A eta" > $testroot/stdout.expected
44 cmp -s $testroot/stdout.expected $testroot/stdout
46 if [ $ret -ne 0 ]; then
47 diff -u $testroot/stdout.expected $testroot/stdout
48 test_done $testroot $ret
52 echo eta > $testroot/wt/eta.expected
53 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
55 if [ $ret -ne 0 ]; then
56 diff -u $testroot/wt/eta.expected $testroot/wt/eta
58 test_done $testroot $ret
61 test_patch_simple_rm_file() {
62 local testroot=`test_init patch_simple_rm_file`
64 got checkout $testroot/repo $testroot/wt > /dev/null
66 if [ $ret -ne 0 ]; then
67 test_done $testroot $ret
71 cat <<EOF > $testroot/wt/patch
78 echo "D alpha" > $testroot/stdout.expected
80 (cd $testroot/wt && got patch patch) > $testroot/stdout
82 if [ $ret -ne 0 ]; then
83 test_done $testroot $ret
87 cmp -s $testroot/stdout.expected $testroot/stdout
89 if [ $ret -ne 0 ]; then
90 diff -u $testroot/stdout.expected $testroot/stdout
91 test_done $testroot $ret
95 if [ -f $testroot/wt/alpha ]; then
97 echo "alpha still exists!"
99 test_done $testroot $ret
102 test_patch_simple_edit_file() {
103 local testroot=`test_init patch_simple_edit_file`
105 got checkout $testroot/repo $testroot/wt > /dev/null
107 if [ $ret -ne 0 ]; then
108 test_done $testroot $ret
112 cat <<EOF > $testroot/wt/patch
117 +alpha is my favourite character
120 echo "M alpha" > $testroot/stdout.expected
122 (cd $testroot/wt && got patch patch) > $testroot/stdout
124 if [ $ret -ne 0 ]; then
125 test_done $testroot $ret
129 cmp -s $testroot/stdout.expected $testroot/stdout
131 if [ $ret -ne 0 ]; then
132 diff -u $testroot/stdout.expected $testroot/stdout
133 test_done $testroot $ret
137 echo 'alpha is my favourite character' > $testroot/wt/alpha.expected
138 cmp -s $testroot/wt/alpha.expected $testroot/wt/alpha
140 if [ $ret -ne 0 ]; then
141 diff -u $testroot/wt/alpha.expected $testroot/wt/alpha
143 test_done $testroot $ret
146 test_patch_prepend_line() {
147 local testroot=`test_init patch_prepend_line`
149 got checkout $testroot/repo $testroot/wt > /dev/null
151 if [ $ret -ne 0 ]; then
152 test_done $testroot $ret
156 cat <<EOF > $testroot/wt/patch
164 echo "M alpha" > $testroot/stdout.expected
166 (cd $testroot/wt && got patch patch) > $testroot/stdout
168 if [ $ret -ne 0 ]; then
169 test_done $testroot $ret
173 cmp -s $testroot/stdout.expected $testroot/stdout
175 if [ $ret -ne 0 ]; then
176 diff -u $testroot/stdout.expected $testroot/stdout
177 test_done $testroot $ret
181 echo hatsuseno > $testroot/wt/alpha.expected
182 echo alpha >> $testroot/wt/alpha.expected
183 cmp -s $testroot/wt/alpha.expected $testroot/wt/alpha
185 if [ $ret -ne 0 ]; then
186 diff -u $testroot/wt/alpha.expected $testroot/wt/alpha
188 test_done $testroot $ret
191 test_patch_replace_line() {
192 local testroot=`test_init patch_replace_line`
194 got checkout $testroot/repo $testroot/wt > /dev/null
196 if [ $ret -ne 0 ]; then
197 test_done $testroot $ret
201 jot 10 > $testroot/wt/numbers
202 (cd $testroot/wt/ && got add numbers && got ci -m 'add numbers') \
205 if [ $ret -ne 0 ]; then
206 test_done $testroot $ret
210 cat <<EOF > $testroot/wt/patch
224 echo "M numbers" > $testroot/stdout.expected
226 (cd $testroot/wt && got patch patch) > $testroot/stdout
228 if [ $ret -ne 0 ]; then
229 test_done $testroot $ret
233 cmp -s $testroot/stdout.expected $testroot/stdout
235 if [ $ret -ne 0 ]; then
236 diff -u $testroot/stdout.expected $testroot/stdout
237 test_done $testroot $ret
241 jot 10 | sed 's/6/foo/' > $testroot/wt/numbers.expected
242 cmp -s $testroot/wt/numbers.expected $testroot/wt/numbers
244 if [ $ret -ne 0 ]; then
245 diff -u $testroot/wt/numbers.expected $testroot/wt/numbers
247 test_done $testroot $ret
250 test_patch_multiple_hunks() {
251 local testroot=`test_init patch_replace_multiple_hunks`
253 got checkout $testroot/repo $testroot/wt > /dev/null
255 if [ $ret -ne 0 ]; then
256 test_done $testroot $ret
260 jot 100 > $testroot/wt/numbers
261 (cd $testroot/wt/ && got add numbers && got ci -m 'add numbers') \
264 if [ $ret -ne 0 ]; then
265 test_done $testroot $ret
269 cat <<EOF > $testroot/wt/patch
299 echo "M numbers" > $testroot/stdout.expected
301 (cd $testroot/wt && got patch patch) > $testroot/stdout
303 if [ $ret -ne 0 ]; then
304 test_done $testroot $ret
308 cmp -s $testroot/stdout.expected $testroot/stdout
310 if [ $ret -ne 0 ]; then
311 diff -u $testroot/stdout.expected $testroot/stdout
312 test_done $testroot $ret
316 jot 100 | sed -e 's/^6$/foo/' -e 's/^60$/foo foo/' \
317 > $testroot/wt/numbers.expected
318 echo "101" >> $testroot/wt/numbers.expected
319 echo "102" >> $testroot/wt/numbers.expected
320 echo "..." >> $testroot/wt/numbers.expected
322 cmp -s $testroot/wt/numbers.expected $testroot/wt/numbers
324 if [ $ret -ne 0 ]; then
325 diff -u $testroot/wt/numbers.expected $testroot/wt/numbers
327 test_done $testroot $ret
330 test_patch_multiple_files() {
331 local testroot=`test_init patch_multiple_files`
333 got checkout $testroot/repo $testroot/wt > /dev/null
335 if [ $ret -ne 0 ]; then
336 test_done $testroot $ret
340 cat <<EOF > $testroot/wt/patch
341 --- alpha Mon Mar 7 19:02:07 2022
342 +++ alpha Mon Mar 7 19:01:53 2022
347 --- beta Mon Mar 7 19:02:11 2022
348 +++ beta Mon Mar 7 19:01:46 2022
353 --- gamma/delta Mon Mar 7 19:02:17 2022
354 +++ gamma/delta Mon Mar 7 19:01:37 2022
360 echo "M alpha" > $testroot/stdout.expected
361 echo "M beta" >> $testroot/stdout.expected
362 echo "M gamma/delta" >> $testroot/stdout.expected
364 (cd $testroot/wt && got patch patch) > $testroot/stdout
366 if [ $ret -ne 0 ]; then
367 test_done $testroot $ret
371 cmp -s $testroot/stdout.expected $testroot/stdout
373 if [ $ret -ne 0 ]; then
374 diff -u $testroot/stdout.expected $testroot/stdout
375 test_done $testroot $ret
379 printf 'new\nalpha\navailable\n' > $testroot/wt/alpha.expected
380 printf 'beta\nwas\nimproved\n' > $testroot/wt/beta.expected
381 printf 'delta new\n' > $testroot/wt/gamma/delta.expected
383 for f in alpha beta gamma/delta; do
384 cmp -s $testroot/wt/$f.expected $testroot/wt/$f
386 if [ $ret -ne 0 ]; then
387 diff -u $testroot/wt/$f.expected $testroot/wt/$f
388 test_done $testroot $ret
393 test_done $testroot 0
396 test_patch_dont_apply() {
397 local testroot=`test_init patch_dont_apply`
399 got checkout $testroot/repo $testroot/wt > /dev/null
401 if [ $ret -ne 0 ]; then
402 test_done $testroot $ret
406 cat <<EOF > $testroot/wt/patch
414 echo -n > $testroot/stdout.expected
415 echo "got: patch doesn't apply" > $testroot/stderr.expected
417 (cd $testroot/wt && got patch patch) \
421 if [ $ret -eq 0 ]; then # should fail
422 test_done $testroot 1
426 cmp -s $testroot/stdout.expected $testroot/stdout
428 if [ $ret -ne 0 ]; then
429 diff -u $testroot/stdout.expected $testroot/stdout
430 test_done $testroot $ret
434 cmp -s $testroot/stderr.expected $testroot/stderr
436 if [ $ret -ne 0 ]; then
437 diff -u $testroot/stderr.expected $testroot/stderr
438 test_done $testroot $ret
442 # try to delete a file with a patch that doesn't match
443 jot 100 > $testroot/wt/numbers
444 (cd $testroot/wt && got add numbers && got commit -m 'add numbers') \
447 if [ $ret -ne 0 ]; then
448 test_done $testroot $ret
452 cat <<EOF > $testroot/wt/patch
467 (cd $testroot/wt && got patch patch) > /dev/null 2> $testroot/stderr
469 if [ $ret -eq 0 ]; then # should fail
470 test_done $testroot 1
474 echo "got: patch doesn't apply" > $testroot/stderr.expected
475 cmp -s $testroot/stderr.expected $testroot/stderr
477 if [ $ret -ne 0 ]; then
478 diff -u $testroot/stderr.expected $testroot/stderr
480 test_done $testroot $ret
483 test_patch_malformed() {
484 local testroot=`test_init patch_malformed`
486 got checkout $testroot/repo $testroot/wt > /dev/null
488 if [ $ret -ne 0 ]; then
489 test_done $testroot $ret
494 cat <<EOF > $testroot/wt/patch
502 echo -n > $testroot/stdout.expected
503 echo "got: malformed patch" > $testroot/stderr.expected
505 (cd $testroot/wt && got patch patch) \
509 if [ $ret -eq 0 ]; then
510 echo "got managed to apply an invalid patch"
511 test_done $testroot 1
515 cmp -s $testroot/stdout.expected $testroot/stdout
517 if [ $ret -ne 0 ]; then
518 diff -u $testroot/stdout.expected $testroot/stdout
519 test_done $testroot $ret
523 cmp -s $testroot/stderr.expected $testroot/stderr
525 if [ $ret -ne 0 ]; then
526 diff -u $testroot/stderr.expected $testroot/stderr
527 test_done $testroot $ret
531 # wrong first character
532 cat <<EOF > $testroot/wt/patch
540 (cd $testroot/wt && got patch patch) \
544 if [ $ret -eq 0 ]; then
545 echo "got managed to apply an invalid patch"
546 test_done $testroot 1
550 cmp -s $testroot/stdout.expected $testroot/stdout
552 if [ $ret -ne 0 ]; then
553 diff -u $testroot/stdout.expected $testroot/stdout
554 test_done $testroot $ret
558 cmp -s $testroot/stderr.expected $testroot/stderr
560 if [ $ret -ne 0 ]; then
561 diff -u $testroot/stderr.expected $testroot/stderr
562 test_done $testroot $ret
566 test_done $testroot $ret
569 test_patch_no_patch() {
570 local testroot=`test_init patch_no_patch`
572 got checkout $testroot/repo $testroot/wt > /dev/null
574 if [ $ret -ne 0 ]; then
575 test_done $testroot $ret
579 cat <<EOF > $testroot/wt/patch
586 there's no patch in here!
589 echo -n > $testroot/stdout.expected
590 echo "got: no patch found" > $testroot/stderr.expected
592 (cd $testroot/wt && got patch patch) \
596 if [ $ret -eq 0 ]; then # should fail
597 test_done $testroot 1
601 cmp -s $testroot/stdout.expected $testroot/stdout
603 if [ $ret -ne 0 ]; then
604 diff -u $testroot/stdout.expected $testroot/stdout
605 test_done $testroot $ret
609 cmp -s $testroot/stderr.expected $testroot/stderr
611 if [ $ret -ne 0 ]; then
612 diff -u $testroot/stderr.expected $testroot/stderr
613 test_done $testroot $ret
617 test_done $testroot $ret
620 test_patch_equals_for_context() {
621 local testroot=`test_init patch_prepend_line`
623 got checkout $testroot/repo $testroot/wt > /dev/null
625 if [ $ret -ne 0 ]; then
626 test_done $testroot $ret
630 cat <<EOF > $testroot/wt/patch
638 echo "M alpha" > $testroot/stdout.expected
640 (cd $testroot/wt && got patch patch) > $testroot/stdout
642 if [ $ret -ne 0 ]; then
643 test_done $testroot $ret
647 cmp -s $testroot/stdout.expected $testroot/stdout
649 if [ $ret -ne 0 ]; then
650 diff -u $testroot/stdout.expected $testroot/stdout
651 test_done $testroot $ret
655 echo hatsuseno > $testroot/wt/alpha.expected
656 echo alpha >> $testroot/wt/alpha.expected
657 cmp -s $testroot/wt/alpha.expected $testroot/wt/alpha
659 if [ $ret -ne 0 ]; then
660 diff -u $testroot/wt/alpha.expected $testroot/wt/alpha
662 test_done $testroot $ret
665 test_patch_rename() {
666 local testroot=`test_init patch_rename`
668 got checkout $testroot/repo $testroot/wt > /dev/null
670 if [ $ret -ne 0 ]; then
671 test_done $testroot $ret
675 cat <<EOF > $testroot/wt/patch
681 echo 'D alpha' > $testroot/stdout.expected
682 echo 'A eta' >> $testroot/stdout.expected
684 (cd $testroot/wt && got patch patch) > $testroot/stdout
686 if [ $ret -ne 0 ]; then
687 test_done $testroot $ret
691 cmp -s $testroot/stdout.expected $testroot/stdout
693 if [ $ret -ne 0 ]; then
694 diff -u $testroot/stdout.expected $testroot/stdout
695 test_done $testroot $ret
699 if [ -f $testroot/wt/alpha ]; then
700 echo "alpha was not removed" >&2
701 test_done $testroot 1
704 if [ ! -f $testroot/wt/eta ]; then
705 echo "eta was not created" >&2
706 test_done $testroot 1
710 echo alpha > $testroot/wt/eta.expected
711 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
713 if [ $ret -ne 0 ]; then
714 diff -u $testroot/wt/eta.expected $testroot/wt/eta
715 test_done $testroot $ret
719 # revert the changes and try again with a rename + edit
720 (cd $testroot/wt && got revert alpha eta) > /dev/null
722 if [ $ret -ne 0 ]; then
723 test_done $testroot $ret
728 cat <<EOF > $testroot/wt/patch
736 (cd $testroot/wt && got patch patch) > $testroot/stdout
738 if [ $ret -ne 0 ]; then
739 test_done $testroot $ret
743 cmp -s $testroot/stdout.expected $testroot/stdout
745 if [ $ret -ne 0 ]; then
746 diff -u $testroot/stdout.expected $testroot/stdout
747 test_done $testroot $ret
751 if [ -f $testroot/wt/alpha ]; then
752 echo "alpha was not removed" >&2
753 test_done $testroot 1
756 if [ ! -f $testroot/wt/eta ]; then
757 echo "eta was not created" >&2
758 test_done $testroot 1
762 echo alpha > $testroot/wt/eta.expected
763 echo 'but now is eta' >> $testroot/wt/eta.expected
764 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
766 if [ $ret -ne 0 ]; then
767 diff -u $testroot/wt/eta.expected $testroot/wt/eta
769 test_done $testroot $ret
772 test_patch_illegal_status() {
773 local testroot=`test_init patch_illegal_status`
775 got checkout $testroot/repo $testroot/wt > /dev/null
777 if [ $ret -ne 0 ]; then
778 test_done $testroot $ret
782 # edit an non-existent and unknown file
783 cat <<EOF > $testroot/wt/patch
791 (cd $testroot/wt && got patch patch) > /dev/null \
794 if [ $ret -eq 0 ]; then
795 echo "edited a missing file" >&2
796 test_done $testroot $ret
800 echo "got: iota: No such file or directory" \
801 > $testroot/stderr.expected
802 cmp -s $testroot/stderr.expected $testroot/stderr
804 if [ $ret -ne 0 ]; then
805 diff -u $testroot/stderr.expected $testroot/stderr
806 test_done $testroot $ret
810 # create iota and re-try
811 echo iota > $testroot/wt/iota
813 (cd $testroot/wt && got patch patch) > /dev/null \
816 if [ $ret -eq 0 ]; then
817 echo "patched an unknown file" >&2
818 test_done $testroot $ret
822 echo "got: iota: file has unexpected status" \
823 > $testroot/stderr.expected
824 cmp -s $testroot/stderr.expected $testroot/stderr
826 if [ $ret -ne 0 ]; then
827 diff -u $testroot/stderr.expected $testroot/stderr
828 test_done $testroot $ret
834 if [ $ret -ne 0 ]; then
835 test_done $testroot $ret
839 # edit obstructed file
840 rm $testroot/wt/alpha
841 mkdir $testroot/wt/alpha
842 cat <<EOF > $testroot/wt/patch
850 (cd $testroot/wt && got patch patch) > /dev/null \
853 if [ $ret -eq 0 ]; then
854 echo "edited a missing file" >&2
855 test_done $testroot $ret
859 echo "got: alpha: file has unexpected status" \
860 > $testroot/stderr.expected
861 cmp -s $testroot/stderr.expected $testroot/stderr
863 if [ $ret -ne 0 ]; then
864 diff -u $testroot/stderr.expected $testroot/stderr
865 test_done $testroot $ret
869 # delete an unknown file
870 cat <<EOF > $testroot/wt/patch
877 (cd $testroot/wt && got patch patch) > /dev/null \
880 if [ $ret -eq 0 ]; then
881 echo "deleted a missing file?" >&2
882 test_done $testroot $ret
886 echo "got: iota: No such file or directory" \
887 > $testroot/stderr.expected
888 cmp -s $testroot/stderr.expected $testroot/stderr
890 if [ $ret -eq 0 ]; then
891 diff -u $testroot/stderr.expected $testroot/stderr
892 test_done $testroot $ret
896 # try again with iota in place but still not registered
897 echo iota > $testroot/wt/iota
898 (cd $testroot/wt && got patch patch) > /dev/null \
901 if [ $ret -eq 0 ]; then
902 echo "deleted an unversioned file?" >&2
903 test_done $testroot $ret
907 echo "got: iota: file has unexpected status" \
908 > $testroot/stderr.expected
909 cmp -s $testroot/stderr.expected $testroot/stderr
911 if [ $ret -eq 0 ]; then
912 diff -u $testroot/stderr.expected $testroot/stderr
914 test_done $testroot $ret
918 local testroot=`test_init patch_nop`
920 got checkout $testroot/repo $testroot/wt > /dev/null
922 if [ $ret -ne 0 ]; then
923 test_done $testroot $ret
927 cat <<EOF > $testroot/wt/patch
941 +delta updated and renamed!
944 (cd $testroot/wt && got patch -n patch)
946 if [ $ret -ne 0 ]; then
947 test_done $testroot $ret
951 # remove the patch to avoid the ? entry
952 rm $testroot/wt/patch
954 (cd $testroot/wt && got status) > $testroot/stdout
956 if [ $ret -ne 0 ]; then
957 test_done $testroot $ret
961 echo -n > $testroot/stdout.expected
962 cmp -s $testroot/stdout.expected $testroot/stdout
964 if [ $ret -ne 0 ]; then
965 diff -u $testroot/stdout.expected $testroot/stdout
967 test_done $testroot $ret
971 run_test test_patch_simple_add_file
972 run_test test_patch_simple_rm_file
973 run_test test_patch_simple_edit_file
974 run_test test_patch_prepend_line
975 run_test test_patch_replace_line
976 run_test test_patch_multiple_hunks
977 run_test test_patch_multiple_files
978 run_test test_patch_dont_apply
979 run_test test_patch_malformed
980 run_test test_patch_no_patch
981 run_test test_patch_equals_for_context
982 run_test test_patch_rename
983 run_test test_patch_illegal_status
984 run_test test_patch_nop