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