t / t9001-send-email.shon commit Merge branch 'jk/autocrlf-overrides-eol-doc' (13e2630)
   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        '
 495
 496done
 497
 498for enc in 7bit 8bit quoted-printable base64
 499do
 500        test_expect_success $PREREQ "--transfer-encoding=$enc produces correct header" '
 501                clean_fake_sendmail &&
 502                git send-email \
 503                        --from="Example <nobody@example.com>" \
 504                        --to=nobody@example.com \
 505                        --smtp-server="$(pwd)/fake.sendmail" \
 506                        --transfer-encoding=$enc \
 507                        $patches &&
 508                grep "Content-Transfer-Encoding: $enc" msgtxt1
 509        '
 510done
 511
 512test_expect_success $PREREQ 'Invalid In-Reply-To' '
 513        clean_fake_sendmail &&
 514        git send-email \
 515                --from="Example <nobody@example.com>" \
 516                --to=nobody@example.com \
 517                --in-reply-to=" " \
 518                --smtp-server="$(pwd)/fake.sendmail" \
 519                $patches \
 520                2>errors &&
 521        ! grep "^In-Reply-To: < *>" msgtxt1
 522'
 523
 524test_expect_success $PREREQ 'Valid In-Reply-To when prompting' '
 525        clean_fake_sendmail &&
 526        (echo "From Example <from@example.com>" &&
 527         echo "To Example <to@example.com>" &&
 528         echo ""
 529        ) | GIT_SEND_EMAIL_NOTTY=1 git send-email \
 530                --smtp-server="$(pwd)/fake.sendmail" \
 531                $patches 2>errors &&
 532        ! grep "^In-Reply-To: < *>" msgtxt1
 533'
 534
 535test_expect_success $PREREQ 'In-Reply-To without --chain-reply-to' '
 536        clean_fake_sendmail &&
 537        echo "<unique-message-id@example.com>" >expect &&
 538        git send-email \
 539                --from="Example <nobody@example.com>" \
 540                --to=nobody@example.com \
 541                --no-chain-reply-to \
 542                --in-reply-to="$(cat expect)" \
 543                --smtp-server="$(pwd)/fake.sendmail" \
 544                $patches $patches $patches \
 545                2>errors &&
 546        # The first message is a reply to --in-reply-to
 547        sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt1 >actual &&
 548        test_cmp expect actual &&
 549        # Second and subsequent messages are replies to the first one
 550        sed -n -e "s/^Message-Id: *\(.*\)/\1/p" msgtxt1 >expect &&
 551        sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt2 >actual &&
 552        test_cmp expect actual &&
 553        sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt3 >actual &&
 554        test_cmp expect actual
 555'
 556
 557test_expect_success $PREREQ 'In-Reply-To with --chain-reply-to' '
 558        clean_fake_sendmail &&
 559        echo "<unique-message-id@example.com>" >expect &&
 560        git send-email \
 561                --from="Example <nobody@example.com>" \
 562                --to=nobody@example.com \
 563                --chain-reply-to \
 564                --in-reply-to="$(cat expect)" \
 565                --smtp-server="$(pwd)/fake.sendmail" \
 566                $patches $patches $patches \
 567                2>errors &&
 568        sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt1 >actual &&
 569        test_cmp expect actual &&
 570        sed -n -e "s/^Message-Id: *\(.*\)/\1/p" msgtxt1 >expect &&
 571        sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt2 >actual &&
 572        test_cmp expect actual &&
 573        sed -n -e "s/^Message-Id: *\(.*\)/\1/p" msgtxt2 >expect &&
 574        sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt3 >actual &&
 575        test_cmp expect actual
 576'
 577
 578test_expect_success $PREREQ 'setup fake editor' '
 579        write_script fake-editor <<-\EOF
 580        echo fake edit >>"$1"
 581        EOF
 582'
 583
 584test_set_editor "$(pwd)/fake-editor"
 585
 586test_expect_success $PREREQ '--compose works' '
 587        clean_fake_sendmail &&
 588        git send-email \
 589        --compose --subject foo \
 590        --from="Example <nobody@example.com>" \
 591        --to=nobody@example.com \
 592        --smtp-server="$(pwd)/fake.sendmail" \
 593        $patches \
 594        2>errors
 595'
 596
 597test_expect_success $PREREQ 'first message is compose text' '
 598        grep "^fake edit" msgtxt1
 599'
 600
 601test_expect_success $PREREQ 'second message is patch' '
 602        grep "Subject:.*Second" msgtxt2
 603'
 604
 605test_expect_success $PREREQ 'setup expect' "
 606cat >expected-suppress-sob <<\EOF
 6070001-Second.patch
 608(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 609(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 610(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 611Dry-OK. Log says:
 612Server: relay.example.com
 613MAIL FROM:<from@example.com>
 614RCPT TO:<to@example.com>
 615RCPT TO:<cc@example.com>
 616RCPT TO:<author@example.com>
 617RCPT TO:<one@example.com>
 618RCPT TO:<two@example.com>
 619From: Example <from@example.com>
 620To: to@example.com
 621Cc: cc@example.com,
 622        A <author@example.com>,
 623        One <one@example.com>,
 624        two@example.com
 625Subject: [PATCH 1/1] Second.
 626Date: DATE-STRING
 627Message-Id: MESSAGE-ID-STRING
 628X-Mailer: X-MAILER-STRING
 629MIME-Version: 1.0
 630Content-Transfer-Encoding: 8bit
 631
 632Result: OK
 633EOF
 634"
 635
 636test_suppression () {
 637        git send-email \
 638                --dry-run \
 639                --suppress-cc=$1 ${2+"--suppress-cc=$2"} \
 640                --from="Example <from@example.com>" \
 641                --to=to@example.com \
 642                --smtp-server relay.example.com \
 643                $patches | replace_variable_fields \
 644                >actual-suppress-$1${2+"-$2"} &&
 645        test_cmp expected-suppress-$1${2+"-$2"} actual-suppress-$1${2+"-$2"}
 646}
 647
 648test_expect_success $PREREQ 'sendemail.cc set' '
 649        git config sendemail.cc cc@example.com &&
 650        test_suppression sob
 651'
 652
 653test_expect_success $PREREQ 'setup expect' "
 654cat >expected-suppress-sob <<\EOF
 6550001-Second.patch
 656(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 657(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 658(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 659Dry-OK. Log says:
 660Server: relay.example.com
 661MAIL FROM:<from@example.com>
 662RCPT TO:<to@example.com>
 663RCPT TO:<author@example.com>
 664RCPT TO:<one@example.com>
 665RCPT TO:<two@example.com>
 666From: Example <from@example.com>
 667To: to@example.com
 668Cc: A <author@example.com>,
 669        One <one@example.com>,
 670        two@example.com
 671Subject: [PATCH 1/1] Second.
 672Date: DATE-STRING
 673Message-Id: MESSAGE-ID-STRING
 674X-Mailer: X-MAILER-STRING
 675MIME-Version: 1.0
 676Content-Transfer-Encoding: 8bit
 677
 678Result: OK
 679EOF
 680"
 681
 682test_expect_success $PREREQ 'sendemail.cc unset' '
 683        git config --unset sendemail.cc &&
 684        test_suppression sob
 685'
 686
 687test_expect_success $PREREQ 'setup expect' "
 688cat >expected-suppress-cccmd <<\EOF
 6890001-Second.patch
 690(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 691(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 692(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 693(body) Adding cc: C O Mitter <committer@example.com> from line 'Signed-off-by: C O Mitter <committer@example.com>'
 694Dry-OK. Log says:
 695Server: relay.example.com
 696MAIL FROM:<from@example.com>
 697RCPT TO:<to@example.com>
 698RCPT TO:<author@example.com>
 699RCPT TO:<one@example.com>
 700RCPT TO:<two@example.com>
 701RCPT TO:<committer@example.com>
 702From: Example <from@example.com>
 703To: to@example.com
 704Cc: A <author@example.com>,
 705        One <one@example.com>,
 706        two@example.com,
 707        C O Mitter <committer@example.com>
 708Subject: [PATCH 1/1] Second.
 709Date: DATE-STRING
 710Message-Id: MESSAGE-ID-STRING
 711X-Mailer: X-MAILER-STRING
 712MIME-Version: 1.0
 713Content-Transfer-Encoding: 8bit
 714
 715Result: OK
 716EOF
 717"
 718
 719test_expect_success $PREREQ 'sendemail.cccmd' '
 720        write_script cccmd <<-\EOF &&
 721        echo cc-cmd@example.com
 722        EOF
 723        git config sendemail.cccmd ./cccmd &&
 724        test_suppression cccmd
 725'
 726
 727test_expect_success $PREREQ 'setup expect' '
 728cat >expected-suppress-all <<\EOF
 7290001-Second.patch
 730Dry-OK. Log says:
 731Server: relay.example.com
 732MAIL FROM:<from@example.com>
 733RCPT TO:<to@example.com>
 734From: Example <from@example.com>
 735To: to@example.com
 736Subject: [PATCH 1/1] Second.
 737Date: DATE-STRING
 738Message-Id: MESSAGE-ID-STRING
 739X-Mailer: X-MAILER-STRING
 740MIME-Version: 1.0
 741Content-Transfer-Encoding: 8bit
 742
 743Result: OK
 744EOF
 745'
 746
 747test_expect_success $PREREQ '--suppress-cc=all' '
 748        test_suppression all
 749'
 750
 751test_expect_success $PREREQ 'setup expect' "
 752cat >expected-suppress-body <<\EOF
 7530001-Second.patch
 754(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 755(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 756(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 757(cc-cmd) Adding cc: cc-cmd@example.com from: './cccmd'
 758Dry-OK. Log says:
 759Server: relay.example.com
 760MAIL FROM:<from@example.com>
 761RCPT TO:<to@example.com>
 762RCPT TO:<author@example.com>
 763RCPT TO:<one@example.com>
 764RCPT TO:<two@example.com>
 765RCPT TO:<cc-cmd@example.com>
 766From: Example <from@example.com>
 767To: to@example.com
 768Cc: A <author@example.com>,
 769        One <one@example.com>,
 770        two@example.com,
 771        cc-cmd@example.com
 772Subject: [PATCH 1/1] Second.
 773Date: DATE-STRING
 774Message-Id: MESSAGE-ID-STRING
 775X-Mailer: X-MAILER-STRING
 776MIME-Version: 1.0
 777Content-Transfer-Encoding: 8bit
 778
 779Result: OK
 780EOF
 781"
 782
 783test_expect_success $PREREQ '--suppress-cc=body' '
 784        test_suppression body
 785'
 786
 787test_expect_success $PREREQ 'setup expect' "
 788cat >expected-suppress-body-cccmd <<\EOF
 7890001-Second.patch
 790(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 791(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 792(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 793Dry-OK. Log says:
 794Server: relay.example.com
 795MAIL FROM:<from@example.com>
 796RCPT TO:<to@example.com>
 797RCPT TO:<author@example.com>
 798RCPT TO:<one@example.com>
 799RCPT TO:<two@example.com>
 800From: Example <from@example.com>
 801To: to@example.com
 802Cc: A <author@example.com>,
 803        One <one@example.com>,
 804        two@example.com
 805Subject: [PATCH 1/1] Second.
 806Date: DATE-STRING
 807Message-Id: MESSAGE-ID-STRING
 808X-Mailer: X-MAILER-STRING
 809MIME-Version: 1.0
 810Content-Transfer-Encoding: 8bit
 811
 812Result: OK
 813EOF
 814"
 815
 816test_expect_success $PREREQ '--suppress-cc=body --suppress-cc=cccmd' '
 817        test_suppression body cccmd
 818'
 819
 820test_expect_success $PREREQ 'setup expect' "
 821cat >expected-suppress-sob <<\EOF
 8220001-Second.patch
 823(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 824(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 825(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 826Dry-OK. Log says:
 827Server: relay.example.com
 828MAIL FROM:<from@example.com>
 829RCPT TO:<to@example.com>
 830RCPT TO:<author@example.com>
 831RCPT TO:<one@example.com>
 832RCPT TO:<two@example.com>
 833From: Example <from@example.com>
 834To: to@example.com
 835Cc: A <author@example.com>,
 836        One <one@example.com>,
 837        two@example.com
 838Subject: [PATCH 1/1] Second.
 839Date: DATE-STRING
 840Message-Id: MESSAGE-ID-STRING
 841X-Mailer: X-MAILER-STRING
 842MIME-Version: 1.0
 843Content-Transfer-Encoding: 8bit
 844
 845Result: OK
 846EOF
 847"
 848
 849test_expect_success $PREREQ '--suppress-cc=sob' '
 850        test_might_fail git config --unset sendemail.cccmd &&
 851        test_suppression sob
 852'
 853
 854test_expect_success $PREREQ 'setup expect' "
 855cat >expected-suppress-bodycc <<\EOF
 8560001-Second.patch
 857(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 858(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 859(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 860(body) Adding cc: C O Mitter <committer@example.com> from line 'Signed-off-by: C O Mitter <committer@example.com>'
 861Dry-OK. Log says:
 862Server: relay.example.com
 863MAIL FROM:<from@example.com>
 864RCPT TO:<to@example.com>
 865RCPT TO:<author@example.com>
 866RCPT TO:<one@example.com>
 867RCPT TO:<two@example.com>
 868RCPT TO:<committer@example.com>
 869From: Example <from@example.com>
 870To: to@example.com
 871Cc: A <author@example.com>,
 872        One <one@example.com>,
 873        two@example.com,
 874        C O Mitter <committer@example.com>
 875Subject: [PATCH 1/1] Second.
 876Date: DATE-STRING
 877Message-Id: MESSAGE-ID-STRING
 878X-Mailer: X-MAILER-STRING
 879MIME-Version: 1.0
 880Content-Transfer-Encoding: 8bit
 881
 882Result: OK
 883EOF
 884"
 885
 886test_expect_success $PREREQ '--suppress-cc=bodycc' '
 887        test_suppression bodycc
 888'
 889
 890test_expect_success $PREREQ 'setup expect' "
 891cat >expected-suppress-cc <<\EOF
 8920001-Second.patch
 893(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 894(body) Adding cc: C O Mitter <committer@example.com> from line 'Signed-off-by: C O Mitter <committer@example.com>'
 895Dry-OK. Log says:
 896Server: relay.example.com
 897MAIL FROM:<from@example.com>
 898RCPT TO:<to@example.com>
 899RCPT TO:<author@example.com>
 900RCPT TO:<committer@example.com>
 901From: Example <from@example.com>
 902To: to@example.com
 903Cc: A <author@example.com>,
 904        C O Mitter <committer@example.com>
 905Subject: [PATCH 1/1] Second.
 906Date: DATE-STRING
 907Message-Id: MESSAGE-ID-STRING
 908X-Mailer: X-MAILER-STRING
 909MIME-Version: 1.0
 910Content-Transfer-Encoding: 8bit
 911
 912Result: OK
 913EOF
 914"
 915
 916test_expect_success $PREREQ '--suppress-cc=cc' '
 917        test_suppression cc
 918'
 919
 920test_confirm () {
 921        echo y | \
 922                GIT_SEND_EMAIL_NOTTY=1 \
 923                git send-email \
 924                --from="Example <nobody@example.com>" \
 925                --to=nobody@example.com \
 926                --smtp-server="$(pwd)/fake.sendmail" \
 927                $@ $patches >stdout &&
 928        grep "Send this email" stdout
 929}
 930
 931test_expect_success $PREREQ '--confirm=always' '
 932        test_confirm --confirm=always --suppress-cc=all
 933'
 934
 935test_expect_success $PREREQ '--confirm=auto' '
 936        test_confirm --confirm=auto
 937'
 938
 939test_expect_success $PREREQ '--confirm=cc' '
 940        test_confirm --confirm=cc
 941'
 942
 943test_expect_success $PREREQ '--confirm=compose' '
 944        test_confirm --confirm=compose --compose
 945'
 946
 947test_expect_success $PREREQ 'confirm by default (due to cc)' '
 948        test_when_finished git config sendemail.confirm never &&
 949        git config --unset sendemail.confirm &&
 950        test_confirm
 951'
 952
 953test_expect_success $PREREQ 'confirm by default (due to --compose)' '
 954        test_when_finished git config sendemail.confirm never &&
 955        git config --unset sendemail.confirm &&
 956        test_confirm --suppress-cc=all --compose
 957'
 958
 959test_expect_success $PREREQ 'confirm detects EOF (inform assumes y)' '
 960        test_when_finished git config sendemail.confirm never &&
 961        git config --unset sendemail.confirm &&
 962        rm -fr outdir &&
 963        git format-patch -2 -o outdir &&
 964        GIT_SEND_EMAIL_NOTTY=1 \
 965                git send-email \
 966                        --from="Example <nobody@example.com>" \
 967                        --to=nobody@example.com \
 968                        --smtp-server="$(pwd)/fake.sendmail" \
 969                        outdir/*.patch </dev/null
 970'
 971
 972test_expect_success $PREREQ 'confirm detects EOF (auto causes failure)' '
 973        test_when_finished git config sendemail.confirm never &&
 974        git config sendemail.confirm auto &&
 975        GIT_SEND_EMAIL_NOTTY=1 &&
 976        export GIT_SEND_EMAIL_NOTTY &&
 977                test_must_fail git send-email \
 978                        --from="Example <nobody@example.com>" \
 979                        --to=nobody@example.com \
 980                        --smtp-server="$(pwd)/fake.sendmail" \
 981                        $patches </dev/null
 982'
 983
 984test_expect_success $PREREQ 'confirm does not loop forever' '
 985        test_when_finished git config sendemail.confirm never &&
 986        git config sendemail.confirm auto &&
 987        GIT_SEND_EMAIL_NOTTY=1 &&
 988        export GIT_SEND_EMAIL_NOTTY &&
 989                yes "bogus" | test_must_fail git send-email \
 990                        --from="Example <nobody@example.com>" \
 991                        --to=nobody@example.com \
 992                        --smtp-server="$(pwd)/fake.sendmail" \
 993                        $patches
 994'
 995
 996test_expect_success $PREREQ 'utf8 Cc is rfc2047 encoded' '
 997        clean_fake_sendmail &&
 998        rm -fr outdir &&
 999        git format-patch -1 -o outdir --cc="àéìöú <utf8@example.com>" &&
1000        git send-email \
1001        --from="Example <nobody@example.com>" \
1002        --to=nobody@example.com \
1003        --smtp-server="$(pwd)/fake.sendmail" \
1004        outdir/*.patch &&
1005        grep "^ " msgtxt1 |
1006        grep "=?UTF-8?q?=C3=A0=C3=A9=C3=AC=C3=B6=C3=BA?= <utf8@example.com>"
1007'
1008
1009test_expect_success $PREREQ '--compose adds MIME for utf8 body' '
1010        clean_fake_sendmail &&
1011        write_script fake-editor-utf8 <<-\EOF &&
1012        echo "utf8 body: àéìöú" >>"$1"
1013        EOF
1014        GIT_EDITOR="\"$(pwd)/fake-editor-utf8\"" \
1015        git send-email \
1016                --compose --subject foo \
1017                --from="Example <nobody@example.com>" \
1018                --to=nobody@example.com \
1019                --smtp-server="$(pwd)/fake.sendmail" \
1020                $patches &&
1021        grep "^utf8 body" msgtxt1 &&
1022        grep "^Content-Type: text/plain; charset=UTF-8" msgtxt1
1023'
1024
1025test_expect_success $PREREQ '--compose respects user mime type' '
1026        clean_fake_sendmail &&
1027        write_script fake-editor-utf8-mime <<-\EOF &&
1028        cat >"$1" <<-\EOM
1029        MIME-Version: 1.0
1030        Content-Type: text/plain; charset=iso-8859-1
1031        Content-Transfer-Encoding: 8bit
1032        Subject: foo
1033
1034        utf8 body: àéìöú
1035        EOM
1036        EOF
1037        GIT_EDITOR="\"$(pwd)/fake-editor-utf8-mime\"" \
1038        git send-email \
1039                --compose --subject foo \
1040                --from="Example <nobody@example.com>" \
1041                --to=nobody@example.com \
1042                --smtp-server="$(pwd)/fake.sendmail" \
1043                $patches &&
1044        grep "^utf8 body" msgtxt1 &&
1045        grep "^Content-Type: text/plain; charset=iso-8859-1" msgtxt1 &&
1046        ! grep "^Content-Type: text/plain; charset=UTF-8" msgtxt1
1047'
1048
1049test_expect_success $PREREQ '--compose adds MIME for utf8 subject' '
1050        clean_fake_sendmail &&
1051        GIT_EDITOR="\"$(pwd)/fake-editor\"" \
1052        git send-email \
1053                --compose --subject utf8-sübjëct \
1054                --from="Example <nobody@example.com>" \
1055                --to=nobody@example.com \
1056                --smtp-server="$(pwd)/fake.sendmail" \
1057                $patches &&
1058        grep "^fake edit" msgtxt1 &&
1059        grep "^Subject: =?UTF-8?q?utf8-s=C3=BCbj=C3=ABct?=" msgtxt1
1060'
1061
1062test_expect_success $PREREQ 'utf8 author is correctly passed on' '
1063        clean_fake_sendmail &&
1064        test_commit weird_author &&
1065        test_when_finished "git reset --hard HEAD^" &&
1066        git commit --amend --author "Füñný Nâmé <odd_?=mail@example.com>" &&
1067        git format-patch --stdout -1 >funny_name.patch &&
1068        git send-email --from="Example <nobody@example.com>" \
1069                --to=nobody@example.com \
1070                --smtp-server="$(pwd)/fake.sendmail" \
1071                funny_name.patch &&
1072        grep "^From: Füñný Nâmé <odd_?=mail@example.com>" msgtxt1
1073'
1074
1075test_expect_success $PREREQ 'utf8 sender is not duplicated' '
1076        clean_fake_sendmail &&
1077        test_commit weird_sender &&
1078        test_when_finished "git reset --hard HEAD^" &&
1079        git commit --amend --author "Füñný Nâmé <odd_?=mail@example.com>" &&
1080        git format-patch --stdout -1 >funny_name.patch &&
1081        git send-email --from="Füñný Nâmé <odd_?=mail@example.com>" \
1082                --to=nobody@example.com \
1083                --smtp-server="$(pwd)/fake.sendmail" \
1084                funny_name.patch &&
1085        grep "^From: " msgtxt1 >msgfrom &&
1086        test_line_count = 1 msgfrom
1087'
1088
1089test_expect_success $PREREQ 'sendemail.composeencoding works' '
1090        clean_fake_sendmail &&
1091        git config sendemail.composeencoding iso-8859-1 &&
1092        write_script fake-editor-utf8 <<-\EOF &&
1093        echo "utf8 body: àéìöú" >>"$1"
1094        EOF
1095        GIT_EDITOR="\"$(pwd)/fake-editor-utf8\"" \
1096        git send-email \
1097                --compose --subject foo \
1098                --from="Example <nobody@example.com>" \
1099                --to=nobody@example.com \
1100                --smtp-server="$(pwd)/fake.sendmail" \
1101                $patches &&
1102        grep "^utf8 body" msgtxt1 &&
1103        grep "^Content-Type: text/plain; charset=iso-8859-1" msgtxt1
1104'
1105
1106test_expect_success $PREREQ '--compose-encoding works' '
1107        clean_fake_sendmail &&
1108        write_script fake-editor-utf8 <<-\EOF &&
1109        echo "utf8 body: àéìöú" >>"$1"
1110        EOF
1111        GIT_EDITOR="\"$(pwd)/fake-editor-utf8\"" \
1112        git send-email \
1113                --compose-encoding iso-8859-1 \
1114                --compose --subject foo \
1115                --from="Example <nobody@example.com>" \
1116                --to=nobody@example.com \
1117                --smtp-server="$(pwd)/fake.sendmail" \
1118                $patches &&
1119        grep "^utf8 body" msgtxt1 &&
1120        grep "^Content-Type: text/plain; charset=iso-8859-1" msgtxt1
1121'
1122
1123test_expect_success $PREREQ '--compose-encoding overrides sendemail.composeencoding' '
1124        clean_fake_sendmail &&
1125        git config sendemail.composeencoding iso-8859-1 &&
1126        write_script fake-editor-utf8 <<-\EOF &&
1127        echo "utf8 body: àéìöú" >>"$1"
1128        EOF
1129        GIT_EDITOR="\"$(pwd)/fake-editor-utf8\"" \
1130        git send-email \
1131                --compose-encoding iso-8859-2 \
1132                --compose --subject foo \
1133                --from="Example <nobody@example.com>" \
1134                --to=nobody@example.com \
1135                --smtp-server="$(pwd)/fake.sendmail" \
1136                $patches &&
1137        grep "^utf8 body" msgtxt1 &&
1138        grep "^Content-Type: text/plain; charset=iso-8859-2" msgtxt1
1139'
1140
1141test_expect_success $PREREQ '--compose-encoding adds correct MIME for subject' '
1142        clean_fake_sendmail &&
1143        GIT_EDITOR="\"$(pwd)/fake-editor\"" \
1144        git send-email \
1145                --compose-encoding iso-8859-2 \
1146                --compose --subject utf8-sübjëct \
1147                --from="Example <nobody@example.com>" \
1148                --to=nobody@example.com \
1149                --smtp-server="$(pwd)/fake.sendmail" \
1150                $patches &&
1151        grep "^fake edit" msgtxt1 &&
1152        grep "^Subject: =?iso-8859-2?q?utf8-s=C3=BCbj=C3=ABct?=" msgtxt1
1153'
1154
1155test_expect_success $PREREQ 'detects ambiguous reference/file conflict' '
1156        echo master >master &&
1157        git add master &&
1158        git commit -m"add master" &&
1159        test_must_fail git send-email --dry-run master 2>errors &&
1160        grep disambiguate errors
1161'
1162
1163test_expect_success $PREREQ 'feed two files' '
1164        rm -fr outdir &&
1165        git format-patch -2 -o outdir &&
1166        git send-email \
1167                --dry-run \
1168                --from="Example <nobody@example.com>" \
1169                --to=nobody@example.com \
1170                outdir/000?-*.patch 2>errors >out &&
1171        grep "^Subject: " out >subjects &&
1172        test "z$(sed -n -e 1p subjects)" = "zSubject: [PATCH 1/2] Second." &&
1173        test "z$(sed -n -e 2p subjects)" = "zSubject: [PATCH 2/2] add master"
1174'
1175
1176test_expect_success $PREREQ 'in-reply-to but no threading' '
1177        git send-email \
1178                --dry-run \
1179                --from="Example <nobody@example.com>" \
1180                --to=nobody@example.com \
1181                --in-reply-to="<in-reply-id@example.com>" \
1182                --no-thread \
1183                $patches |
1184        grep "In-Reply-To: <in-reply-id@example.com>"
1185'
1186
1187test_expect_success $PREREQ 'no in-reply-to and no threading' '
1188        git send-email \
1189                --dry-run \
1190                --from="Example <nobody@example.com>" \
1191                --to=nobody@example.com \
1192                --no-thread \
1193                $patches $patches >stdout &&
1194        ! grep "In-Reply-To: " stdout
1195'
1196
1197test_expect_success $PREREQ 'threading but no chain-reply-to' '
1198        git send-email \
1199                --dry-run \
1200                --from="Example <nobody@example.com>" \
1201                --to=nobody@example.com \
1202                --thread \
1203                --no-chain-reply-to \
1204                $patches $patches >stdout &&
1205        grep "In-Reply-To: " stdout
1206'
1207
1208test_expect_success $PREREQ 'sendemail.to works' '
1209        git config --replace-all sendemail.to "Somebody <somebody@ex.com>" &&
1210        git send-email \
1211                --dry-run \
1212                --from="Example <nobody@example.com>" \
1213                $patches $patches >stdout &&
1214        grep "To: Somebody <somebody@ex.com>" stdout
1215'
1216
1217test_expect_success $PREREQ '--no-to overrides sendemail.to' '
1218        git send-email \
1219                --dry-run \
1220                --from="Example <nobody@example.com>" \
1221                --no-to \
1222                --to=nobody@example.com \
1223                $patches $patches >stdout &&
1224        grep "To: nobody@example.com" stdout &&
1225        ! grep "To: Somebody <somebody@ex.com>" stdout
1226'
1227
1228test_expect_success $PREREQ 'sendemail.cc works' '
1229        git config --replace-all sendemail.cc "Somebody <somebody@ex.com>" &&
1230        git send-email \
1231                --dry-run \
1232                --from="Example <nobody@example.com>" \
1233                --to=nobody@example.com \
1234                $patches $patches >stdout &&
1235        grep "Cc: Somebody <somebody@ex.com>" stdout
1236'
1237
1238test_expect_success $PREREQ '--no-cc overrides sendemail.cc' '
1239        git send-email \
1240                --dry-run \
1241                --from="Example <nobody@example.com>" \
1242                --no-cc \
1243                --cc=bodies@example.com \
1244                --to=nobody@example.com \
1245                $patches $patches >stdout &&
1246        grep "Cc: bodies@example.com" stdout &&
1247        ! grep "Cc: Somebody <somebody@ex.com>" stdout
1248'
1249
1250test_expect_success $PREREQ 'sendemail.bcc works' '
1251        git config --replace-all sendemail.bcc "Other <other@ex.com>" &&
1252        git send-email \
1253                --dry-run \
1254                --from="Example <nobody@example.com>" \
1255                --to=nobody@example.com \
1256                --smtp-server relay.example.com \
1257                $patches $patches >stdout &&
1258        grep "RCPT TO:<other@ex.com>" stdout
1259'
1260
1261test_expect_success $PREREQ '--no-bcc overrides sendemail.bcc' '
1262        git send-email \
1263                --dry-run \
1264                --from="Example <nobody@example.com>" \
1265                --no-bcc \
1266                --bcc=bodies@example.com \
1267                --to=nobody@example.com \
1268                --smtp-server relay.example.com \
1269                $patches $patches >stdout &&
1270        grep "RCPT TO:<bodies@example.com>" stdout &&
1271        ! grep "RCPT TO:<other@ex.com>" stdout
1272'
1273
1274test_expect_success $PREREQ 'patches To headers are used by default' '
1275        patch=$(git format-patch -1 --to="bodies@example.com") &&
1276        test_when_finished "rm $patch" &&
1277        git send-email \
1278                --dry-run \
1279                --from="Example <nobody@example.com>" \
1280                --smtp-server relay.example.com \
1281                $patch >stdout &&
1282        grep "RCPT TO:<bodies@example.com>" stdout
1283'
1284
1285test_expect_success $PREREQ 'patches To headers are appended to' '
1286        patch=$(git format-patch -1 --to="bodies@example.com") &&
1287        test_when_finished "rm $patch" &&
1288        git send-email \
1289                --dry-run \
1290                --from="Example <nobody@example.com>" \
1291                --to=nobody@example.com \
1292                --smtp-server relay.example.com \
1293                $patch >stdout &&
1294        grep "RCPT TO:<bodies@example.com>" stdout &&
1295        grep "RCPT TO:<nobody@example.com>" stdout
1296'
1297
1298test_expect_success $PREREQ 'To headers from files reset each patch' '
1299        patch1=$(git format-patch -1 --to="bodies@example.com") &&
1300        patch2=$(git format-patch -1 --to="other@example.com" HEAD~) &&
1301        test_when_finished "rm $patch1 && rm $patch2" &&
1302        git send-email \
1303                --dry-run \
1304                --from="Example <nobody@example.com>" \
1305                --to="nobody@example.com" \
1306                --smtp-server relay.example.com \
1307                $patch1 $patch2 >stdout &&
1308        test $(grep -c "RCPT TO:<bodies@example.com>" stdout) = 1 &&
1309        test $(grep -c "RCPT TO:<nobody@example.com>" stdout) = 2 &&
1310        test $(grep -c "RCPT TO:<other@example.com>" stdout) = 1
1311'
1312
1313test_expect_success $PREREQ 'setup expect' '
1314cat >email-using-8bit <<\EOF
1315From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
1316Message-Id: <bogus-message-id@example.com>
1317From: author@example.com
1318Date: Sat, 12 Jun 2010 15:53:58 +0200
1319Subject: subject goes here
1320
1321Dieser deutsche Text enthält einen Umlaut!
1322EOF
1323'
1324
1325test_expect_success $PREREQ 'setup expect' '
1326        echo "Subject: subject goes here" >expected
1327'
1328
1329test_expect_success $PREREQ 'ASCII subject is not RFC2047 quoted' '
1330        clean_fake_sendmail &&
1331        echo bogus |
1332        git send-email --from=author@example.com --to=nobody@example.com \
1333                        --smtp-server="$(pwd)/fake.sendmail" \
1334                        --8bit-encoding=UTF-8 \
1335                        email-using-8bit >stdout &&
1336        grep "Subject" msgtxt1 >actual &&
1337        test_cmp expected actual
1338'
1339
1340test_expect_success $PREREQ 'setup expect' '
1341        cat >content-type-decl <<-\EOF
1342        MIME-Version: 1.0
1343        Content-Type: text/plain; charset=UTF-8
1344        Content-Transfer-Encoding: 8bit
1345        EOF
1346'
1347
1348test_expect_success $PREREQ 'asks about and fixes 8bit encodings' '
1349        clean_fake_sendmail &&
1350        echo |
1351        git send-email --from=author@example.com --to=nobody@example.com \
1352                        --smtp-server="$(pwd)/fake.sendmail" \
1353                        email-using-8bit >stdout &&
1354        grep "do not declare a Content-Transfer-Encoding" stdout &&
1355        grep email-using-8bit stdout &&
1356        grep "Which 8bit encoding" stdout &&
1357        egrep "Content|MIME" msgtxt1 >actual &&
1358        test_cmp content-type-decl actual
1359'
1360
1361test_expect_success $PREREQ 'sendemail.8bitEncoding works' '
1362        clean_fake_sendmail &&
1363        git config sendemail.assume8bitEncoding UTF-8 &&
1364        echo bogus |
1365        git send-email --from=author@example.com --to=nobody@example.com \
1366                        --smtp-server="$(pwd)/fake.sendmail" \
1367                        email-using-8bit >stdout &&
1368        egrep "Content|MIME" msgtxt1 >actual &&
1369        test_cmp content-type-decl actual
1370'
1371
1372test_expect_success $PREREQ '--8bit-encoding overrides sendemail.8bitEncoding' '
1373        clean_fake_sendmail &&
1374        git config sendemail.assume8bitEncoding "bogus too" &&
1375        echo bogus |
1376        git send-email --from=author@example.com --to=nobody@example.com \
1377                        --smtp-server="$(pwd)/fake.sendmail" \
1378                        --8bit-encoding=UTF-8 \
1379                        email-using-8bit >stdout &&
1380        egrep "Content|MIME" msgtxt1 >actual &&
1381        test_cmp content-type-decl actual
1382'
1383
1384test_expect_success $PREREQ 'setup expect' '
1385        cat >email-using-8bit <<-\EOF
1386        From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
1387        Message-Id: <bogus-message-id@example.com>
1388        From: author@example.com
1389        Date: Sat, 12 Jun 2010 15:53:58 +0200
1390        Subject: Dieser Betreff enthält auch einen Umlaut!
1391
1392        Nothing to see here.
1393        EOF
1394'
1395
1396test_expect_success $PREREQ 'setup expect' '
1397        cat >expected <<-\EOF
1398        Subject: =?UTF-8?q?Dieser=20Betreff=20enth=C3=A4lt=20auch=20einen=20Umlaut!?=
1399        EOF
1400'
1401
1402test_expect_success $PREREQ '--8bit-encoding also treats subject' '
1403        clean_fake_sendmail &&
1404        echo bogus |
1405        git send-email --from=author@example.com --to=nobody@example.com \
1406                        --smtp-server="$(pwd)/fake.sendmail" \
1407                        --8bit-encoding=UTF-8 \
1408                        email-using-8bit >stdout &&
1409        grep "Subject" msgtxt1 >actual &&
1410        test_cmp expected actual
1411'
1412
1413test_expect_success $PREREQ 'setup expect' '
1414        cat >email-using-8bit <<-\EOF
1415        From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
1416        Message-Id: <bogus-message-id@example.com>
1417        From: A U Thor <author@example.com>
1418        Date: Sat, 12 Jun 2010 15:53:58 +0200
1419        Content-Type: text/plain; charset=UTF-8
1420        Subject: Nothing to see here.
1421
1422        Dieser Betreff enthält auch einen Umlaut!
1423        EOF
1424'
1425
1426test_expect_success $PREREQ 'sendemail.transferencoding=7bit fails on 8bit data' '
1427        clean_fake_sendmail &&
1428        git config sendemail.transferEncoding 7bit &&
1429        test_must_fail git send-email \
1430                --transfer-encoding=7bit \
1431                --smtp-server="$(pwd)/fake.sendmail" \
1432                email-using-8bit \
1433                2>errors >out &&
1434        grep "cannot send message as 7bit" errors &&
1435        test -z "$(ls msgtxt*)"
1436'
1437
1438test_expect_success $PREREQ '--transfer-encoding overrides sendemail.transferEncoding' '
1439        clean_fake_sendmail &&
1440        git config sendemail.transferEncoding 8bit &&
1441        test_must_fail git send-email \
1442                --transfer-encoding=7bit \
1443                --smtp-server="$(pwd)/fake.sendmail" \
1444                email-using-8bit \
1445                2>errors >out &&
1446        grep "cannot send message as 7bit" errors &&
1447        test -z "$(ls msgtxt*)"
1448'
1449
1450test_expect_success $PREREQ 'sendemail.transferencoding=8bit' '
1451        clean_fake_sendmail &&
1452        git send-email \
1453                --transfer-encoding=8bit \
1454                --smtp-server="$(pwd)/fake.sendmail" \
1455                email-using-8bit \
1456                2>errors >out &&
1457        sed '1,/^$/d' msgtxt1 >actual &&
1458        sed '1,/^$/d' email-using-8bit >expected &&
1459        test_cmp expected actual
1460'
1461
1462test_expect_success $PREREQ 'setup expect' '
1463        cat >expected <<-\EOF
1464        Dieser Betreff enth=C3=A4lt auch einen Umlaut!
1465        EOF
1466'
1467
1468test_expect_success $PREREQ '8-bit and sendemail.transferencoding=quoted-printable' '
1469        clean_fake_sendmail &&
1470        git send-email \
1471                --transfer-encoding=quoted-printable \
1472                --smtp-server="$(pwd)/fake.sendmail" \
1473                email-using-8bit \
1474                2>errors >out &&
1475        sed '1,/^$/d' msgtxt1 >actual &&
1476        test_cmp expected actual
1477'
1478
1479test_expect_success $PREREQ 'setup expect' '
1480        cat >expected <<-\EOF
1481        RGllc2VyIEJldHJlZmYgZW50aMOkbHQgYXVjaCBlaW5lbiBVbWxhdXQhCg==
1482        EOF
1483'
1484
1485test_expect_success $PREREQ '8-bit and sendemail.transferencoding=base64' '
1486        clean_fake_sendmail &&
1487        git send-email \
1488                --transfer-encoding=base64 \
1489                --smtp-server="$(pwd)/fake.sendmail" \
1490                email-using-8bit \
1491                2>errors >out &&
1492        sed '1,/^$/d' msgtxt1 >actual &&
1493        test_cmp expected actual
1494'
1495
1496test_expect_success $PREREQ 'setup expect' '
1497        cat >email-using-qp <<-\EOF
1498        From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
1499        Message-Id: <bogus-message-id@example.com>
1500        From: A U Thor <author@example.com>
1501        Date: Sat, 12 Jun 2010 15:53:58 +0200
1502        MIME-Version: 1.0
1503        Content-Transfer-Encoding: quoted-printable
1504        Content-Type: text/plain; charset=UTF-8
1505        Subject: Nothing to see here.
1506
1507        Dieser Betreff enth=C3=A4lt auch einen Umlaut!
1508        EOF
1509'
1510
1511test_expect_success $PREREQ 'convert from quoted-printable to base64' '
1512        clean_fake_sendmail &&
1513        git send-email \
1514                --transfer-encoding=base64 \
1515                --smtp-server="$(pwd)/fake.sendmail" \
1516                email-using-qp \
1517                2>errors >out &&
1518        sed '1,/^$/d' msgtxt1 >actual &&
1519        test_cmp expected actual
1520'
1521
1522test_expect_success $PREREQ 'setup expect' "
1523tr -d '\\015' | tr '%' '\\015' >email-using-crlf <<EOF
1524From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
1525Message-Id: <bogus-message-id@example.com>
1526From: A U Thor <author@example.com>
1527Date: Sat, 12 Jun 2010 15:53:58 +0200
1528Content-Type: text/plain; charset=UTF-8
1529Subject: Nothing to see here.
1530
1531Look, I have a CRLF and an = sign!%
1532EOF
1533"
1534
1535test_expect_success $PREREQ 'setup expect' '
1536        cat >expected <<-\EOF
1537        Look, I have a CRLF and an =3D sign!=0D
1538        EOF
1539'
1540
1541test_expect_success $PREREQ 'CRLF and sendemail.transferencoding=quoted-printable' '
1542        clean_fake_sendmail &&
1543        git send-email \
1544                --transfer-encoding=quoted-printable \
1545                --smtp-server="$(pwd)/fake.sendmail" \
1546                email-using-crlf \
1547                2>errors >out &&
1548        sed '1,/^$/d' msgtxt1 >actual &&
1549        test_cmp expected actual
1550'
1551
1552test_expect_success $PREREQ 'setup expect' '
1553        cat >expected <<-\EOF
1554        TG9vaywgSSBoYXZlIGEgQ1JMRiBhbmQgYW4gPSBzaWduIQ0K
1555        EOF
1556'
1557
1558test_expect_success $PREREQ 'CRLF and sendemail.transferencoding=base64' '
1559        clean_fake_sendmail &&
1560        git send-email \
1561                --transfer-encoding=base64 \
1562                --smtp-server="$(pwd)/fake.sendmail" \
1563                email-using-crlf \
1564                2>errors >out &&
1565        sed '1,/^$/d' msgtxt1 >actual &&
1566        test_cmp expected actual
1567'
1568
1569
1570# Note that the patches in this test are deliberately out of order; we
1571# want to make sure it works even if the cover-letter is not in the
1572# first mail.
1573test_expect_success $PREREQ 'refusing to send cover letter template' '
1574        clean_fake_sendmail &&
1575        rm -fr outdir &&
1576        git format-patch --cover-letter -2 -o outdir &&
1577        test_must_fail git send-email \
1578                --from="Example <nobody@example.com>" \
1579                --to=nobody@example.com \
1580                --smtp-server="$(pwd)/fake.sendmail" \
1581                outdir/0002-*.patch \
1582                outdir/0000-*.patch \
1583                outdir/0001-*.patch \
1584                2>errors >out &&
1585        grep "SUBJECT HERE" errors &&
1586        test -z "$(ls msgtxt*)"
1587'
1588
1589test_expect_success $PREREQ '--force sends cover letter template anyway' '
1590        clean_fake_sendmail &&
1591        rm -fr outdir &&
1592        git format-patch --cover-letter -2 -o outdir &&
1593        git send-email \
1594                --force \
1595                --from="Example <nobody@example.com>" \
1596                --to=nobody@example.com \
1597                --smtp-server="$(pwd)/fake.sendmail" \
1598                outdir/0002-*.patch \
1599                outdir/0000-*.patch \
1600                outdir/0001-*.patch \
1601                2>errors >out &&
1602        ! grep "SUBJECT HERE" errors &&
1603        test -n "$(ls msgtxt*)"
1604'
1605
1606test_cover_addresses () {
1607        header="$1"
1608        shift
1609        clean_fake_sendmail &&
1610        rm -fr outdir &&
1611        git format-patch --cover-letter -2 -o outdir &&
1612        cover=$(echo outdir/0000-*.patch) &&
1613        mv $cover cover-to-edit.patch &&
1614        perl -pe "s/^From:/$header: extra\@address.com\nFrom:/" cover-to-edit.patch >"$cover" &&
1615        git send-email \
1616                --force \
1617                --from="Example <nobody@example.com>" \
1618                --no-to --no-cc \
1619                "$@" \
1620                --smtp-server="$(pwd)/fake.sendmail" \
1621                outdir/0000-*.patch \
1622                outdir/0001-*.patch \
1623                outdir/0002-*.patch \
1624                2>errors >out &&
1625        grep "^$header: extra@address.com" msgtxt1 >to1 &&
1626        grep "^$header: extra@address.com" msgtxt2 >to2 &&
1627        grep "^$header: extra@address.com" msgtxt3 >to3 &&
1628        test_line_count = 1 to1 &&
1629        test_line_count = 1 to2 &&
1630        test_line_count = 1 to3
1631}
1632
1633test_expect_success $PREREQ 'to-cover adds To to all mail' '
1634        test_cover_addresses "To" --to-cover
1635'
1636
1637test_expect_success $PREREQ 'cc-cover adds Cc to all mail' '
1638        test_cover_addresses "Cc" --cc-cover
1639'
1640
1641test_expect_success $PREREQ 'tocover adds To to all mail' '
1642        test_config sendemail.tocover true &&
1643        test_cover_addresses "To"
1644'
1645
1646test_expect_success $PREREQ 'cccover adds Cc to all mail' '
1647        test_config sendemail.cccover true &&
1648        test_cover_addresses "Cc"
1649'
1650
1651test_expect_success $PREREQ 'escaped quotes in sendemail.aliasfiletype=mutt' '
1652        clean_fake_sendmail &&
1653        echo "alias sbd \\\"Dot U. Sir\\\" <somebody@example.org>" >.mutt &&
1654        git config --replace-all sendemail.aliasesfile "$(pwd)/.mutt" &&
1655        git config sendemail.aliasfiletype mutt &&
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        grep -F "To: \"Dot U. Sir\" <somebody@example.org>" out
1664'
1665
1666test_expect_success $PREREQ 'sendemail.aliasfiletype=mailrc' '
1667        clean_fake_sendmail &&
1668        echo "alias sbd  somebody@example.org" >.mailrc &&
1669        git config --replace-all sendemail.aliasesfile "$(pwd)/.mailrc" &&
1670        git config sendemail.aliasfiletype mailrc &&
1671        git send-email \
1672                --from="Example <nobody@example.com>" \
1673                --to=sbd \
1674                --smtp-server="$(pwd)/fake.sendmail" \
1675                outdir/0001-*.patch \
1676                2>errors >out &&
1677        grep "^!somebody@example\.org!$" commandline1
1678'
1679
1680test_expect_success $PREREQ 'sendemail.aliasfile=~/.mailrc' '
1681        clean_fake_sendmail &&
1682        echo "alias sbd  someone@example.org" >"$HOME/.mailrc" &&
1683        git config --replace-all sendemail.aliasesfile "~/.mailrc" &&
1684        git config sendemail.aliasfiletype mailrc &&
1685        git send-email \
1686                --from="Example <nobody@example.com>" \
1687                --to=sbd \
1688                --smtp-server="$(pwd)/fake.sendmail" \
1689                outdir/0001-*.patch \
1690                2>errors >out &&
1691        grep "^!someone@example\.org!$" commandline1
1692'
1693
1694test_dump_aliases () {
1695        msg="$1" && shift &&
1696        filetype="$1" && shift &&
1697        printf '%s\n' "$@" >expect &&
1698        cat >.tmp-email-aliases &&
1699
1700        test_expect_success $PREREQ "$msg" '
1701                clean_fake_sendmail && rm -fr outdir &&
1702                git config --replace-all sendemail.aliasesfile \
1703                        "$(pwd)/.tmp-email-aliases" &&
1704                git config sendemail.aliasfiletype "$filetype" &&
1705                git send-email --dump-aliases 2>errors >actual &&
1706                test_cmp expect actual
1707        '
1708}
1709
1710test_dump_aliases '--dump-aliases sendmail format' \
1711        'sendmail' \
1712        'abgroup' \
1713        'alice' \
1714        'bcgrp' \
1715        'bob' \
1716        'chloe' <<-\EOF
1717        alice: Alice W Land <awol@example.com>
1718        bob: Robert Bobbyton <bob@example.com>
1719        chloe: chloe@example.com
1720        abgroup: alice, bob
1721        bcgrp: bob, chloe, Other <o@example.com>
1722        EOF
1723
1724test_dump_aliases '--dump-aliases mutt format' \
1725        'mutt' \
1726        'alice' \
1727        'bob' \
1728        'chloe' \
1729        'donald' <<-\EOF
1730        alias alice Alice W Land <awol@example.com>
1731        alias donald Donald C Carlton <donc@example.com>
1732        alias bob Robert Bobbyton <bob@example.com>
1733        alias chloe chloe@example.com
1734        EOF
1735
1736test_dump_aliases '--dump-aliases mailrc format' \
1737        'mailrc' \
1738        'alice' \
1739        'bob' \
1740        'chloe' \
1741        'eve' <<-\EOF
1742        alias alice   Alice W Land <awol@example.com>
1743        alias eve     Eve <eve@example.com>
1744        alias bob     Robert Bobbyton <bob@example.com>
1745        alias chloe   chloe@example.com
1746        EOF
1747
1748test_dump_aliases '--dump-aliases pine format' \
1749        'pine' \
1750        'alice' \
1751        'bob' \
1752        'chloe' \
1753        'eve' <<-\EOF
1754        alice   Alice W Land    <awol@example.com>
1755        eve     Eve     <eve@example.com>
1756        bob     Robert  Bobbyton <bob@example.com>
1757        chloe           chloe@example.com
1758        EOF
1759
1760test_dump_aliases '--dump-aliases gnus format' \
1761        'gnus' \
1762        'alice' \
1763        'bob' \
1764        'chloe' \
1765        'eve' <<-\EOF
1766        (define-mail-alias "alice" "awol@example.com")
1767        (define-mail-alias "eve" "eve@example.com")
1768        (define-mail-alias "bob" "bob@example.com")
1769        (define-mail-alias "chloe" "chloe@example.com")
1770        EOF
1771
1772test_expect_success '--dump-aliases must be used alone' '
1773        test_must_fail git send-email --dump-aliases --to=janice@example.com -1 refs/heads/accounting
1774'
1775
1776test_sendmail_aliases () {
1777        msg="$1" && shift &&
1778        expect="$@" &&
1779        cat >.tmp-email-aliases &&
1780
1781        test_expect_success $PREREQ "$msg" '
1782                clean_fake_sendmail && rm -fr outdir &&
1783                git format-patch -1 -o outdir &&
1784                git config --replace-all sendemail.aliasesfile \
1785                        "$(pwd)/.tmp-email-aliases" &&
1786                git config sendemail.aliasfiletype sendmail &&
1787                git send-email \
1788                        --from="Example <nobody@example.com>" \
1789                        --to=alice --to=bcgrp \
1790                        --smtp-server="$(pwd)/fake.sendmail" \
1791                        outdir/0001-*.patch \
1792                        2>errors >out &&
1793                for i in $expect
1794                do
1795                        grep "^!$i!$" commandline1 || return 1
1796                done
1797        '
1798}
1799
1800test_sendmail_aliases 'sendemail.aliasfiletype=sendmail' \
1801        'awol@example\.com' \
1802        'bob@example\.com' \
1803        'chloe@example\.com' \
1804        'o@example\.com' <<-\EOF
1805        alice: Alice W Land <awol@example.com>
1806        bob: Robert Bobbyton <bob@example.com>
1807        # this is a comment
1808           # this is also a comment
1809        chloe: chloe@example.com
1810        abgroup: alice, bob
1811        bcgrp: bob, chloe, Other <o@example.com>
1812        EOF
1813
1814test_sendmail_aliases 'sendmail aliases line folding' \
1815        alice1 \
1816        bob1 bob2 \
1817        chuck1 chuck2 \
1818        darla1 darla2 darla3 \
1819        elton1 elton2 elton3 \
1820        fred1 fred2 \
1821        greg1 <<-\EOF
1822        alice: alice1
1823        bob: bob1,\
1824        bob2
1825        chuck: chuck1,
1826            chuck2
1827        darla: darla1,\
1828        darla2,
1829            darla3
1830        elton: elton1,
1831            elton2,\
1832        elton3
1833        fred: fred1,\
1834            fred2
1835        greg: greg1
1836        bcgrp: bob, chuck, darla, elton, fred, greg
1837        EOF
1838
1839test_sendmail_aliases 'sendmail aliases tolerate bogus line folding' \
1840        alice1 bob1 <<-\EOF
1841            alice: alice1
1842        bcgrp: bob1\
1843        EOF
1844
1845test_sendmail_aliases 'sendmail aliases empty' alice bcgrp <<-\EOF
1846        EOF
1847
1848test_expect_success $PREREQ 'alias support in To header' '
1849        clean_fake_sendmail &&
1850        echo "alias sbd  someone@example.org" >.mailrc &&
1851        test_config sendemail.aliasesfile ".mailrc" &&
1852        test_config sendemail.aliasfiletype mailrc &&
1853        git format-patch --stdout -1 --to=sbd >aliased.patch &&
1854        git send-email \
1855                --from="Example <nobody@example.com>" \
1856                --smtp-server="$(pwd)/fake.sendmail" \
1857                aliased.patch \
1858                2>errors >out &&
1859        grep "^!someone@example\.org!$" commandline1
1860'
1861
1862test_expect_success $PREREQ 'alias support in Cc header' '
1863        clean_fake_sendmail &&
1864        echo "alias sbd  someone@example.org" >.mailrc &&
1865        test_config sendemail.aliasesfile ".mailrc" &&
1866        test_config sendemail.aliasfiletype mailrc &&
1867        git format-patch --stdout -1 --cc=sbd >aliased.patch &&
1868        git send-email \
1869                --from="Example <nobody@example.com>" \
1870                --smtp-server="$(pwd)/fake.sendmail" \
1871                aliased.patch \
1872                2>errors >out &&
1873        grep "^!someone@example\.org!$" commandline1
1874'
1875
1876test_expect_success $PREREQ 'tocmd works with aliases' '
1877        clean_fake_sendmail &&
1878        echo "alias sbd  someone@example.org" >.mailrc &&
1879        test_config sendemail.aliasesfile ".mailrc" &&
1880        test_config sendemail.aliasfiletype mailrc &&
1881        git format-patch --stdout -1 >tocmd.patch &&
1882        echo tocmd--sbd >>tocmd.patch &&
1883        git send-email \
1884                --from="Example <nobody@example.com>" \
1885                --to-cmd=./tocmd-sed \
1886                --smtp-server="$(pwd)/fake.sendmail" \
1887                tocmd.patch \
1888                2>errors >out &&
1889        grep "^!someone@example\.org!$" commandline1
1890'
1891
1892test_expect_success $PREREQ 'cccmd works with aliases' '
1893        clean_fake_sendmail &&
1894        echo "alias sbd  someone@example.org" >.mailrc &&
1895        test_config sendemail.aliasesfile ".mailrc" &&
1896        test_config sendemail.aliasfiletype mailrc &&
1897        git format-patch --stdout -1 >cccmd.patch &&
1898        echo cccmd--sbd >>cccmd.patch &&
1899        git send-email \
1900                --from="Example <nobody@example.com>" \
1901                --cc-cmd=./cccmd-sed \
1902                --smtp-server="$(pwd)/fake.sendmail" \
1903                cccmd.patch \
1904                2>errors >out &&
1905        grep "^!someone@example\.org!$" commandline1
1906'
1907
1908do_xmailer_test () {
1909        expected=$1 params=$2 &&
1910        git format-patch -1 &&
1911        git send-email \
1912                --from="Example <nobody@example.com>" \
1913                --to=someone@example.com \
1914                --smtp-server="$(pwd)/fake.sendmail" \
1915                $params \
1916                0001-*.patch \
1917                2>errors >out &&
1918        { grep '^X-Mailer:' out || :; } >mailer &&
1919        test_line_count = $expected mailer
1920}
1921
1922test_expect_success $PREREQ '--[no-]xmailer without any configuration' '
1923        do_xmailer_test 1 "--xmailer" &&
1924        do_xmailer_test 0 "--no-xmailer"
1925'
1926
1927test_expect_success $PREREQ '--[no-]xmailer with sendemail.xmailer=true' '
1928        test_config sendemail.xmailer true &&
1929        do_xmailer_test 1 "" &&
1930        do_xmailer_test 0 "--no-xmailer" &&
1931        do_xmailer_test 1 "--xmailer"
1932'
1933
1934test_expect_success $PREREQ '--[no-]xmailer with sendemail.xmailer=false' '
1935        test_config sendemail.xmailer false &&
1936        do_xmailer_test 0 "" &&
1937        do_xmailer_test 0 "--no-xmailer" &&
1938        do_xmailer_test 1 "--xmailer"
1939'
1940
1941test_expect_success $PREREQ 'setup expected-list' '
1942        git send-email \
1943        --dry-run \
1944        --from="Example <from@example.com>" \
1945        --to="To 1 <to1@example.com>" \
1946        --to="to2@example.com" \
1947        --to="to3@example.com" \
1948        --cc="Cc 1 <cc1@example.com>" \
1949        --cc="Cc2 <cc2@example.com>" \
1950        --bcc="bcc1@example.com" \
1951        --bcc="bcc2@example.com" \
1952        0001-add-master.patch | replace_variable_fields \
1953        >expected-list
1954'
1955
1956test_expect_success $PREREQ 'use email list in --cc --to and --bcc' '
1957        git send-email \
1958        --dry-run \
1959        --from="Example <from@example.com>" \
1960        --to="To 1 <to1@example.com>, to2@example.com" \
1961        --to="to3@example.com" \
1962        --cc="Cc 1 <cc1@example.com>, Cc2 <cc2@example.com>" \
1963        --bcc="bcc1@example.com, bcc2@example.com" \
1964        0001-add-master.patch | replace_variable_fields \
1965        >actual-list &&
1966        test_cmp expected-list actual-list
1967'
1968
1969test_expect_success $PREREQ 'aliases work with email list' '
1970        echo "alias to2 to2@example.com" >.mutt &&
1971        echo "alias cc1 Cc 1 <cc1@example.com>" >>.mutt &&
1972        test_config sendemail.aliasesfile ".mutt" &&
1973        test_config sendemail.aliasfiletype mutt &&
1974        git send-email \
1975        --dry-run \
1976        --from="Example <from@example.com>" \
1977        --to="To 1 <to1@example.com>, to2, to3@example.com" \
1978        --cc="cc1, Cc2 <cc2@example.com>" \
1979        --bcc="bcc1@example.com, bcc2@example.com" \
1980        0001-add-master.patch | replace_variable_fields \
1981        >actual-list &&
1982        test_cmp expected-list actual-list
1983'
1984
1985test_expect_success $PREREQ 'leading and trailing whitespaces are removed' '
1986        echo "alias to2 to2@example.com" >.mutt &&
1987        echo "alias cc1 Cc 1 <cc1@example.com>" >>.mutt &&
1988        test_config sendemail.aliasesfile ".mutt" &&
1989        test_config sendemail.aliasfiletype mutt &&
1990        TO1=$(echo "QTo 1 <to1@example.com>" | q_to_tab) &&
1991        TO2=$(echo "QZto2" | qz_to_tab_space) &&
1992        CC1=$(echo "cc1" | append_cr) &&
1993        BCC1=$(echo "Q bcc1@example.com Q" | q_to_nul) &&
1994        git send-email \
1995        --dry-run \
1996        --from="        Example <from@example.com>" \
1997        --to="$TO1" \
1998        --to="$TO2" \
1999        --to="  to3@example.com   " \
2000        --cc="$CC1" \
2001        --cc="Cc2 <cc2@example.com>" \
2002        --bcc="$BCC1" \
2003        --bcc="bcc2@example.com" \
2004        0001-add-master.patch | replace_variable_fields \
2005        >actual-list &&
2006        test_cmp expected-list actual-list
2007'
2008
2009test_expect_success $PREREQ 'invoke hook' '
2010        mkdir -p .git/hooks &&
2011
2012        write_script .git/hooks/sendemail-validate <<-\EOF &&
2013        # test that we have the correct environment variable, pwd, and
2014        # argument
2015        case "$GIT_DIR" in
2016        *.git)
2017                true
2018                ;;
2019        *)
2020                false
2021                ;;
2022        esac &&
2023        test -f 0001-add-master.patch &&
2024        grep "add master" "$1"
2025        EOF
2026
2027        mkdir subdir &&
2028        (
2029                # Test that it works even if we are not at the root of the
2030                # working tree
2031                cd subdir &&
2032                git send-email \
2033                        --from="Example <nobody@example.com>" \
2034                        --to=nobody@example.com \
2035                        --smtp-server="$(pwd)/../fake.sendmail" \
2036                        ../0001-add-master.patch &&
2037
2038                # Verify error message when a patch is rejected by the hook
2039                sed -e "s/add master/x/" ../0001-add-master.patch >../another.patch &&
2040                test_must_fail git send-email \
2041                        --from="Example <nobody@example.com>" \
2042                        --to=nobody@example.com \
2043                        --smtp-server="$(pwd)/../fake.sendmail" \
2044                        ../another.patch 2>err &&
2045                test_i18ngrep "rejected by sendemail-validate hook" err
2046        )
2047'
2048
2049test_expect_success $PREREQ 'test that send-email works outside a repo' '
2050        nongit git send-email \
2051                --from="Example <nobody@example.com>" \
2052                --to=nobody@example.com \
2053                --smtp-server="$(pwd)/fake.sendmail" \
2054                "$(pwd)/0001-add-master.patch"
2055'
2056
2057test_done