t / t9001-send-email.shon commit cocci: simplify "if (++u > 1)" to "if (u++)" (05b4ed6)
   1#!/bin/sh
   2
   3test_description='git send-email'
   4. ./test-lib.sh
   5
   6# May be altered later in the test
   7PREREQ="PERL"
   8
   9replace_variable_fields () {
  10        sed     -e "s/^\(Date:\).*/\1 DATE-STRING/" \
  11                -e "s/^\(Message-Id:\).*/\1 MESSAGE-ID-STRING/" \
  12                -e "s/^\(X-Mailer:\).*/\1 X-MAILER-STRING/"
  13}
  14
  15test_expect_success $PREREQ 'prepare reference tree' '
  16        echo "1A quick brown fox jumps over the" >file &&
  17        echo "lazy dog" >>file &&
  18        git add file &&
  19        GIT_AUTHOR_NAME="A" git commit -a -m "Initial."
  20'
  21
  22test_expect_success $PREREQ 'Setup helper tool' '
  23        write_script fake.sendmail <<-\EOF &&
  24        shift
  25        output=1
  26        while test -f commandline$output
  27        do
  28                output=$(($output+1))
  29        done
  30        for a
  31        do
  32                echo "!$a!"
  33        done >commandline$output
  34        cat >"msgtxt$output"
  35        EOF
  36        git add fake.sendmail &&
  37        GIT_AUTHOR_NAME="A" git commit -a -m "Second."
  38'
  39
  40clean_fake_sendmail () {
  41        rm -f commandline* msgtxt*
  42}
  43
  44test_expect_success $PREREQ 'Extract patches' '
  45        patches=$(git format-patch -s --cc="One <one@example.com>" --cc=two@example.com -n HEAD^1)
  46'
  47
  48# Test no confirm early to ensure remaining tests will not hang
  49test_no_confirm () {
  50        rm -f no_confirm_okay
  51        echo n | \
  52                GIT_SEND_EMAIL_NOTTY=1 \
  53                git send-email \
  54                --from="Example <from@example.com>" \
  55                --to=nobody@example.com \
  56                --smtp-server="$(pwd)/fake.sendmail" \
  57                $@ \
  58                $patches >stdout &&
  59                ! grep "Send this email" stdout &&
  60                >no_confirm_okay
  61}
  62
  63# Exit immediately to prevent hang if a no-confirm test fails
  64check_no_confirm () {
  65        if ! test -f no_confirm_okay
  66        then
  67                say 'confirm test failed; skipping remaining tests to prevent hanging'
  68                PREREQ="$PREREQ,CHECK_NO_CONFIRM"
  69        fi
  70        return 0
  71}
  72
  73test_expect_success $PREREQ 'No confirm with --suppress-cc' '
  74        test_no_confirm --suppress-cc=sob &&
  75        check_no_confirm
  76'
  77
  78
  79test_expect_success $PREREQ 'No confirm with --confirm=never' '
  80        test_no_confirm --confirm=never &&
  81        check_no_confirm
  82'
  83
  84# leave sendemail.confirm set to never after this so that none of the
  85# remaining tests prompt unintentionally.
  86test_expect_success $PREREQ 'No confirm with sendemail.confirm=never' '
  87        git config sendemail.confirm never &&
  88        test_no_confirm --compose --subject=foo &&
  89        check_no_confirm
  90'
  91
  92test_expect_success $PREREQ 'Send patches' '
  93        git send-email --suppress-cc=sob --from="Example <nobody@example.com>" --to=nobody@example.com --smtp-server="$(pwd)/fake.sendmail" $patches 2>errors
  94'
  95
  96test_expect_success $PREREQ 'setup expect' '
  97        cat >expected <<-\EOF
  98        !nobody@example.com!
  99        !author@example.com!
 100        !one@example.com!
 101        !two@example.com!
 102        EOF
 103'
 104
 105test_expect_success $PREREQ 'Verify commandline' '
 106        test_cmp expected commandline1
 107'
 108
 109test_expect_success $PREREQ 'Send patches with --envelope-sender' '
 110        clean_fake_sendmail &&
 111        git send-email --envelope-sender="Patch Contributor <patch@example.com>" --suppress-cc=sob --from="Example <nobody@example.com>" --to=nobody@example.com --smtp-server="$(pwd)/fake.sendmail" $patches 2>errors
 112'
 113
 114test_expect_success $PREREQ 'setup expect' '
 115        cat >expected <<-\EOF
 116        !patch@example.com!
 117        !-i!
 118        !nobody@example.com!
 119        !author@example.com!
 120        !one@example.com!
 121        !two@example.com!
 122        EOF
 123'
 124
 125test_expect_success $PREREQ 'Verify commandline' '
 126        test_cmp expected commandline1
 127'
 128
 129test_expect_success $PREREQ 'Send patches with --envelope-sender=auto' '
 130        clean_fake_sendmail &&
 131        git send-email --envelope-sender=auto --suppress-cc=sob --from="Example <nobody@example.com>" --to=nobody@example.com --smtp-server="$(pwd)/fake.sendmail" $patches 2>errors
 132'
 133
 134test_expect_success $PREREQ 'setup expect' '
 135        cat >expected <<-\EOF
 136        !nobody@example.com!
 137        !-i!
 138        !nobody@example.com!
 139        !author@example.com!
 140        !one@example.com!
 141        !two@example.com!
 142        EOF
 143'
 144
 145test_expect_success $PREREQ 'Verify commandline' '
 146        test_cmp expected commandline1
 147'
 148
 149test_expect_success $PREREQ 'setup expect for cc trailer' "
 150cat >expected-cc <<\EOF
 151!recipient@example.com!
 152!author@example.com!
 153!one@example.com!
 154!two@example.com!
 155!three@example.com!
 156!four@example.com!
 157!five@example.com!
 158!six@example.com!
 159EOF
 160"
 161
 162test_expect_success $PREREQ 'cc trailer with various syntax' '
 163        test_commit cc-trailer &&
 164        test_when_finished "git reset --hard HEAD^" &&
 165        git commit --amend -F - <<-EOF &&
 166        Test Cc: trailers.
 167
 168        Cc: one@example.com
 169        Cc: <two@example.com> # trailing comments are ignored
 170        Cc: <three@example.com>, <not.four@example.com> one address per line
 171        Cc: "Some # Body" <four@example.com> [ <also.a.comment> ]
 172        Cc: five@example.com # not.six@example.com
 173        Cc: six@example.com, not.seven@example.com
 174        EOF
 175        clean_fake_sendmail &&
 176        git send-email -1 --to=recipient@example.com \
 177                --smtp-server="$(pwd)/fake.sendmail" &&
 178        test_cmp expected-cc commandline1
 179'
 180
 181test_expect_success $PREREQ 'setup fake get_maintainer.pl script for cc trailer' "
 182        write_script expected-cc-script.sh <<-EOF
 183        echo 'One Person <one@example.com> (supporter:THIS (FOO/bar))'
 184        echo 'Two Person <two@example.com> (maintainer:THIS THING)'
 185        echo 'Third List <three@example.com> (moderated list:THIS THING (FOO/bar))'
 186        echo '<four@example.com> (moderated list:FOR THING)'
 187        echo 'five@example.com (open list:FOR THING (FOO/bar))'
 188        echo 'six@example.com (open list)'
 189        EOF
 190"
 191
 192test_expect_success $PREREQ 'cc trailer with get_maintainer.pl output' '
 193        clean_fake_sendmail &&
 194        git send-email -1 --to=recipient@example.com \
 195                --cc-cmd=./expected-cc-script.sh \
 196                --smtp-server="$(pwd)/fake.sendmail" &&
 197        test_cmp expected-cc commandline1
 198'
 199
 200test_expect_success $PREREQ 'setup expect' "
 201cat >expected-show-all-headers <<\EOF
 2020001-Second.patch
 203(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 204(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 205(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 206Dry-OK. Log says:
 207Server: relay.example.com
 208MAIL FROM:<from@example.com>
 209RCPT TO:<to@example.com>
 210RCPT TO:<cc@example.com>
 211RCPT TO:<author@example.com>
 212RCPT TO:<one@example.com>
 213RCPT TO:<two@example.com>
 214RCPT TO:<bcc@example.com>
 215From: Example <from@example.com>
 216To: to@example.com
 217Cc: cc@example.com,
 218        A <author@example.com>,
 219        One <one@example.com>,
 220        two@example.com
 221Subject: [PATCH 1/1] Second.
 222Date: DATE-STRING
 223Message-Id: MESSAGE-ID-STRING
 224X-Mailer: X-MAILER-STRING
 225In-Reply-To: <unique-message-id@example.com>
 226References: <unique-message-id@example.com>
 227Reply-To: Reply <reply@example.com>
 228MIME-Version: 1.0
 229Content-Transfer-Encoding: 8bit
 230
 231Result: OK
 232EOF
 233"
 234
 235test_suppress_self () {
 236        test_commit $3 &&
 237        test_when_finished "git reset --hard HEAD^" &&
 238
 239        write_script cccmd-sed <<-EOF &&
 240                sed -n -e s/^cccmd--//p "\$1"
 241        EOF
 242
 243        git commit --amend --author="$1 <$2>" -F - &&
 244        clean_fake_sendmail &&
 245        git format-patch --stdout -1 >"suppress-self-$3.patch" &&
 246
 247        git send-email --from="$1 <$2>" \
 248                --to=nobody@example.com \
 249                --cc-cmd=./cccmd-sed \
 250                --suppress-cc=self \
 251                --smtp-server="$(pwd)/fake.sendmail" \
 252                suppress-self-$3.patch &&
 253
 254        mv msgtxt1 msgtxt1-$3 &&
 255        sed -e '/^$/q' msgtxt1-$3 >"msghdr1-$3" &&
 256
 257        (grep '^Cc:' msghdr1-$3 >"actual-no-cc-$3";
 258         test_must_be_empty actual-no-cc-$3)
 259}
 260
 261test_suppress_self_unquoted () {
 262        test_suppress_self "$1" "$2" "unquoted-$3" <<-EOF
 263                test suppress-cc.self unquoted-$3 with name $1 email $2
 264
 265                unquoted-$3
 266
 267                cccmd--$1 <$2>
 268
 269                Cc: $1 <$2>
 270                Signed-off-by: $1 <$2>
 271        EOF
 272}
 273
 274test_suppress_self_quoted () {
 275        test_suppress_self "$1" "$2" "quoted-$3" <<-EOF
 276                test suppress-cc.self quoted-$3 with name $1 email $2
 277
 278                quoted-$3
 279
 280                cccmd--"$1" <$2>
 281
 282                Cc: $1 <$2>
 283                Cc: "$1" <$2>
 284                Signed-off-by: $1 <$2>
 285                Signed-off-by: "$1" <$2>
 286        EOF
 287}
 288
 289test_expect_success $PREREQ 'self name is suppressed' "
 290        test_suppress_self_unquoted 'A U Thor' 'author@example.com' \
 291                'self_name_suppressed'
 292"
 293
 294test_expect_success $PREREQ 'self name with dot is suppressed' "
 295        test_suppress_self_quoted 'A U. Thor' 'author@example.com' \
 296                'self_name_dot_suppressed'
 297"
 298
 299test_expect_success $PREREQ 'non-ascii self name is suppressed' "
 300        test_suppress_self_quoted 'Füñný Nâmé' 'odd_?=mail@example.com' \
 301                'non_ascii_self_suppressed'
 302"
 303
 304# This name is long enough to force format-patch to split it into multiple
 305# encoded-words, assuming it uses UTF-8 with the "Q" encoding.
 306test_expect_success $PREREQ 'long non-ascii self name is suppressed' "
 307        test_suppress_self_quoted 'Ƒüñníęř €. Nâṁé' 'odd_?=mail@example.com' \
 308                'long_non_ascii_self_suppressed'
 309"
 310
 311test_expect_success $PREREQ 'sanitized self name is suppressed' "
 312        test_suppress_self_unquoted '\"A U. Thor\"' 'author@example.com' \
 313                'self_name_sanitized_suppressed'
 314"
 315
 316test_expect_success $PREREQ 'Show all headers' '
 317        git send-email \
 318                --dry-run \
 319                --suppress-cc=sob \
 320                --from="Example <from@example.com>" \
 321                --reply-to="Reply <reply@example.com>" \
 322                --to=to@example.com \
 323                --cc=cc@example.com \
 324                --bcc=bcc@example.com \
 325                --in-reply-to="<unique-message-id@example.com>" \
 326                --smtp-server relay.example.com \
 327                $patches | replace_variable_fields \
 328                >actual-show-all-headers &&
 329        test_cmp expected-show-all-headers actual-show-all-headers
 330'
 331
 332test_expect_success $PREREQ 'Prompting works' '
 333        clean_fake_sendmail &&
 334        (echo "to@example.com" &&
 335         echo ""
 336        ) | GIT_SEND_EMAIL_NOTTY=1 git send-email \
 337                --smtp-server="$(pwd)/fake.sendmail" \
 338                $patches \
 339                2>errors &&
 340                grep "^From: A U Thor <author@example.com>\$" msgtxt1 &&
 341                grep "^To: to@example.com\$" msgtxt1
 342'
 343
 344test_expect_success $PREREQ,AUTOIDENT 'implicit ident is allowed' '
 345        clean_fake_sendmail &&
 346        (sane_unset GIT_AUTHOR_NAME &&
 347        sane_unset GIT_AUTHOR_EMAIL &&
 348        sane_unset GIT_COMMITTER_NAME &&
 349        sane_unset GIT_COMMITTER_EMAIL &&
 350        GIT_SEND_EMAIL_NOTTY=1 git send-email \
 351                --smtp-server="$(pwd)/fake.sendmail" \
 352                --to=to@example.com \
 353                $patches </dev/null 2>errors
 354        )
 355'
 356
 357test_expect_success $PREREQ,!AUTOIDENT 'broken implicit ident aborts send-email' '
 358        clean_fake_sendmail &&
 359        (sane_unset GIT_AUTHOR_NAME &&
 360        sane_unset GIT_AUTHOR_EMAIL &&
 361        sane_unset GIT_COMMITTER_NAME &&
 362        sane_unset GIT_COMMITTER_EMAIL &&
 363        GIT_SEND_EMAIL_NOTTY=1 && export GIT_SEND_EMAIL_NOTTY &&
 364        test_must_fail git send-email \
 365                --smtp-server="$(pwd)/fake.sendmail" \
 366                --to=to@example.com \
 367                $patches </dev/null 2>errors &&
 368        test_i18ngrep "tell me who you are" errors
 369        )
 370'
 371
 372test_expect_success $PREREQ 'setup tocmd and cccmd scripts' '
 373        write_script tocmd-sed <<-\EOF &&
 374        sed -n -e "s/^tocmd--//p" "$1"
 375        EOF
 376        write_script cccmd-sed <<-\EOF
 377        sed -n -e "s/^cccmd--//p" "$1"
 378        EOF
 379'
 380
 381test_expect_success $PREREQ 'tocmd works' '
 382        clean_fake_sendmail &&
 383        cp $patches tocmd.patch &&
 384        echo tocmd--tocmd@example.com >>tocmd.patch &&
 385        git send-email \
 386                --from="Example <nobody@example.com>" \
 387                --to-cmd=./tocmd-sed \
 388                --smtp-server="$(pwd)/fake.sendmail" \
 389                tocmd.patch \
 390                &&
 391        grep "^To: tocmd@example.com" msgtxt1
 392'
 393
 394test_expect_success $PREREQ 'cccmd works' '
 395        clean_fake_sendmail &&
 396        cp $patches cccmd.patch &&
 397        echo "cccmd--  cccmd@example.com" >>cccmd.patch &&
 398        git send-email \
 399                --from="Example <nobody@example.com>" \
 400                --to=nobody@example.com \
 401                --cc-cmd=./cccmd-sed \
 402                --smtp-server="$(pwd)/fake.sendmail" \
 403                cccmd.patch \
 404                &&
 405        grep "^ cccmd@example.com" msgtxt1
 406'
 407
 408test_expect_success $PREREQ 'reject long lines' '
 409        z8=zzzzzzzz &&
 410        z64=$z8$z8$z8$z8$z8$z8$z8$z8 &&
 411        z512=$z64$z64$z64$z64$z64$z64$z64$z64 &&
 412        clean_fake_sendmail &&
 413        cp $patches longline.patch &&
 414        echo $z512$z512 >>longline.patch &&
 415        test_must_fail git send-email \
 416                --from="Example <nobody@example.com>" \
 417                --to=nobody@example.com \
 418                --smtp-server="$(pwd)/fake.sendmail" \
 419                --transfer-encoding=8bit \
 420                $patches longline.patch \
 421                2>errors &&
 422        grep longline.patch errors
 423'
 424
 425test_expect_success $PREREQ 'no patch was sent' '
 426        ! test -e commandline1
 427'
 428
 429test_expect_success $PREREQ 'Author From: in message body' '
 430        clean_fake_sendmail &&
 431        git send-email \
 432                --from="Example <nobody@example.com>" \
 433                --to=nobody@example.com \
 434                --smtp-server="$(pwd)/fake.sendmail" \
 435                $patches &&
 436        sed "1,/^\$/d" <msgtxt1 >msgbody1 &&
 437        grep "From: A <author@example.com>" msgbody1
 438'
 439
 440test_expect_success $PREREQ 'Author From: not in message body' '
 441        clean_fake_sendmail &&
 442        git send-email \
 443                --from="A <author@example.com>" \
 444                --to=nobody@example.com \
 445                --smtp-server="$(pwd)/fake.sendmail" \
 446                $patches &&
 447        sed "1,/^\$/d" <msgtxt1 >msgbody1 &&
 448        ! grep "From: A <author@example.com>" msgbody1
 449'
 450
 451test_expect_success $PREREQ 'allow long lines with --no-validate' '
 452        git send-email \
 453                --from="Example <nobody@example.com>" \
 454                --to=nobody@example.com \
 455                --smtp-server="$(pwd)/fake.sendmail" \
 456                --no-validate \
 457                $patches longline.patch \
 458                2>errors
 459'
 460
 461test_expect_success $PREREQ 'short lines with auto encoding are 8bit' '
 462        clean_fake_sendmail &&
 463        git send-email \
 464                --from="A <author@example.com>" \
 465                --to=nobody@example.com \
 466                --smtp-server="$(pwd)/fake.sendmail" \
 467                --transfer-encoding=auto \
 468                $patches &&
 469        grep "Content-Transfer-Encoding: 8bit" msgtxt1
 470'
 471
 472test_expect_success $PREREQ 'long lines with auto encoding are quoted-printable' '
 473        clean_fake_sendmail &&
 474        git send-email \
 475                --from="Example <nobody@example.com>" \
 476                --to=nobody@example.com \
 477                --smtp-server="$(pwd)/fake.sendmail" \
 478                --transfer-encoding=auto \
 479                --no-validate \
 480                longline.patch &&
 481        grep "Content-Transfer-Encoding: quoted-printable" msgtxt1
 482'
 483
 484for enc in auto quoted-printable base64
 485do
 486        test_expect_success $PREREQ "--validate passes with encoding $enc" '
 487                git send-email \
 488                        --from="Example <nobody@example.com>" \
 489                        --to=nobody@example.com \
 490                        --smtp-server="$(pwd)/fake.sendmail" \
 491                        --transfer-encoding=$enc \
 492                        --validate \
 493                        $patches longline.patch
 494        '
 495done
 496
 497test_expect_success $PREREQ 'Invalid In-Reply-To' '
 498        clean_fake_sendmail &&
 499        git send-email \
 500                --from="Example <nobody@example.com>" \
 501                --to=nobody@example.com \
 502                --in-reply-to=" " \
 503                --smtp-server="$(pwd)/fake.sendmail" \
 504                $patches \
 505                2>errors &&
 506        ! grep "^In-Reply-To: < *>" msgtxt1
 507'
 508
 509test_expect_success $PREREQ 'Valid In-Reply-To when prompting' '
 510        clean_fake_sendmail &&
 511        (echo "From Example <from@example.com>" &&
 512         echo "To Example <to@example.com>" &&
 513         echo ""
 514        ) | GIT_SEND_EMAIL_NOTTY=1 git send-email \
 515                --smtp-server="$(pwd)/fake.sendmail" \
 516                $patches 2>errors &&
 517        ! grep "^In-Reply-To: < *>" msgtxt1
 518'
 519
 520test_expect_success $PREREQ 'In-Reply-To without --chain-reply-to' '
 521        clean_fake_sendmail &&
 522        echo "<unique-message-id@example.com>" >expect &&
 523        git send-email \
 524                --from="Example <nobody@example.com>" \
 525                --to=nobody@example.com \
 526                --no-chain-reply-to \
 527                --in-reply-to="$(cat expect)" \
 528                --smtp-server="$(pwd)/fake.sendmail" \
 529                $patches $patches $patches \
 530                2>errors &&
 531        # The first message is a reply to --in-reply-to
 532        sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt1 >actual &&
 533        test_cmp expect actual &&
 534        # Second and subsequent messages are replies to the first one
 535        sed -n -e "s/^Message-Id: *\(.*\)/\1/p" msgtxt1 >expect &&
 536        sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt2 >actual &&
 537        test_cmp expect actual &&
 538        sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt3 >actual &&
 539        test_cmp expect actual
 540'
 541
 542test_expect_success $PREREQ 'In-Reply-To with --chain-reply-to' '
 543        clean_fake_sendmail &&
 544        echo "<unique-message-id@example.com>" >expect &&
 545        git send-email \
 546                --from="Example <nobody@example.com>" \
 547                --to=nobody@example.com \
 548                --chain-reply-to \
 549                --in-reply-to="$(cat expect)" \
 550                --smtp-server="$(pwd)/fake.sendmail" \
 551                $patches $patches $patches \
 552                2>errors &&
 553        sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt1 >actual &&
 554        test_cmp expect actual &&
 555        sed -n -e "s/^Message-Id: *\(.*\)/\1/p" msgtxt1 >expect &&
 556        sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt2 >actual &&
 557        test_cmp expect actual &&
 558        sed -n -e "s/^Message-Id: *\(.*\)/\1/p" msgtxt2 >expect &&
 559        sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt3 >actual &&
 560        test_cmp expect actual
 561'
 562
 563test_expect_success $PREREQ 'setup fake editor' '
 564        write_script fake-editor <<-\EOF
 565        echo fake edit >>"$1"
 566        EOF
 567'
 568
 569test_set_editor "$(pwd)/fake-editor"
 570
 571test_expect_success $PREREQ '--compose works' '
 572        clean_fake_sendmail &&
 573        git send-email \
 574        --compose --subject foo \
 575        --from="Example <nobody@example.com>" \
 576        --to=nobody@example.com \
 577        --smtp-server="$(pwd)/fake.sendmail" \
 578        $patches \
 579        2>errors
 580'
 581
 582test_expect_success $PREREQ 'first message is compose text' '
 583        grep "^fake edit" msgtxt1
 584'
 585
 586test_expect_success $PREREQ 'second message is patch' '
 587        grep "Subject:.*Second" msgtxt2
 588'
 589
 590test_expect_success $PREREQ 'setup expect' "
 591cat >expected-suppress-sob <<\EOF
 5920001-Second.patch
 593(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 594(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 595(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 596Dry-OK. Log says:
 597Server: relay.example.com
 598MAIL FROM:<from@example.com>
 599RCPT TO:<to@example.com>
 600RCPT TO:<cc@example.com>
 601RCPT TO:<author@example.com>
 602RCPT TO:<one@example.com>
 603RCPT TO:<two@example.com>
 604From: Example <from@example.com>
 605To: to@example.com
 606Cc: cc@example.com,
 607        A <author@example.com>,
 608        One <one@example.com>,
 609        two@example.com
 610Subject: [PATCH 1/1] Second.
 611Date: DATE-STRING
 612Message-Id: MESSAGE-ID-STRING
 613X-Mailer: X-MAILER-STRING
 614MIME-Version: 1.0
 615Content-Transfer-Encoding: 8bit
 616
 617Result: OK
 618EOF
 619"
 620
 621test_suppression () {
 622        git send-email \
 623                --dry-run \
 624                --suppress-cc=$1 ${2+"--suppress-cc=$2"} \
 625                --from="Example <from@example.com>" \
 626                --to=to@example.com \
 627                --smtp-server relay.example.com \
 628                $patches | replace_variable_fields \
 629                >actual-suppress-$1${2+"-$2"} &&
 630        test_cmp expected-suppress-$1${2+"-$2"} actual-suppress-$1${2+"-$2"}
 631}
 632
 633test_expect_success $PREREQ 'sendemail.cc set' '
 634        git config sendemail.cc cc@example.com &&
 635        test_suppression sob
 636'
 637
 638test_expect_success $PREREQ 'setup expect' "
 639cat >expected-suppress-sob <<\EOF
 6400001-Second.patch
 641(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 642(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 643(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 644Dry-OK. Log says:
 645Server: relay.example.com
 646MAIL FROM:<from@example.com>
 647RCPT TO:<to@example.com>
 648RCPT TO:<author@example.com>
 649RCPT TO:<one@example.com>
 650RCPT TO:<two@example.com>
 651From: Example <from@example.com>
 652To: to@example.com
 653Cc: A <author@example.com>,
 654        One <one@example.com>,
 655        two@example.com
 656Subject: [PATCH 1/1] Second.
 657Date: DATE-STRING
 658Message-Id: MESSAGE-ID-STRING
 659X-Mailer: X-MAILER-STRING
 660MIME-Version: 1.0
 661Content-Transfer-Encoding: 8bit
 662
 663Result: OK
 664EOF
 665"
 666
 667test_expect_success $PREREQ 'sendemail.cc unset' '
 668        git config --unset sendemail.cc &&
 669        test_suppression sob
 670'
 671
 672test_expect_success $PREREQ 'setup expect' "
 673cat >expected-suppress-cccmd <<\EOF
 6740001-Second.patch
 675(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 676(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 677(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 678(body) Adding cc: C O Mitter <committer@example.com> from line 'Signed-off-by: C O Mitter <committer@example.com>'
 679Dry-OK. Log says:
 680Server: relay.example.com
 681MAIL FROM:<from@example.com>
 682RCPT TO:<to@example.com>
 683RCPT TO:<author@example.com>
 684RCPT TO:<one@example.com>
 685RCPT TO:<two@example.com>
 686RCPT TO:<committer@example.com>
 687From: Example <from@example.com>
 688To: to@example.com
 689Cc: A <author@example.com>,
 690        One <one@example.com>,
 691        two@example.com,
 692        C O Mitter <committer@example.com>
 693Subject: [PATCH 1/1] Second.
 694Date: DATE-STRING
 695Message-Id: MESSAGE-ID-STRING
 696X-Mailer: X-MAILER-STRING
 697MIME-Version: 1.0
 698Content-Transfer-Encoding: 8bit
 699
 700Result: OK
 701EOF
 702"
 703
 704test_expect_success $PREREQ 'sendemail.cccmd' '
 705        write_script cccmd <<-\EOF &&
 706        echo cc-cmd@example.com
 707        EOF
 708        git config sendemail.cccmd ./cccmd &&
 709        test_suppression cccmd
 710'
 711
 712test_expect_success $PREREQ 'setup expect' '
 713cat >expected-suppress-all <<\EOF
 7140001-Second.patch
 715Dry-OK. Log says:
 716Server: relay.example.com
 717MAIL FROM:<from@example.com>
 718RCPT TO:<to@example.com>
 719From: Example <from@example.com>
 720To: to@example.com
 721Subject: [PATCH 1/1] Second.
 722Date: DATE-STRING
 723Message-Id: MESSAGE-ID-STRING
 724X-Mailer: X-MAILER-STRING
 725MIME-Version: 1.0
 726Content-Transfer-Encoding: 8bit
 727
 728Result: OK
 729EOF
 730'
 731
 732test_expect_success $PREREQ '--suppress-cc=all' '
 733        test_suppression all
 734'
 735
 736test_expect_success $PREREQ 'setup expect' "
 737cat >expected-suppress-body <<\EOF
 7380001-Second.patch
 739(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 740(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 741(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 742(cc-cmd) Adding cc: cc-cmd@example.com from: './cccmd'
 743Dry-OK. Log says:
 744Server: relay.example.com
 745MAIL FROM:<from@example.com>
 746RCPT TO:<to@example.com>
 747RCPT TO:<author@example.com>
 748RCPT TO:<one@example.com>
 749RCPT TO:<two@example.com>
 750RCPT TO:<cc-cmd@example.com>
 751From: Example <from@example.com>
 752To: to@example.com
 753Cc: A <author@example.com>,
 754        One <one@example.com>,
 755        two@example.com,
 756        cc-cmd@example.com
 757Subject: [PATCH 1/1] Second.
 758Date: DATE-STRING
 759Message-Id: MESSAGE-ID-STRING
 760X-Mailer: X-MAILER-STRING
 761MIME-Version: 1.0
 762Content-Transfer-Encoding: 8bit
 763
 764Result: OK
 765EOF
 766"
 767
 768test_expect_success $PREREQ '--suppress-cc=body' '
 769        test_suppression body
 770'
 771
 772test_expect_success $PREREQ 'setup expect' "
 773cat >expected-suppress-body-cccmd <<\EOF
 7740001-Second.patch
 775(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 776(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 777(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 778Dry-OK. Log says:
 779Server: relay.example.com
 780MAIL FROM:<from@example.com>
 781RCPT TO:<to@example.com>
 782RCPT TO:<author@example.com>
 783RCPT TO:<one@example.com>
 784RCPT TO:<two@example.com>
 785From: Example <from@example.com>
 786To: to@example.com
 787Cc: A <author@example.com>,
 788        One <one@example.com>,
 789        two@example.com
 790Subject: [PATCH 1/1] Second.
 791Date: DATE-STRING
 792Message-Id: MESSAGE-ID-STRING
 793X-Mailer: X-MAILER-STRING
 794MIME-Version: 1.0
 795Content-Transfer-Encoding: 8bit
 796
 797Result: OK
 798EOF
 799"
 800
 801test_expect_success $PREREQ '--suppress-cc=body --suppress-cc=cccmd' '
 802        test_suppression body cccmd
 803'
 804
 805test_expect_success $PREREQ 'setup expect' "
 806cat >expected-suppress-sob <<\EOF
 8070001-Second.patch
 808(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 809(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 810(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 811Dry-OK. Log says:
 812Server: relay.example.com
 813MAIL FROM:<from@example.com>
 814RCPT TO:<to@example.com>
 815RCPT TO:<author@example.com>
 816RCPT TO:<one@example.com>
 817RCPT TO:<two@example.com>
 818From: Example <from@example.com>
 819To: to@example.com
 820Cc: A <author@example.com>,
 821        One <one@example.com>,
 822        two@example.com
 823Subject: [PATCH 1/1] Second.
 824Date: DATE-STRING
 825Message-Id: MESSAGE-ID-STRING
 826X-Mailer: X-MAILER-STRING
 827MIME-Version: 1.0
 828Content-Transfer-Encoding: 8bit
 829
 830Result: OK
 831EOF
 832"
 833
 834test_expect_success $PREREQ '--suppress-cc=sob' '
 835        test_might_fail git config --unset sendemail.cccmd &&
 836        test_suppression sob
 837'
 838
 839test_expect_success $PREREQ 'setup expect' "
 840cat >expected-suppress-bodycc <<\EOF
 8410001-Second.patch
 842(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 843(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 844(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 845(body) Adding cc: C O Mitter <committer@example.com> from line 'Signed-off-by: C O Mitter <committer@example.com>'
 846Dry-OK. Log says:
 847Server: relay.example.com
 848MAIL FROM:<from@example.com>
 849RCPT TO:<to@example.com>
 850RCPT TO:<author@example.com>
 851RCPT TO:<one@example.com>
 852RCPT TO:<two@example.com>
 853RCPT TO:<committer@example.com>
 854From: Example <from@example.com>
 855To: to@example.com
 856Cc: A <author@example.com>,
 857        One <one@example.com>,
 858        two@example.com,
 859        C O Mitter <committer@example.com>
 860Subject: [PATCH 1/1] Second.
 861Date: DATE-STRING
 862Message-Id: MESSAGE-ID-STRING
 863X-Mailer: X-MAILER-STRING
 864MIME-Version: 1.0
 865Content-Transfer-Encoding: 8bit
 866
 867Result: OK
 868EOF
 869"
 870
 871test_expect_success $PREREQ '--suppress-cc=bodycc' '
 872        test_suppression bodycc
 873'
 874
 875test_expect_success $PREREQ 'setup expect' "
 876cat >expected-suppress-cc <<\EOF
 8770001-Second.patch
 878(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 879(body) Adding cc: C O Mitter <committer@example.com> from line 'Signed-off-by: C O Mitter <committer@example.com>'
 880Dry-OK. Log says:
 881Server: relay.example.com
 882MAIL FROM:<from@example.com>
 883RCPT TO:<to@example.com>
 884RCPT TO:<author@example.com>
 885RCPT TO:<committer@example.com>
 886From: Example <from@example.com>
 887To: to@example.com
 888Cc: A <author@example.com>,
 889        C O Mitter <committer@example.com>
 890Subject: [PATCH 1/1] Second.
 891Date: DATE-STRING
 892Message-Id: MESSAGE-ID-STRING
 893X-Mailer: X-MAILER-STRING
 894MIME-Version: 1.0
 895Content-Transfer-Encoding: 8bit
 896
 897Result: OK
 898EOF
 899"
 900
 901test_expect_success $PREREQ '--suppress-cc=cc' '
 902        test_suppression cc
 903'
 904
 905test_confirm () {
 906        echo y | \
 907                GIT_SEND_EMAIL_NOTTY=1 \
 908                git send-email \
 909                --from="Example <nobody@example.com>" \
 910                --to=nobody@example.com \
 911                --smtp-server="$(pwd)/fake.sendmail" \
 912                $@ $patches >stdout &&
 913        grep "Send this email" stdout
 914}
 915
 916test_expect_success $PREREQ '--confirm=always' '
 917        test_confirm --confirm=always --suppress-cc=all
 918'
 919
 920test_expect_success $PREREQ '--confirm=auto' '
 921        test_confirm --confirm=auto
 922'
 923
 924test_expect_success $PREREQ '--confirm=cc' '
 925        test_confirm --confirm=cc
 926'
 927
 928test_expect_success $PREREQ '--confirm=compose' '
 929        test_confirm --confirm=compose --compose
 930'
 931
 932test_expect_success $PREREQ 'confirm by default (due to cc)' '
 933        test_when_finished git config sendemail.confirm never &&
 934        git config --unset sendemail.confirm &&
 935        test_confirm
 936'
 937
 938test_expect_success $PREREQ 'confirm by default (due to --compose)' '
 939        test_when_finished git config sendemail.confirm never &&
 940        git config --unset sendemail.confirm &&
 941        test_confirm --suppress-cc=all --compose
 942'
 943
 944test_expect_success $PREREQ 'confirm detects EOF (inform assumes y)' '
 945        test_when_finished git config sendemail.confirm never &&
 946        git config --unset sendemail.confirm &&
 947        rm -fr outdir &&
 948        git format-patch -2 -o outdir &&
 949        GIT_SEND_EMAIL_NOTTY=1 \
 950                git send-email \
 951                        --from="Example <nobody@example.com>" \
 952                        --to=nobody@example.com \
 953                        --smtp-server="$(pwd)/fake.sendmail" \
 954                        outdir/*.patch </dev/null
 955'
 956
 957test_expect_success $PREREQ 'confirm detects EOF (auto causes failure)' '
 958        test_when_finished git config sendemail.confirm never &&
 959        git config sendemail.confirm auto &&
 960        GIT_SEND_EMAIL_NOTTY=1 &&
 961        export GIT_SEND_EMAIL_NOTTY &&
 962                test_must_fail git send-email \
 963                        --from="Example <nobody@example.com>" \
 964                        --to=nobody@example.com \
 965                        --smtp-server="$(pwd)/fake.sendmail" \
 966                        $patches </dev/null
 967'
 968
 969test_expect_success $PREREQ 'confirm does not loop forever' '
 970        test_when_finished git config sendemail.confirm never &&
 971        git config sendemail.confirm auto &&
 972        GIT_SEND_EMAIL_NOTTY=1 &&
 973        export GIT_SEND_EMAIL_NOTTY &&
 974                yes "bogus" | test_must_fail git send-email \
 975                        --from="Example <nobody@example.com>" \
 976                        --to=nobody@example.com \
 977                        --smtp-server="$(pwd)/fake.sendmail" \
 978                        $patches
 979'
 980
 981test_expect_success $PREREQ 'utf8 Cc is rfc2047 encoded' '
 982        clean_fake_sendmail &&
 983        rm -fr outdir &&
 984        git format-patch -1 -o outdir --cc="àéìöú <utf8@example.com>" &&
 985        git send-email \
 986        --from="Example <nobody@example.com>" \
 987        --to=nobody@example.com \
 988        --smtp-server="$(pwd)/fake.sendmail" \
 989        outdir/*.patch &&
 990        grep "^ " msgtxt1 |
 991        grep "=?UTF-8?q?=C3=A0=C3=A9=C3=AC=C3=B6=C3=BA?= <utf8@example.com>"
 992'
 993
 994test_expect_success $PREREQ '--compose adds MIME for utf8 body' '
 995        clean_fake_sendmail &&
 996        write_script fake-editor-utf8 <<-\EOF &&
 997        echo "utf8 body: àéìöú" >>"$1"
 998        EOF
 999        GIT_EDITOR="\"$(pwd)/fake-editor-utf8\"" \
1000        git send-email \
1001                --compose --subject foo \
1002                --from="Example <nobody@example.com>" \
1003                --to=nobody@example.com \
1004                --smtp-server="$(pwd)/fake.sendmail" \
1005                $patches &&
1006        grep "^utf8 body" msgtxt1 &&
1007        grep "^Content-Type: text/plain; charset=UTF-8" msgtxt1
1008'
1009
1010test_expect_success $PREREQ '--compose respects user mime type' '
1011        clean_fake_sendmail &&
1012        write_script fake-editor-utf8-mime <<-\EOF &&
1013        cat >"$1" <<-\EOM
1014        MIME-Version: 1.0
1015        Content-Type: text/plain; charset=iso-8859-1
1016        Content-Transfer-Encoding: 8bit
1017        Subject: foo
1018
1019        utf8 body: àéìöú
1020        EOM
1021        EOF
1022        GIT_EDITOR="\"$(pwd)/fake-editor-utf8-mime\"" \
1023        git send-email \
1024                --compose --subject foo \
1025                --from="Example <nobody@example.com>" \
1026                --to=nobody@example.com \
1027                --smtp-server="$(pwd)/fake.sendmail" \
1028                $patches &&
1029        grep "^utf8 body" msgtxt1 &&
1030        grep "^Content-Type: text/plain; charset=iso-8859-1" msgtxt1 &&
1031        ! grep "^Content-Type: text/plain; charset=UTF-8" msgtxt1
1032'
1033
1034test_expect_success $PREREQ '--compose adds MIME for utf8 subject' '
1035        clean_fake_sendmail &&
1036        GIT_EDITOR="\"$(pwd)/fake-editor\"" \
1037        git send-email \
1038                --compose --subject utf8-sübjëct \
1039                --from="Example <nobody@example.com>" \
1040                --to=nobody@example.com \
1041                --smtp-server="$(pwd)/fake.sendmail" \
1042                $patches &&
1043        grep "^fake edit" msgtxt1 &&
1044        grep "^Subject: =?UTF-8?q?utf8-s=C3=BCbj=C3=ABct?=" msgtxt1
1045'
1046
1047test_expect_success $PREREQ 'utf8 author is correctly passed on' '
1048        clean_fake_sendmail &&
1049        test_commit weird_author &&
1050        test_when_finished "git reset --hard HEAD^" &&
1051        git commit --amend --author "Füñný Nâmé <odd_?=mail@example.com>" &&
1052        git format-patch --stdout -1 >funny_name.patch &&
1053        git send-email --from="Example <nobody@example.com>" \
1054                --to=nobody@example.com \
1055                --smtp-server="$(pwd)/fake.sendmail" \
1056                funny_name.patch &&
1057        grep "^From: Füñný Nâmé <odd_?=mail@example.com>" msgtxt1
1058'
1059
1060test_expect_success $PREREQ 'utf8 sender is not duplicated' '
1061        clean_fake_sendmail &&
1062        test_commit weird_sender &&
1063        test_when_finished "git reset --hard HEAD^" &&
1064        git commit --amend --author "Füñný Nâmé <odd_?=mail@example.com>" &&
1065        git format-patch --stdout -1 >funny_name.patch &&
1066        git send-email --from="Füñný Nâmé <odd_?=mail@example.com>" \
1067                --to=nobody@example.com \
1068                --smtp-server="$(pwd)/fake.sendmail" \
1069                funny_name.patch &&
1070        grep "^From: " msgtxt1 >msgfrom &&
1071        test_line_count = 1 msgfrom
1072'
1073
1074test_expect_success $PREREQ 'sendemail.composeencoding works' '
1075        clean_fake_sendmail &&
1076        git config sendemail.composeencoding iso-8859-1 &&
1077        write_script fake-editor-utf8 <<-\EOF &&
1078        echo "utf8 body: àéìöú" >>"$1"
1079        EOF
1080        GIT_EDITOR="\"$(pwd)/fake-editor-utf8\"" \
1081        git send-email \
1082                --compose --subject foo \
1083                --from="Example <nobody@example.com>" \
1084                --to=nobody@example.com \
1085                --smtp-server="$(pwd)/fake.sendmail" \
1086                $patches &&
1087        grep "^utf8 body" msgtxt1 &&
1088        grep "^Content-Type: text/plain; charset=iso-8859-1" msgtxt1
1089'
1090
1091test_expect_success $PREREQ '--compose-encoding works' '
1092        clean_fake_sendmail &&
1093        write_script fake-editor-utf8 <<-\EOF &&
1094        echo "utf8 body: àéìöú" >>"$1"
1095        EOF
1096        GIT_EDITOR="\"$(pwd)/fake-editor-utf8\"" \
1097        git send-email \
1098                --compose-encoding iso-8859-1 \
1099                --compose --subject foo \
1100                --from="Example <nobody@example.com>" \
1101                --to=nobody@example.com \
1102                --smtp-server="$(pwd)/fake.sendmail" \
1103                $patches &&
1104        grep "^utf8 body" msgtxt1 &&
1105        grep "^Content-Type: text/plain; charset=iso-8859-1" msgtxt1
1106'
1107
1108test_expect_success $PREREQ '--compose-encoding overrides sendemail.composeencoding' '
1109        clean_fake_sendmail &&
1110        git config sendemail.composeencoding iso-8859-1 &&
1111        write_script fake-editor-utf8 <<-\EOF &&
1112        echo "utf8 body: àéìöú" >>"$1"
1113        EOF
1114        GIT_EDITOR="\"$(pwd)/fake-editor-utf8\"" \
1115        git send-email \
1116                --compose-encoding iso-8859-2 \
1117                --compose --subject foo \
1118                --from="Example <nobody@example.com>" \
1119                --to=nobody@example.com \
1120                --smtp-server="$(pwd)/fake.sendmail" \
1121                $patches &&
1122        grep "^utf8 body" msgtxt1 &&
1123        grep "^Content-Type: text/plain; charset=iso-8859-2" msgtxt1
1124'
1125
1126test_expect_success $PREREQ '--compose-encoding adds correct MIME for subject' '
1127        clean_fake_sendmail &&
1128        GIT_EDITOR="\"$(pwd)/fake-editor\"" \
1129        git send-email \
1130                --compose-encoding iso-8859-2 \
1131                --compose --subject utf8-sübjëct \
1132                --from="Example <nobody@example.com>" \
1133                --to=nobody@example.com \
1134                --smtp-server="$(pwd)/fake.sendmail" \
1135                $patches &&
1136        grep "^fake edit" msgtxt1 &&
1137        grep "^Subject: =?iso-8859-2?q?utf8-s=C3=BCbj=C3=ABct?=" msgtxt1
1138'
1139
1140test_expect_success $PREREQ 'detects ambiguous reference/file conflict' '
1141        echo master >master &&
1142        git add master &&
1143        git commit -m"add master" &&
1144        test_must_fail git send-email --dry-run master 2>errors &&
1145        grep disambiguate errors
1146'
1147
1148test_expect_success $PREREQ 'feed two files' '
1149        rm -fr outdir &&
1150        git format-patch -2 -o outdir &&
1151        git send-email \
1152                --dry-run \
1153                --from="Example <nobody@example.com>" \
1154                --to=nobody@example.com \
1155                outdir/000?-*.patch 2>errors >out &&
1156        grep "^Subject: " out >subjects &&
1157        test "z$(sed -n -e 1p subjects)" = "zSubject: [PATCH 1/2] Second." &&
1158        test "z$(sed -n -e 2p subjects)" = "zSubject: [PATCH 2/2] add master"
1159'
1160
1161test_expect_success $PREREQ 'in-reply-to but no threading' '
1162        git send-email \
1163                --dry-run \
1164                --from="Example <nobody@example.com>" \
1165                --to=nobody@example.com \
1166                --in-reply-to="<in-reply-id@example.com>" \
1167                --no-thread \
1168                $patches |
1169        grep "In-Reply-To: <in-reply-id@example.com>"
1170'
1171
1172test_expect_success $PREREQ 'no in-reply-to and no threading' '
1173        git send-email \
1174                --dry-run \
1175                --from="Example <nobody@example.com>" \
1176                --to=nobody@example.com \
1177                --no-thread \
1178                $patches $patches >stdout &&
1179        ! grep "In-Reply-To: " stdout
1180'
1181
1182test_expect_success $PREREQ 'threading but no chain-reply-to' '
1183        git send-email \
1184                --dry-run \
1185                --from="Example <nobody@example.com>" \
1186                --to=nobody@example.com \
1187                --thread \
1188                --no-chain-reply-to \
1189                $patches $patches >stdout &&
1190        grep "In-Reply-To: " stdout
1191'
1192
1193test_expect_success $PREREQ 'sendemail.to works' '
1194        git config --replace-all sendemail.to "Somebody <somebody@ex.com>" &&
1195        git send-email \
1196                --dry-run \
1197                --from="Example <nobody@example.com>" \
1198                $patches $patches >stdout &&
1199        grep "To: Somebody <somebody@ex.com>" stdout
1200'
1201
1202test_expect_success $PREREQ '--no-to overrides sendemail.to' '
1203        git send-email \
1204                --dry-run \
1205                --from="Example <nobody@example.com>" \
1206                --no-to \
1207                --to=nobody@example.com \
1208                $patches $patches >stdout &&
1209        grep "To: nobody@example.com" stdout &&
1210        ! grep "To: Somebody <somebody@ex.com>" stdout
1211'
1212
1213test_expect_success $PREREQ 'sendemail.cc works' '
1214        git config --replace-all sendemail.cc "Somebody <somebody@ex.com>" &&
1215        git send-email \
1216                --dry-run \
1217                --from="Example <nobody@example.com>" \
1218                --to=nobody@example.com \
1219                $patches $patches >stdout &&
1220        grep "Cc: Somebody <somebody@ex.com>" stdout
1221'
1222
1223test_expect_success $PREREQ '--no-cc overrides sendemail.cc' '
1224        git send-email \
1225                --dry-run \
1226                --from="Example <nobody@example.com>" \
1227                --no-cc \
1228                --cc=bodies@example.com \
1229                --to=nobody@example.com \
1230                $patches $patches >stdout &&
1231        grep "Cc: bodies@example.com" stdout &&
1232        ! grep "Cc: Somebody <somebody@ex.com>" stdout
1233'
1234
1235test_expect_success $PREREQ 'sendemail.bcc works' '
1236        git config --replace-all sendemail.bcc "Other <other@ex.com>" &&
1237        git send-email \
1238                --dry-run \
1239                --from="Example <nobody@example.com>" \
1240                --to=nobody@example.com \
1241                --smtp-server relay.example.com \
1242                $patches $patches >stdout &&
1243        grep "RCPT TO:<other@ex.com>" stdout
1244'
1245
1246test_expect_success $PREREQ '--no-bcc overrides sendemail.bcc' '
1247        git send-email \
1248                --dry-run \
1249                --from="Example <nobody@example.com>" \
1250                --no-bcc \
1251                --bcc=bodies@example.com \
1252                --to=nobody@example.com \
1253                --smtp-server relay.example.com \
1254                $patches $patches >stdout &&
1255        grep "RCPT TO:<bodies@example.com>" stdout &&
1256        ! grep "RCPT TO:<other@ex.com>" stdout
1257'
1258
1259test_expect_success $PREREQ 'patches To headers are used by default' '
1260        patch=$(git format-patch -1 --to="bodies@example.com") &&
1261        test_when_finished "rm $patch" &&
1262        git send-email \
1263                --dry-run \
1264                --from="Example <nobody@example.com>" \
1265                --smtp-server relay.example.com \
1266                $patch >stdout &&
1267        grep "RCPT TO:<bodies@example.com>" stdout
1268'
1269
1270test_expect_success $PREREQ 'patches To headers are appended to' '
1271        patch=$(git format-patch -1 --to="bodies@example.com") &&
1272        test_when_finished "rm $patch" &&
1273        git send-email \
1274                --dry-run \
1275                --from="Example <nobody@example.com>" \
1276                --to=nobody@example.com \
1277                --smtp-server relay.example.com \
1278                $patch >stdout &&
1279        grep "RCPT TO:<bodies@example.com>" stdout &&
1280        grep "RCPT TO:<nobody@example.com>" stdout
1281'
1282
1283test_expect_success $PREREQ 'To headers from files reset each patch' '
1284        patch1=$(git format-patch -1 --to="bodies@example.com") &&
1285        patch2=$(git format-patch -1 --to="other@example.com" HEAD~) &&
1286        test_when_finished "rm $patch1 && rm $patch2" &&
1287        git send-email \
1288                --dry-run \
1289                --from="Example <nobody@example.com>" \
1290                --to="nobody@example.com" \
1291                --smtp-server relay.example.com \
1292                $patch1 $patch2 >stdout &&
1293        test $(grep -c "RCPT TO:<bodies@example.com>" stdout) = 1 &&
1294        test $(grep -c "RCPT TO:<nobody@example.com>" stdout) = 2 &&
1295        test $(grep -c "RCPT TO:<other@example.com>" stdout) = 1
1296'
1297
1298test_expect_success $PREREQ 'setup expect' '
1299cat >email-using-8bit <<\EOF
1300From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
1301Message-Id: <bogus-message-id@example.com>
1302From: author@example.com
1303Date: Sat, 12 Jun 2010 15:53:58 +0200
1304Subject: subject goes here
1305
1306Dieser deutsche Text enthält einen Umlaut!
1307EOF
1308'
1309
1310test_expect_success $PREREQ 'setup expect' '
1311        echo "Subject: subject goes here" >expected
1312'
1313
1314test_expect_success $PREREQ 'ASCII subject is not RFC2047 quoted' '
1315        clean_fake_sendmail &&
1316        echo bogus |
1317        git send-email --from=author@example.com --to=nobody@example.com \
1318                        --smtp-server="$(pwd)/fake.sendmail" \
1319                        --8bit-encoding=UTF-8 \
1320                        email-using-8bit >stdout &&
1321        grep "Subject" msgtxt1 >actual &&
1322        test_cmp expected actual
1323'
1324
1325test_expect_success $PREREQ 'setup expect' '
1326        cat >content-type-decl <<-\EOF
1327        MIME-Version: 1.0
1328        Content-Type: text/plain; charset=UTF-8
1329        Content-Transfer-Encoding: 8bit
1330        EOF
1331'
1332
1333test_expect_success $PREREQ 'asks about and fixes 8bit encodings' '
1334        clean_fake_sendmail &&
1335        echo |
1336        git send-email --from=author@example.com --to=nobody@example.com \
1337                        --smtp-server="$(pwd)/fake.sendmail" \
1338                        email-using-8bit >stdout &&
1339        grep "do not declare a Content-Transfer-Encoding" stdout &&
1340        grep email-using-8bit stdout &&
1341        grep "Which 8bit encoding" stdout &&
1342        egrep "Content|MIME" msgtxt1 >actual &&
1343        test_cmp content-type-decl actual
1344'
1345
1346test_expect_success $PREREQ 'sendemail.8bitEncoding works' '
1347        clean_fake_sendmail &&
1348        git config sendemail.assume8bitEncoding UTF-8 &&
1349        echo bogus |
1350        git send-email --from=author@example.com --to=nobody@example.com \
1351                        --smtp-server="$(pwd)/fake.sendmail" \
1352                        email-using-8bit >stdout &&
1353        egrep "Content|MIME" msgtxt1 >actual &&
1354        test_cmp content-type-decl actual
1355'
1356
1357test_expect_success $PREREQ '--8bit-encoding overrides sendemail.8bitEncoding' '
1358        clean_fake_sendmail &&
1359        git config sendemail.assume8bitEncoding "bogus too" &&
1360        echo bogus |
1361        git send-email --from=author@example.com --to=nobody@example.com \
1362                        --smtp-server="$(pwd)/fake.sendmail" \
1363                        --8bit-encoding=UTF-8 \
1364                        email-using-8bit >stdout &&
1365        egrep "Content|MIME" msgtxt1 >actual &&
1366        test_cmp content-type-decl actual
1367'
1368
1369test_expect_success $PREREQ 'setup expect' '
1370        cat >email-using-8bit <<-\EOF
1371        From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
1372        Message-Id: <bogus-message-id@example.com>
1373        From: author@example.com
1374        Date: Sat, 12 Jun 2010 15:53:58 +0200
1375        Subject: Dieser Betreff enthält auch einen Umlaut!
1376
1377        Nothing to see here.
1378        EOF
1379'
1380
1381test_expect_success $PREREQ 'setup expect' '
1382        cat >expected <<-\EOF
1383        Subject: =?UTF-8?q?Dieser=20Betreff=20enth=C3=A4lt=20auch=20einen=20Umlaut!?=
1384        EOF
1385'
1386
1387test_expect_success $PREREQ '--8bit-encoding also treats subject' '
1388        clean_fake_sendmail &&
1389        echo bogus |
1390        git send-email --from=author@example.com --to=nobody@example.com \
1391                        --smtp-server="$(pwd)/fake.sendmail" \
1392                        --8bit-encoding=UTF-8 \
1393                        email-using-8bit >stdout &&
1394        grep "Subject" msgtxt1 >actual &&
1395        test_cmp expected actual
1396'
1397
1398test_expect_success $PREREQ 'setup expect' '
1399        cat >email-using-8bit <<-\EOF
1400        From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
1401        Message-Id: <bogus-message-id@example.com>
1402        From: A U Thor <author@example.com>
1403        Date: Sat, 12 Jun 2010 15:53:58 +0200
1404        Content-Type: text/plain; charset=UTF-8
1405        Subject: Nothing to see here.
1406
1407        Dieser Betreff enthält auch einen Umlaut!
1408        EOF
1409'
1410
1411test_expect_success $PREREQ 'sendemail.transferencoding=7bit fails on 8bit data' '
1412        clean_fake_sendmail &&
1413        git config sendemail.transferEncoding 7bit &&
1414        test_must_fail git send-email \
1415                --transfer-encoding=7bit \
1416                --smtp-server="$(pwd)/fake.sendmail" \
1417                email-using-8bit \
1418                2>errors >out &&
1419        grep "cannot send message as 7bit" errors &&
1420        test -z "$(ls msgtxt*)"
1421'
1422
1423test_expect_success $PREREQ '--transfer-encoding overrides sendemail.transferEncoding' '
1424        clean_fake_sendmail &&
1425        git config sendemail.transferEncoding 8bit &&
1426        test_must_fail git send-email \
1427                --transfer-encoding=7bit \
1428                --smtp-server="$(pwd)/fake.sendmail" \
1429                email-using-8bit \
1430                2>errors >out &&
1431        grep "cannot send message as 7bit" errors &&
1432        test -z "$(ls msgtxt*)"
1433'
1434
1435test_expect_success $PREREQ 'sendemail.transferencoding=8bit' '
1436        clean_fake_sendmail &&
1437        git send-email \
1438                --transfer-encoding=8bit \
1439                --smtp-server="$(pwd)/fake.sendmail" \
1440                email-using-8bit \
1441                2>errors >out &&
1442        sed '1,/^$/d' msgtxt1 >actual &&
1443        sed '1,/^$/d' email-using-8bit >expected &&
1444        test_cmp expected actual
1445'
1446
1447test_expect_success $PREREQ 'setup expect' '
1448        cat >expected <<-\EOF
1449        Dieser Betreff enth=C3=A4lt auch einen Umlaut!
1450        EOF
1451'
1452
1453test_expect_success $PREREQ '8-bit and sendemail.transferencoding=quoted-printable' '
1454        clean_fake_sendmail &&
1455        git send-email \
1456                --transfer-encoding=quoted-printable \
1457                --smtp-server="$(pwd)/fake.sendmail" \
1458                email-using-8bit \
1459                2>errors >out &&
1460        sed '1,/^$/d' msgtxt1 >actual &&
1461        test_cmp expected actual
1462'
1463
1464test_expect_success $PREREQ 'setup expect' '
1465        cat >expected <<-\EOF
1466        RGllc2VyIEJldHJlZmYgZW50aMOkbHQgYXVjaCBlaW5lbiBVbWxhdXQhCg==
1467        EOF
1468'
1469
1470test_expect_success $PREREQ '8-bit and sendemail.transferencoding=base64' '
1471        clean_fake_sendmail &&
1472        git send-email \
1473                --transfer-encoding=base64 \
1474                --smtp-server="$(pwd)/fake.sendmail" \
1475                email-using-8bit \
1476                2>errors >out &&
1477        sed '1,/^$/d' msgtxt1 >actual &&
1478        test_cmp expected actual
1479'
1480
1481test_expect_success $PREREQ 'setup expect' '
1482        cat >email-using-qp <<-\EOF
1483        From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
1484        Message-Id: <bogus-message-id@example.com>
1485        From: A U Thor <author@example.com>
1486        Date: Sat, 12 Jun 2010 15:53:58 +0200
1487        MIME-Version: 1.0
1488        Content-Transfer-Encoding: quoted-printable
1489        Content-Type: text/plain; charset=UTF-8
1490        Subject: Nothing to see here.
1491
1492        Dieser Betreff enth=C3=A4lt auch einen Umlaut!
1493        EOF
1494'
1495
1496test_expect_success $PREREQ 'convert from quoted-printable to base64' '
1497        clean_fake_sendmail &&
1498        git send-email \
1499                --transfer-encoding=base64 \
1500                --smtp-server="$(pwd)/fake.sendmail" \
1501                email-using-qp \
1502                2>errors >out &&
1503        sed '1,/^$/d' msgtxt1 >actual &&
1504        test_cmp expected actual
1505'
1506
1507test_expect_success $PREREQ 'setup expect' "
1508tr -d '\\015' | tr '%' '\\015' >email-using-crlf <<EOF
1509From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
1510Message-Id: <bogus-message-id@example.com>
1511From: A U Thor <author@example.com>
1512Date: Sat, 12 Jun 2010 15:53:58 +0200
1513Content-Type: text/plain; charset=UTF-8
1514Subject: Nothing to see here.
1515
1516Look, I have a CRLF and an = sign!%
1517EOF
1518"
1519
1520test_expect_success $PREREQ 'setup expect' '
1521        cat >expected <<-\EOF
1522        Look, I have a CRLF and an =3D sign!=0D
1523        EOF
1524'
1525
1526test_expect_success $PREREQ 'CRLF and sendemail.transferencoding=quoted-printable' '
1527        clean_fake_sendmail &&
1528        git send-email \
1529                --transfer-encoding=quoted-printable \
1530                --smtp-server="$(pwd)/fake.sendmail" \
1531                email-using-crlf \
1532                2>errors >out &&
1533        sed '1,/^$/d' msgtxt1 >actual &&
1534        test_cmp expected actual
1535'
1536
1537test_expect_success $PREREQ 'setup expect' '
1538        cat >expected <<-\EOF
1539        TG9vaywgSSBoYXZlIGEgQ1JMRiBhbmQgYW4gPSBzaWduIQ0K
1540        EOF
1541'
1542
1543test_expect_success $PREREQ 'CRLF and sendemail.transferencoding=base64' '
1544        clean_fake_sendmail &&
1545        git send-email \
1546                --transfer-encoding=base64 \
1547                --smtp-server="$(pwd)/fake.sendmail" \
1548                email-using-crlf \
1549                2>errors >out &&
1550        sed '1,/^$/d' msgtxt1 >actual &&
1551        test_cmp expected actual
1552'
1553
1554
1555# Note that the patches in this test are deliberately out of order; we
1556# want to make sure it works even if the cover-letter is not in the
1557# first mail.
1558test_expect_success $PREREQ 'refusing to send cover letter template' '
1559        clean_fake_sendmail &&
1560        rm -fr outdir &&
1561        git format-patch --cover-letter -2 -o outdir &&
1562        test_must_fail git send-email \
1563                --from="Example <nobody@example.com>" \
1564                --to=nobody@example.com \
1565                --smtp-server="$(pwd)/fake.sendmail" \
1566                outdir/0002-*.patch \
1567                outdir/0000-*.patch \
1568                outdir/0001-*.patch \
1569                2>errors >out &&
1570        grep "SUBJECT HERE" errors &&
1571        test -z "$(ls msgtxt*)"
1572'
1573
1574test_expect_success $PREREQ '--force sends cover letter template anyway' '
1575        clean_fake_sendmail &&
1576        rm -fr outdir &&
1577        git format-patch --cover-letter -2 -o outdir &&
1578        git send-email \
1579                --force \
1580                --from="Example <nobody@example.com>" \
1581                --to=nobody@example.com \
1582                --smtp-server="$(pwd)/fake.sendmail" \
1583                outdir/0002-*.patch \
1584                outdir/0000-*.patch \
1585                outdir/0001-*.patch \
1586                2>errors >out &&
1587        ! grep "SUBJECT HERE" errors &&
1588        test -n "$(ls msgtxt*)"
1589'
1590
1591test_cover_addresses () {
1592        header="$1"
1593        shift
1594        clean_fake_sendmail &&
1595        rm -fr outdir &&
1596        git format-patch --cover-letter -2 -o outdir &&
1597        cover=$(echo outdir/0000-*.patch) &&
1598        mv $cover cover-to-edit.patch &&
1599        perl -pe "s/^From:/$header: extra\@address.com\nFrom:/" cover-to-edit.patch >"$cover" &&
1600        git send-email \
1601                --force \
1602                --from="Example <nobody@example.com>" \
1603                --no-to --no-cc \
1604                "$@" \
1605                --smtp-server="$(pwd)/fake.sendmail" \
1606                outdir/0000-*.patch \
1607                outdir/0001-*.patch \
1608                outdir/0002-*.patch \
1609                2>errors >out &&
1610        grep "^$header: extra@address.com" msgtxt1 >to1 &&
1611        grep "^$header: extra@address.com" msgtxt2 >to2 &&
1612        grep "^$header: extra@address.com" msgtxt3 >to3 &&
1613        test_line_count = 1 to1 &&
1614        test_line_count = 1 to2 &&
1615        test_line_count = 1 to3
1616}
1617
1618test_expect_success $PREREQ 'to-cover adds To to all mail' '
1619        test_cover_addresses "To" --to-cover
1620'
1621
1622test_expect_success $PREREQ 'cc-cover adds Cc to all mail' '
1623        test_cover_addresses "Cc" --cc-cover
1624'
1625
1626test_expect_success $PREREQ 'tocover adds To to all mail' '
1627        test_config sendemail.tocover true &&
1628        test_cover_addresses "To"
1629'
1630
1631test_expect_success $PREREQ 'cccover adds Cc to all mail' '
1632        test_config sendemail.cccover true &&
1633        test_cover_addresses "Cc"
1634'
1635
1636test_expect_success $PREREQ 'escaped quotes in sendemail.aliasfiletype=mutt' '
1637        clean_fake_sendmail &&
1638        echo "alias sbd \\\"Dot U. Sir\\\" <somebody@example.org>" >.mutt &&
1639        git config --replace-all sendemail.aliasesfile "$(pwd)/.mutt" &&
1640        git config sendemail.aliasfiletype mutt &&
1641        git send-email \
1642                --from="Example <nobody@example.com>" \
1643                --to=sbd \
1644                --smtp-server="$(pwd)/fake.sendmail" \
1645                outdir/0001-*.patch \
1646                2>errors >out &&
1647        grep "^!somebody@example\.org!$" commandline1 &&
1648        grep -F "To: \"Dot U. Sir\" <somebody@example.org>" out
1649'
1650
1651test_expect_success $PREREQ 'sendemail.aliasfiletype=mailrc' '
1652        clean_fake_sendmail &&
1653        echo "alias sbd  somebody@example.org" >.mailrc &&
1654        git config --replace-all sendemail.aliasesfile "$(pwd)/.mailrc" &&
1655        git config sendemail.aliasfiletype mailrc &&
1656        git send-email \
1657                --from="Example <nobody@example.com>" \
1658                --to=sbd \
1659                --smtp-server="$(pwd)/fake.sendmail" \
1660                outdir/0001-*.patch \
1661                2>errors >out &&
1662        grep "^!somebody@example\.org!$" commandline1
1663'
1664
1665test_expect_success $PREREQ 'sendemail.aliasfile=~/.mailrc' '
1666        clean_fake_sendmail &&
1667        echo "alias sbd  someone@example.org" >"$HOME/.mailrc" &&
1668        git config --replace-all sendemail.aliasesfile "~/.mailrc" &&
1669        git config sendemail.aliasfiletype mailrc &&
1670        git send-email \
1671                --from="Example <nobody@example.com>" \
1672                --to=sbd \
1673                --smtp-server="$(pwd)/fake.sendmail" \
1674                outdir/0001-*.patch \
1675                2>errors >out &&
1676        grep "^!someone@example\.org!$" commandline1
1677'
1678
1679test_dump_aliases () {
1680        msg="$1" && shift &&
1681        filetype="$1" && shift &&
1682        printf '%s\n' "$@" >expect &&
1683        cat >.tmp-email-aliases &&
1684
1685        test_expect_success $PREREQ "$msg" '
1686                clean_fake_sendmail && rm -fr outdir &&
1687                git config --replace-all sendemail.aliasesfile \
1688                        "$(pwd)/.tmp-email-aliases" &&
1689                git config sendemail.aliasfiletype "$filetype" &&
1690                git send-email --dump-aliases 2>errors >actual &&
1691                test_cmp expect actual
1692        '
1693}
1694
1695test_dump_aliases '--dump-aliases sendmail format' \
1696        'sendmail' \
1697        'abgroup' \
1698        'alice' \
1699        'bcgrp' \
1700        'bob' \
1701        'chloe' <<-\EOF
1702        alice: Alice W Land <awol@example.com>
1703        bob: Robert Bobbyton <bob@example.com>
1704        chloe: chloe@example.com
1705        abgroup: alice, bob
1706        bcgrp: bob, chloe, Other <o@example.com>
1707        EOF
1708
1709test_dump_aliases '--dump-aliases mutt format' \
1710        'mutt' \
1711        'alice' \
1712        'bob' \
1713        'chloe' \
1714        'donald' <<-\EOF
1715        alias alice Alice W Land <awol@example.com>
1716        alias donald Donald C Carlton <donc@example.com>
1717        alias bob Robert Bobbyton <bob@example.com>
1718        alias chloe chloe@example.com
1719        EOF
1720
1721test_dump_aliases '--dump-aliases mailrc format' \
1722        'mailrc' \
1723        'alice' \
1724        'bob' \
1725        'chloe' \
1726        'eve' <<-\EOF
1727        alias alice   Alice W Land <awol@example.com>
1728        alias eve     Eve <eve@example.com>
1729        alias bob     Robert Bobbyton <bob@example.com>
1730        alias chloe   chloe@example.com
1731        EOF
1732
1733test_dump_aliases '--dump-aliases pine format' \
1734        'pine' \
1735        'alice' \
1736        'bob' \
1737        'chloe' \
1738        'eve' <<-\EOF
1739        alice   Alice W Land    <awol@example.com>
1740        eve     Eve     <eve@example.com>
1741        bob     Robert  Bobbyton <bob@example.com>
1742        chloe           chloe@example.com
1743        EOF
1744
1745test_dump_aliases '--dump-aliases gnus format' \
1746        'gnus' \
1747        'alice' \
1748        'bob' \
1749        'chloe' \
1750        'eve' <<-\EOF
1751        (define-mail-alias "alice" "awol@example.com")
1752        (define-mail-alias "eve" "eve@example.com")
1753        (define-mail-alias "bob" "bob@example.com")
1754        (define-mail-alias "chloe" "chloe@example.com")
1755        EOF
1756
1757test_expect_success '--dump-aliases must be used alone' '
1758        test_must_fail git send-email --dump-aliases --to=janice@example.com -1 refs/heads/accounting
1759'
1760
1761test_sendmail_aliases () {
1762        msg="$1" && shift &&
1763        expect="$@" &&
1764        cat >.tmp-email-aliases &&
1765
1766        test_expect_success $PREREQ "$msg" '
1767                clean_fake_sendmail && rm -fr outdir &&
1768                git format-patch -1 -o outdir &&
1769                git config --replace-all sendemail.aliasesfile \
1770                        "$(pwd)/.tmp-email-aliases" &&
1771                git config sendemail.aliasfiletype sendmail &&
1772                git send-email \
1773                        --from="Example <nobody@example.com>" \
1774                        --to=alice --to=bcgrp \
1775                        --smtp-server="$(pwd)/fake.sendmail" \
1776                        outdir/0001-*.patch \
1777                        2>errors >out &&
1778                for i in $expect
1779                do
1780                        grep "^!$i!$" commandline1 || return 1
1781                done
1782        '
1783}
1784
1785test_sendmail_aliases 'sendemail.aliasfiletype=sendmail' \
1786        'awol@example\.com' \
1787        'bob@example\.com' \
1788        'chloe@example\.com' \
1789        'o@example\.com' <<-\EOF
1790        alice: Alice W Land <awol@example.com>
1791        bob: Robert Bobbyton <bob@example.com>
1792        # this is a comment
1793           # this is also a comment
1794        chloe: chloe@example.com
1795        abgroup: alice, bob
1796        bcgrp: bob, chloe, Other <o@example.com>
1797        EOF
1798
1799test_sendmail_aliases 'sendmail aliases line folding' \
1800        alice1 \
1801        bob1 bob2 \
1802        chuck1 chuck2 \
1803        darla1 darla2 darla3 \
1804        elton1 elton2 elton3 \
1805        fred1 fred2 \
1806        greg1 <<-\EOF
1807        alice: alice1
1808        bob: bob1,\
1809        bob2
1810        chuck: chuck1,
1811            chuck2
1812        darla: darla1,\
1813        darla2,
1814            darla3
1815        elton: elton1,
1816            elton2,\
1817        elton3
1818        fred: fred1,\
1819            fred2
1820        greg: greg1
1821        bcgrp: bob, chuck, darla, elton, fred, greg
1822        EOF
1823
1824test_sendmail_aliases 'sendmail aliases tolerate bogus line folding' \
1825        alice1 bob1 <<-\EOF
1826            alice: alice1
1827        bcgrp: bob1\
1828        EOF
1829
1830test_sendmail_aliases 'sendmail aliases empty' alice bcgrp <<-\EOF
1831        EOF
1832
1833test_expect_success $PREREQ 'alias support in To header' '
1834        clean_fake_sendmail &&
1835        echo "alias sbd  someone@example.org" >.mailrc &&
1836        test_config sendemail.aliasesfile ".mailrc" &&
1837        test_config sendemail.aliasfiletype mailrc &&
1838        git format-patch --stdout -1 --to=sbd >aliased.patch &&
1839        git send-email \
1840                --from="Example <nobody@example.com>" \
1841                --smtp-server="$(pwd)/fake.sendmail" \
1842                aliased.patch \
1843                2>errors >out &&
1844        grep "^!someone@example\.org!$" commandline1
1845'
1846
1847test_expect_success $PREREQ 'alias support in Cc header' '
1848        clean_fake_sendmail &&
1849        echo "alias sbd  someone@example.org" >.mailrc &&
1850        test_config sendemail.aliasesfile ".mailrc" &&
1851        test_config sendemail.aliasfiletype mailrc &&
1852        git format-patch --stdout -1 --cc=sbd >aliased.patch &&
1853        git send-email \
1854                --from="Example <nobody@example.com>" \
1855                --smtp-server="$(pwd)/fake.sendmail" \
1856                aliased.patch \
1857                2>errors >out &&
1858        grep "^!someone@example\.org!$" commandline1
1859'
1860
1861test_expect_success $PREREQ 'tocmd works with aliases' '
1862        clean_fake_sendmail &&
1863        echo "alias sbd  someone@example.org" >.mailrc &&
1864        test_config sendemail.aliasesfile ".mailrc" &&
1865        test_config sendemail.aliasfiletype mailrc &&
1866        git format-patch --stdout -1 >tocmd.patch &&
1867        echo tocmd--sbd >>tocmd.patch &&
1868        git send-email \
1869                --from="Example <nobody@example.com>" \
1870                --to-cmd=./tocmd-sed \
1871                --smtp-server="$(pwd)/fake.sendmail" \
1872                tocmd.patch \
1873                2>errors >out &&
1874        grep "^!someone@example\.org!$" commandline1
1875'
1876
1877test_expect_success $PREREQ 'cccmd works with aliases' '
1878        clean_fake_sendmail &&
1879        echo "alias sbd  someone@example.org" >.mailrc &&
1880        test_config sendemail.aliasesfile ".mailrc" &&
1881        test_config sendemail.aliasfiletype mailrc &&
1882        git format-patch --stdout -1 >cccmd.patch &&
1883        echo cccmd--sbd >>cccmd.patch &&
1884        git send-email \
1885                --from="Example <nobody@example.com>" \
1886                --cc-cmd=./cccmd-sed \
1887                --smtp-server="$(pwd)/fake.sendmail" \
1888                cccmd.patch \
1889                2>errors >out &&
1890        grep "^!someone@example\.org!$" commandline1
1891'
1892
1893do_xmailer_test () {
1894        expected=$1 params=$2 &&
1895        git format-patch -1 &&
1896        git send-email \
1897                --from="Example <nobody@example.com>" \
1898                --to=someone@example.com \
1899                --smtp-server="$(pwd)/fake.sendmail" \
1900                $params \
1901                0001-*.patch \
1902                2>errors >out &&
1903        { grep '^X-Mailer:' out || :; } >mailer &&
1904        test_line_count = $expected mailer
1905}
1906
1907test_expect_success $PREREQ '--[no-]xmailer without any configuration' '
1908        do_xmailer_test 1 "--xmailer" &&
1909        do_xmailer_test 0 "--no-xmailer"
1910'
1911
1912test_expect_success $PREREQ '--[no-]xmailer with sendemail.xmailer=true' '
1913        test_config sendemail.xmailer true &&
1914        do_xmailer_test 1 "" &&
1915        do_xmailer_test 0 "--no-xmailer" &&
1916        do_xmailer_test 1 "--xmailer"
1917'
1918
1919test_expect_success $PREREQ '--[no-]xmailer with sendemail.xmailer=false' '
1920        test_config sendemail.xmailer false &&
1921        do_xmailer_test 0 "" &&
1922        do_xmailer_test 0 "--no-xmailer" &&
1923        do_xmailer_test 1 "--xmailer"
1924'
1925
1926test_expect_success $PREREQ 'setup expected-list' '
1927        git send-email \
1928        --dry-run \
1929        --from="Example <from@example.com>" \
1930        --to="To 1 <to1@example.com>" \
1931        --to="to2@example.com" \
1932        --to="to3@example.com" \
1933        --cc="Cc 1 <cc1@example.com>" \
1934        --cc="Cc2 <cc2@example.com>" \
1935        --bcc="bcc1@example.com" \
1936        --bcc="bcc2@example.com" \
1937        0001-add-master.patch | replace_variable_fields \
1938        >expected-list
1939'
1940
1941test_expect_success $PREREQ 'use email list in --cc --to and --bcc' '
1942        git send-email \
1943        --dry-run \
1944        --from="Example <from@example.com>" \
1945        --to="To 1 <to1@example.com>, to2@example.com" \
1946        --to="to3@example.com" \
1947        --cc="Cc 1 <cc1@example.com>, Cc2 <cc2@example.com>" \
1948        --bcc="bcc1@example.com, bcc2@example.com" \
1949        0001-add-master.patch | replace_variable_fields \
1950        >actual-list &&
1951        test_cmp expected-list actual-list
1952'
1953
1954test_expect_success $PREREQ 'aliases work with email list' '
1955        echo "alias to2 to2@example.com" >.mutt &&
1956        echo "alias cc1 Cc 1 <cc1@example.com>" >>.mutt &&
1957        test_config sendemail.aliasesfile ".mutt" &&
1958        test_config sendemail.aliasfiletype mutt &&
1959        git send-email \
1960        --dry-run \
1961        --from="Example <from@example.com>" \
1962        --to="To 1 <to1@example.com>, to2, to3@example.com" \
1963        --cc="cc1, Cc2 <cc2@example.com>" \
1964        --bcc="bcc1@example.com, bcc2@example.com" \
1965        0001-add-master.patch | replace_variable_fields \
1966        >actual-list &&
1967        test_cmp expected-list actual-list
1968'
1969
1970test_expect_success $PREREQ 'leading and trailing whitespaces are removed' '
1971        echo "alias to2 to2@example.com" >.mutt &&
1972        echo "alias cc1 Cc 1 <cc1@example.com>" >>.mutt &&
1973        test_config sendemail.aliasesfile ".mutt" &&
1974        test_config sendemail.aliasfiletype mutt &&
1975        TO1=$(echo "QTo 1 <to1@example.com>" | q_to_tab) &&
1976        TO2=$(echo "QZto2" | qz_to_tab_space) &&
1977        CC1=$(echo "cc1" | append_cr) &&
1978        BCC1=$(echo "Q bcc1@example.com Q" | q_to_nul) &&
1979        git send-email \
1980        --dry-run \
1981        --from="        Example <from@example.com>" \
1982        --to="$TO1" \
1983        --to="$TO2" \
1984        --to="  to3@example.com   " \
1985        --cc="$CC1" \
1986        --cc="Cc2 <cc2@example.com>" \
1987        --bcc="$BCC1" \
1988        --bcc="bcc2@example.com" \
1989        0001-add-master.patch | replace_variable_fields \
1990        >actual-list &&
1991        test_cmp expected-list actual-list
1992'
1993
1994test_expect_success $PREREQ 'invoke hook' '
1995        mkdir -p .git/hooks &&
1996
1997        write_script .git/hooks/sendemail-validate <<-\EOF &&
1998        # test that we have the correct environment variable, pwd, and
1999        # argument
2000        case "$GIT_DIR" in
2001        *.git)
2002                true
2003                ;;
2004        *)
2005                false
2006                ;;
2007        esac &&
2008        test -f 0001-add-master.patch &&
2009        grep "add master" "$1"
2010        EOF
2011
2012        mkdir subdir &&
2013        (
2014                # Test that it works even if we are not at the root of the
2015                # working tree
2016                cd subdir &&
2017                git send-email \
2018                        --from="Example <nobody@example.com>" \
2019                        --to=nobody@example.com \
2020                        --smtp-server="$(pwd)/../fake.sendmail" \
2021                        ../0001-add-master.patch &&
2022
2023                # Verify error message when a patch is rejected by the hook
2024                sed -e "s/add master/x/" ../0001-add-master.patch >../another.patch &&
2025                test_must_fail git send-email \
2026                        --from="Example <nobody@example.com>" \
2027                        --to=nobody@example.com \
2028                        --smtp-server="$(pwd)/../fake.sendmail" \
2029                        ../another.patch 2>err &&
2030                test_i18ngrep "rejected by sendemail-validate hook" err
2031        )
2032'
2033
2034test_expect_success $PREREQ 'test that send-email works outside a repo' '
2035        nongit git send-email \
2036                --from="Example <nobody@example.com>" \
2037                --to=nobody@example.com \
2038                --smtp-server="$(pwd)/fake.sendmail" \
2039                "$(pwd)/0001-add-master.patch"
2040'
2041
2042test_done