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