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 jot 100 > $testroot/wt/numbers
407 (cd $testroot/wt && got add numbers && got commit -m 'add numbers') \
410 if [ $ret -ne 0 ]; then
411 test_done $testroot $ret
415 cat <<EOF > $testroot/wt/patch
435 (cd $testroot/wt && got patch patch) > $testroot/stdout 2> /dev/null
437 if [ $ret -eq 0 ]; then # should fail
438 test_done $testroot 1
442 cat <<EOF > $testroot/stdout.expected
444 @@ -1,1 +1,2 @@ hunk failed to apply
446 @@ -1,9 +0,0 @@ hunk failed to apply
449 cmp -s $testroot/stdout.expected $testroot/stdout
451 if [ $ret -ne 0 ]; then
452 diff -u $testroot/stdout.expected $testroot/stdout
454 test_done $testroot $ret
457 test_patch_malformed() {
458 local testroot=`test_init patch_malformed`
460 got checkout $testroot/repo $testroot/wt > /dev/null
462 if [ $ret -ne 0 ]; then
463 test_done $testroot $ret
468 cat <<EOF > $testroot/wt/patch
476 echo -n > $testroot/stdout.expected
477 echo "got: malformed patch" > $testroot/stderr.expected
479 (cd $testroot/wt && got patch patch) \
483 if [ $ret -eq 0 ]; then
484 echo "got managed to apply an invalid patch"
485 test_done $testroot 1
489 cmp -s $testroot/stdout.expected $testroot/stdout
491 if [ $ret -ne 0 ]; then
492 diff -u $testroot/stdout.expected $testroot/stdout
493 test_done $testroot $ret
497 cmp -s $testroot/stderr.expected $testroot/stderr
499 if [ $ret -ne 0 ]; then
500 diff -u $testroot/stderr.expected $testroot/stderr
501 test_done $testroot $ret
505 # wrong first character
506 cat <<EOF > $testroot/wt/patch
514 (cd $testroot/wt && got patch patch) \
518 if [ $ret -eq 0 ]; then
519 echo "got managed to apply an invalid patch"
520 test_done $testroot 1
524 cmp -s $testroot/stdout.expected $testroot/stdout
526 if [ $ret -ne 0 ]; then
527 diff -u $testroot/stdout.expected $testroot/stdout
528 test_done $testroot $ret
532 cmp -s $testroot/stderr.expected $testroot/stderr
534 if [ $ret -ne 0 ]; then
535 diff -u $testroot/stderr.expected $testroot/stderr
536 test_done $testroot $ret
540 test_done $testroot $ret
543 test_patch_no_patch() {
544 local testroot=`test_init patch_no_patch`
546 got checkout $testroot/repo $testroot/wt > /dev/null
548 if [ $ret -ne 0 ]; then
549 test_done $testroot $ret
553 cat <<EOF > $testroot/wt/patch
560 there's no patch in here!
563 echo -n > $testroot/stdout.expected
564 echo "got: no patch found" > $testroot/stderr.expected
566 (cd $testroot/wt && got patch patch) \
570 if [ $ret -eq 0 ]; then # should fail
571 test_done $testroot 1
575 cmp -s $testroot/stdout.expected $testroot/stdout
577 if [ $ret -ne 0 ]; then
578 diff -u $testroot/stdout.expected $testroot/stdout
579 test_done $testroot $ret
583 cmp -s $testroot/stderr.expected $testroot/stderr
585 if [ $ret -ne 0 ]; then
586 diff -u $testroot/stderr.expected $testroot/stderr
587 test_done $testroot $ret
591 test_done $testroot $ret
594 test_patch_equals_for_context() {
595 local testroot=`test_init patch_prepend_line`
597 got checkout $testroot/repo $testroot/wt > /dev/null
599 if [ $ret -ne 0 ]; then
600 test_done $testroot $ret
604 cat <<EOF > $testroot/wt/patch
612 echo "M alpha" > $testroot/stdout.expected
614 (cd $testroot/wt && got patch patch) > $testroot/stdout
616 if [ $ret -ne 0 ]; then
617 test_done $testroot $ret
621 cmp -s $testroot/stdout.expected $testroot/stdout
623 if [ $ret -ne 0 ]; then
624 diff -u $testroot/stdout.expected $testroot/stdout
625 test_done $testroot $ret
629 echo hatsuseno > $testroot/wt/alpha.expected
630 echo alpha >> $testroot/wt/alpha.expected
631 cmp -s $testroot/wt/alpha.expected $testroot/wt/alpha
633 if [ $ret -ne 0 ]; then
634 diff -u $testroot/wt/alpha.expected $testroot/wt/alpha
636 test_done $testroot $ret
639 test_patch_rename() {
640 local testroot=`test_init patch_rename`
642 got checkout $testroot/repo $testroot/wt > /dev/null
644 if [ $ret -ne 0 ]; then
645 test_done $testroot $ret
649 cat <<EOF > $testroot/wt/patch
655 echo 'D alpha' > $testroot/stdout.expected
656 echo 'A eta' >> $testroot/stdout.expected
658 (cd $testroot/wt && got patch patch) > $testroot/stdout
660 if [ $ret -ne 0 ]; then
661 test_done $testroot $ret
665 cmp -s $testroot/stdout.expected $testroot/stdout
667 if [ $ret -ne 0 ]; then
668 diff -u $testroot/stdout.expected $testroot/stdout
669 test_done $testroot $ret
673 if [ -f $testroot/wt/alpha ]; then
674 echo "alpha was not removed" >&2
675 test_done $testroot 1
678 if [ ! -f $testroot/wt/eta ]; then
679 echo "eta was not created" >&2
680 test_done $testroot 1
684 echo alpha > $testroot/wt/eta.expected
685 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
687 if [ $ret -ne 0 ]; then
688 diff -u $testroot/wt/eta.expected $testroot/wt/eta
689 test_done $testroot $ret
693 # revert the changes and try again with a rename + edit
694 (cd $testroot/wt && got revert alpha eta) > /dev/null
696 if [ $ret -ne 0 ]; then
697 test_done $testroot $ret
702 cat <<EOF > $testroot/wt/patch
710 (cd $testroot/wt && got patch patch) > $testroot/stdout
712 if [ $ret -ne 0 ]; then
713 test_done $testroot $ret
717 cmp -s $testroot/stdout.expected $testroot/stdout
719 if [ $ret -ne 0 ]; then
720 diff -u $testroot/stdout.expected $testroot/stdout
721 test_done $testroot $ret
725 if [ -f $testroot/wt/alpha ]; then
726 echo "alpha was not removed" >&2
727 test_done $testroot 1
730 if [ ! -f $testroot/wt/eta ]; then
731 echo "eta was not created" >&2
732 test_done $testroot 1
736 echo alpha > $testroot/wt/eta.expected
737 echo 'but now is eta' >> $testroot/wt/eta.expected
738 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
740 if [ $ret -ne 0 ]; then
741 diff -u $testroot/wt/eta.expected $testroot/wt/eta
743 test_done $testroot $ret
746 test_patch_illegal_status() {
747 local testroot=`test_init patch_illegal_status`
749 got checkout $testroot/repo $testroot/wt > /dev/null
751 if [ $ret -ne 0 ]; then
752 test_done $testroot $ret
756 # try to patch an obstructed file, add a versioned one, edit a
757 # non existent file and an unversioned one, and remove a
759 cat <<EOF > $testroot/wt/patch
785 echo kappa > $testroot/wt/kappa
786 rm $testroot/wt/alpha
787 mkdir $testroot/wt/alpha
789 (cd $testroot/wt && got patch patch) > $testroot/stdout \
792 if [ $ret -eq 0 ]; then
793 echo "edited a missing file" >&2
794 test_done $testroot $ret
798 cat <<EOF > $testroot/stdout.expected
806 cat <<EOF > $testroot/stderr.expected
807 got: alpha: file has unexpected status
808 got: beta: file has unexpected status
809 got: iota: No such file or directory
810 got: kappa: file has unexpected status
811 got: lambda: No such file or directory
812 got: patch failed to apply
815 cmp -s $testroot/stdout.expected $testroot/stdout
817 if [ $ret -ne 0 ]; then
818 diff -u $testroot/stdout.expected $testroot/stdout
819 test_done $testroot $ret
823 cmp -s $testroot/stderr.expected $testroot/stderr
825 if [ $ret -ne 0 ]; then
826 diff -u $testroot/stderr.expected $testroot/stderr
827 test_done $testroot $ret
831 (cd $testroot/wt && got status) > $testroot/stdout
832 cat <<EOF > $testroot/stdout.expected
838 cmp -s $testroot/stdout.expected $testroot/stdout
840 if [ $ret -ne 0 ]; then
841 diff -u $testroot/stdout.expected $testroot/stdout
843 test_done $testroot $ret
847 local testroot=`test_init patch_nop`
849 got checkout $testroot/repo $testroot/wt > /dev/null
851 if [ $ret -ne 0 ]; then
852 test_done $testroot $ret
856 cat <<EOF > $testroot/wt/patch
870 +delta updated and renamed!
873 (cd $testroot/wt && got patch -n patch)
875 if [ $ret -ne 0 ]; then
876 test_done $testroot $ret
880 # remove the patch to avoid the ? entry
881 rm $testroot/wt/patch
883 (cd $testroot/wt && got status) > $testroot/stdout
885 if [ $ret -ne 0 ]; then
886 test_done $testroot $ret
890 echo -n > $testroot/stdout.expected
891 cmp -s $testroot/stdout.expected $testroot/stdout
893 if [ $ret -ne 0 ]; then
894 diff -u $testroot/stdout.expected $testroot/stdout
896 test_done $testroot $ret
899 test_patch_preserve_perm() {
900 local testroot=`test_init patch_preserve_perm`
902 got checkout $testroot/repo $testroot/wt > /dev/null
904 if [ $ret -ne 0 ]; then
905 test_done $testroot $ret
909 chmod +x $testroot/wt/alpha
910 (cd $testroot/wt && got commit -m 'alpha executable') > /dev/null
912 if [ $ret -ne 0 ]; then
913 test_done $testroot $ret
917 cat <<EOF > $testroot/wt/patch
925 (cd $testroot/wt && got patch patch) > /dev/null
927 if [ $ret -ne 0 ]; then
928 test_done $testroot $ret
932 if [ ! -x $testroot/wt/alpha ]; then
933 echo "alpha is no more executable!" >&2
934 test_done $testroot 1
937 test_done $testroot 0
940 test_patch_create_dirs() {
941 local testroot=`test_init patch_create_dirs`
943 got checkout $testroot/repo $testroot/wt > /dev/null
945 if [ $ret -ne 0 ]; then
946 test_done $testroot $ret
950 cat <<EOF > $testroot/wt/patch
952 +++ iota/kappa/lambda
957 (cd $testroot/wt && got patch patch) > $testroot/stdout
959 if [ $ret -ne 0 ]; then
960 test_done $testroot $ret
964 echo 'A iota/kappa/lambda' >> $testroot/stdout.expected
965 cmp -s $testroot/stdout.expected $testroot/stdout
967 if [ $ret -ne 0 ]; then
968 diff -u $testroot/stdout.expected $testroot/stdout
969 test_done $testroot $ret
973 if [ ! -f $testroot/wt/iota/kappa/lambda ]; then
974 echo "file not created!" >&2
975 test_done $testroot $ret
978 test_done $testroot 0
981 test_patch_with_offset() {
982 local testroot=`test_init patch_with_offset`
984 got checkout $testroot/repo $testroot/wt > /dev/null
986 if [ $ret -ne 0 ]; then
987 test_done $testroot $ret
991 cat <<EOF > $testroot/wt/patch
1014 jot 100 > $testroot/wt/numbers
1015 ed $testroot/wt/numbers <<EOF > /dev/null 2> /dev/null
1022 (cd $testroot/wt && got add numbers && got commit -m '+numbers') \
1025 if [ $ret -ne 0 ]; then
1026 test_done $testroot $ret
1030 (cd $testroot/wt && got patch patch) > $testroot/stdout
1032 if [ $ret -ne 0 ]; then
1033 test_done $testroot/wt $ret
1037 cat <<EOF > $testroot/stdout.expected
1039 @@ -47,7 +47,7 @@ applied with offset -10
1040 @@ -87,7 +87,7 @@ applied with offset 10
1043 cmp -s $testroot/stdout.expected $testroot/stdout
1045 if [ $ret -ne 0 ]; then
1046 diff -u $testroot/stdout.expected $testroot/stdout
1048 test_done $testroot $ret
1052 run_test test_patch_simple_add_file
1053 run_test test_patch_simple_rm_file
1054 run_test test_patch_simple_edit_file
1055 run_test test_patch_prepend_line
1056 run_test test_patch_replace_line
1057 run_test test_patch_multiple_hunks
1058 run_test test_patch_multiple_files
1059 run_test test_patch_dont_apply
1060 run_test test_patch_malformed
1061 run_test test_patch_no_patch
1062 run_test test_patch_equals_for_context
1063 run_test test_patch_rename
1064 run_test test_patch_illegal_status
1065 run_test test_patch_nop
1066 run_test test_patch_preserve_perm
1067 run_test test_patch_create_dirs
1068 run_test test_patch_with_offset