t / t9001-send-email.shon commit t0005: fix broken &&-chains (635ce72)
   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                test_must_fail 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' "
 144cat >expected-show-all-headers <<\EOF
 1450001-Second.patch
 146(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 147(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 148(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 149Dry-OK. Log says:
 150Server: relay.example.com
 151MAIL FROM:<from@example.com>
 152RCPT TO:<to@example.com>
 153RCPT TO:<cc@example.com>
 154RCPT TO:<author@example.com>
 155RCPT TO:<one@example.com>
 156RCPT TO:<two@example.com>
 157RCPT TO:<bcc@example.com>
 158From: Example <from@example.com>
 159To: to@example.com
 160Cc: cc@example.com,
 161        A <author@example.com>,
 162        One <one@example.com>,
 163        two@example.com
 164Subject: [PATCH 1/1] Second.
 165Date: DATE-STRING
 166Message-Id: MESSAGE-ID-STRING
 167X-Mailer: X-MAILER-STRING
 168In-Reply-To: <unique-message-id@example.com>
 169References: <unique-message-id@example.com>
 170
 171Result: OK
 172EOF
 173"
 174
 175test_suppress_self () {
 176        test_commit $3 &&
 177        test_when_finished "git reset --hard HEAD^" &&
 178
 179        write_script cccmd-sed <<-EOF &&
 180                sed -n -e s/^cccmd--//p "\$1"
 181        EOF
 182
 183        git commit --amend --author="$1 <$2>" -F - &&
 184        clean_fake_sendmail &&
 185        git format-patch --stdout -1 >"suppress-self-$3.patch" &&
 186
 187        git send-email --from="$1 <$2>" \
 188                --to=nobody@example.com \
 189                --cc-cmd=./cccmd-sed \
 190                --suppress-cc=self \
 191                --smtp-server="$(pwd)/fake.sendmail" \
 192                suppress-self-$3.patch &&
 193
 194        mv msgtxt1 msgtxt1-$3 &&
 195        sed -e '/^$/q' msgtxt1-$3 >"msghdr1-$3" &&
 196        >"expected-no-cc-$3" &&
 197
 198        (grep '^Cc:' msghdr1-$3 >"actual-no-cc-$3";
 199         test_cmp expected-no-cc-$3 actual-no-cc-$3)
 200}
 201
 202test_suppress_self_unquoted () {
 203        test_suppress_self "$1" "$2" "unquoted-$3" <<-EOF
 204                test suppress-cc.self unquoted-$3 with name $1 email $2
 205
 206                unquoted-$3
 207
 208                cccmd--$1 <$2>
 209
 210                Cc: $1 <$2>
 211                Signed-off-by: $1 <$2>
 212        EOF
 213}
 214
 215test_suppress_self_quoted () {
 216        test_suppress_self "$1" "$2" "quoted-$3" <<-EOF
 217                test suppress-cc.self quoted-$3 with name $1 email $2
 218
 219                quoted-$3
 220
 221                cccmd--"$1" <$2>
 222
 223                Cc: $1 <$2>
 224                Cc: "$1" <$2>
 225                Signed-off-by: $1 <$2>
 226                Signed-off-by: "$1" <$2>
 227        EOF
 228}
 229
 230test_expect_success $PREREQ 'self name is suppressed' "
 231        test_suppress_self_unquoted 'A U Thor' 'author@example.com' \
 232                'self_name_suppressed'
 233"
 234
 235test_expect_success $PREREQ 'self name with dot is suppressed' "
 236        test_suppress_self_quoted 'A U. Thor' 'author@example.com' \
 237                'self_name_dot_suppressed'
 238"
 239
 240test_expect_success $PREREQ 'non-ascii self name is suppressed' "
 241        test_suppress_self_quoted 'Füñný Nâmé' 'odd_?=mail@example.com' \
 242                'non_ascii_self_suppressed'
 243"
 244
 245# This name is long enough to force format-patch to split it into multiple
 246# encoded-words, assuming it uses UTF-8 with the "Q" encoding.
 247test_expect_success $PREREQ 'long non-ascii self name is suppressed' "
 248        test_suppress_self_quoted 'Ƒüñníęř €. Nâṁé' 'odd_?=mail@example.com' \
 249                'long_non_ascii_self_suppressed'
 250"
 251
 252test_expect_success $PREREQ 'sanitized self name is suppressed' "
 253        test_suppress_self_unquoted '\"A U. Thor\"' 'author@example.com' \
 254                'self_name_sanitized_suppressed'
 255"
 256
 257test_expect_success $PREREQ 'Show all headers' '
 258        git send-email \
 259                --dry-run \
 260                --suppress-cc=sob \
 261                --from="Example <from@example.com>" \
 262                --to=to@example.com \
 263                --cc=cc@example.com \
 264                --bcc=bcc@example.com \
 265                --in-reply-to="<unique-message-id@example.com>" \
 266                --smtp-server relay.example.com \
 267                $patches |
 268        sed     -e "s/^\(Date:\).*/\1 DATE-STRING/" \
 269                -e "s/^\(Message-Id:\).*/\1 MESSAGE-ID-STRING/" \
 270                -e "s/^\(X-Mailer:\).*/\1 X-MAILER-STRING/" \
 271                >actual-show-all-headers &&
 272        test_cmp expected-show-all-headers actual-show-all-headers
 273'
 274
 275test_expect_success $PREREQ 'Prompting works' '
 276        clean_fake_sendmail &&
 277        (echo "to@example.com"
 278         echo ""
 279        ) | GIT_SEND_EMAIL_NOTTY=1 git send-email \
 280                --smtp-server="$(pwd)/fake.sendmail" \
 281                $patches \
 282                2>errors &&
 283                grep "^From: A U Thor <author@example.com>\$" msgtxt1 &&
 284                grep "^To: to@example.com\$" msgtxt1
 285'
 286
 287test_expect_success $PREREQ,AUTOIDENT 'implicit ident is allowed' '
 288        clean_fake_sendmail &&
 289        (sane_unset GIT_AUTHOR_NAME &&
 290        sane_unset GIT_AUTHOR_EMAIL &&
 291        sane_unset GIT_COMMITTER_NAME &&
 292        sane_unset GIT_COMMITTER_EMAIL &&
 293        GIT_SEND_EMAIL_NOTTY=1 git send-email \
 294                --smtp-server="$(pwd)/fake.sendmail" \
 295                --to=to@example.com \
 296                $patches </dev/null 2>errors
 297        )
 298'
 299
 300test_expect_success $PREREQ,!AUTOIDENT 'broken implicit ident aborts send-email' '
 301        clean_fake_sendmail &&
 302        (sane_unset GIT_AUTHOR_NAME &&
 303        sane_unset GIT_AUTHOR_EMAIL &&
 304        sane_unset GIT_COMMITTER_NAME &&
 305        sane_unset GIT_COMMITTER_EMAIL &&
 306        GIT_SEND_EMAIL_NOTTY=1 && export GIT_SEND_EMAIL_NOTTY &&
 307        test_must_fail git send-email \
 308                --smtp-server="$(pwd)/fake.sendmail" \
 309                --to=to@example.com \
 310                $patches </dev/null 2>errors &&
 311        test_i18ngrep "tell me who you are" errors
 312        )
 313'
 314
 315test_expect_success $PREREQ 'tocmd works' '
 316        clean_fake_sendmail &&
 317        cp $patches tocmd.patch &&
 318        echo tocmd--tocmd@example.com >>tocmd.patch &&
 319        write_script tocmd-sed <<-\EOF &&
 320        sed -n -e "s/^tocmd--//p" "$1"
 321        EOF
 322        git send-email \
 323                --from="Example <nobody@example.com>" \
 324                --to-cmd=./tocmd-sed \
 325                --smtp-server="$(pwd)/fake.sendmail" \
 326                tocmd.patch \
 327                &&
 328        grep "^To: tocmd@example.com" msgtxt1
 329'
 330
 331test_expect_success $PREREQ 'cccmd works' '
 332        clean_fake_sendmail &&
 333        cp $patches cccmd.patch &&
 334        echo "cccmd--  cccmd@example.com" >>cccmd.patch &&
 335        write_script cccmd-sed <<-\EOF &&
 336        sed -n -e "s/^cccmd--//p" "$1"
 337        EOF
 338        git send-email \
 339                --from="Example <nobody@example.com>" \
 340                --to=nobody@example.com \
 341                --cc-cmd=./cccmd-sed \
 342                --smtp-server="$(pwd)/fake.sendmail" \
 343                cccmd.patch \
 344                &&
 345        grep "^ cccmd@example.com" msgtxt1
 346'
 347
 348test_expect_success $PREREQ 'reject long lines' '
 349        z8=zzzzzzzz &&
 350        z64=$z8$z8$z8$z8$z8$z8$z8$z8 &&
 351        z512=$z64$z64$z64$z64$z64$z64$z64$z64 &&
 352        clean_fake_sendmail &&
 353        cp $patches longline.patch &&
 354        echo $z512$z512 >>longline.patch &&
 355        test_must_fail git send-email \
 356                --from="Example <nobody@example.com>" \
 357                --to=nobody@example.com \
 358                --smtp-server="$(pwd)/fake.sendmail" \
 359                $patches longline.patch \
 360                2>errors &&
 361        grep longline.patch errors
 362'
 363
 364test_expect_success $PREREQ 'no patch was sent' '
 365        ! test -e commandline1
 366'
 367
 368test_expect_success $PREREQ 'Author From: in message body' '
 369        clean_fake_sendmail &&
 370        git send-email \
 371                --from="Example <nobody@example.com>" \
 372                --to=nobody@example.com \
 373                --smtp-server="$(pwd)/fake.sendmail" \
 374                $patches &&
 375        sed "1,/^\$/d" <msgtxt1 >msgbody1 &&
 376        grep "From: A <author@example.com>" msgbody1
 377'
 378
 379test_expect_success $PREREQ 'Author From: not in message body' '
 380        clean_fake_sendmail &&
 381        git send-email \
 382                --from="A <author@example.com>" \
 383                --to=nobody@example.com \
 384                --smtp-server="$(pwd)/fake.sendmail" \
 385                $patches &&
 386        sed "1,/^\$/d" <msgtxt1 >msgbody1 &&
 387        ! grep "From: A <author@example.com>" msgbody1
 388'
 389
 390test_expect_success $PREREQ 'allow long lines with --no-validate' '
 391        git send-email \
 392                --from="Example <nobody@example.com>" \
 393                --to=nobody@example.com \
 394                --smtp-server="$(pwd)/fake.sendmail" \
 395                --no-validate \
 396                $patches longline.patch \
 397                2>errors
 398'
 399
 400test_expect_success $PREREQ 'Invalid In-Reply-To' '
 401        clean_fake_sendmail &&
 402        git send-email \
 403                --from="Example <nobody@example.com>" \
 404                --to=nobody@example.com \
 405                --in-reply-to=" " \
 406                --smtp-server="$(pwd)/fake.sendmail" \
 407                $patches \
 408                2>errors &&
 409        ! grep "^In-Reply-To: < *>" msgtxt1
 410'
 411
 412test_expect_success $PREREQ 'Valid In-Reply-To when prompting' '
 413        clean_fake_sendmail &&
 414        (echo "From Example <from@example.com>"
 415         echo "To Example <to@example.com>"
 416         echo ""
 417        ) | GIT_SEND_EMAIL_NOTTY=1 git send-email \
 418                --smtp-server="$(pwd)/fake.sendmail" \
 419                $patches 2>errors &&
 420        ! grep "^In-Reply-To: < *>" msgtxt1
 421'
 422
 423test_expect_success $PREREQ 'In-Reply-To without --chain-reply-to' '
 424        clean_fake_sendmail &&
 425        echo "<unique-message-id@example.com>" >expect &&
 426        git send-email \
 427                --from="Example <nobody@example.com>" \
 428                --to=nobody@example.com \
 429                --no-chain-reply-to \
 430                --in-reply-to="$(cat expect)" \
 431                --smtp-server="$(pwd)/fake.sendmail" \
 432                $patches $patches $patches \
 433                2>errors &&
 434        # The first message is a reply to --in-reply-to
 435        sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt1 >actual &&
 436        test_cmp expect actual &&
 437        # Second and subsequent messages are replies to the first one
 438        sed -n -e "s/^Message-Id: *\(.*\)/\1/p" msgtxt1 >expect &&
 439        sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt2 >actual &&
 440        test_cmp expect actual &&
 441        sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt3 >actual &&
 442        test_cmp expect actual
 443'
 444
 445test_expect_success $PREREQ 'In-Reply-To with --chain-reply-to' '
 446        clean_fake_sendmail &&
 447        echo "<unique-message-id@example.com>" >expect &&
 448        git send-email \
 449                --from="Example <nobody@example.com>" \
 450                --to=nobody@example.com \
 451                --chain-reply-to \
 452                --in-reply-to="$(cat expect)" \
 453                --smtp-server="$(pwd)/fake.sendmail" \
 454                $patches $patches $patches \
 455                2>errors &&
 456        sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt1 >actual &&
 457        test_cmp expect actual &&
 458        sed -n -e "s/^Message-Id: *\(.*\)/\1/p" msgtxt1 >expect &&
 459        sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt2 >actual &&
 460        test_cmp expect actual &&
 461        sed -n -e "s/^Message-Id: *\(.*\)/\1/p" msgtxt2 >expect &&
 462        sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt3 >actual &&
 463        test_cmp expect actual
 464'
 465
 466test_expect_success $PREREQ 'setup fake editor' '
 467        write_script fake-editor <<-\EOF
 468        echo fake edit >>"$1"
 469        EOF
 470'
 471
 472test_set_editor "$(pwd)/fake-editor"
 473
 474test_expect_success $PREREQ '--compose works' '
 475        clean_fake_sendmail &&
 476        git send-email \
 477        --compose --subject foo \
 478        --from="Example <nobody@example.com>" \
 479        --to=nobody@example.com \
 480        --smtp-server="$(pwd)/fake.sendmail" \
 481        $patches \
 482        2>errors
 483'
 484
 485test_expect_success $PREREQ 'first message is compose text' '
 486        grep "^fake edit" msgtxt1
 487'
 488
 489test_expect_success $PREREQ 'second message is patch' '
 490        grep "Subject:.*Second" msgtxt2
 491'
 492
 493test_expect_success $PREREQ 'setup expect' "
 494cat >expected-suppress-sob <<\EOF
 4950001-Second.patch
 496(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 497(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 498(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 499Dry-OK. Log says:
 500Server: relay.example.com
 501MAIL FROM:<from@example.com>
 502RCPT TO:<to@example.com>
 503RCPT TO:<cc@example.com>
 504RCPT TO:<author@example.com>
 505RCPT TO:<one@example.com>
 506RCPT TO:<two@example.com>
 507From: Example <from@example.com>
 508To: to@example.com
 509Cc: cc@example.com,
 510        A <author@example.com>,
 511        One <one@example.com>,
 512        two@example.com
 513Subject: [PATCH 1/1] Second.
 514Date: DATE-STRING
 515Message-Id: MESSAGE-ID-STRING
 516X-Mailer: X-MAILER-STRING
 517
 518Result: OK
 519EOF
 520"
 521
 522test_suppression () {
 523        git send-email \
 524                --dry-run \
 525                --suppress-cc=$1 ${2+"--suppress-cc=$2"} \
 526                --from="Example <from@example.com>" \
 527                --to=to@example.com \
 528                --smtp-server relay.example.com \
 529                $patches |
 530        sed     -e "s/^\(Date:\).*/\1 DATE-STRING/" \
 531                -e "s/^\(Message-Id:\).*/\1 MESSAGE-ID-STRING/" \
 532                -e "s/^\(X-Mailer:\).*/\1 X-MAILER-STRING/" \
 533                >actual-suppress-$1${2+"-$2"} &&
 534        test_cmp expected-suppress-$1${2+"-$2"} actual-suppress-$1${2+"-$2"}
 535}
 536
 537test_expect_success $PREREQ 'sendemail.cc set' '
 538        git config sendemail.cc cc@example.com &&
 539        test_suppression sob
 540'
 541
 542test_expect_success $PREREQ 'setup expect' "
 543cat >expected-suppress-sob <<\EOF
 5440001-Second.patch
 545(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 546(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 547(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 548Dry-OK. Log says:
 549Server: relay.example.com
 550MAIL FROM:<from@example.com>
 551RCPT TO:<to@example.com>
 552RCPT TO:<author@example.com>
 553RCPT TO:<one@example.com>
 554RCPT TO:<two@example.com>
 555From: Example <from@example.com>
 556To: to@example.com
 557Cc: A <author@example.com>,
 558        One <one@example.com>,
 559        two@example.com
 560Subject: [PATCH 1/1] Second.
 561Date: DATE-STRING
 562Message-Id: MESSAGE-ID-STRING
 563X-Mailer: X-MAILER-STRING
 564
 565Result: OK
 566EOF
 567"
 568
 569test_expect_success $PREREQ 'sendemail.cc unset' '
 570        git config --unset sendemail.cc &&
 571        test_suppression sob
 572'
 573
 574test_expect_success $PREREQ 'setup expect' "
 575cat >expected-suppress-cccmd <<\EOF
 5760001-Second.patch
 577(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 578(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 579(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 580(body) Adding cc: C O Mitter <committer@example.com> from line 'Signed-off-by: C O Mitter <committer@example.com>'
 581Dry-OK. Log says:
 582Server: relay.example.com
 583MAIL FROM:<from@example.com>
 584RCPT TO:<to@example.com>
 585RCPT TO:<author@example.com>
 586RCPT TO:<one@example.com>
 587RCPT TO:<two@example.com>
 588RCPT TO:<committer@example.com>
 589From: Example <from@example.com>
 590To: to@example.com
 591Cc: A <author@example.com>,
 592        One <one@example.com>,
 593        two@example.com,
 594        C O Mitter <committer@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.cccmd' '
 605        write_script cccmd <<-\EOF &&
 606        echo cc-cmd@example.com
 607        EOF
 608        git config sendemail.cccmd ./cccmd &&
 609        test_suppression cccmd
 610'
 611
 612test_expect_success $PREREQ 'setup expect' '
 613cat >expected-suppress-all <<\EOF
 6140001-Second.patch
 615Dry-OK. Log says:
 616Server: relay.example.com
 617MAIL FROM:<from@example.com>
 618RCPT TO:<to@example.com>
 619From: Example <from@example.com>
 620To: to@example.com
 621Subject: [PATCH 1/1] Second.
 622Date: DATE-STRING
 623Message-Id: MESSAGE-ID-STRING
 624X-Mailer: X-MAILER-STRING
 625
 626Result: OK
 627EOF
 628'
 629
 630test_expect_success $PREREQ '--suppress-cc=all' '
 631        test_suppression all
 632'
 633
 634test_expect_success $PREREQ 'setup expect' "
 635cat >expected-suppress-body <<\EOF
 6360001-Second.patch
 637(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 638(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 639(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 640(cc-cmd) Adding cc: cc-cmd@example.com from: './cccmd'
 641Dry-OK. Log says:
 642Server: relay.example.com
 643MAIL FROM:<from@example.com>
 644RCPT TO:<to@example.com>
 645RCPT TO:<author@example.com>
 646RCPT TO:<one@example.com>
 647RCPT TO:<two@example.com>
 648RCPT TO:<cc-cmd@example.com>
 649From: Example <from@example.com>
 650To: to@example.com
 651Cc: A <author@example.com>,
 652        One <one@example.com>,
 653        two@example.com,
 654        cc-cmd@example.com
 655Subject: [PATCH 1/1] Second.
 656Date: DATE-STRING
 657Message-Id: MESSAGE-ID-STRING
 658X-Mailer: X-MAILER-STRING
 659
 660Result: OK
 661EOF
 662"
 663
 664test_expect_success $PREREQ '--suppress-cc=body' '
 665        test_suppression body
 666'
 667
 668test_expect_success $PREREQ 'setup expect' "
 669cat >expected-suppress-body-cccmd <<\EOF
 6700001-Second.patch
 671(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 672(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 673(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 674Dry-OK. Log says:
 675Server: relay.example.com
 676MAIL FROM:<from@example.com>
 677RCPT TO:<to@example.com>
 678RCPT TO:<author@example.com>
 679RCPT TO:<one@example.com>
 680RCPT TO:<two@example.com>
 681From: Example <from@example.com>
 682To: to@example.com
 683Cc: A <author@example.com>,
 684        One <one@example.com>,
 685        two@example.com
 686Subject: [PATCH 1/1] Second.
 687Date: DATE-STRING
 688Message-Id: MESSAGE-ID-STRING
 689X-Mailer: X-MAILER-STRING
 690
 691Result: OK
 692EOF
 693"
 694
 695test_expect_success $PREREQ '--suppress-cc=body --suppress-cc=cccmd' '
 696        test_suppression body cccmd
 697'
 698
 699test_expect_success $PREREQ 'setup expect' "
 700cat >expected-suppress-sob <<\EOF
 7010001-Second.patch
 702(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 703(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 704(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 705Dry-OK. Log says:
 706Server: relay.example.com
 707MAIL FROM:<from@example.com>
 708RCPT TO:<to@example.com>
 709RCPT TO:<author@example.com>
 710RCPT TO:<one@example.com>
 711RCPT TO:<two@example.com>
 712From: Example <from@example.com>
 713To: to@example.com
 714Cc: A <author@example.com>,
 715        One <one@example.com>,
 716        two@example.com
 717Subject: [PATCH 1/1] Second.
 718Date: DATE-STRING
 719Message-Id: MESSAGE-ID-STRING
 720X-Mailer: X-MAILER-STRING
 721
 722Result: OK
 723EOF
 724"
 725
 726test_expect_success $PREREQ '--suppress-cc=sob' '
 727        test_might_fail git config --unset sendemail.cccmd &&
 728        test_suppression sob
 729'
 730
 731test_expect_success $PREREQ 'setup expect' "
 732cat >expected-suppress-bodycc <<\EOF
 7330001-Second.patch
 734(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 735(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 736(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 737(body) Adding cc: C O Mitter <committer@example.com> from line 'Signed-off-by: C O Mitter <committer@example.com>'
 738Dry-OK. Log says:
 739Server: relay.example.com
 740MAIL FROM:<from@example.com>
 741RCPT TO:<to@example.com>
 742RCPT TO:<author@example.com>
 743RCPT TO:<one@example.com>
 744RCPT TO:<two@example.com>
 745RCPT TO:<committer@example.com>
 746From: Example <from@example.com>
 747To: to@example.com
 748Cc: A <author@example.com>,
 749        One <one@example.com>,
 750        two@example.com,
 751        C O Mitter <committer@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=bodycc' '
 762        test_suppression bodycc
 763'
 764
 765test_expect_success $PREREQ 'setup expect' "
 766cat >expected-suppress-cc <<\EOF
 7670001-Second.patch
 768(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 769(body) Adding cc: C O Mitter <committer@example.com> from line 'Signed-off-by: C O Mitter <committer@example.com>'
 770Dry-OK. Log says:
 771Server: relay.example.com
 772MAIL FROM:<from@example.com>
 773RCPT TO:<to@example.com>
 774RCPT TO:<author@example.com>
 775RCPT TO:<committer@example.com>
 776From: Example <from@example.com>
 777To: to@example.com
 778Cc: A <author@example.com>,
 779        C O Mitter <committer@example.com>
 780Subject: [PATCH 1/1] Second.
 781Date: DATE-STRING
 782Message-Id: MESSAGE-ID-STRING
 783X-Mailer: X-MAILER-STRING
 784
 785Result: OK
 786EOF
 787"
 788
 789test_expect_success $PREREQ '--suppress-cc=cc' '
 790        test_suppression cc
 791'
 792
 793test_confirm () {
 794        echo y | \
 795                GIT_SEND_EMAIL_NOTTY=1 \
 796                git send-email \
 797                --from="Example <nobody@example.com>" \
 798                --to=nobody@example.com \
 799                --smtp-server="$(pwd)/fake.sendmail" \
 800                $@ $patches >stdout &&
 801        grep "Send this email" stdout
 802}
 803
 804test_expect_success $PREREQ '--confirm=always' '
 805        test_confirm --confirm=always --suppress-cc=all
 806'
 807
 808test_expect_success $PREREQ '--confirm=auto' '
 809        test_confirm --confirm=auto
 810'
 811
 812test_expect_success $PREREQ '--confirm=cc' '
 813        test_confirm --confirm=cc
 814'
 815
 816test_expect_success $PREREQ '--confirm=compose' '
 817        test_confirm --confirm=compose --compose
 818'
 819
 820save_confirm () {
 821        CONFIRM=$(git config --get sendemail.confirm) &&
 822        test_when_finished "git config sendemail.confirm ${CONFIRM:-never}"
 823}
 824
 825test_expect_success $PREREQ 'confirm by default (due to cc)' '
 826        save_confirm &&
 827        git config --unset sendemail.confirm &&
 828        test_confirm
 829'
 830
 831test_expect_success $PREREQ 'confirm by default (due to --compose)' '
 832        save_confirm &&
 833        git config --unset sendemail.confirm &&
 834        test_confirm --suppress-cc=all --compose
 835'
 836
 837test_expect_success $PREREQ 'confirm detects EOF (inform assumes y)' '
 838        save_confirm &&
 839        git config --unset sendemail.confirm &&
 840        rm -fr outdir &&
 841        git format-patch -2 -o outdir &&
 842        GIT_SEND_EMAIL_NOTTY=1 \
 843                git send-email \
 844                        --from="Example <nobody@example.com>" \
 845                        --to=nobody@example.com \
 846                        --smtp-server="$(pwd)/fake.sendmail" \
 847                        outdir/*.patch </dev/null
 848'
 849
 850test_expect_success $PREREQ 'confirm detects EOF (auto causes failure)' '
 851        save_confirm &&
 852        git config sendemail.confirm auto &&
 853        GIT_SEND_EMAIL_NOTTY=1 &&
 854        export GIT_SEND_EMAIL_NOTTY &&
 855                test_must_fail git send-email \
 856                        --from="Example <nobody@example.com>" \
 857                        --to=nobody@example.com \
 858                        --smtp-server="$(pwd)/fake.sendmail" \
 859                        $patches </dev/null
 860'
 861
 862test_expect_success $PREREQ 'confirm does not loop forever' '
 863        save_confirm &&
 864        git config sendemail.confirm auto &&
 865        GIT_SEND_EMAIL_NOTTY=1 &&
 866        export GIT_SEND_EMAIL_NOTTY &&
 867                yes "bogus" | test_must_fail git send-email \
 868                        --from="Example <nobody@example.com>" \
 869                        --to=nobody@example.com \
 870                        --smtp-server="$(pwd)/fake.sendmail" \
 871                        $patches
 872'
 873
 874test_expect_success $PREREQ 'utf8 Cc is rfc2047 encoded' '
 875        clean_fake_sendmail &&
 876        rm -fr outdir &&
 877        git format-patch -1 -o outdir --cc="àéìöú <utf8@example.com>" &&
 878        git send-email \
 879        --from="Example <nobody@example.com>" \
 880        --to=nobody@example.com \
 881        --smtp-server="$(pwd)/fake.sendmail" \
 882        outdir/*.patch &&
 883        grep "^ " msgtxt1 |
 884        grep "=?UTF-8?q?=C3=A0=C3=A9=C3=AC=C3=B6=C3=BA?= <utf8@example.com>"
 885'
 886
 887test_expect_success $PREREQ '--compose adds MIME for utf8 body' '
 888        clean_fake_sendmail &&
 889        write_script fake-editor-utf8 <<-\EOF &&
 890        echo "utf8 body: àéìöú" >>"$1"
 891        EOF
 892        GIT_EDITOR="\"$(pwd)/fake-editor-utf8\"" \
 893        git send-email \
 894                --compose --subject foo \
 895                --from="Example <nobody@example.com>" \
 896                --to=nobody@example.com \
 897                --smtp-server="$(pwd)/fake.sendmail" \
 898                $patches &&
 899        grep "^utf8 body" msgtxt1 &&
 900        grep "^Content-Type: text/plain; charset=UTF-8" msgtxt1
 901'
 902
 903test_expect_success $PREREQ '--compose respects user mime type' '
 904        clean_fake_sendmail &&
 905        write_script fake-editor-utf8-mime <<-\EOF &&
 906        cat >"$1" <<-\EOM
 907        MIME-Version: 1.0
 908        Content-Type: text/plain; charset=iso-8859-1
 909        Content-Transfer-Encoding: 8bit
 910        Subject: foo
 911
 912        utf8 body: àéìöú
 913        EOM
 914        EOF
 915        GIT_EDITOR="\"$(pwd)/fake-editor-utf8-mime\"" \
 916        git send-email \
 917                --compose --subject foo \
 918                --from="Example <nobody@example.com>" \
 919                --to=nobody@example.com \
 920                --smtp-server="$(pwd)/fake.sendmail" \
 921                $patches &&
 922        grep "^utf8 body" msgtxt1 &&
 923        grep "^Content-Type: text/plain; charset=iso-8859-1" msgtxt1 &&
 924        ! grep "^Content-Type: text/plain; charset=UTF-8" msgtxt1
 925'
 926
 927test_expect_success $PREREQ '--compose adds MIME for utf8 subject' '
 928        clean_fake_sendmail &&
 929        GIT_EDITOR="\"$(pwd)/fake-editor\"" \
 930        git send-email \
 931                --compose --subject utf8-sübjëct \
 932                --from="Example <nobody@example.com>" \
 933                --to=nobody@example.com \
 934                --smtp-server="$(pwd)/fake.sendmail" \
 935                $patches &&
 936        grep "^fake edit" msgtxt1 &&
 937        grep "^Subject: =?UTF-8?q?utf8-s=C3=BCbj=C3=ABct?=" msgtxt1
 938'
 939
 940test_expect_success $PREREQ 'utf8 author is correctly passed on' '
 941        clean_fake_sendmail &&
 942        test_commit weird_author &&
 943        test_when_finished "git reset --hard HEAD^" &&
 944        git commit --amend --author "Füñný Nâmé <odd_?=mail@example.com>" &&
 945        git format-patch --stdout -1 >funny_name.patch &&
 946        git send-email --from="Example <nobody@example.com>" \
 947                --to=nobody@example.com \
 948                --smtp-server="$(pwd)/fake.sendmail" \
 949                funny_name.patch &&
 950        grep "^From: Füñný Nâmé <odd_?=mail@example.com>" msgtxt1
 951'
 952
 953test_expect_success $PREREQ 'utf8 sender is not duplicated' '
 954        clean_fake_sendmail &&
 955        test_commit weird_sender &&
 956        test_when_finished "git reset --hard HEAD^" &&
 957        git commit --amend --author "Füñný Nâmé <odd_?=mail@example.com>" &&
 958        git format-patch --stdout -1 >funny_name.patch &&
 959        git send-email --from="Füñný Nâmé <odd_?=mail@example.com>" \
 960                --to=nobody@example.com \
 961                --smtp-server="$(pwd)/fake.sendmail" \
 962                funny_name.patch &&
 963        grep "^From: " msgtxt1 >msgfrom &&
 964        test_line_count = 1 msgfrom
 965'
 966
 967test_expect_success $PREREQ 'sendemail.composeencoding works' '
 968        clean_fake_sendmail &&
 969        git config sendemail.composeencoding iso-8859-1 &&
 970        write_script fake-editor-utf8 <<-\EOF &&
 971        echo "utf8 body: àéìöú" >>"$1"
 972        EOF
 973        GIT_EDITOR="\"$(pwd)/fake-editor-utf8\"" \
 974        git send-email \
 975                --compose --subject foo \
 976                --from="Example <nobody@example.com>" \
 977                --to=nobody@example.com \
 978                --smtp-server="$(pwd)/fake.sendmail" \
 979                $patches &&
 980        grep "^utf8 body" msgtxt1 &&
 981        grep "^Content-Type: text/plain; charset=iso-8859-1" msgtxt1
 982'
 983
 984test_expect_success $PREREQ '--compose-encoding works' '
 985        clean_fake_sendmail &&
 986        write_script fake-editor-utf8 <<-\EOF &&
 987        echo "utf8 body: àéìöú" >>"$1"
 988        EOF
 989        GIT_EDITOR="\"$(pwd)/fake-editor-utf8\"" \
 990        git send-email \
 991                --compose-encoding iso-8859-1 \
 992                --compose --subject foo \
 993                --from="Example <nobody@example.com>" \
 994                --to=nobody@example.com \
 995                --smtp-server="$(pwd)/fake.sendmail" \
 996                $patches &&
 997        grep "^utf8 body" msgtxt1 &&
 998        grep "^Content-Type: text/plain; charset=iso-8859-1" msgtxt1
 999'
1000
1001test_expect_success $PREREQ '--compose-encoding overrides sendemail.composeencoding' '
1002        clean_fake_sendmail &&
1003        git config sendemail.composeencoding iso-8859-1 &&
1004        write_script fake-editor-utf8 <<-\EOF &&
1005        echo "utf8 body: àéìöú" >>"$1"
1006        EOF
1007        GIT_EDITOR="\"$(pwd)/fake-editor-utf8\"" \
1008        git send-email \
1009                --compose-encoding iso-8859-2 \
1010                --compose --subject foo \
1011                --from="Example <nobody@example.com>" \
1012                --to=nobody@example.com \
1013                --smtp-server="$(pwd)/fake.sendmail" \
1014                $patches &&
1015        grep "^utf8 body" msgtxt1 &&
1016        grep "^Content-Type: text/plain; charset=iso-8859-2" msgtxt1
1017'
1018
1019test_expect_success $PREREQ '--compose-encoding adds correct MIME for subject' '
1020        clean_fake_sendmail &&
1021        GIT_EDITOR="\"$(pwd)/fake-editor\"" \
1022        git send-email \
1023                --compose-encoding iso-8859-2 \
1024                --compose --subject utf8-sübjëct \
1025                --from="Example <nobody@example.com>" \
1026                --to=nobody@example.com \
1027                --smtp-server="$(pwd)/fake.sendmail" \
1028                $patches &&
1029        grep "^fake edit" msgtxt1 &&
1030        grep "^Subject: =?iso-8859-2?q?utf8-s=C3=BCbj=C3=ABct?=" msgtxt1
1031'
1032
1033test_expect_success $PREREQ 'detects ambiguous reference/file conflict' '
1034        echo master >master &&
1035        git add master &&
1036        git commit -m"add master" &&
1037        test_must_fail git send-email --dry-run master 2>errors &&
1038        grep disambiguate errors
1039'
1040
1041test_expect_success $PREREQ 'feed two files' '
1042        rm -fr outdir &&
1043        git format-patch -2 -o outdir &&
1044        git send-email \
1045                --dry-run \
1046                --from="Example <nobody@example.com>" \
1047                --to=nobody@example.com \
1048                outdir/000?-*.patch 2>errors >out &&
1049        grep "^Subject: " out >subjects &&
1050        test "z$(sed -n -e 1p subjects)" = "zSubject: [PATCH 1/2] Second." &&
1051        test "z$(sed -n -e 2p subjects)" = "zSubject: [PATCH 2/2] add master"
1052'
1053
1054test_expect_success $PREREQ 'in-reply-to but no threading' '
1055        git send-email \
1056                --dry-run \
1057                --from="Example <nobody@example.com>" \
1058                --to=nobody@example.com \
1059                --in-reply-to="<in-reply-id@example.com>" \
1060                --no-thread \
1061                $patches |
1062        grep "In-Reply-To: <in-reply-id@example.com>"
1063'
1064
1065test_expect_success $PREREQ 'no in-reply-to and no threading' '
1066        git send-email \
1067                --dry-run \
1068                --from="Example <nobody@example.com>" \
1069                --to=nobody@example.com \
1070                --no-thread \
1071                $patches $patches >stdout &&
1072        ! grep "In-Reply-To: " stdout
1073'
1074
1075test_expect_success $PREREQ 'threading but no chain-reply-to' '
1076        git send-email \
1077                --dry-run \
1078                --from="Example <nobody@example.com>" \
1079                --to=nobody@example.com \
1080                --thread \
1081                --no-chain-reply-to \
1082                $patches $patches >stdout &&
1083        grep "In-Reply-To: " stdout
1084'
1085
1086test_expect_success $PREREQ 'sendemail.to works' '
1087        git config --replace-all sendemail.to "Somebody <somebody@ex.com>" &&
1088        git send-email \
1089                --dry-run \
1090                --from="Example <nobody@example.com>" \
1091                $patches $patches >stdout &&
1092        grep "To: Somebody <somebody@ex.com>" stdout
1093'
1094
1095test_expect_success $PREREQ '--no-to overrides sendemail.to' '
1096        git send-email \
1097                --dry-run \
1098                --from="Example <nobody@example.com>" \
1099                --no-to \
1100                --to=nobody@example.com \
1101                $patches $patches >stdout &&
1102        grep "To: nobody@example.com" stdout &&
1103        ! grep "To: Somebody <somebody@ex.com>" stdout
1104'
1105
1106test_expect_success $PREREQ 'sendemail.cc works' '
1107        git config --replace-all sendemail.cc "Somebody <somebody@ex.com>" &&
1108        git send-email \
1109                --dry-run \
1110                --from="Example <nobody@example.com>" \
1111                --to=nobody@example.com \
1112                $patches $patches >stdout &&
1113        grep "Cc: Somebody <somebody@ex.com>" stdout
1114'
1115
1116test_expect_success $PREREQ '--no-cc overrides sendemail.cc' '
1117        git send-email \
1118                --dry-run \
1119                --from="Example <nobody@example.com>" \
1120                --no-cc \
1121                --cc=bodies@example.com \
1122                --to=nobody@example.com \
1123                $patches $patches >stdout &&
1124        grep "Cc: bodies@example.com" stdout &&
1125        ! grep "Cc: Somebody <somebody@ex.com>" stdout
1126'
1127
1128test_expect_success $PREREQ 'sendemail.bcc works' '
1129        git config --replace-all sendemail.bcc "Other <other@ex.com>" &&
1130        git send-email \
1131                --dry-run \
1132                --from="Example <nobody@example.com>" \
1133                --to=nobody@example.com \
1134                --smtp-server relay.example.com \
1135                $patches $patches >stdout &&
1136        grep "RCPT TO:<other@ex.com>" stdout
1137'
1138
1139test_expect_success $PREREQ '--no-bcc overrides sendemail.bcc' '
1140        git send-email \
1141                --dry-run \
1142                --from="Example <nobody@example.com>" \
1143                --no-bcc \
1144                --bcc=bodies@example.com \
1145                --to=nobody@example.com \
1146                --smtp-server relay.example.com \
1147                $patches $patches >stdout &&
1148        grep "RCPT TO:<bodies@example.com>" stdout &&
1149        ! grep "RCPT TO:<other@ex.com>" stdout
1150'
1151
1152test_expect_success $PREREQ 'patches To headers are used by default' '
1153        patch=`git format-patch -1 --to="bodies@example.com"` &&
1154        test_when_finished "rm $patch" &&
1155        git send-email \
1156                --dry-run \
1157                --from="Example <nobody@example.com>" \
1158                --smtp-server relay.example.com \
1159                $patch >stdout &&
1160        grep "RCPT TO:<bodies@example.com>" stdout
1161'
1162
1163test_expect_success $PREREQ 'patches To headers are appended to' '
1164        patch=`git format-patch -1 --to="bodies@example.com"` &&
1165        test_when_finished "rm $patch" &&
1166        git send-email \
1167                --dry-run \
1168                --from="Example <nobody@example.com>" \
1169                --to=nobody@example.com \
1170                --smtp-server relay.example.com \
1171                $patch >stdout &&
1172        grep "RCPT TO:<bodies@example.com>" stdout &&
1173        grep "RCPT TO:<nobody@example.com>" stdout
1174'
1175
1176test_expect_success $PREREQ 'To headers from files reset each patch' '
1177        patch1=`git format-patch -1 --to="bodies@example.com"` &&
1178        patch2=`git format-patch -1 --to="other@example.com" HEAD~` &&
1179        test_when_finished "rm $patch1 && rm $patch2" &&
1180        git send-email \
1181                --dry-run \
1182                --from="Example <nobody@example.com>" \
1183                --to="nobody@example.com" \
1184                --smtp-server relay.example.com \
1185                $patch1 $patch2 >stdout &&
1186        test $(grep -c "RCPT TO:<bodies@example.com>" stdout) = 1 &&
1187        test $(grep -c "RCPT TO:<nobody@example.com>" stdout) = 2 &&
1188        test $(grep -c "RCPT TO:<other@example.com>" stdout) = 1
1189'
1190
1191test_expect_success $PREREQ 'setup expect' '
1192cat >email-using-8bit <<\EOF
1193From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
1194Message-Id: <bogus-message-id@example.com>
1195From: author@example.com
1196Date: Sat, 12 Jun 2010 15:53:58 +0200
1197Subject: subject goes here
1198
1199Dieser deutsche Text enthält einen Umlaut!
1200EOF
1201'
1202
1203test_expect_success $PREREQ 'setup expect' '
1204        echo "Subject: subject goes here" >expected
1205'
1206
1207test_expect_success $PREREQ 'ASCII subject is not RFC2047 quoted' '
1208        clean_fake_sendmail &&
1209        echo bogus |
1210        git send-email --from=author@example.com --to=nobody@example.com \
1211                        --smtp-server="$(pwd)/fake.sendmail" \
1212                        --8bit-encoding=UTF-8 \
1213                        email-using-8bit >stdout &&
1214        grep "Subject" msgtxt1 >actual &&
1215        test_cmp expected actual
1216'
1217
1218test_expect_success $PREREQ 'setup expect' '
1219        cat >content-type-decl <<-\EOF
1220        MIME-Version: 1.0
1221        Content-Type: text/plain; charset=UTF-8
1222        Content-Transfer-Encoding: 8bit
1223        EOF
1224'
1225
1226test_expect_success $PREREQ 'asks about and fixes 8bit encodings' '
1227        clean_fake_sendmail &&
1228        echo |
1229        git send-email --from=author@example.com --to=nobody@example.com \
1230                        --smtp-server="$(pwd)/fake.sendmail" \
1231                        email-using-8bit >stdout &&
1232        grep "do not declare a Content-Transfer-Encoding" stdout &&
1233        grep email-using-8bit stdout &&
1234        grep "Which 8bit encoding" stdout &&
1235        egrep "Content|MIME" msgtxt1 >actual &&
1236        test_cmp actual content-type-decl
1237'
1238
1239test_expect_success $PREREQ 'sendemail.8bitEncoding works' '
1240        clean_fake_sendmail &&
1241        git config sendemail.assume8bitEncoding UTF-8 &&
1242        echo bogus |
1243        git send-email --from=author@example.com --to=nobody@example.com \
1244                        --smtp-server="$(pwd)/fake.sendmail" \
1245                        email-using-8bit >stdout &&
1246        egrep "Content|MIME" msgtxt1 >actual &&
1247        test_cmp actual content-type-decl
1248'
1249
1250test_expect_success $PREREQ '--8bit-encoding overrides sendemail.8bitEncoding' '
1251        clean_fake_sendmail &&
1252        git config sendemail.assume8bitEncoding "bogus too" &&
1253        echo bogus |
1254        git send-email --from=author@example.com --to=nobody@example.com \
1255                        --smtp-server="$(pwd)/fake.sendmail" \
1256                        --8bit-encoding=UTF-8 \
1257                        email-using-8bit >stdout &&
1258        egrep "Content|MIME" msgtxt1 >actual &&
1259        test_cmp actual content-type-decl
1260'
1261
1262test_expect_success $PREREQ 'setup expect' '
1263        cat >email-using-8bit <<-\EOF
1264        From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
1265        Message-Id: <bogus-message-id@example.com>
1266        From: author@example.com
1267        Date: Sat, 12 Jun 2010 15:53:58 +0200
1268        Subject: Dieser Betreff enthält auch einen Umlaut!
1269
1270        Nothing to see here.
1271        EOF
1272'
1273
1274test_expect_success $PREREQ 'setup expect' '
1275        cat >expected <<-\EOF
1276        Subject: =?UTF-8?q?Dieser=20Betreff=20enth=C3=A4lt=20auch=20einen=20Umlaut!?=
1277        EOF
1278'
1279
1280test_expect_success $PREREQ '--8bit-encoding also treats subject' '
1281        clean_fake_sendmail &&
1282        echo bogus |
1283        git send-email --from=author@example.com --to=nobody@example.com \
1284                        --smtp-server="$(pwd)/fake.sendmail" \
1285                        --8bit-encoding=UTF-8 \
1286                        email-using-8bit >stdout &&
1287        grep "Subject" msgtxt1 >actual &&
1288        test_cmp expected actual
1289'
1290
1291test_expect_success $PREREQ 'setup expect' '
1292        cat >email-using-8bit <<-\EOF
1293        From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
1294        Message-Id: <bogus-message-id@example.com>
1295        From: A U Thor <author@example.com>
1296        Date: Sat, 12 Jun 2010 15:53:58 +0200
1297        Content-Type: text/plain; charset=UTF-8
1298        Subject: Nothing to see here.
1299
1300        Dieser Betreff enthält auch einen Umlaut!
1301        EOF
1302'
1303
1304test_expect_success $PREREQ 'sendemail.transferencoding=7bit fails on 8bit data' '
1305        clean_fake_sendmail &&
1306        git config sendemail.transferEncoding 7bit &&
1307        test_must_fail git send-email \
1308                --transfer-encoding=7bit \
1309                --smtp-server="$(pwd)/fake.sendmail" \
1310                email-using-8bit \
1311                2>errors >out &&
1312        grep "cannot send message as 7bit" errors &&
1313        test -z "$(ls msgtxt*)"
1314'
1315
1316test_expect_success $PREREQ '--transfer-encoding overrides sendemail.transferEncoding' '
1317        clean_fake_sendmail &&
1318        git config sendemail.transferEncoding 8bit &&
1319        test_must_fail git send-email \
1320                --transfer-encoding=7bit \
1321                --smtp-server="$(pwd)/fake.sendmail" \
1322                email-using-8bit \
1323                2>errors >out &&
1324        grep "cannot send message as 7bit" errors &&
1325        test -z "$(ls msgtxt*)"
1326'
1327
1328test_expect_success $PREREQ 'sendemail.transferencoding=8bit' '
1329        clean_fake_sendmail &&
1330        git send-email \
1331                --transfer-encoding=8bit \
1332                --smtp-server="$(pwd)/fake.sendmail" \
1333                email-using-8bit \
1334                2>errors >out &&
1335        sed '1,/^$/d' msgtxt1 >actual &&
1336        sed '1,/^$/d' email-using-8bit >expected &&
1337        test_cmp expected actual
1338'
1339
1340test_expect_success $PREREQ 'setup expect' '
1341        cat >expected <<-\EOF
1342        Dieser Betreff enth=C3=A4lt auch einen Umlaut!
1343        EOF
1344'
1345
1346test_expect_success $PREREQ '8-bit and sendemail.transferencoding=quoted-printable' '
1347        clean_fake_sendmail &&
1348        git send-email \
1349                --transfer-encoding=quoted-printable \
1350                --smtp-server="$(pwd)/fake.sendmail" \
1351                email-using-8bit \
1352                2>errors >out &&
1353        sed '1,/^$/d' msgtxt1 >actual &&
1354        test_cmp expected actual
1355'
1356
1357test_expect_success $PREREQ 'setup expect' '
1358        cat >expected <<-\EOF
1359        RGllc2VyIEJldHJlZmYgZW50aMOkbHQgYXVjaCBlaW5lbiBVbWxhdXQhCg==
1360        EOF
1361'
1362
1363test_expect_success $PREREQ '8-bit and sendemail.transferencoding=base64' '
1364        clean_fake_sendmail &&
1365        git send-email \
1366                --transfer-encoding=base64 \
1367                --smtp-server="$(pwd)/fake.sendmail" \
1368                email-using-8bit \
1369                2>errors >out &&
1370        sed '1,/^$/d' msgtxt1 >actual &&
1371        test_cmp expected actual
1372'
1373
1374test_expect_success $PREREQ 'setup expect' '
1375        cat >email-using-qp <<-\EOF
1376        From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
1377        Message-Id: <bogus-message-id@example.com>
1378        From: A U Thor <author@example.com>
1379        Date: Sat, 12 Jun 2010 15:53:58 +0200
1380        MIME-Version: 1.0
1381        Content-Transfer-Encoding: quoted-printable
1382        Content-Type: text/plain; charset=UTF-8
1383        Subject: Nothing to see here.
1384
1385        Dieser Betreff enth=C3=A4lt auch einen Umlaut!
1386        EOF
1387'
1388
1389test_expect_success $PREREQ 'convert from quoted-printable to base64' '
1390        clean_fake_sendmail &&
1391        git send-email \
1392                --transfer-encoding=base64 \
1393                --smtp-server="$(pwd)/fake.sendmail" \
1394                email-using-qp \
1395                2>errors >out &&
1396        sed '1,/^$/d' msgtxt1 >actual &&
1397        test_cmp expected actual
1398'
1399
1400test_expect_success $PREREQ 'setup expect' "
1401tr -d '\\015' | tr '%' '\\015' >email-using-crlf <<EOF
1402From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
1403Message-Id: <bogus-message-id@example.com>
1404From: A U Thor <author@example.com>
1405Date: Sat, 12 Jun 2010 15:53:58 +0200
1406Content-Type: text/plain; charset=UTF-8
1407Subject: Nothing to see here.
1408
1409Look, I have a CRLF and an = sign!%
1410EOF
1411"
1412
1413test_expect_success $PREREQ 'setup expect' '
1414        cat >expected <<-\EOF
1415        Look, I have a CRLF and an =3D sign!=0D
1416        EOF
1417'
1418
1419test_expect_success $PREREQ 'CRLF and sendemail.transferencoding=quoted-printable' '
1420        clean_fake_sendmail &&
1421        git send-email \
1422                --transfer-encoding=quoted-printable \
1423                --smtp-server="$(pwd)/fake.sendmail" \
1424                email-using-crlf \
1425                2>errors >out &&
1426        sed '1,/^$/d' msgtxt1 >actual &&
1427        test_cmp expected actual
1428'
1429
1430test_expect_success $PREREQ 'setup expect' '
1431        cat >expected <<-\EOF
1432        TG9vaywgSSBoYXZlIGEgQ1JMRiBhbmQgYW4gPSBzaWduIQ0K
1433        EOF
1434'
1435
1436test_expect_success $PREREQ 'CRLF and sendemail.transferencoding=base64' '
1437        clean_fake_sendmail &&
1438        git send-email \
1439                --transfer-encoding=base64 \
1440                --smtp-server="$(pwd)/fake.sendmail" \
1441                email-using-crlf \
1442                2>errors >out &&
1443        sed '1,/^$/d' msgtxt1 >actual &&
1444        test_cmp expected actual
1445'
1446
1447
1448# Note that the patches in this test are deliberately out of order; we
1449# want to make sure it works even if the cover-letter is not in the
1450# first mail.
1451test_expect_success $PREREQ 'refusing to send cover letter template' '
1452        clean_fake_sendmail &&
1453        rm -fr outdir &&
1454        git format-patch --cover-letter -2 -o outdir &&
1455        test_must_fail git send-email \
1456                --from="Example <nobody@example.com>" \
1457                --to=nobody@example.com \
1458                --smtp-server="$(pwd)/fake.sendmail" \
1459                outdir/0002-*.patch \
1460                outdir/0000-*.patch \
1461                outdir/0001-*.patch \
1462                2>errors >out &&
1463        grep "SUBJECT HERE" errors &&
1464        test -z "$(ls msgtxt*)"
1465'
1466
1467test_expect_success $PREREQ '--force sends cover letter template anyway' '
1468        clean_fake_sendmail &&
1469        rm -fr outdir &&
1470        git format-patch --cover-letter -2 -o outdir &&
1471        git send-email \
1472                --force \
1473                --from="Example <nobody@example.com>" \
1474                --to=nobody@example.com \
1475                --smtp-server="$(pwd)/fake.sendmail" \
1476                outdir/0002-*.patch \
1477                outdir/0000-*.patch \
1478                outdir/0001-*.patch \
1479                2>errors >out &&
1480        ! grep "SUBJECT HERE" errors &&
1481        test -n "$(ls msgtxt*)"
1482'
1483
1484test_cover_addresses () {
1485        header="$1"
1486        shift
1487        clean_fake_sendmail &&
1488        rm -fr outdir &&
1489        git format-patch --cover-letter -2 -o outdir &&
1490        cover=`echo outdir/0000-*.patch` &&
1491        mv $cover cover-to-edit.patch &&
1492        perl -pe "s/^From:/$header: extra\@address.com\nFrom:/" cover-to-edit.patch >"$cover" &&
1493        git send-email \
1494                --force \
1495                --from="Example <nobody@example.com>" \
1496                --no-to --no-cc \
1497                "$@" \
1498                --smtp-server="$(pwd)/fake.sendmail" \
1499                outdir/0000-*.patch \
1500                outdir/0001-*.patch \
1501                outdir/0002-*.patch \
1502                2>errors >out &&
1503        grep "^$header: extra@address.com" msgtxt1 >to1 &&
1504        grep "^$header: extra@address.com" msgtxt2 >to2 &&
1505        grep "^$header: extra@address.com" msgtxt3 >to3 &&
1506        test_line_count = 1 to1 &&
1507        test_line_count = 1 to2 &&
1508        test_line_count = 1 to3
1509}
1510
1511test_expect_success $PREREQ 'to-cover adds To to all mail' '
1512        test_cover_addresses "To" --to-cover
1513'
1514
1515test_expect_success $PREREQ 'cc-cover adds Cc to all mail' '
1516        test_cover_addresses "Cc" --cc-cover
1517'
1518
1519test_expect_success $PREREQ 'tocover adds To to all mail' '
1520        test_config sendemail.tocover true &&
1521        test_cover_addresses "To"
1522'
1523
1524test_expect_success $PREREQ 'cccover adds Cc to all mail' '
1525        test_config sendemail.cccover true &&
1526        test_cover_addresses "Cc"
1527'
1528
1529test_expect_success $PREREQ 'sendemail.aliasfiletype=mailrc' '
1530        clean_fake_sendmail &&
1531        echo "alias sbd  somebody@example.org" >.mailrc &&
1532        git config --replace-all sendemail.aliasesfile "$(pwd)/.mailrc" &&
1533        git config sendemail.aliasfiletype mailrc &&
1534        git send-email \
1535                --from="Example <nobody@example.com>" \
1536                --to=sbd \
1537                --smtp-server="$(pwd)/fake.sendmail" \
1538                outdir/0001-*.patch \
1539                2>errors >out &&
1540        grep "^!somebody@example\.org!$" commandline1
1541'
1542
1543test_expect_success $PREREQ 'sendemail.aliasfile=~/.mailrc' '
1544        clean_fake_sendmail &&
1545        echo "alias sbd  someone@example.org" >~/.mailrc &&
1546        git config --replace-all sendemail.aliasesfile "~/.mailrc" &&
1547        git config sendemail.aliasfiletype mailrc &&
1548        git send-email \
1549                --from="Example <nobody@example.com>" \
1550                --to=sbd \
1551                --smtp-server="$(pwd)/fake.sendmail" \
1552                outdir/0001-*.patch \
1553                2>errors >out &&
1554        grep "^!someone@example\.org!$" commandline1
1555'
1556
1557do_xmailer_test () {
1558        expected=$1 params=$2 &&
1559        git format-patch -1 &&
1560        git send-email \
1561                --from="Example <nobody@example.com>" \
1562                --to=someone@example.com \
1563                --smtp-server="$(pwd)/fake.sendmail" \
1564                $params \
1565                0001-*.patch \
1566                2>errors >out &&
1567        { grep '^X-Mailer:' out || :; } >mailer &&
1568        test_line_count = $expected mailer
1569}
1570
1571test_expect_success $PREREQ '--[no-]xmailer without any configuration' '
1572        do_xmailer_test 1 "--xmailer" &&
1573        do_xmailer_test 0 "--no-xmailer"
1574'
1575
1576test_expect_success $PREREQ '--[no-]xmailer with sendemail.xmailer=true' '
1577        test_config sendemail.xmailer true &&
1578        do_xmailer_test 1 "" &&
1579        do_xmailer_test 0 "--no-xmailer" &&
1580        do_xmailer_test 1 "--xmailer"
1581'
1582
1583test_expect_success $PREREQ '--[no-]xmailer with sendemail.xmailer=false' '
1584        test_config sendemail.xmailer false &&
1585        do_xmailer_test 0 "" &&
1586        do_xmailer_test 0 "--no-xmailer" &&
1587        do_xmailer_test 1 "--xmailer"
1588'
1589
1590test_done