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 test_done $testroot $ret
445 test_patch_malformed() {
446 local testroot=`test_init patch_malformed`
448 got checkout $testroot/repo $testroot/wt > /dev/null
450 if [ $ret -ne 0 ]; then
451 test_done $testroot $ret
456 cat <<EOF > $testroot/wt/patch
464 echo -n > $testroot/stdout.expected
465 echo "got: malformed patch" > $testroot/stderr.expected
467 (cd $testroot/wt && got patch patch) \
471 if [ $ret -eq 0 ]; then
472 echo "got managed to apply an invalid patch"
473 test_done $testroot 1
477 cmp -s $testroot/stdout.expected $testroot/stdout
479 if [ $ret -ne 0 ]; then
480 diff -u $testroot/stdout.expected $testroot/stdout
481 test_done $testroot $ret
485 cmp -s $testroot/stderr.expected $testroot/stderr
487 if [ $ret -ne 0 ]; then
488 diff -u $testroot/stderr.expected $testroot/stderr
489 test_done $testroot $ret
493 # wrong first character
494 cat <<EOF > $testroot/wt/patch
502 (cd $testroot/wt && got patch patch) \
506 if [ $ret -eq 0 ]; then
507 echo "got managed to apply an invalid patch"
508 test_done $testroot 1
512 cmp -s $testroot/stdout.expected $testroot/stdout
514 if [ $ret -ne 0 ]; then
515 diff -u $testroot/stdout.expected $testroot/stdout
516 test_done $testroot $ret
520 cmp -s $testroot/stderr.expected $testroot/stderr
522 if [ $ret -ne 0 ]; then
523 diff -u $testroot/stderr.expected $testroot/stderr
524 test_done $testroot $ret
528 test_done $testroot $ret
531 test_patch_no_patch() {
532 local testroot=`test_init patch_no_patch`
534 got checkout $testroot/repo $testroot/wt > /dev/null
536 if [ $ret -ne 0 ]; then
537 test_done $testroot $ret
541 cat <<EOF > $testroot/wt/patch
548 there's no patch in here!
551 echo -n > $testroot/stdout.expected
552 echo "got: no patch found" > $testroot/stderr.expected
554 (cd $testroot/wt && got patch patch) \
558 if [ $ret -eq 0 ]; then # should fail
559 test_done $testroot 1
563 cmp -s $testroot/stdout.expected $testroot/stdout
565 if [ $ret -ne 0 ]; then
566 diff -u $testroot/stdout.expected $testroot/stdout
567 test_done $testroot $ret
571 cmp -s $testroot/stderr.expected $testroot/stderr
573 if [ $ret -ne 0 ]; then
574 diff -u $testroot/stderr.expected $testroot/stderr
575 test_done $testroot $ret
579 test_done $testroot $ret
582 test_patch_equals_for_context() {
583 local testroot=`test_init patch_prepend_line`
585 got checkout $testroot/repo $testroot/wt > /dev/null
587 if [ $ret -ne 0 ]; then
588 test_done $testroot $ret
592 cat <<EOF > $testroot/wt/patch
600 echo "M alpha" > $testroot/stdout.expected
602 (cd $testroot/wt && got patch patch) > $testroot/stdout
604 if [ $ret -ne 0 ]; then
605 test_done $testroot $ret
609 cmp -s $testroot/stdout.expected $testroot/stdout
611 if [ $ret -ne 0 ]; then
612 diff -u $testroot/stdout.expected $testroot/stdout
613 test_done $testroot $ret
617 echo hatsuseno > $testroot/wt/alpha.expected
618 echo alpha >> $testroot/wt/alpha.expected
619 cmp -s $testroot/wt/alpha.expected $testroot/wt/alpha
621 if [ $ret -ne 0 ]; then
622 diff -u $testroot/wt/alpha.expected $testroot/wt/alpha
624 test_done $testroot $ret
628 run_test test_patch_simple_add_file
629 run_test test_patch_simple_rm_file
630 run_test test_patch_simple_edit_file
631 run_test test_patch_prepend_line
632 run_test test_patch_replace_line
633 run_test test_patch_multiple_hunks
634 run_test test_patch_multiple_files
635 run_test test_patch_dont_apply
636 run_test test_patch_malformed
637 run_test test_patch_no_patch
638 run_test test_patch_equals_for_context