Blob


1 #!/bin/sh
2 #
3 # Copyright (c) 2022 Omar Polo <op@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_patch_simple_add_file() {
20 local testroot=`test_init patch_simple_add_file`
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 cat <<EOF > $testroot/wt/patch
30 --- /dev/null
31 +++ eta
32 @@ -0,0 +1 @@
33 +eta
34 EOF
36 (cd $testroot/wt && got patch patch) > $testroot/stdout
37 ret=$?
38 if [ $ret -ne 0 ]; then
39 test_done $testroot $ret
40 return 1
41 fi
43 echo "A eta" > $testroot/stdout.expected
44 cmp -s $testroot/stdout.expected $testroot/stdout
45 ret=$?
46 if [ $ret -ne 0 ]; then
47 diff -u $testroot/stdout.expected $testroot/stdout
48 test_done $testroot $ret
49 return 1
50 fi
52 echo eta > $testroot/wt/eta.expected
53 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
54 ret=$?
55 if [ $ret -ne 0 ]; then
56 diff -u $testroot/wt/eta.expected $testroot/wt/eta
57 fi
58 test_done $testroot $ret
59 }
61 test_patch_simple_rm_file() {
62 local testroot=`test_init patch_simple_rm_file`
64 got checkout $testroot/repo $testroot/wt > /dev/null
65 ret=$?
66 if [ $ret -ne 0 ]; then
67 test_done $testroot $ret
68 return 1
69 fi
71 cat <<EOF > $testroot/wt/patch
72 --- alpha
73 +++ /dev/null
74 @@ -1 +0,0 @@
75 -alpha
76 EOF
78 echo "D alpha" > $testroot/stdout.expected
80 (cd $testroot/wt && got patch patch) > $testroot/stdout
81 ret=$?
82 if [ $ret -ne 0 ]; then
83 test_done $testroot $ret
84 return 1
85 fi
87 cmp -s $testroot/stdout.expected $testroot/stdout
88 ret=$?
89 if [ $ret -ne 0 ]; then
90 diff -u $testroot/stdout.expected $testroot/stdout
91 test_done $testroot $ret
92 return 1
93 fi
95 if [ -f $testroot/wt/alpha ]; then
96 ret=1
97 echo "alpha still exists!"
98 fi
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
106 ret=$?
107 if [ $ret -ne 0 ]; then
108 test_done $testroot $ret
109 return 1
110 fi
112 cat <<EOF > $testroot/wt/patch
113 --- alpha
114 +++ alpha
115 @@ -1 +1 @@
116 -alpha
117 +alpha is my favourite character
118 EOF
120 echo "M alpha" > $testroot/stdout.expected
122 (cd $testroot/wt && got patch patch) > $testroot/stdout
123 ret=$?
124 if [ $ret -ne 0 ]; then
125 test_done $testroot $ret
126 return 1
127 fi
129 cmp -s $testroot/stdout.expected $testroot/stdout
130 ret=$?
131 if [ $ret -ne 0 ]; then
132 diff -u $testroot/stdout.expected $testroot/stdout
133 test_done $testroot $ret
134 return 1
135 fi
137 echo 'alpha is my favourite character' > $testroot/wt/alpha.expected
138 cmp -s $testroot/wt/alpha.expected $testroot/wt/alpha
139 ret=$?
140 if [ $ret -ne 0 ]; then
141 diff -u $testroot/wt/alpha.expected $testroot/wt/alpha
142 fi
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
150 ret=$?
151 if [ $ret -ne 0 ]; then
152 test_done $testroot $ret
153 return 1
154 fi
156 cat <<EOF > $testroot/wt/patch
157 --- alpha
158 +++ alpha
159 @@ -1 +1,2 @@
160 +hatsuseno
161 alpha
162 EOF
164 echo "M alpha" > $testroot/stdout.expected
166 (cd $testroot/wt && got patch patch) > $testroot/stdout
167 ret=$?
168 if [ $ret -ne 0 ]; then
169 test_done $testroot $ret
170 return 1
171 fi
173 cmp -s $testroot/stdout.expected $testroot/stdout
174 ret=$?
175 if [ $ret -ne 0 ]; then
176 diff -u $testroot/stdout.expected $testroot/stdout
177 test_done $testroot $ret
178 return 1
179 fi
181 echo hatsuseno > $testroot/wt/alpha.expected
182 echo alpha >> $testroot/wt/alpha.expected
183 cmp -s $testroot/wt/alpha.expected $testroot/wt/alpha
184 ret=$?
185 if [ $ret -ne 0 ]; then
186 diff -u $testroot/wt/alpha.expected $testroot/wt/alpha
187 fi
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
195 ret=$?
196 if [ $ret -ne 0 ]; then
197 test_done $testroot $ret
198 return 1
199 fi
201 jot 10 > $testroot/wt/numbers
202 (cd $testroot/wt/ && got add numbers && got ci -m 'add numbers') \
203 >/dev/null
204 ret=$?
205 if [ $ret -ne 0 ]; then
206 test_done $testroot $ret
207 return 1
208 fi
210 cat <<EOF > $testroot/wt/patch
211 --- numbers
212 +++ numbers
213 @@ -3,7 +3,7 @@
217 -6
218 +foo
222 EOF
224 echo "M numbers" > $testroot/stdout.expected
226 (cd $testroot/wt && got patch patch) > $testroot/stdout
227 ret=$?
228 if [ $ret -ne 0 ]; then
229 test_done $testroot $ret
230 return 1
231 fi
233 cmp -s $testroot/stdout.expected $testroot/stdout
234 ret=$?
235 if [ $ret -ne 0 ]; then
236 diff -u $testroot/stdout.expected $testroot/stdout
237 test_done $testroot $ret
238 return 1
239 fi
241 jot 10 | sed 's/6/foo/' > $testroot/wt/numbers.expected
242 cmp -s $testroot/wt/numbers.expected $testroot/wt/numbers
243 ret=$?
244 if [ $ret -ne 0 ]; then
245 diff -u $testroot/wt/numbers.expected $testroot/wt/numbers
246 fi
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
254 ret=$?
255 if [ $ret -ne 0 ]; then
256 test_done $testroot $ret
257 return 1
258 fi
260 jot 100 > $testroot/wt/numbers
261 (cd $testroot/wt/ && got add numbers && got ci -m 'add numbers') \
262 >/dev/null
263 ret=$?
264 if [ $ret -ne 0 ]; then
265 test_done $testroot $ret
266 return 1
267 fi
269 cat <<EOF > $testroot/wt/patch
270 --- numbers
271 +++ numbers
272 @@ -3,7 +3,7 @@
276 -6
277 +foo
281 @@ -57,7 +57,7 @@
282 57
283 58
284 59
285 -60
286 +foo foo
287 61
288 62
289 63
290 @@ -98,3 +98,6 @@
291 98
292 99
293 100
294 +101
295 +102
296 +...
297 EOF
299 echo "M numbers" > $testroot/stdout.expected
301 (cd $testroot/wt && got patch patch) > $testroot/stdout
302 ret=$?
303 if [ $ret -ne 0 ]; then
304 test_done $testroot $ret
305 return 1
306 fi
308 cmp -s $testroot/stdout.expected $testroot/stdout
309 ret=$?
310 if [ $ret -ne 0 ]; then
311 diff -u $testroot/stdout.expected $testroot/stdout
312 test_done $testroot $ret
313 return 1
314 fi
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
323 ret=$?
324 if [ $ret -ne 0 ]; then
325 diff -u $testroot/wt/numbers.expected $testroot/wt/numbers
326 fi
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
334 ret=$?
335 if [ $ret -ne 0 ]; then
336 test_done $testroot $ret
337 return 1
338 fi
340 cat <<EOF > $testroot/wt/patch
341 --- alpha Mon Mar 7 19:02:07 2022
342 +++ alpha Mon Mar 7 19:01:53 2022
343 @@ -1 +1,3 @@
344 +new
345 alpha
346 +available
347 --- beta Mon Mar 7 19:02:11 2022
348 +++ beta Mon Mar 7 19:01:46 2022
349 @@ -1 +1,3 @@
350 beta
351 +was
352 +improved
353 --- gamma/delta Mon Mar 7 19:02:17 2022
354 +++ gamma/delta Mon Mar 7 19:01:37 2022
355 @@ -1 +1 @@
356 -delta
357 +delta new
358 EOF
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
365 ret=$?
366 if [ $ret -ne 0 ]; then
367 test_done $testroot $ret
368 return 1
369 fi
371 cmp -s $testroot/stdout.expected $testroot/stdout
372 ret=$?
373 if [ $ret -ne 0 ]; then
374 diff -u $testroot/stdout.expected $testroot/stdout
375 test_done $testroot $ret
376 return 1
377 fi
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
385 ret=$?
386 if [ $ret -ne 0 ]; then
387 diff -u $testroot/wt/$f.expected $testroot/wt/$f
388 test_done $testroot $ret
389 return 1
390 fi
391 done
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
400 ret=$?
401 if [ $ret -ne 0 ]; then
402 test_done $testroot $ret
403 return 1
404 fi
406 jot 100 > $testroot/wt/numbers
407 (cd $testroot/wt && got add numbers && got commit -m 'add numbers') \
408 >/dev/null
409 ret=$?
410 if [ $ret -ne 0 ]; then
411 test_done $testroot $ret
412 return 1
413 fi
415 cat <<EOF > $testroot/wt/patch
416 --- alpha
417 +++ alpha
418 @@ -1 +1,2 @@
419 +hatsuseno
420 alpha something
421 --- numbers
422 +++ /dev/null
423 @@ -1,9 +0,0 @@
424 -1
425 -2
426 -3
427 -4
428 -5
429 -6
430 -7
431 -8
432 -9
433 EOF
435 (cd $testroot/wt && got patch patch) > $testroot/stdout 2> /dev/null
436 ret=$?
437 if [ $ret -eq 0 ]; then # should fail
438 test_done $testroot 1
439 return 1
440 fi
442 cat <<EOF > $testroot/stdout.expected
443 # alpha
444 @@ -1,1 +1,2 @@ hunk failed to apply
445 # numbers
446 @@ -1,9 +0,0 @@ hunk failed to apply
447 EOF
449 cmp -s $testroot/stdout.expected $testroot/stdout
450 ret=$?
451 if [ $ret -ne 0 ]; then
452 diff -u $testroot/stdout.expected $testroot/stdout
453 fi
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
461 ret=$?
462 if [ $ret -ne 0 ]; then
463 test_done $testroot $ret
464 return 1
465 fi
467 # missing "@@"
468 cat <<EOF > $testroot/wt/patch
469 --- alpha
470 +++ alpha
471 @@ -1 +1,2
472 +hatsuseno
473 alpha
474 EOF
476 echo -n > $testroot/stdout.expected
477 echo "got: malformed patch" > $testroot/stderr.expected
479 (cd $testroot/wt && got patch patch) \
480 > $testroot/stdout \
481 2> $testroot/stderr
482 ret=$?
483 if [ $ret -eq 0 ]; then
484 echo "got managed to apply an invalid patch"
485 test_done $testroot 1
486 return 1
487 fi
489 cmp -s $testroot/stdout.expected $testroot/stdout
490 ret=$?
491 if [ $ret -ne 0 ]; then
492 diff -u $testroot/stdout.expected $testroot/stdout
493 test_done $testroot $ret
494 return 1
495 fi
497 cmp -s $testroot/stderr.expected $testroot/stderr
498 ret=$?
499 if [ $ret -ne 0 ]; then
500 diff -u $testroot/stderr.expected $testroot/stderr
501 test_done $testroot $ret
502 return 1
503 fi
505 # wrong first character
506 cat <<EOF > $testroot/wt/patch
507 --- alpha
508 +++ alpha
509 @@ -1 +1,2 @@
510 +hatsuseno
511 alpha
512 EOF
514 (cd $testroot/wt && got patch patch) \
515 > $testroot/stdout \
516 2> $testroot/stderr
517 ret=$?
518 if [ $ret -eq 0 ]; then
519 echo "got managed to apply an invalid patch"
520 test_done $testroot 1
521 return 1
522 fi
524 cmp -s $testroot/stdout.expected $testroot/stdout
525 ret=$?
526 if [ $ret -ne 0 ]; then
527 diff -u $testroot/stdout.expected $testroot/stdout
528 test_done $testroot $ret
529 return 1
530 fi
532 cmp -s $testroot/stderr.expected $testroot/stderr
533 ret=$?
534 if [ $ret -ne 0 ]; then
535 diff -u $testroot/stderr.expected $testroot/stderr
536 test_done $testroot $ret
537 return 1
538 fi
540 # empty hunk
541 cat <<EOF > $testroot/wt/patch
542 diff --git a/alpha b/iota
543 --- a/alpha
544 +++ b/iota
545 @@ -0,0 +0,0 @@
546 EOF
548 (cd $testroot/wt && got patch patch) \
549 > $testroot/stdout \
550 2> $testroot/stderr
551 ret=$?
552 if [ $ret -eq 0 ]; then
553 echo "got managed to apply an invalid patch"
554 test_done $testroot 1
555 return 1
556 fi
558 cmp -s $testroot/stdout.expected $testroot/stdout
559 ret=$?
560 if [ $ret -ne 0 ]; then
561 diff -u $testroot/stdout.expected $testroot/stdout
562 test_done $testroot $ret
563 return 1
564 fi
566 cmp -s $testroot/stderr.expected $testroot/stderr
567 ret=$?
568 if [ $ret -ne 0 ]; then
569 diff -u $testroot/stderr.expected $testroot/stderr
570 test_done $testroot $ret
571 return 1
572 fi
574 test_done $testroot $ret
577 test_patch_no_patch() {
578 local testroot=`test_init patch_no_patch`
580 got checkout $testroot/repo $testroot/wt > /dev/null
581 ret=$?
582 if [ $ret -ne 0 ]; then
583 test_done $testroot $ret
584 return 1
585 fi
587 cat <<EOF > $testroot/wt/patch
588 hello world!
589 ...
591 some other nonsense
592 ...
594 there's no patch in here!
595 EOF
597 echo -n > $testroot/stdout.expected
598 echo "got: no patch found" > $testroot/stderr.expected
600 (cd $testroot/wt && got patch patch) \
601 > $testroot/stdout \
602 2> $testroot/stderr
603 ret=$?
604 if [ $ret -eq 0 ]; then # should fail
605 test_done $testroot 1
606 return 1
607 fi
609 cmp -s $testroot/stdout.expected $testroot/stdout
610 ret=$?
611 if [ $ret -ne 0 ]; then
612 diff -u $testroot/stdout.expected $testroot/stdout
613 test_done $testroot $ret
614 return 1
615 fi
617 cmp -s $testroot/stderr.expected $testroot/stderr
618 ret=$?
619 if [ $ret -ne 0 ]; then
620 diff -u $testroot/stderr.expected $testroot/stderr
621 test_done $testroot $ret
622 return 1
623 fi
625 test_done $testroot $ret
628 test_patch_equals_for_context() {
629 local testroot=`test_init patch_equals_for_context`
631 got checkout $testroot/repo $testroot/wt > /dev/null
632 ret=$?
633 if [ $ret -ne 0 ]; then
634 test_done $testroot $ret
635 return 1
636 fi
638 cat <<EOF > $testroot/wt/patch
639 --- alpha
640 +++ alpha
641 @@ -1 +1,2 @@
642 +hatsuseno
643 =alpha
644 EOF
646 echo "M alpha" > $testroot/stdout.expected
648 (cd $testroot/wt && got patch patch) > $testroot/stdout
649 ret=$?
650 if [ $ret -ne 0 ]; then
651 test_done $testroot $ret
652 return 1
653 fi
655 cmp -s $testroot/stdout.expected $testroot/stdout
656 ret=$?
657 if [ $ret -ne 0 ]; then
658 diff -u $testroot/stdout.expected $testroot/stdout
659 test_done $testroot $ret
660 return 1
661 fi
663 echo hatsuseno > $testroot/wt/alpha.expected
664 echo alpha >> $testroot/wt/alpha.expected
665 cmp -s $testroot/wt/alpha.expected $testroot/wt/alpha
666 ret=$?
667 if [ $ret -ne 0 ]; then
668 diff -u $testroot/wt/alpha.expected $testroot/wt/alpha
669 fi
670 test_done $testroot $ret
673 test_patch_rename() {
674 local testroot=`test_init patch_rename`
676 got checkout $testroot/repo $testroot/wt > /dev/null
677 ret=$?
678 if [ $ret -ne 0 ]; then
679 test_done $testroot $ret
680 return 1
681 fi
683 cat <<EOF > $testroot/wt/patch
684 diff --git a/beta b/iota
685 similarity index 100%
686 rename from beta
687 rename to iota
688 diff --git a/alpha b/eta
689 --- a/alpha
690 +++ b/eta
691 @@ -1 +1 @@
692 -alpha
693 +eta
694 EOF
696 echo 'D beta' > $testroot/stdout.expected
697 echo 'A iota' >> $testroot/stdout.expected
698 echo 'D alpha' >> $testroot/stdout.expected
699 echo 'A eta' >> $testroot/stdout.expected
701 (cd $testroot/wt && got patch patch) > $testroot/stdout
702 ret=$?
703 if [ $ret -ne 0 ]; then
704 test_done $testroot $ret
705 return 1
706 fi
708 cmp -s $testroot/stdout.expected $testroot/stdout
709 ret=$?
710 if [ $ret -ne 0 ]; then
711 diff -u $testroot/stdout.expected $testroot/stdout
712 test_done $testroot $ret
713 return 1
714 fi
716 if [ -f $testroot/wt/alpha -o -f $testroot/wt/beta ]; then
717 echo "alpha or beta were not removed" >&2
718 test_done $testroot 1
719 return 1
720 fi
721 if [ ! -f $testroot/wt/iota -o ! -f $testroot/wt/eta ]; then
722 echo "iota or eta were not created" >&2
723 test_done $testroot 1
724 return 1
725 fi
727 echo beta > $testroot/wt/iota.expected
728 cmp -s $testroot/wt/iota.expected $testroot/wt/iota
729 ret=$?
730 if [ $ret -ne 0 ]; then
731 diff -u $testroot/wt/iota.expected $testroot/wt/iota
732 test_done $testroot $ret
733 return 1
734 fi
736 echo eta > $testroot/wt/eta.expected
737 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
738 ret=$?
739 if [ $ret -ne 0 ]; then
740 diff -u $testroot/wt/eta.expected $testroot/wt/eta
741 test_done $testroot $ret
742 return 1
743 fi
745 test_done $testroot $ret
748 test_patch_illegal_status() {
749 local testroot=`test_init patch_illegal_status`
751 got checkout $testroot/repo $testroot/wt > /dev/null
752 ret=$?
753 if [ $ret -ne 0 ]; then
754 test_done $testroot $ret
755 return 1
756 fi
758 # try to patch an obstructed file, add a versioned one, edit a
759 # non existent file and an unversioned one, and remove a
760 # non existent file.
761 cat <<EOF > $testroot/wt/patch
762 --- alpha
763 +++ alpha
764 @@ -1 +1,2 @@
765 alpha
766 +was edited
767 --- /dev/null
768 +++ beta
769 @@ -0,0 +1 @@
770 +beta
771 --- iota
772 +++ iota
773 @@ -1 +1 @@
774 -iota
775 +IOTA
776 --- kappa
777 +++ kappa
778 @@ -1 +1 @@
779 -kappa
780 +KAPPA
781 --- lambda
782 +++ /dev/null
783 @@ -1 +0,0 @@
784 -lambda
785 EOF
787 echo kappa > $testroot/wt/kappa
788 rm $testroot/wt/alpha
789 mkdir $testroot/wt/alpha
791 (cd $testroot/wt && got patch patch) > $testroot/stdout \
792 2> $testroot/stderr
793 ret=$?
794 if [ $ret -eq 0 ]; then
795 echo "edited a missing file" >&2
796 test_done $testroot 1
797 return 1
798 fi
800 cat <<EOF > $testroot/stdout.expected
801 # alpha
802 # beta
803 # iota
804 # kappa
805 # lambda
806 EOF
808 cat <<EOF > $testroot/stderr.expected
809 got: alpha: file has unexpected status
810 got: beta: file has unexpected status
811 got: iota: No such file or directory
812 got: kappa: file has unexpected status
813 got: lambda: No such file or directory
814 got: patch failed to apply
815 EOF
817 cmp -s $testroot/stdout.expected $testroot/stdout
818 ret=$?
819 if [ $ret -ne 0 ]; then
820 diff -u $testroot/stdout.expected $testroot/stdout
821 test_done $testroot $ret
822 return 1
823 fi
825 cmp -s $testroot/stderr.expected $testroot/stderr
826 ret=$?
827 if [ $ret -ne 0 ]; then
828 diff -u $testroot/stderr.expected $testroot/stderr
829 test_done $testroot $ret
830 return 1
831 fi
833 (cd $testroot/wt && got status) > $testroot/stdout
834 cat <<EOF > $testroot/stdout.expected
835 ~ alpha
836 ? kappa
837 ? patch
838 EOF
840 cmp -s $testroot/stdout.expected $testroot/stdout
841 ret=$?
842 if [ $ret -ne 0 ]; then
843 diff -u $testroot/stdout.expected $testroot/stdout
844 fi
845 test_done $testroot $ret
848 test_patch_nop() {
849 local testroot=`test_init patch_nop`
851 got checkout $testroot/repo $testroot/wt > /dev/null
852 ret=$?
853 if [ $ret -ne 0 ]; then
854 test_done $testroot $ret
855 return 1
856 fi
858 cat <<EOF > $testroot/wt/patch
859 --- alpha
860 +++ alpha
861 @@ -1 +1 @@
862 -alpha
863 +cafe alpha
864 --- beta
865 +++ /dev/null
866 @@ -1 +0,0 @@
867 -beta
868 diff --git a/gamma/delta b/gamma/delta.new
869 --- gamma/delta
870 +++ gamma/delta.new
871 @@ -1 +1 @@
872 -delta
873 +delta updated and renamed!
874 EOF
876 (cd $testroot/wt && got patch -n patch)
877 ret=$?
878 if [ $ret -ne 0 ]; then
879 test_done $testroot $ret
880 return 1
881 fi
883 # remove the patch to avoid the ? entry
884 rm $testroot/wt/patch
886 (cd $testroot/wt && got status) > $testroot/stdout
887 ret=$?
888 if [ $ret -ne 0 ]; then
889 test_done $testroot $ret
890 return 1
891 fi
893 echo -n > $testroot/stdout.expected
894 cmp -s $testroot/stdout.expected $testroot/stdout
895 ret=$?
896 if [ $ret -ne 0 ]; then
897 diff -u $testroot/stdout.expected $testroot/stdout
898 fi
899 test_done $testroot $ret
902 test_patch_preserve_perm() {
903 local testroot=`test_init patch_preserve_perm`
905 got checkout $testroot/repo $testroot/wt > /dev/null
906 ret=$?
907 if [ $ret -ne 0 ]; then
908 test_done $testroot $ret
909 return 1
910 fi
912 chmod +x $testroot/wt/alpha
913 (cd $testroot/wt && got commit -m 'alpha executable') > /dev/null
914 ret=$?
915 if [ $ret -ne 0 ]; then
916 test_done $testroot $ret
917 return 1
918 fi
920 cat <<EOF > $testroot/wt/patch
921 --- alpha
922 +++ alpha
923 @@ -1 +1,2 @@
924 alpha
925 +was edited
926 EOF
928 (cd $testroot/wt && got patch patch) > /dev/null
929 ret=$?
930 if [ $ret -ne 0 ]; then
931 test_done $testroot $ret
932 return 1
933 fi
935 if [ ! -x $testroot/wt/alpha ]; then
936 echo "alpha is no more executable!" >&2
937 test_done $testroot 1
938 return 1
939 fi
940 test_done $testroot 0
943 test_patch_create_dirs() {
944 local testroot=`test_init patch_create_dirs`
946 got checkout $testroot/repo $testroot/wt > /dev/null
947 ret=$?
948 if [ $ret -ne 0 ]; then
949 test_done $testroot $ret
950 return 1
951 fi
953 cat <<EOF > $testroot/wt/patch
954 --- /dev/null
955 +++ iota/kappa/lambda
956 @@ -0,0 +1 @@
957 +lambda
958 EOF
960 (cd $testroot/wt && got patch patch) > $testroot/stdout
961 ret=$?
962 if [ $ret -ne 0 ]; then
963 test_done $testroot $ret
964 return 1
965 fi
967 echo 'A iota/kappa/lambda' >> $testroot/stdout.expected
968 cmp -s $testroot/stdout.expected $testroot/stdout
969 ret=$?
970 if [ $ret -ne 0 ]; then
971 diff -u $testroot/stdout.expected $testroot/stdout
972 test_done $testroot $ret
973 return 1
974 fi
976 if [ ! -f $testroot/wt/iota/kappa/lambda ]; then
977 echo "file not created!" >&2
978 test_done $testroot $ret
979 return 1
980 fi
981 test_done $testroot 0
984 test_patch_with_offset() {
985 local testroot=`test_init patch_with_offset`
987 got checkout $testroot/repo $testroot/wt > /dev/null
988 ret=$?
989 if [ $ret -ne 0 ]; then
990 test_done $testroot $ret
991 return 1
992 fi
994 cat <<EOF > $testroot/wt/patch
995 --- numbers
996 +++ numbers
997 @@ -47,7 +47,7 @@
998 47
999 48
1001 -50
1002 +midway tru it!
1006 @@ -87,7 +87,7 @@
1010 -90
1011 +almost there!
1015 EOF
1017 jot 100 > $testroot/wt/numbers
1018 ed $testroot/wt/numbers <<EOF > /dev/null 2> /dev/null
1019 1,10d
1020 50r !jot 20
1023 EOF
1025 (cd $testroot/wt && got add numbers && got commit -m '+numbers') \
1026 > /dev/null
1027 ret=$?
1028 if [ $ret -ne 0 ]; then
1029 test_done $testroot $ret
1030 return 1
1033 (cd $testroot/wt && got patch patch) > $testroot/stdout
1034 ret=$?
1035 if [ $ret -ne 0 ]; then
1036 test_done $testroot/wt $ret
1037 return 1
1040 cat <<EOF > $testroot/stdout.expected
1041 M numbers
1042 @@ -47,7 +47,7 @@ applied with offset -10
1043 @@ -87,7 +87,7 @@ applied with offset 10
1044 EOF
1046 cmp -s $testroot/stdout.expected $testroot/stdout
1047 ret=$?
1048 if [ $ret -ne 0 ]; then
1049 diff -u $testroot/stdout.expected $testroot/stdout
1051 test_done $testroot $ret
1054 test_patch_prefer_new_path() {
1055 local testroot=`test_init patch_orig`
1057 got checkout $testroot/repo $testroot/wt > /dev/null
1058 ret=$?
1059 if [ $ret -ne 0 ]; then
1060 test_done $testroot $ret
1061 return 1
1064 cat <<EOF > $testroot/wt/patch
1065 --- alpha.orig
1066 +++ alpha
1067 @@ -1 +1,2 @@
1068 alpha
1069 +was edited
1070 EOF
1072 (cd $testroot/wt && got patch patch) > $testroot/stdout
1073 ret=$?
1074 if [ $ret -ne 0 ]; then
1075 test_done $testroot $ret
1076 return 1
1079 echo 'M alpha' > $testroot/stdout.expected
1080 cmp -s $testroot/stdout.expected $testroot/stdout
1081 ret=$?
1082 if [ $ret -ne 0 ]; then
1083 diff -u $testroot/stdout.expected $testroot/stdout
1085 test_done $testroot $ret
1088 test_patch_no_newline() {
1089 local testroot=`test_init patch_no_newline`
1091 got checkout $testroot/repo $testroot/wt > /dev/null
1092 ret=$?
1093 if [ $ret -ne 0 ]; then
1094 test_done $testroot $ret
1095 return 1
1098 cat <<EOF > $testroot/wt/patch
1099 --- /dev/null
1100 +++ eta
1101 @@ -0,0 +1 @@
1102 +eta
1103 \ No newline at end of file
1104 EOF
1106 (cd $testroot/wt && got patch patch) > $testroot/stdout
1107 ret=$?
1108 if [ $ret -ne 0 ]; then
1109 test_done $testroot $ret
1110 return 1
1113 echo "A eta" > $testroot/stdout.expected
1114 cmp -s $testroot/stdout.expected $testroot/stdout
1115 ret=$?
1116 if [ $ret -ne 0 ]; then
1117 diff -u $testroot/stdout.expected $testroot/stdout
1118 test_done $testroot $ret
1119 return 1
1122 echo -n eta > $testroot/wt/eta.expected
1123 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
1124 ret=$?
1125 if [ $ret -ne 0 ]; then
1126 diff -u $testroot/wt/eta.expected $testroot/wt/eta
1127 test_done $testroot $ret
1128 return 1
1131 (cd $testroot/wt && got commit -m 'add eta') > /dev/null
1132 ret=$?
1133 if [ $ret -ne 0 ]; then
1134 test_done $testroot $ret
1135 return 1
1138 cat <<EOF > $testroot/wt/patch
1139 --- eta
1140 +++ eta
1141 @@ -1 +1 @@
1142 -eta
1143 \ No newline at end of file
1144 +ETA
1145 \ No newline at end of file
1146 EOF
1148 (cd $testroot/wt && got patch patch) > $testroot/stdout
1149 ret=$?
1150 if [ $ret -ne 0 ]; then
1151 test_done $testroot $ret
1152 return 1
1155 echo "M eta" > $testroot/stdout.expected
1156 cmp -s $testroot/stdout.expected $testroot/stdout
1157 ret=$?
1158 if [ $ret -ne 0 ]; then
1159 diff -u $testroot/stdout.expected $testroot/stdout
1160 test_done $testroot $ret
1161 return 1
1164 echo -n ETA > $testroot/wt/eta.expected
1165 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
1166 ret=$?
1167 if [ $ret -ne 0 ]; then
1168 diff -u $testroot/wt/eta.expected $testroot/wt/eta
1169 test_done $testroot $ret
1170 return 1
1173 (cd $testroot/wt && got commit -m 'edit eta') > /dev/null
1174 ret=$?
1175 if [ $ret -ne 0 ]; then
1176 test_done $testroot $ret
1177 return 1
1180 cat <<EOF > $testroot/wt/patch
1181 --- eta
1182 +++ eta
1183 @@ -1 +1 @@
1184 -ETA
1185 \ No newline at end of file
1186 +eta
1187 EOF
1189 (cd $testroot/wt && got patch patch) > $testroot/stdout
1190 ret=$?
1191 if [ $ret -ne 0 ]; then
1192 test_done $testroot $ret
1193 return 1
1196 echo "M eta" > $testroot/stdout.expected
1197 cmp -s $testroot/stdout.expected $testroot/stdout
1198 ret=$?
1199 if [ $ret -ne 0 ]; then
1200 diff -u $testroot/stdout.expected $testroot/stdout
1201 test_done $testroot $ret
1202 return 1
1205 echo eta > $testroot/wt/eta.expected
1206 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
1207 ret=$?
1208 if [ $ret -ne 0 ]; then
1209 diff -u $testroot/wt/eta.expected $testroot/wt/eta
1211 test_done $testroot $ret
1214 test_patch_strip() {
1215 local testroot=`test_init patch_strip`
1217 got checkout $testroot/repo $testroot/wt > /dev/null
1218 ret=$?
1219 if [ $ret -ne 0 ]; then
1220 test_done $testroot $ret
1221 return 1
1224 cat <<EOF > $testroot/wt/patch
1225 --- foo/bar/alpha.orig
1226 +++ foo/bar/alpha
1227 @@ -1 +1 @@
1228 -alpha
1229 +ALPHA
1230 EOF
1232 (cd $testroot/wt && got patch -p2 patch) > $testroot/stdout
1233 ret=$?
1234 if [ $ret -ne 0 ]; then
1235 test_done $testroot $ret
1236 return 1
1239 echo "M alpha" >> $testroot/stdout.expected
1240 cmp -s $testroot/stdout.expected $testroot/stdout
1241 ret=$?
1242 if [ $ret -ne 0 ]; then
1243 diff -u $testroot/stdout.expected $testroot/stdout
1244 test_done $testroot $ret
1245 return 1
1248 (cd $testroot/wt && got revert alpha) > /dev/null 2>&1
1249 ret=$?
1250 if [ $ret -ne 0 ]; then
1251 test_done $testroot $ret
1252 return 1
1255 (cd $testroot/wt && got patch -p3 patch) \
1256 2> $testroot/stderr
1257 ret=$?
1258 if [ $ret -eq 0 ]; then
1259 echo "stripped more components than available!"
1260 test_done $testroot 1
1261 return 1
1264 cat <<EOF > $testroot/stderr.expected
1265 got: can't strip 1 path-components from foo/bar/alpha: bad path
1266 EOF
1268 cmp -s $testroot/stderr.expected $testroot/stderr
1269 ret=$?
1270 if [ $ret -ne 0 ]; then
1271 diff -u $testroot/stderr.expected $testroot/stderr
1273 test_done $testroot 0
1276 test_patch_relative_paths() {
1277 local testroot=`test_init patch_relative_paths`
1279 got checkout $testroot/repo $testroot/wt > /dev/null
1280 ret=$?
1281 if [ $ret -ne 0 ]; then
1282 test_done $testroot $ret
1283 return 1
1286 cat <<EOF > $testroot/wt/gamma/patch
1287 --- delta
1288 +++ delta
1289 @@ -1 +1 @@
1290 -delta
1291 +DELTA
1292 --- /dev/null
1293 +++ eta
1294 @@ -0,0 +1 @@
1295 +eta
1296 EOF
1298 (cd $testroot/wt/gamma && got patch patch) > $testroot/stdout
1299 ret=$?
1300 if [ $ret -ne 0 ]; then
1301 test_done $testroot $ret
1302 return 1
1305 echo 'M gamma/delta' > $testroot/stdout.expected
1306 echo 'A gamma/eta' >> $testroot/stdout.expected
1308 cmp -s $testroot/stdout.expected $testroot/stdout
1309 ret=$?
1310 if [ $ret -ne 0 ]; then
1311 diff -u $testroot/stdout.expected $testroot/stdout
1313 test_done $testroot $ret
1316 test_patch_with_path_prefix() {
1317 local testroot=`test_init patch_with_path_prefix`
1319 got checkout -p gamma $testroot/repo $testroot/wt > /dev/null
1320 ret=$?
1321 if [ $ret -ne 0 ]; then
1322 test_done $testroot $ret
1323 return 1
1326 cat <<EOF > $testroot/wt/patch
1327 --- delta
1328 +++ delta
1329 @@ -1 +1 @@
1330 -delta
1331 +DELTA
1332 --- /dev/null
1333 +++ eta
1334 @@ -0,0 +1 @@
1335 +eta
1336 EOF
1338 (cd $testroot/wt && got patch patch) > $testroot/stdout
1339 ret=$?
1340 if [ $ret -ne 0 ]; then
1341 test_done $testroot $ret
1342 return 1
1345 echo 'M delta' > $testroot/stdout.expected
1346 echo 'A eta' >> $testroot/stdout.expected
1348 cmp -s $testroot/stdout.expected $testroot/stdout
1349 ret=$?
1350 if [ $ret -ne 0 ]; then
1351 diff -u $testroot/stdout.expected $testroot/stdout
1353 test_done $testroot $ret
1356 test_patch_relpath_with_path_prefix() {
1357 local testroot=`test_init patch_relpaths_with_path_prefix`
1359 got checkout -p gamma $testroot/repo $testroot/wt > /dev/null
1360 ret=$?
1361 if [ $ret -ne 0 ]; then
1362 test_done $testroot $ret
1363 return 1
1366 mkdir -p $testroot/wt/epsilon/zeta/
1368 cat <<EOF > $testroot/wt/patch
1369 --- /dev/null
1370 +++ zeta/theta
1371 @@ -0,0 +1 @@
1372 +theta
1373 EOF
1375 (cd $testroot/wt/epsilon/zeta && got patch -p1 $testroot/wt/patch) \
1376 > $testroot/stdout
1377 ret=$?
1378 if [ $ret -ne 0 ]; then
1379 test_done $testroot $ret
1380 return 1
1383 echo 'A epsilon/zeta/theta' >> $testroot/stdout.expected
1385 cmp -s $testroot/stdout.expected $testroot/stdout
1386 ret=$?
1387 if [ $ret -ne 0 ]; then
1388 diff -u $testroot/stdout.expected $testroot/stdout
1389 test_done $testroot $ret
1390 return 1
1393 echo 'theta' > $testroot/theta.expected
1394 cmp -s $testroot/wt/epsilon/zeta/theta $testroot/theta.expected
1395 ret=$?
1396 if [ $ret -ne 0 ]; then
1397 diff -u $testroot/wt/epsilon/zeta/theta $testroot/theta.expected
1399 test_done $testroot $ret
1402 test_patch_reverse() {
1403 local testroot=`test_init patch_reverse`
1405 got checkout $testroot/repo $testroot/wt > /dev/null
1406 ret=$?
1407 if [ $ret -ne 0 ]; then
1408 test_done $testroot $ret
1409 return 1
1412 cat <<EOF > $testroot/wt/patch
1413 --- alpha
1414 +++ alpha
1415 @@ -1 +1 @@
1416 -ALPHA
1417 \ No newline at end of file
1418 +alpha
1419 EOF
1421 (cd $testroot/wt && got patch -R patch) > $testroot/stdout
1422 ret=$?
1423 if [ $ret -ne 0 ]; then
1424 test_done $testroot $ret
1425 return 1
1428 echo "M alpha" > $testroot/stdout.expected
1429 cmp -s $testroot/stdout.expected $testroot/stdout
1430 ret=$?
1431 if [ $ret -ne 0 ]; then
1432 diff -u $testroot/stdout.expected $testroot/stdout
1433 test_done $testroot $ret
1434 return 1
1437 echo -n ALPHA > $testroot/wt/alpha.expected
1438 cmp -s $testroot/wt/alpha.expected $testroot/wt/alpha
1439 ret=$?
1440 if [ $ret -ne 0 ]; then
1441 diff -u $testroot/wt/alpha.expected $testroot/wt/alpha
1443 test_done $testroot $ret
1446 test_parseargs "$@"
1447 run_test test_patch_simple_add_file
1448 run_test test_patch_simple_rm_file
1449 run_test test_patch_simple_edit_file
1450 run_test test_patch_prepend_line
1451 run_test test_patch_replace_line
1452 run_test test_patch_multiple_hunks
1453 run_test test_patch_multiple_files
1454 run_test test_patch_dont_apply
1455 run_test test_patch_malformed
1456 run_test test_patch_no_patch
1457 run_test test_patch_equals_for_context
1458 run_test test_patch_rename
1459 run_test test_patch_illegal_status
1460 run_test test_patch_nop
1461 run_test test_patch_preserve_perm
1462 run_test test_patch_create_dirs
1463 run_test test_patch_with_offset
1464 run_test test_patch_prefer_new_path
1465 run_test test_patch_no_newline
1466 run_test test_patch_strip
1467 run_test test_patch_relative_paths
1468 run_test test_patch_with_path_prefix
1469 run_test test_patch_relpath_with_path_prefix
1470 run_test test_patch_reverse