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 != 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 != 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 != 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 != 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 != 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 != 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 != 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 != 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 != 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 != 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 != 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 != 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 != 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 != 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 != 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 != 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 != 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 != 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 != 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 != 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_lines`
253 got checkout $testroot/repo $testroot/wt > /dev/null
254 ret=$?
255 if [ $ret != 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 != 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 != 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 != 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 != 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 != 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 != 0 ]; then
367 test_done $testrot $ret
368 return 1
369 fi
371 cmp -s $testroot/stdout.expected $testroot/stdout
372 ret=$?
373 if [ $ret != 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 != 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 != 0 ]; then
402 test_done $testroot $ret
403 return 1
404 fi
406 cat <<EOF > $testroot/wt/patch
407 --- alpha
408 +++ alpha
409 @@ -1 +1,2 @@
410 +hatsuseno
411 alpha something
412 EOF
414 echo -n > $testroot/stdout.expected
415 echo "got: patch doesn't apply" > $testroot/stderr.expected
417 (cd $testroot/wt && got patch patch) \
418 > $testroot/stdout \
419 2> $testroot/stderr
420 ret=$?
421 if [ $ret == 0 ]; then # should fail
422 test_done $testroot 1
423 return 1
424 fi
426 cmp -s $testroot/stdout.expected $testroot/stdout
427 ret=$?
428 if [ $ret != 0 ]; then
429 diff -u $testroot/stdout.expected $testroot/stdout
430 test_done $testroot $ret
431 return 1
432 fi
434 cmp -s $testroot/stderr.expected $testroot/stderr
435 ret=$?
436 if [ $ret != 0 ]; then
437 diff -u $testroot/stderr.expected $testroot/stderr
438 test_done $testroot $ret
439 return 1
440 fi
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
449 ret=$?
450 if [ $ret != 0 ]; then
451 test_done $testroot $ret
452 return 1
453 fi
455 # missing "@@"
456 cat <<EOF > $testroot/wt/patch
457 --- alpha
458 +++ alpha
459 @@ -1 +1,2
460 +hatsuseno
461 alpha
462 EOF
464 echo -n > $testroot/stdout.expected
465 echo "got: malformed patch" > $testroot/stderr.expected
467 (cd $testroot/wt && got patch patch) \
468 > $testroot/stdout \
469 2> $testroot/stderr
470 ret=$?
471 if [ $ret == 0 ]; then
472 echo "got managed to apply an invalid patch"
473 test_done $testroot 1
474 return 1
475 fi
477 cmp -s $testroot/stdout.expected $testroot/stdout
478 ret=$?
479 if [ $ret != 0 ]; then
480 diff -u $testroot/stdout.expected $testroot/stdout
481 test_done $testroot $ret
482 return 1
483 fi
485 cmp -s $testroot/stderr.expected $testroot/stderr
486 ret=$?
487 if [ $ret != 0 ]; then
488 diff -u $testroot/stderr.expected $testroot/stderr
489 test_done $testroot $ret
490 return 1
491 fi
493 # wrong first character
494 cat <<EOF > $testroot/wt/patch
495 --- alpha
496 +++ alpha
497 @@ -1 +1,2 @@
498 +hatsuseno
499 alpha
500 EOF
502 (cd $testroot/wt && got patch patch) \
503 > $testroot/stdout \
504 2> $testroot/stderr
505 ret=$?
506 if [ $ret == 0 ]; then
507 echo "got managed to apply an invalid patch"
508 test_done $testroot 1
509 return 1
510 fi
512 cmp -s $testroot/stdout.expected $testroot/stdout
513 ret=$?
514 if [ $ret != 0 ]; then
515 diff -u $testroot/stdout.expected $testroot/stdout
516 test_done $testroot $ret
517 return 1
518 fi
520 cmp -s $testroot/stderr.expected $testroot/stderr
521 ret=$?
522 if [ $ret != 0 ]; then
523 diff -u $testroot/stderr.expected $testroot/stderr
524 test_done $testroot $ret
525 return 1
526 fi
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
535 ret=$?
536 if [ $ret != 0 ]; then
537 test_done $testroot $ret
538 return 1
539 fi
541 cat <<EOF > $testroot/wt/patch
542 hello world!
543 ...
545 some other nonsense
546 ...
548 there's no patch in here!
549 EOF
551 echo -n > $testroot/stdout.expected
552 echo "got: no patch found" > $testroot/stderr.expected
554 (cd $testroot/wt && got patch patch) \
555 > $testroot/stdout \
556 2> $testroot/stderr
557 ret=$?
558 if [ $ret == 0 ]; then # should fail
559 test_done $testroot 1
560 return 1
561 fi
564 cmp -s $testroot/stdout.expected $testroot/stdout
565 ret=$?
566 if [ $ret != 0 ]; then
567 diff -u $testroot/stdout.expected $testroot/stdout
568 test_done $testroot $ret
569 return 1
570 fi
572 cmp -s $testroot/stderr.expected $testroot/stderr
573 ret=$?
574 if [ $ret != 0 ]; then
575 diff -u $testroot/stderr.expected $testroot/stderr
576 test_done $testroot $ret
577 return 1
578 fi
580 test_done $testroot $ret
583 test_patch_equals_for_context() {
584 local testroot=`test_init patch_prepend_line`
586 got checkout $testroot/repo $testroot/wt > /dev/null
587 ret=$?
588 if [ $ret != 0 ]; then
589 test_done $testroot $ret
590 return 1
591 fi
593 cat <<EOF > $testroot/wt/patch
594 --- alpha
595 +++ alpha
596 @@ -1 +1,2 @@
597 +hatsuseno
598 =alpha
599 EOF
601 echo "M alpha" > $testroot/stdout.expected
603 (cd $testroot/wt && got patch patch) > $testroot/stdout
604 ret=$?
605 if [ $ret != 0 ]; then
606 test_done $testroot $ret
607 return 1
608 fi
610 cmp -s $testroot/stdout.expected $testroot/stdout
611 ret=$?
612 if [ $ret != 0 ]; then
613 diff -u $testroot/stdout.expected $testroot/stdout
614 test_done $testroot $ret
615 return 1
616 fi
618 echo hatsuseno > $testroot/wt/alpha.expected
619 echo alpha >> $testroot/wt/alpha.expected
620 cmp -s $testroot/wt/alpha.expected $testroot/wt/alpha
621 ret=$?
622 if [ $ret != 0 ]; then
623 diff -u $testroot/wt/alpha.expected $testroot/wt/alpha
624 fi
625 test_done $testroot $ret
628 test_parseargs "$@"
629 run_test test_patch_simple_add_file
630 run_test test_patch_simple_rm_file
631 run_test test_patch_simple_edit_file
632 run_test test_patch_prepend_line
633 run_test test_patch_replace_line
634 run_test test_patch_multiple_hunks
635 run_test test_patch_multiple_files
636 run_test test_patch_dont_apply
637 run_test test_patch_malformed
638 run_test test_patch_no_patch
639 run_test test_patch_equals_for_context