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 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
|
#!/usr/bin/env bash
include './src/send_patch.sh'
include './tests/unit/utils.sh'
function oneTimeSetUp()
{
declare -gr ORIGINAL_DIR="$PWD"
declare -gr FAKE_GIT="$SHUNIT_TMPDIR/fake_git/"
declare -gr FAKE_KERNEL="$FAKE_GIT/fake_kernel/"
declare -ga test_config_opts=('test0' 'test1' 'test2' 'user.name' 'sendemail.smtpuser')
export KW_ETC_DIR="$SHUNIT_TMPDIR/etc/"
export KW_CACHE_DIR="$SHUNIT_TMPDIR/cache/"
mk_fake_kernel_root "$FAKE_KERNEL"
mkdir -p "$KW_ETC_DIR/mail_templates/"
touch "$KW_ETC_DIR/mail_templates/test1"
printf '%s\n' 'sendemail.smtpserver=smtp.test1.com' > "$KW_ETC_DIR/mail_templates/test1"
touch "$KW_ETC_DIR/mail_templates/test2"
printf '%s\n' 'sendemail.smtpserver=smtp.test2.com' > "$KW_ETC_DIR/mail_templates/test2"
cd "$FAKE_GIT" || {
ret="$?"
fail "($LINENO): Failed to move to fake git dir"
exit "$ret"
}
mk_fake_git
cd "$ORIGINAL_DIR" || {
ret="$?"
fail "($LINENO): Failed to move back to original dir"
exit "$ret"
}
}
function oneTimeTearDown()
{
rm -rf "$FAKE_GIT"
}
function setUp()
{
declare -gA options_values
declare -gA set_confs
}
function tearDown()
{
unset options_values
unset set_confs
}
function test_validate_encryption()
{
local ret
# invalid values
validate_encryption 'xpto' &> /dev/null
ret="$?"
assert_equals_helper 'Expected an error' "$LINENO" 22 "$ret"
validate_encryption 'rsa' &> /dev/null
ret="$?"
assert_equals_helper 'Expected an error' "$LINENO" 22 "$ret"
validate_encryption 'tlss' &> /dev/null
ret="$?"
assert_equals_helper 'Expected an error' "$LINENO" 22 "$ret"
validate_encryption 'ssll' &> /dev/null
ret="$?"
assert_equals_helper 'Expected an error' "$LINENO" 22 "$ret"
validate_encryption &> /dev/null
ret="$?"
assert_equals_helper 'Expected an error' "$LINENO" 22 "$ret"
# valid values
validate_encryption 'ssl'
ret="$?"
assert_equals_helper 'Expected no error for ssl' "$LINENO" 0 "$ret"
validate_encryption 'tls'
ret="$?"
assert_equals_helper 'Expected no error for tls' "$LINENO" 0 "$ret"
}
function test_validate_email()
{
local expected
local output
local ret
# invalid values
output="$(validate_email 'invalid email')"
ret="$?"
expected='Invalid email: invalid email'
assert_equals_helper 'Invalid email was passed' "$LINENO" "$expected" "$output"
assert_equals_helper 'Expected an error' "$LINENO" 22 "$ret"
output="$(validate_email 'lalala')"
ret="$?"
expected='Invalid email: lalala'
assert_equals_helper 'Invalid email was passed' "$LINENO" "$expected" "$output"
assert_equals_helper 'Expected an error' "$LINENO" 22 "$ret"
# valid values
validate_email 'test@email.com'
ret="$?"
assert_equals_helper 'Expected a success' "$LINENO" 0 "$ret"
validate_email 'test123@serious.gov'
ret="$?"
assert_equals_helper 'Expected a success' "$LINENO" 0 "$ret"
}
function test_find_commit_references()
{
local output
local ret
cd "$SHUNIT_TMPDIR" || {
ret="$?"
fail "($LINENO): Failed to move to temp dir"
exit "$ret"
}
find_commit_references
ret="$?"
assert_equals_helper 'No arguments given' "$LINENO" 22 "$ret"
find_commit_references @^
ret="$?"
assert_equals_helper 'Outside git repo should return 125' "$LINENO" 125 "$ret"
cd "$FAKE_GIT" || {
ret="$?"
fail "($LINENO): Failed to move to fake git repo"
exit "$ret"
}
output="$(find_commit_references invalid_ref)"
ret="$?"
assert_equals_helper 'Invalid ref should not work' "$LINENO" 22 "$ret"
assertTrue "($LINENO) Invalid ref should be empty" '[[ -z "$output" ]]'
output="$(find_commit_references '@^..@')"
ret="$?"
assert_equals_helper '@^..@ should be a valid reference' "$LINENO" 0 "$ret"
assertTrue "($LINENO) @^..@ should generate a reference" '[[ -n "$output" ]]'
output="$(find_commit_references @)"
ret="$?"
assert_equals_helper '@ should be a valid reference' "$LINENO" 0 "$ret"
assertTrue "($LINENO) @ should generate a reference" '[[ -n "$output" ]]'
output="$(find_commit_references some args @ around)"
ret="$?"
assert_equals_helper '@ should be a valid reference' "$LINENO" 0 "$ret"
assertTrue "($LINENO) @ should generate a reference" '[[ -n "$output" ]]'
cd "$ORIGINAL_DIR" || {
ret="$?"
fail "($LINENO): Failed to move back to original dir"
exit "$ret"
}
}
function test_validate_email_list()
{
local expected
local output
local ret
# invalid values
output="$(validate_email_list 'invalid email')"
ret="$?"
expected='The given recipient: invalid email does not contain a valid e-mail.'
assert_equals_helper 'Invalid email was passed' "$LINENO" "$expected" "$output"
assert_equals_helper 'Expected an error' "$LINENO" 22 "$ret"
output="$(validate_email_list 'lalala')"
ret="$?"
expected='The given recipient: lalala does not contain a valid e-mail.'
assert_equals_helper 'Invalid email was passed' "$LINENO" "$expected" "$output"
assert_equals_helper 'Expected an error' "$LINENO" 22 "$ret"
output="$(validate_email_list 'name1@lala.com,name2@lala.xpto,LastName, FirstName <last.first@lala.com>,test123@serious.gov')"
ret="$?"
expected='The given recipient: LastName does not contain a valid e-mail.'
assert_equals_helper 'Expected an error' "$LINENO" 22 "$ret"
# valid values
validate_email_list 'test@email.com'
ret="$?"
assert_equals_helper 'Expected a success' "$LINENO" 0 "$ret"
validate_email_list 'name1@lala.com,name2@lala.xpto,name3 second <name3second@lala.com>,test123@serious.gov'
ret="$?"
assert_equals_helper 'Expected a success' "$LINENO" 0 "$ret"
}
function test_reposition_commit_count_arg()
{
local output
local expected
output="$(reposition_commit_count_arg --any --amount --of --args)"
expected=' "--any" "--amount" "--of" "--args"'
assert_equals_helper 'Should not change arguments' "$LINENO" "$expected" "$output"
output="$(reposition_commit_count_arg --arg='some options, lala')"
expected=' "--arg=some options, lala"'
assert_equals_helper 'Should correctly quote arguments' "$LINENO" "$expected" "$output"
output="$(reposition_commit_count_arg -375)"
expected=' -- -375'
assert_equals_helper 'Should place count argument at the end' "$LINENO" "$expected" "$output"
output="$(reposition_commit_count_arg --arg='some options, lala' -375)"
expected=' "--arg=some options, lala" -- -375'
assert_equals_helper 'Should handle multiple arguments' "$LINENO" "$expected" "$output"
}
function test_remove_blocked_recipients()
{
local output
local expected
local recipients=$'test@mail.com\nXpto Lala <xpto@mail.com>\nlala@mail.com\n'
recipients+=$'xpto.lala@mail.com'
output="$(remove_blocked_recipients '' test)"
assertTrue "($LINENO) Empty recipients." '[[ -z "$output" ]]'
output="$(remove_blocked_recipients "$recipients" test)"
expected="$recipients"
multilineAssertEquals "($LINENO) Expected no change." "$expected" "$output"
output="$(remove_blocked_recipients "$recipients" test@mail.com)"
expected=$'Xpto Lala <xpto@mail.com>\nlala@mail.com\nxpto.lala@mail.com'
multilineAssertEquals "($LINENO) Removing one email." "$expected" "$output"
output="$(remove_blocked_recipients "$recipients" lala@mail.com)"
expected=$'test@mail.com\nXpto Lala <xpto@mail.com>\nxpto.lala@mail.com'
multilineAssertEquals "($LINENO) Removing one email." "$expected" "$output"
output="$(remove_blocked_recipients "$recipients" test@mail.com,xpto@mail.com)"
expected=$'lala@mail.com\nxpto.lala@mail.com'
multilineAssertEquals "($LINENO) Removing two emails." "$expected" "$output"
}
function test_mail_parser()
{
local output
local expected
local ret
cd "$FAKE_GIT" || {
ret="$?"
fail "($LINENO): Failed to move to fake git dir"
exit "$ret"
}
# Invalid options
parse_mail_options '-t' '--smtpuser'
ret="$?"
assert_equals_helper 'Option without argument' "$LINENO" 22 "$ret"
output=$(parse_mail_options '--name' 'Xpto')
ret="$?"
assert_equals_helper 'Option without --setup' "$LINENO" 95 "$ret"
parse_mail_options '--smtpLalaXpto' 'lala xpto'
ret="$?"
assert_equals_helper 'Invalid option passed' "$LINENO" 22 "$ret"
parse_mail_options '--wrongOption' 'lala xpto'
ret="$?"
assert_equals_helper 'Invalid option passed' "$LINENO" 22 "$ret"
# valid options
parse_mail_options some -- extra -1 args HEAD^
expected='some extra args HEAD^ -1'
assert_equals_helper 'Set passthrough options' "$LINENO" "$expected" "${options_values['PASS_OPTION_TO_SEND_EMAIL']}"
assert_equals_helper 'Set passthrough options' "$LINENO" '-1 HEAD^' "${options_values['COMMIT_RANGE']}"
parse_mail_options -- --subject-prefix="PATCH i-g-t" HEAD^
expected="'--subject-prefix=PATCH i-g-t' HEAD^"
assert_equals_helper 'Set passthrough options with space' "$LINENO" "$expected" "${options_values['PASS_OPTION_TO_SEND_EMAIL']}"
parse_mail_options -375
expected='-375'
assert_equals_helper 'Set commit count option' "$LINENO" "$expected" "${options_values['PASS_OPTION_TO_SEND_EMAIL']}"
assert_equals_helper 'Set commit count option' "$LINENO" "$expected " "${options_values['COMMIT_RANGE']}"
parse_mail_options -v3
expected='-v3'
assert_equals_helper 'Set version option' "$LINENO" "$expected" "${options_values['PATCH_VERSION']}"
expected='-v3 @^'
assert_equals_helper 'Set version option' "$LINENO" "$expected" "${options_values['PASS_OPTION_TO_SEND_EMAIL']}"
assert_equals_helper 'Set version option' "$LINENO" '@^' "${options_values['COMMIT_RANGE']}"
parse_mail_options '--send'
assert_equals_helper 'Set send flag' "$LINENO" 1 "${options_values['SEND']}"
parse_mail_options '--verbose'
assert_equals_helper 'Set verbose option' "$LINENO" 1 "${options_values['VERBOSE']}"
parse_mail_options '--private'
expected='--suppress-cc=all'
assert_equals_helper 'Set private flag' "$LINENO" "$expected" "${options_values['PRIVATE']}"
parse_mail_options '--rfc'
expected='--rfc'
assert_equals_helper 'Set rfc flag' "$LINENO" "$expected" "${options_values['RFC']}"
parse_mail_options '--to=some@mail.com'
expected='some@mail.com'
assert_equals_helper 'Set to flag' "$LINENO" "$expected" "${options_values['TO']}"
parse_mail_options '--cc=some@mail.com'
expected='some@mail.com'
assert_equals_helper 'Set cc flag' "$LINENO" "$expected" "${options_values['CC']}"
parse_mail_options '--simulate'
expected='--dry-run'
assert_equals_helper 'Set simulate flag' "$LINENO" "$expected" "${options_values['SIMULATE']}"
parse_mail_options '--to=name1@lala.com,name2@lala.xpto,name3 second <name3second@lala.com>'
expected='name1@lala.com,name2@lala.xpto,name3 second <name3second@lala.com>'
assert_equals_helper 'Set to flag' "$LINENO" "$expected" "${options_values['TO']}"
parse_mail_options '--setup'
expected=1
assert_equals_helper 'Set setup flag' "$LINENO" "$expected" "${options_values['SETUP']}"
parse_mail_options '--force'
expected=1
assert_equals_helper 'Set force flag' "$LINENO" "$expected" "${options_values['FORCE']}"
parse_mail_options '--verify'
expected_result=1
assert_equals_helper 'Set verify flag' "$LINENO" "$expected_result" "${options_values['VERIFY']}"
parse_mail_options '--template'
expected_result=':'
assert_equals_helper 'Template without options' "$LINENO" "$expected_result" "${options_values['TEMPLATE']}"
parse_mail_options '--template=test'
expected_result=':test'
assert_equals_helper 'Set template flag' "$LINENO" "$expected_result" "${options_values['TEMPLATE']}"
parse_mail_options '--template= Test '
expected_result=':test'
assert_equals_helper 'Set template flag, case and spaces' "$LINENO" "$expected_result" "${options_values['TEMPLATE']}"
parse_mail_options '--interactive'
expected_result='parser'
assert_equals_helper 'Set interactive flag' "$LINENO" "$expected_result" "${options_values['INTERACTIVE']}"
parse_mail_options '--no-interactive'
expected_result=1
assert_equals_helper 'Set no-interactive flag' "$LINENO" "$expected_result" "${options_values['NO_INTERACTIVE']}"
expected=''
assert_equals_helper 'Unset local or global flag' "$LINENO" "$expected" "${options_values['CMD_SCOPE']}"
expected='local'
assert_equals_helper 'Unset local or global flag' "$LINENO" "$expected" "${options_values['SCOPE']}"
parse_mail_options '--local'
assert_equals_helper 'Set local flag' "$LINENO" "$expected" "${options_values['SCOPE']}"
assert_equals_helper 'Set local flag' "$LINENO" "$expected" "${options_values['CMD_SCOPE']}"
parse_mail_options '--global'
expected='global'
assert_equals_helper 'Set global flag' "$LINENO" "$expected" "${options_values['SCOPE']}"
assert_equals_helper 'Set global flag' "$LINENO" "$expected" "${options_values['CMD_SCOPE']}"
parse_mail_options '-t' '--name' 'Xpto Lala'
expected='Xpto Lala'
assert_equals_helper 'Set name' "$LINENO" "$expected" "${options_values['user.name']}"
parse_mail_options '-t' '--email' 'test@email.com'
expected='test@email.com'
assert_equals_helper 'Set email' "$LINENO" "$expected" "${options_values['user.email']}"
parse_mail_options '-t' '--smtpuser' 'test@email.com'
expected='test@email.com'
assert_equals_helper 'Set smtp user' "$LINENO" "$expected" "${options_values['sendemail.smtpuser']}"
parse_mail_options '-t' '--smtpencryption' 'tls'
expected='tls'
assert_equals_helper 'Set smtp encryption to tls' "$LINENO" "$expected" "${options_values['sendemail.smtpencryption']}"
parse_mail_options '-t' '--smtpencryption' 'ssl'
expected='ssl'
assert_equals_helper 'Set smtp encryption to ssl' "$LINENO" "$expected" "${options_values['sendemail.smtpencryption']}"
parse_mail_options '-t' '--smtpserver' 'test.email.com'
expected='test.email.com'
assert_equals_helper 'Set smtp server' "$LINENO" "$expected" "${options_values['sendemail.smtpserver']}"
parse_mail_options '-t' '--smtpserverport' '123'
expected='123'
assert_equals_helper 'Set smtp serverport' "$LINENO" "$expected" "${options_values['sendemail.smtpserverport']}"
parse_mail_options '-t' '--smtppass' 'verySafePass'
expected='verySafePass'
assert_equals_helper 'Set smtp pass' "$LINENO" "$expected" "${options_values['sendemail.smtppass']}"
cd "$ORIGINAL_DIR" || {
ret="$?"
fail "($LINENO): Failed to move back to original dir"
exit "$ret"
}
}
function test_mail_send()
{
local expected
local output
local ret
cd "$FAKE_GIT" || {
ret="$?"
fail "($LINENO): Failed to move to fake git repo"
exit "$ret"
}
parse_mail_options
output=$(mail_send 'TEST_MODE')
expected='git send-email @^'
assert_equals_helper 'Testing send without options' "$LINENO" "$expected" "$output"
parse_mail_options '--to=mail@test.com'
output=$(mail_send 'TEST_MODE')
expected='git send-email --to="mail@test.com" @^'
assert_equals_helper 'Testing send with to option' "$LINENO" "$expected" "$output"
parse_mail_options '--to=name1@lala.com,name2@lala.xpto,name3 second <name3second@lala.com>,test123@serious.gov'
output=$(mail_send 'TEST_MODE')
expected='git send-email --to="name1@lala.com,name2@lala.xpto,name3 second <name3second@lala.com>,test123@serious.gov" @^'
assert_equals_helper 'Testing send with to option' "$LINENO" "$expected" "$output"
parse_mail_options '--cc=mail@test.com'
output=$(mail_send 'TEST_MODE')
expected='git send-email --cc="mail@test.com" @^'
assert_equals_helper 'Testing send with c option' "$LINENO" "$expected" "$output"
parse_mail_options '--cc=name1@lala.com,name2@lala.xpto,name3 second <name3second@lala.com>,test123@serious.gov'
output=$(mail_send 'TEST_MODE')
expected='git send-email --cc="name1@lala.com,name2@lala.xpto,name3 second <name3second@lala.com>,test123@serious.gov" @^'
assert_equals_helper 'Testing send with cc option' "$LINENO" "$expected" "$output"
parse_mail_options '--simulate'
output=$(mail_send 'TEST_MODE')
expected='git send-email --dry-run @^'
assert_equals_helper 'Testing send with simulate option' "$LINENO" "$expected" "$output"
parse_mail_options '--private'
output=$(mail_send 'TEST_MODE')
expected="git send-email --suppress-cc=all @^"
assert_equals_helper 'Testing send with to option' "$LINENO" "$expected" "$output"
parse_mail_options '--rfc'
output=$(mail_send 'TEST_MODE')
expected="git send-email --rfc @^"
assert_equals_helper 'Testing send with rfc option' "$LINENO" "$expected" "$output"
parse_mail_options '--to=mail@test.com' 'HEAD~'
output=$(mail_send 'TEST_MODE')
expected='git send-email --to="mail@test.com" HEAD~'
assert_equals_helper 'Testing send with patch option' "$LINENO" "$expected" "$output"
parse_mail_options '--to=mail@test.com' -13 -v2 extra_args -- --other_arg
output=$(mail_send 'TEST_MODE')
expected='git send-email --to="mail@test.com" extra_args --other_arg -13 -v2'
assert_equals_helper 'Testing no options option' "$LINENO" "$expected" "$output"
parse_mail_options '--to=mail@test.com'
parse_configuration "$KW_MAIL_CONFIG_SAMPLE" send_patch_config
output=$(mail_send 'TEST_MODE')
expected='git send-email --to="mail@test.com" --annotate --no-chain-reply-to --thread @^'
assert_equals_helper 'Testing default option' "$LINENO" "$expected" "$output"
parse_mail_options '--to=mail@test.com' '@^^'
parse_configuration "$KW_CONFIG_SAMPLE"
output=$(mail_send 'TEST_MODE')
expected='git send-email --to="mail@test.com" --annotate --cover-letter --no-chain-reply-to --thread @^^'
assert_equals_helper 'Testing default option' "$LINENO" "$expected" "$output"
cd "$ORIGINAL_DIR" || {
ret="$?"
fail "($LINENO): Failed to move back to original dir"
exit "$ret"
}
}
function test_get_configs()
{
local output
local expected
local ret
cd "$FAKE_GIT" || {
ret="$?"
fail "($LINENO): Failed to move to fake git repo"
exit "$ret"
}
options_values['CMD_SCOPE']=''
git config --local sendemail.smtpuser ''
git config --local sendemail.smtppass safePass
get_configs
output=${set_confs['local_user.name']}
expected='Xpto Lala'
assert_equals_helper 'Checking local name' "$LINENO" "$expected" "$output"
output=${set_confs['local_user.email']}
expected='test@email.com'
assert_equals_helper 'Checking local email' "$LINENO" "$expected" "$output"
output=${set_confs['local_sendemail.smtppass']}
expected='********'
assert_equals_helper 'Checking local smtppass' "$LINENO" "$expected" "$output"
output=${set_confs['local_sendemail.smtpuser']}
expected='<empty>'
assert_equals_helper 'Checking local smtpuser' "$LINENO" "$expected" "$output"
git config --local --unset sendemail.smtpuser
cd "$ORIGINAL_DIR" || {
ret="$?"
fail "($LINENO): Failed to move back to original dir"
exit "$ret"
}
}
function test_missing_options()
{
local -a output
local -a expected_arr
cd "$FAKE_GIT" || {
ret="$?"
fail "($LINENO): Failed to move to fake git repo"
exit "$ret"
}
parse_mail_options --local
get_configs
mapfile -t output < <(missing_options 'essential_config_options')
expected_arr=('sendemail.smtpuser' 'sendemail.smtpserver' 'sendemail.smtpserverport')
compare_array_values 'expected_arr' 'output' "$LINENO"
mapfile -t output < <(missing_options 'optional_config_options')
expected_arr=('sendemail.smtpencryption')
compare_array_values 'expected_arr' 'output' "$LINENO"
cd "$ORIGINAL_DIR" || {
ret="$?"
fail "($LINENO): Failed to move back to original dir"
exit "$ret"
}
}
function test_config_values()
{
local -A output
local -A expected
cd "$FAKE_GIT" || {
ret="$?"
fail "($LINENO): Failed to move to fake git repo"
exit "$ret"
}
get_configs
options_values['user.name']='Loaded Name'
config_values 'output' 'user.name'
expected['local']='Xpto Lala'
expected['loaded']='Loaded Name'
assert_equals_helper 'Checking local name' "$LINENO" "${expected['local']}" "${output['local']}"
assert_equals_helper 'Checking loaded name' "$LINENO" "${expected['loaded']}" "${output['loaded']}"
cd "$ORIGINAL_DIR" || {
ret="$?"
fail "($LINENO): Failed to move back to original dir"
exit "$ret"
}
}
function test_add_config()
{
local output
local expected
local ret
options_values['test.opt']='value'
options_values['CMD_SCOPE']='global'
# test default values
output=$(add_config 'test.opt' '' '' 'TEST_MODE')
expected="git config --global test.opt 'value'"
assert_equals_helper 'Testing serverport option' "$LINENO" "$expected" "$output"
output=$(add_config 'test.option' 'test_value' 'local' 'TEST_MODE')
expected="git config --local test.option 'test_value'"
assert_equals_helper 'Testing serverport option' "$LINENO" "$expected" "$output"
}
function test_mail_setup()
{
local expected
local output
local ret
local -a expected_results=(
"git config -- sendemail.smtpencryption 'ssl'"
"git config -- sendemail.smtppass 'verySafePass'"
"git config -- sendemail.smtpserver 'test.email.com'"
"git config -- sendemail.smtpuser 'test@email.com'"
"git config -- user.email 'test@email.com'"
"git config -- user.name 'Xpto Lala'"
)
cd "$FAKE_GIT" || {
ret="$?"
fail "($LINENO): Failed to move to fake git repo"
exit "$ret"
}
# prepare options for testing
parse_mail_options '-t' '--force' '--smtpencryption' 'ssl' '--smtppass' 'verySafePass' \
'--email' 'test@email.com' '--name' 'Xpto Lala' \
'--smtpuser' 'test@email.com' '--smtpserver' 'test.email.com'
output=$(mail_setup 'TEST_MODE' | sort -d)
compare_command_sequence '' "$LINENO" 'expected_results' "$output"
unset options_values
declare -gA options_values
get_configs
parse_mail_options '-t' '--name' 'Xpto Lala'
output=$(mail_setup 'TEST_MODE')
expected="git config -- user.name 'Xpto Lala'"
assert_equals_helper 'Testing config with same value' "$LINENO" "$expected" "$output"
parse_mail_options '-t' '--name' 'Lala Xpto'
output=$(printf 'n\n' | mail_setup 'TEST_MODE' | tail -n 1)
expected='No configuration options were set.'
assert_equals_helper 'Operation should be skipped' "$LINENO" "$expected" "$output"
output=$(printf 'y\n' | mail_setup 'TEST_MODE' | tail -n 1)
expected="git config -- user.name 'Lala Xpto'"
assert_equals_helper 'Testing confirmation' "$LINENO" "$expected" "$output"
unset options_values
declare -gA options_values
parse_mail_options '-t' '--local' '--smtpserverport' '123'
output=$(mail_setup 'TEST_MODE')
expected="git config --local sendemail.smtpserverport '123'"
assert_equals_helper 'Testing serverport option' "$LINENO" "$expected" "$output"
options_values['sendemail.smtpserverport']=''
options_values['user.name']='Xpto Lala'
output=$(mail_setup 'TEST_MODE')
expected="git config --local user.name 'Xpto Lala'"
assert_equals_helper 'Testing config with same value' "$LINENO" "$expected" "$output"
unset options_values
declare -gA options_values
parse_mail_options '-t' '--local' '--smtpuser' 'username'
output=$(mail_setup 'TEST_MODE')
expected="git config --local sendemail.smtpuser 'username'"
assert_equals_helper 'Testing smtpuser option' "$LINENO" "$expected" "$output"
unset options_values
declare -gA options_values
# we need to force in case the user has set config at a global scope
parse_mail_options '-t' '--force' '--global' '--smtppass' 'verySafePass'
output=$(mail_setup 'TEST_MODE')
expected="git config --global sendemail.smtppass 'verySafePass'"
assert_equals_helper 'Testing global option' "$LINENO" "$expected" "$output"
cd "$SHUNIT_TMPDIR" || {
ret="$?"
fail "($LINENO): Failed to move to shunit temp dir"
exit "$ret"
}
unset options_values
declare -gA options_values
# we need to force in case the user has set config at a global scope
parse_mail_options '-t' '--force' '--global' '--smtppass' 'verySafePass'
output=$(mail_setup 'TEST_MODE')
expected="git config --global sendemail.smtppass 'verySafePass'"
assert_equals_helper 'Testing global option outside git' "$LINENO" "$expected" "$output"
cd "$ORIGINAL_DIR" || {
ret="$?"
fail "($LINENO): Failed to move back to original dir"
exit "$ret"
}
}
function test_interactive_prompt()
{
local expected
local output
local -a inputs=(
'' # test essential check
'y' # skip test0
'value1' # input test1
'Lala Xpto' # input name
'n' # don't accept change
'Lala Xpto' # input name
'y' # accept change
'n' # don't change smtpuser
)
cd "$FAKE_GIT" || {
ret="$?"
fail "($LINENO): Failed to move to fake git repo"
exit "$ret"
}
parse_mail_options
options_values['test2']='value2'
options_values['sendemail.smtpuser']='test@email.com'
smtpuser_autoset=1
get_configs
output="$(printf '%s\n' "${inputs[@]}" | interactive_prompt 'test_config_opts')"
smtpuser_autoset=0
expected='Skipping test0...'
assertTrue "($LINENO) Testing test0 skipped" '[[ $output =~ "$expected" ]]'
expected='[local] Setup your test1:'
assertTrue "($LINENO) Testing test1" '[[ $output =~ "$expected" ]]'
expected='[local] Setup your test2:'
assertTrue "($LINENO) test2 shouldn't prompt" '[[ ! $output =~ "$expected" ]]'
expected='[local] Setup your name:'
assertTrue "($LINENO) Testing user.name" '[[ $output =~ "$expected" ]]'
expected='Xpto Lala --> Lala Xpto'
assertTrue "($LINENO) Testing user.name proposed" '[[ $output =~ "$expected" ]]'
expected='kw will set this option to test@email.com'
assertTrue "($LINENO) Testing smtpuser autoset" '[[ $output =~ "$expected" ]]'
# Testing options_values is not working, I suspect it has something to do with
# the way bash handles variables and subshells
# TODO: fix these tests
# expected='value1'
# assert_equals_helper 'Testing test1 value' "$LINENO" "$expected" "${options_values['test1']}"
# expected='value2'
# assert_equals_helper 'Testing test2 value' "$LINENO" "$expected" "${options_values['test2']}"
# expected='Lala Xpto'
# assert_equals_helper 'Testing user.name value' "$LINENO" "$expected" "${options_values['user.name']}"
cd "$ORIGINAL_DIR" || {
ret="$?"
fail "($LINENO): Failed to move back to original dir"
exit "$ret"
}
}
function test_interactive_setup()
{
local expected
local output
local ret
local -a inputs=(
'y' # list
'1' # pick first template; loads smtpserver
'y' # accept name change
'' # user.email
'user@smtp.com' # smtpuser
'123' # smtpserverport
'ssl' # smtpencryption
'' # smtppass
)
cd "$FAKE_GIT" || {
ret="$?"
fail "($LINENO): Failed to move to fake git repo"
exit "$ret"
}
parse_mail_options '-i' '--name' 'Lala Xpto'
get_configs
output=$(printf '%s\n' "${inputs[@]}" | interactive_setup 'TEST_MODE' 2>&1)
# printf '***\n%s\n***' "$output" 1>&2
expected="[local: Xpto Lala]"
assertTrue "($LINENO) Testing user.name on list" '[[ $output =~ "$expected" ]]'
expected="[local] 'user.name' was set to: Lala Xpto"
assertTrue "($LINENO) Testing user.name config" '[[ $output =~ "$expected" ]]'
expected="[local] 'sendemail.smtpuser' was set to: user@smtp.com"
assertTrue "($LINENO) Testing sendemail.smtpuser config" '[[ $output =~ "$expected" ]]'
expected="[local] 'sendemail.smtpserver' was set to: smtp.test1.com"
assertTrue "($LINENO) Testing sendemail.smtpserver config" '[[ $output =~ "$expected" ]]'
expected="[local] 'sendemail.smtpserverport' was set to: 123"
assertTrue "($LINENO) Testing sendemail.smtpserverport config" '[[ $output =~ "$expected" ]]'
expected="[local] 'sendemail.smtpencryption' was set to: ssl"
assertTrue "($LINENO) Testing smtpencryption config" '[[ $output =~ "$expected" ]]'
cd "$ORIGINAL_DIR" || {
ret="$?"
fail "($LINENO): Failed to move back to original dir"
exit "$ret"
}
}
function test_load_template()
{
local output
local expected
local ret
output=$(load_template 'invalid' &> /dev/null)
ret="$?"
expected=22
assert_equals_helper 'Invalid template' "$LINENO" "$expected" "$ret"
load_template 'test1'
expected='smtp.test1.com'
assert_equals_helper 'Load template 1' "$LINENO" "$expected" "${options_values['sendemail.smtpserver']}"
tearDown
setUp
load_template 'test2'
expected='smtp.test2.com'
assert_equals_helper 'Load template 2' "$LINENO" "$expected" "${options_values['sendemail.smtpserver']}"
parse_mail_options -t --smtpserver 'user.given.server'
load_template 'test2'
expected='user.given.server'
assert_equals_helper 'Load template 2 should not overwrite user given values' "$LINENO" "$expected" "${options_values['sendemail.smtpserver']}"
}
function test_template_setup()
{
local output
local expected
local -a expected_results=(
'You may choose one of the following templates to start your configuration.'
'(enter the corresponding number to choose)'
'1) Test1'
'2) Test2'
'3) Exit kw send-patch'
'#?'
)
# empty template flag should trigger menu
output=$(printf '1\n' | template_setup 2>&1)
# couldn't find a way to test the loaded values
compare_command_sequence '' "$LINENO" 'expected_results' "$output"
options_values['TEMPLATE']=':test1'
template_setup
expected='smtp.test1.com'
assert_equals_helper 'Load template 1' "$LINENO" "$expected" "${options_values['sendemail.smtpserver']}"
options_values['TEMPLATE']=':test2'
options_values['sendemail.smtpserver']=''
template_setup
expected='smtp.test2.com'
assert_equals_helper 'Load template 2' "$LINENO" "$expected" "${options_values['sendemail.smtpserver']}"
parse_mail_options --smtpserver 'user.input' --template='test2'
template_setup
expected='user.input'
assert_equals_helper 'Load template 2' "$LINENO" "$expected" "${options_values['sendemail.smtpserver']}"
}
# This test can only be done on a local scope, as we have no control over the
# user's system
function test_mail_verify()
{
local expected
local output
local ret
local -a expected_results=(
'Missing configurations required for send-email:'
'sendemail.smtpuser'
'sendemail.smtpserver'
'sendemail.smtpserverport'
)
cd "$FAKE_GIT" || {
ret="$?"
fail "($LINENO): Failed to move to fake git repo"
exit "$ret"
}
parse_mail_options '--local'
get_configs
output=$(mail_verify)
ret="$?"
assert_equals_helper 'Failed verify expected an error' "$LINENO" 22 "$ret"
compare_command_sequence '' "$LINENO" 'expected_results' "$output"
unset options_values
unset set_confs
declare -gA options_values
declare -gA set_confs
# fulfill required options
parse_mail_options '-t' '--local' '--smtpuser' 'test@email.com' '--smtpserver' \
'test.email.com' '--smtpserverport' '123'
mail_setup &> /dev/null
get_configs
expected_results=(
'It looks like you are ready to send patches as:'
'Xpto Lala <test@email.com>'
''
'If you encounter problems you might need to configure these options:'
'sendemail.smtpencryption'
'sendemail.smtppass'
)
output=$(mail_verify)
ret="$?"
assert_equals_helper 'Expected a success' "$LINENO" 0 "$ret"
compare_command_sequence '' "$LINENO" 'expected_results' "$output"
unset options_values
unset set_confs
declare -gA options_values
declare -gA set_confs
# complete all the settings
parse_mail_options '-t' '--local' '--smtpuser' 'test@email.com' '--smtpserver' \
'test.email.com' '--smtpserverport' '123' '--smtpencryption' 'ssl' \
'--smtppass' 'verySafePass'
mail_setup &> /dev/null
get_configs
output=$(mail_verify | head -1)
expected='It looks like you are ready to send patches as:'
assert_equals_helper 'Expected successful verification' "$LINENO" "$expected" "$output"
unset options_values
unset set_confs
declare -gA options_values
declare -gA set_confs
# test custom local smtpserver
mkdir -p ./fake_server
expected_results=(
'It appears you are using a local smtpserver with custom configurations.'
"Unfortunately we can't verify these configurations yet."
'Current value is: ./fake_server/'
)
parse_mail_options '-t' '--local' '--smtpserver' './fake_server/'
mail_setup &> /dev/null
get_configs
output=$(mail_verify)
compare_command_sequence '' "$LINENO" 'expected_results' "$output"
rm -rf ./fake_server
cd "$ORIGINAL_DIR" || {
ret="$?"
fail "($LINENO): Failed to move back to original dir"
exit "$ret"
}
}
function test_mail_list()
{
local expected
local output
local ret
local -a expected_results=(
'These are the essential configurations for git send-email:'
'NAME'
'[local: Xpto Lala]'
'EMAIL'
'[local: test@email.com]'
'SMTPUSER'
'[local: test@email.com], [loaded: test@email.com]'
'SMTPSERVER'
'[local: test.email.com], [loaded: test.email.com]'
'SMTPSERVERPORT'
'[local: 123], [loaded: 123]'
'These are the optional configurations for git send-email:'
'SMTPENCRYPTION'
'[loaded: ssl]'
'SMTPPASS'
'[local: ********], [loaded: verySafePass]'
)
cd "$FAKE_GIT" || {
ret="$?"
fail "($LINENO): Failed to move to fake git repo"
exit "$ret"
}
parse_mail_options '-t' '--force' '--local' '--smtpuser' 'test@email.com' '--smtpserver' \
'test.email.com' '--smtpserverport' '123' '--smtppass' 'verySafePass'
mail_setup &> /dev/null
git config --local --unset sendemail.smtpencryption
parse_mail_options '-t' '--local' '--smtpencryption' 'ssl'
output=$(mail_list)
compare_command_sequence '' "$LINENO" 'expected_results' "$output"
cd "$ORIGINAL_DIR" || {
ret="$?"
fail "($LINENO): Failed to move back to original dir"
exit "$ret"
}
}
function test_add_recipients()
{
local initial_recipients
local additional_recipients
local output
local expected
initial_recipients=''
additional_recipients=''
output=$(add_recipients "$initial_recipients" "$additional_recipients")
expected=''
assert_equals_helper 'No recipients should output nothing' "$LINENO" "$expected" "$output"
initial_recipients='recipient1@email.com'$'\n'
initial_recipients+='recipient2@email.com'$'\n'
initial_recipients+='recipient3@email.com'$'\n'
initial_recipients+='recipient4@email.com'
output=$(add_recipients "$initial_recipients" "$additional_recipients")
expected="$initial_recipients"
assert_equals_helper 'No additional recipients should output initial recipients' "$LINENO" "$expected" "$output"
additional_recipients='additional1@email.com,additional2@email.com'
output=$(add_recipients "$initial_recipients" "$additional_recipients")
expected="$initial_recipients"$'\n'
expected+='additional1@email.com'$'\n'
expected+='additional2@email.com'
assert_equals_helper 'Wrong output' "$LINENO" "$expected" "$output"
}
invoke_shunit
|