Hash :
bfbccd0a
Author :
Date :
2022-03-08T12:22:36
use test(1) -eq and -ne to compare integers; ok tb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
#!/bin/sh
#
# Copyright (c) 2022 Omar Polo <op@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
. ./common.sh
test_patch_simple_add_file() {
local testroot=`test_init patch_simple_add_file`
got checkout $testroot/repo $testroot/wt > /dev/null
ret=$?
if [ $ret -ne 0 ]; then
test_done $testroot $ret
return 1
fi
cat <<EOF > $testroot/wt/patch
--- /dev/null
+++ eta
@@ -0,0 +1 @@
+eta
EOF
(cd $testroot/wt && got patch patch) > $testroot/stdout
ret=$?
if [ $ret -ne 0 ]; then
test_done $testroot $ret
return 1
fi
echo "A eta" > $testroot/stdout.expected
cmp -s $testroot/stdout.expected $testroot/stdout
ret=$?
if [ $ret -ne 0 ]; then
diff -u $testroot/stdout.expected $testroot/stdout
test_done $testroot $ret
return 1
fi
echo eta > $testroot/wt/eta.expected
cmp -s $testroot/wt/eta.expected $testroot/wt/eta
ret=$?
if [ $ret -ne 0 ]; then
diff -u $testroot/wt/eta.expected $testroot/wt/eta
fi
test_done $testroot $ret
}
test_patch_simple_rm_file() {
local testroot=`test_init patch_simple_rm_file`
got checkout $testroot/repo $testroot/wt > /dev/null
ret=$?
if [ $ret -ne 0 ]; then
test_done $testroot $ret
return 1
fi
cat <<EOF > $testroot/wt/patch
--- alpha
+++ /dev/null
@@ -1 +0,0 @@
-alpha
EOF
echo "D alpha" > $testroot/stdout.expected
(cd $testroot/wt && got patch patch) > $testroot/stdout
ret=$?
if [ $ret -ne 0 ]; then
test_done $testroot $ret
return 1
fi
cmp -s $testroot/stdout.expected $testroot/stdout
ret=$?
if [ $ret -ne 0 ]; then
diff -u $testroot/stdout.expected $testroot/stdout
test_done $testroot $ret
return 1
fi
if [ -f $testroot/wt/alpha ]; then
ret=1
echo "alpha still exists!"
fi
test_done $testroot $ret
}
test_patch_simple_edit_file() {
local testroot=`test_init patch_simple_edit_file`
got checkout $testroot/repo $testroot/wt > /dev/null
ret=$?
if [ $ret -ne 0 ]; then
test_done $testroot $ret
return 1
fi
cat <<EOF > $testroot/wt/patch
--- alpha
+++ alpha
@@ -1 +1 @@
-alpha
+alpha is my favourite character
EOF
echo "M alpha" > $testroot/stdout.expected
(cd $testroot/wt && got patch patch) > $testroot/stdout
ret=$?
if [ $ret -ne 0 ]; then
test_done $testroot $ret
return 1
fi
cmp -s $testroot/stdout.expected $testroot/stdout
ret=$?
if [ $ret -ne 0 ]; then
diff -u $testroot/stdout.expected $testroot/stdout
test_done $testroot $ret
return 1
fi
echo 'alpha is my favourite character' > $testroot/wt/alpha.expected
cmp -s $testroot/wt/alpha.expected $testroot/wt/alpha
ret=$?
if [ $ret -ne 0 ]; then
diff -u $testroot/wt/alpha.expected $testroot/wt/alpha
fi
test_done $testroot $ret
}
test_patch_prepend_line() {
local testroot=`test_init patch_prepend_line`
got checkout $testroot/repo $testroot/wt > /dev/null
ret=$?
if [ $ret -ne 0 ]; then
test_done $testroot $ret
return 1
fi
cat <<EOF > $testroot/wt/patch
--- alpha
+++ alpha
@@ -1 +1,2 @@
+hatsuseno
alpha
EOF
echo "M alpha" > $testroot/stdout.expected
(cd $testroot/wt && got patch patch) > $testroot/stdout
ret=$?
if [ $ret -ne 0 ]; then
test_done $testroot $ret
return 1
fi
cmp -s $testroot/stdout.expected $testroot/stdout
ret=$?
if [ $ret -ne 0 ]; then
diff -u $testroot/stdout.expected $testroot/stdout
test_done $testroot $ret
return 1
fi
echo hatsuseno > $testroot/wt/alpha.expected
echo alpha >> $testroot/wt/alpha.expected
cmp -s $testroot/wt/alpha.expected $testroot/wt/alpha
ret=$?
if [ $ret -ne 0 ]; then
diff -u $testroot/wt/alpha.expected $testroot/wt/alpha
fi
test_done $testroot $ret
}
test_patch_replace_line() {
local testroot=`test_init patch_replace_line`
got checkout $testroot/repo $testroot/wt > /dev/null
ret=$?
if [ $ret -ne 0 ]; then
test_done $testroot $ret
return 1
fi
jot 10 > $testroot/wt/numbers
(cd $testroot/wt/ && got add numbers && got ci -m 'add numbers') \
>/dev/null
ret=$?
if [ $ret -ne 0 ]; then
test_done $testroot $ret
return 1
fi
cat <<EOF > $testroot/wt/patch
--- numbers
+++ numbers
@@ -3,7 +3,7 @@
3
4
5
-6
+foo
7
8
9
EOF
echo "M numbers" > $testroot/stdout.expected
(cd $testroot/wt && got patch patch) > $testroot/stdout
ret=$?
if [ $ret -ne 0 ]; then
test_done $testroot $ret
return 1
fi
cmp -s $testroot/stdout.expected $testroot/stdout
ret=$?
if [ $ret -ne 0 ]; then
diff -u $testroot/stdout.expected $testroot/stdout
test_done $testroot $ret
return 1
fi
jot 10 | sed 's/6/foo/' > $testroot/wt/numbers.expected
cmp -s $testroot/wt/numbers.expected $testroot/wt/numbers
ret=$?
if [ $ret -ne 0 ]; then
diff -u $testroot/wt/numbers.expected $testroot/wt/numbers
fi
test_done $testroot $ret
}
test_patch_multiple_hunks() {
local testroot=`test_init patch_replace_multiple_hunks`
got checkout $testroot/repo $testroot/wt > /dev/null
ret=$?
if [ $ret -ne 0 ]; then
test_done $testroot $ret
return 1
fi
jot 100 > $testroot/wt/numbers
(cd $testroot/wt/ && got add numbers && got ci -m 'add numbers') \
>/dev/null
ret=$?
if [ $ret -ne 0 ]; then
test_done $testroot $ret
return 1
fi
cat <<EOF > $testroot/wt/patch
--- numbers
+++ numbers
@@ -3,7 +3,7 @@
3
4
5
-6
+foo
7
8
9
@@ -57,7 +57,7 @@
57
58
59
-60
+foo foo
61
62
63
@@ -98,3 +98,6 @@
98
99
100
+101
+102
+...
EOF
echo "M numbers" > $testroot/stdout.expected
(cd $testroot/wt && got patch patch) > $testroot/stdout
ret=$?
if [ $ret -ne 0 ]; then
test_done $testroot $ret
return 1
fi
cmp -s $testroot/stdout.expected $testroot/stdout
ret=$?
if [ $ret -ne 0 ]; then
diff -u $testroot/stdout.expected $testroot/stdout
test_done $testroot $ret
return 1
fi
jot 100 | sed -e 's/^6$/foo/' -e 's/^60$/foo foo/' \
> $testroot/wt/numbers.expected
echo "101" >> $testroot/wt/numbers.expected
echo "102" >> $testroot/wt/numbers.expected
echo "..." >> $testroot/wt/numbers.expected
cmp -s $testroot/wt/numbers.expected $testroot/wt/numbers
ret=$?
if [ $ret -ne 0 ]; then
diff -u $testroot/wt/numbers.expected $testroot/wt/numbers
fi
test_done $testroot $ret
}
test_patch_multiple_files() {
local testroot=`test_init patch_multiple_files`
got checkout $testroot/repo $testroot/wt > /dev/null
ret=$?
if [ $ret -ne 0 ]; then
test_done $testroot $ret
return 1
fi
cat <<EOF > $testroot/wt/patch
--- alpha Mon Mar 7 19:02:07 2022
+++ alpha Mon Mar 7 19:01:53 2022
@@ -1 +1,3 @@
+new
alpha
+available
--- beta Mon Mar 7 19:02:11 2022
+++ beta Mon Mar 7 19:01:46 2022
@@ -1 +1,3 @@
beta
+was
+improved
--- gamma/delta Mon Mar 7 19:02:17 2022
+++ gamma/delta Mon Mar 7 19:01:37 2022
@@ -1 +1 @@
-delta
+delta new
EOF
echo "M alpha" > $testroot/stdout.expected
echo "M beta" >> $testroot/stdout.expected
echo "M gamma/delta" >> $testroot/stdout.expected
(cd $testroot/wt && got patch patch) > $testroot/stdout
ret=$?
if [ $ret -ne 0 ]; then
test_done $testroot $ret
return 1
fi
cmp -s $testroot/stdout.expected $testroot/stdout
ret=$?
if [ $ret -ne 0 ]; then
diff -u $testroot/stdout.expected $testroot/stdout
test_done $testroot $ret
return 1
fi
printf 'new\nalpha\navailable\n' > $testroot/wt/alpha.expected
printf 'beta\nwas\nimproved\n' > $testroot/wt/beta.expected
printf 'delta new\n' > $testroot/wt/gamma/delta.expected
for f in alpha beta gamma/delta; do
cmp -s $testroot/wt/$f.expected $testroot/wt/$f
ret=$?
if [ $ret -ne 0 ]; then
diff -u $testroot/wt/$f.expected $testroot/wt/$f
test_done $testroot $ret
return 1
fi
done
test_done $testroot 0
}
test_patch_dont_apply() {
local testroot=`test_init patch_dont_apply`
got checkout $testroot/repo $testroot/wt > /dev/null
ret=$?
if [ $ret -ne 0 ]; then
test_done $testroot $ret
return 1
fi
cat <<EOF > $testroot/wt/patch
--- alpha
+++ alpha
@@ -1 +1,2 @@
+hatsuseno
alpha something
EOF
echo -n > $testroot/stdout.expected
echo "got: patch doesn't apply" > $testroot/stderr.expected
(cd $testroot/wt && got patch patch) \
> $testroot/stdout \
2> $testroot/stderr
ret=$?
if [ $ret -eq 0 ]; then # should fail
test_done $testroot 1
return 1
fi
cmp -s $testroot/stdout.expected $testroot/stdout
ret=$?
if [ $ret -ne 0 ]; then
diff -u $testroot/stdout.expected $testroot/stdout
test_done $testroot $ret
return 1
fi
cmp -s $testroot/stderr.expected $testroot/stderr
ret=$?
if [ $ret -ne 0 ]; then
diff -u $testroot/stderr.expected $testroot/stderr
test_done $testroot $ret
return 1
fi
test_done $testroot $ret
}
test_patch_malformed() {
local testroot=`test_init patch_malformed`
got checkout $testroot/repo $testroot/wt > /dev/null
ret=$?
if [ $ret -ne 0 ]; then
test_done $testroot $ret
return 1
fi
# missing "@@"
cat <<EOF > $testroot/wt/patch
--- alpha
+++ alpha
@@ -1 +1,2
+hatsuseno
alpha
EOF
echo -n > $testroot/stdout.expected
echo "got: malformed patch" > $testroot/stderr.expected
(cd $testroot/wt && got patch patch) \
> $testroot/stdout \
2> $testroot/stderr
ret=$?
if [ $ret -eq 0 ]; then
echo "got managed to apply an invalid patch"
test_done $testroot 1
return 1
fi
cmp -s $testroot/stdout.expected $testroot/stdout
ret=$?
if [ $ret -ne 0 ]; then
diff -u $testroot/stdout.expected $testroot/stdout
test_done $testroot $ret
return 1
fi
cmp -s $testroot/stderr.expected $testroot/stderr
ret=$?
if [ $ret -ne 0 ]; then
diff -u $testroot/stderr.expected $testroot/stderr
test_done $testroot $ret
return 1
fi
# wrong first character
cat <<EOF > $testroot/wt/patch
--- alpha
+++ alpha
@@ -1 +1,2 @@
+hatsuseno
alpha
EOF
(cd $testroot/wt && got patch patch) \
> $testroot/stdout \
2> $testroot/stderr
ret=$?
if [ $ret -eq 0 ]; then
echo "got managed to apply an invalid patch"
test_done $testroot 1
return 1
fi
cmp -s $testroot/stdout.expected $testroot/stdout
ret=$?
if [ $ret -ne 0 ]; then
diff -u $testroot/stdout.expected $testroot/stdout
test_done $testroot $ret
return 1
fi
cmp -s $testroot/stderr.expected $testroot/stderr
ret=$?
if [ $ret -ne 0 ]; then
diff -u $testroot/stderr.expected $testroot/stderr
test_done $testroot $ret
return 1
fi
test_done $testroot $ret
}
test_patch_no_patch() {
local testroot=`test_init patch_no_patch`
got checkout $testroot/repo $testroot/wt > /dev/null
ret=$?
if [ $ret -ne 0 ]; then
test_done $testroot $ret
return 1
fi
cat <<EOF > $testroot/wt/patch
hello world!
...
some other nonsense
...
there's no patch in here!
EOF
echo -n > $testroot/stdout.expected
echo "got: no patch found" > $testroot/stderr.expected
(cd $testroot/wt && got patch patch) \
> $testroot/stdout \
2> $testroot/stderr
ret=$?
if [ $ret -eq 0 ]; then # should fail
test_done $testroot 1
return 1
fi
cmp -s $testroot/stdout.expected $testroot/stdout
ret=$?
if [ $ret -ne 0 ]; then
diff -u $testroot/stdout.expected $testroot/stdout
test_done $testroot $ret
return 1
fi
cmp -s $testroot/stderr.expected $testroot/stderr
ret=$?
if [ $ret -ne 0 ]; then
diff -u $testroot/stderr.expected $testroot/stderr
test_done $testroot $ret
return 1
fi
test_done $testroot $ret
}
test_patch_equals_for_context() {
local testroot=`test_init patch_prepend_line`
got checkout $testroot/repo $testroot/wt > /dev/null
ret=$?
if [ $ret -ne 0 ]; then
test_done $testroot $ret
return 1
fi
cat <<EOF > $testroot/wt/patch
--- alpha
+++ alpha
@@ -1 +1,2 @@
+hatsuseno
=alpha
EOF
echo "M alpha" > $testroot/stdout.expected
(cd $testroot/wt && got patch patch) > $testroot/stdout
ret=$?
if [ $ret -ne 0 ]; then
test_done $testroot $ret
return 1
fi
cmp -s $testroot/stdout.expected $testroot/stdout
ret=$?
if [ $ret -ne 0 ]; then
diff -u $testroot/stdout.expected $testroot/stdout
test_done $testroot $ret
return 1
fi
echo hatsuseno > $testroot/wt/alpha.expected
echo alpha >> $testroot/wt/alpha.expected
cmp -s $testroot/wt/alpha.expected $testroot/wt/alpha
ret=$?
if [ $ret -ne 0 ]; then
diff -u $testroot/wt/alpha.expected $testroot/wt/alpha
fi
test_done $testroot $ret
}
test_parseargs "$@"
run_test test_patch_simple_add_file
run_test test_patch_simple_rm_file
run_test test_patch_simple_edit_file
run_test test_patch_prepend_line
run_test test_patch_replace_line
run_test test_patch_multiple_hunks
run_test test_patch_multiple_files
run_test test_patch_dont_apply
run_test test_patch_malformed
run_test test_patch_no_patch
run_test test_patch_equals_for_context