t / t9001-send-email.shon commit t9001: send-email interation with --in-reply-to and --chain-reply-to (54aae5e)
   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 \
  10    'prepare reference tree' \
  11    'echo "1A quick brown fox jumps over the" >file &&
  12     echo "lazy dog" >>file &&
  13     git add file &&
  14     GIT_AUTHOR_NAME="A" git commit -a -m "Initial."'
  15
  16test_expect_success $PREREQ \
  17    'Setup helper tool' \
  18    '(echo "#!$SHELL_PATH"
  19      echo shift
  20      echo output=1
  21      echo "while test -f commandline\$output; do output=\$((\$output+1)); done"
  22      echo for a
  23      echo do
  24      echo "  echo \"!\$a!\""
  25      echo "done >commandline\$output"
  26      echo "cat > msgtxt\$output"
  27      ) >fake.sendmail &&
  28     chmod +x ./fake.sendmail &&
  29     git add fake.sendmail &&
  30     GIT_AUTHOR_NAME="A" git commit -a -m "Second."'
  31
  32clean_fake_sendmail() {
  33        rm -f commandline* msgtxt*
  34}
  35
  36test_expect_success $PREREQ 'Extract patches' '
  37    patches=`git format-patch -s --cc="One <one@example.com>" --cc=two@example.com -n HEAD^1`
  38'
  39
  40# Test no confirm early to ensure remaining tests will not hang
  41test_no_confirm () {
  42        rm -f no_confirm_okay
  43        echo n | \
  44                GIT_SEND_EMAIL_NOTTY=1 \
  45                git send-email \
  46                --from="Example <from@example.com>" \
  47                --to=nobody@example.com \
  48                --smtp-server="$(pwd)/fake.sendmail" \
  49                $@ \
  50                $patches > stdout &&
  51                test_must_fail grep "Send this email" stdout &&
  52                > no_confirm_okay
  53}
  54
  55# Exit immediately to prevent hang if a no-confirm test fails
  56check_no_confirm () {
  57        if ! test -f no_confirm_okay
  58        then
  59                say 'confirm test failed; skipping remaining tests to prevent hanging'
  60                PREREQ="$PREREQ,CHECK_NO_CONFIRM"
  61        fi
  62        return 0
  63}
  64
  65test_expect_success $PREREQ 'No confirm with --suppress-cc' '
  66        test_no_confirm --suppress-cc=sob &&
  67        check_no_confirm
  68'
  69
  70
  71test_expect_success $PREREQ 'No confirm with --confirm=never' '
  72        test_no_confirm --confirm=never &&
  73        check_no_confirm
  74'
  75
  76# leave sendemail.confirm set to never after this so that none of the
  77# remaining tests prompt unintentionally.
  78test_expect_success $PREREQ 'No confirm with sendemail.confirm=never' '
  79        git config sendemail.confirm never &&
  80        test_no_confirm --compose --subject=foo &&
  81        check_no_confirm
  82'
  83
  84test_expect_success $PREREQ 'Send patches' '
  85     git send-email --suppress-cc=sob --from="Example <nobody@example.com>" --to=nobody@example.com --smtp-server="$(pwd)/fake.sendmail" $patches 2>errors
  86'
  87
  88test_expect_success $PREREQ 'setup expect' '
  89cat >expected <<\EOF
  90!nobody@example.com!
  91!author@example.com!
  92!one@example.com!
  93!two@example.com!
  94EOF
  95'
  96
  97test_expect_success $PREREQ \
  98    'Verify commandline' \
  99    'test_cmp expected commandline1'
 100
 101test_expect_success $PREREQ 'Send patches with --envelope-sender' '
 102    clean_fake_sendmail &&
 103     git send-email --envelope-sender="Patch Contributer <patch@example.com>" --suppress-cc=sob --from="Example <nobody@example.com>" --to=nobody@example.com --smtp-server="$(pwd)/fake.sendmail" $patches 2>errors
 104'
 105
 106test_expect_success $PREREQ 'setup expect' '
 107cat >expected <<\EOF
 108!patch@example.com!
 109!-i!
 110!nobody@example.com!
 111!author@example.com!
 112!one@example.com!
 113!two@example.com!
 114EOF
 115'
 116
 117test_expect_success $PREREQ \
 118    'Verify commandline' \
 119    'test_cmp expected commandline1'
 120
 121test_expect_success $PREREQ 'Send patches with --envelope-sender=auto' '
 122    clean_fake_sendmail &&
 123     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
 124'
 125
 126test_expect_success $PREREQ 'setup expect' '
 127cat >expected <<\EOF
 128!nobody@example.com!
 129!-i!
 130!nobody@example.com!
 131!author@example.com!
 132!one@example.com!
 133!two@example.com!
 134EOF
 135'
 136
 137test_expect_success $PREREQ \
 138    'Verify commandline' \
 139    'test_cmp expected commandline1'
 140
 141test_expect_success $PREREQ 'setup expect' "
 142cat >expected-show-all-headers <<\EOF
 1430001-Second.patch
 144(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 145(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 146(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 147Dry-OK. Log says:
 148Server: relay.example.com
 149MAIL FROM:<from@example.com>
 150RCPT TO:<to@example.com>
 151RCPT TO:<cc@example.com>
 152RCPT TO:<author@example.com>
 153RCPT TO:<one@example.com>
 154RCPT TO:<two@example.com>
 155RCPT TO:<bcc@example.com>
 156From: Example <from@example.com>
 157To: to@example.com
 158Cc: cc@example.com,
 159        A <author@example.com>,
 160        One <one@example.com>,
 161        two@example.com
 162Subject: [PATCH 1/1] Second.
 163Date: DATE-STRING
 164Message-Id: MESSAGE-ID-STRING
 165X-Mailer: X-MAILER-STRING
 166In-Reply-To: <unique-message-id@example.com>
 167References: <unique-message-id@example.com>
 168
 169Result: OK
 170EOF
 171"
 172
 173test_expect_success $PREREQ 'Show all headers' '
 174        git send-email \
 175                --dry-run \
 176                --suppress-cc=sob \
 177                --from="Example <from@example.com>" \
 178                --to=to@example.com \
 179                --cc=cc@example.com \
 180                --bcc=bcc@example.com \
 181                --in-reply-to="<unique-message-id@example.com>" \
 182                --smtp-server relay.example.com \
 183                $patches |
 184        sed     -e "s/^\(Date:\).*/\1 DATE-STRING/" \
 185                -e "s/^\(Message-Id:\).*/\1 MESSAGE-ID-STRING/" \
 186                -e "s/^\(X-Mailer:\).*/\1 X-MAILER-STRING/" \
 187                >actual-show-all-headers &&
 188        test_cmp expected-show-all-headers actual-show-all-headers
 189'
 190
 191test_expect_success $PREREQ 'Prompting works' '
 192        clean_fake_sendmail &&
 193        (echo "Example <from@example.com>"
 194         echo "to@example.com"
 195         echo ""
 196        ) | GIT_SEND_EMAIL_NOTTY=1 git send-email \
 197                --smtp-server="$(pwd)/fake.sendmail" \
 198                $patches \
 199                2>errors &&
 200                grep "^From: Example <from@example.com>\$" msgtxt1 &&
 201                grep "^To: to@example.com\$" msgtxt1
 202'
 203
 204test_expect_success $PREREQ 'tocmd works' '
 205        clean_fake_sendmail &&
 206        cp $patches tocmd.patch &&
 207        echo tocmd--tocmd@example.com >>tocmd.patch &&
 208        {
 209          echo "#!$SHELL_PATH"
 210          echo sed -n -e s/^tocmd--//p \"\$1\"
 211        } > tocmd-sed &&
 212        chmod +x tocmd-sed &&
 213        git send-email \
 214                --from="Example <nobody@example.com>" \
 215                --to-cmd=./tocmd-sed \
 216                --smtp-server="$(pwd)/fake.sendmail" \
 217                tocmd.patch \
 218                &&
 219        grep "^To: tocmd@example.com" msgtxt1
 220'
 221
 222test_expect_success $PREREQ 'cccmd works' '
 223        clean_fake_sendmail &&
 224        cp $patches cccmd.patch &&
 225        echo "cccmd--  cccmd@example.com" >>cccmd.patch &&
 226        {
 227          echo "#!$SHELL_PATH"
 228          echo sed -n -e s/^cccmd--//p \"\$1\"
 229        } > cccmd-sed &&
 230        chmod +x cccmd-sed &&
 231        git send-email \
 232                --from="Example <nobody@example.com>" \
 233                --to=nobody@example.com \
 234                --cc-cmd=./cccmd-sed \
 235                --smtp-server="$(pwd)/fake.sendmail" \
 236                cccmd.patch \
 237                &&
 238        grep "^ cccmd@example.com" msgtxt1
 239'
 240
 241test_expect_success $PREREQ 'reject long lines' '
 242        z8=zzzzzzzz &&
 243        z64=$z8$z8$z8$z8$z8$z8$z8$z8 &&
 244        z512=$z64$z64$z64$z64$z64$z64$z64$z64 &&
 245        clean_fake_sendmail &&
 246        cp $patches longline.patch &&
 247        echo $z512$z512 >>longline.patch &&
 248        test_must_fail git send-email \
 249                --from="Example <nobody@example.com>" \
 250                --to=nobody@example.com \
 251                --smtp-server="$(pwd)/fake.sendmail" \
 252                $patches longline.patch \
 253                2>errors &&
 254        grep longline.patch errors
 255'
 256
 257test_expect_success $PREREQ 'no patch was sent' '
 258        ! test -e commandline1
 259'
 260
 261test_expect_success $PREREQ 'Author From: in message body' '
 262        clean_fake_sendmail &&
 263        git send-email \
 264                --from="Example <nobody@example.com>" \
 265                --to=nobody@example.com \
 266                --smtp-server="$(pwd)/fake.sendmail" \
 267                $patches &&
 268        sed "1,/^\$/d" < msgtxt1 > msgbody1
 269        grep "From: A <author@example.com>" msgbody1
 270'
 271
 272test_expect_success $PREREQ 'Author From: not in message body' '
 273        clean_fake_sendmail &&
 274        git send-email \
 275                --from="A <author@example.com>" \
 276                --to=nobody@example.com \
 277                --smtp-server="$(pwd)/fake.sendmail" \
 278                $patches &&
 279        sed "1,/^\$/d" < msgtxt1 > msgbody1
 280        ! grep "From: A <author@example.com>" msgbody1
 281'
 282
 283test_expect_success $PREREQ 'allow long lines with --no-validate' '
 284        git send-email \
 285                --from="Example <nobody@example.com>" \
 286                --to=nobody@example.com \
 287                --smtp-server="$(pwd)/fake.sendmail" \
 288                --novalidate \
 289                $patches longline.patch \
 290                2>errors
 291'
 292
 293test_expect_success $PREREQ 'Invalid In-Reply-To' '
 294        clean_fake_sendmail &&
 295        git send-email \
 296                --from="Example <nobody@example.com>" \
 297                --to=nobody@example.com \
 298                --in-reply-to=" " \
 299                --smtp-server="$(pwd)/fake.sendmail" \
 300                $patches \
 301                2>errors
 302        ! grep "^In-Reply-To: < *>" msgtxt1
 303'
 304
 305test_expect_success $PREREQ 'Valid In-Reply-To when prompting' '
 306        clean_fake_sendmail &&
 307        (echo "From Example <from@example.com>"
 308         echo "To Example <to@example.com>"
 309         echo ""
 310        ) | env GIT_SEND_EMAIL_NOTTY=1 git send-email \
 311                --smtp-server="$(pwd)/fake.sendmail" \
 312                $patches 2>errors &&
 313        ! grep "^In-Reply-To: < *>" msgtxt1
 314'
 315
 316test_expect_success $PREREQ 'In-Reply-To without --chain-reply-to' '
 317        clean_fake_sendmail &&
 318        echo "<unique-message-id@example.com>" >expect &&
 319        git send-email \
 320                --from="Example <nobody@example.com>" \
 321                --to=nobody@example.com \
 322                --no-chain-reply-to \
 323                --in-reply-to="$(cat expect)" \
 324                --smtp-server="$(pwd)/fake.sendmail" \
 325                $patches $patches $patches \
 326                2>errors &&
 327        # All the messages are replies to --in-reply-to
 328        sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt1 >actual &&
 329        test_cmp expect actual &&
 330        sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt2 >actual &&
 331        test_cmp expect actual &&
 332        sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt3 >actual &&
 333        test_cmp expect actual
 334'
 335
 336test_expect_success $PREREQ 'In-Reply-To with --chain-reply-to' '
 337        clean_fake_sendmail &&
 338        echo "<unique-message-id@example.com>" >expect &&
 339        git send-email \
 340                --from="Example <nobody@example.com>" \
 341                --to=nobody@example.com \
 342                --chain-reply-to \
 343                --in-reply-to="$(cat expect)" \
 344                --smtp-server="$(pwd)/fake.sendmail" \
 345                $patches $patches $patches \
 346                2>errors &&
 347        sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt1 >actual &&
 348        test_cmp expect actual &&
 349        sed -n -e "s/^Message-Id: *\(.*\)/\1/p" msgtxt1 >expect &&
 350        sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt2 >actual &&
 351        test_cmp expect actual &&
 352        sed -n -e "s/^Message-Id: *\(.*\)/\1/p" msgtxt2 >expect &&
 353        sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt3 >actual &&
 354        test_cmp expect actual
 355'
 356
 357test_expect_success $PREREQ 'setup fake editor' '
 358        (echo "#!$SHELL_PATH" &&
 359         echo "echo fake edit >>\"\$1\""
 360        ) >fake-editor &&
 361        chmod +x fake-editor
 362'
 363
 364test_set_editor "$(pwd)/fake-editor"
 365
 366test_expect_success $PREREQ '--compose works' '
 367        clean_fake_sendmail &&
 368        git send-email \
 369        --compose --subject foo \
 370        --from="Example <nobody@example.com>" \
 371        --to=nobody@example.com \
 372        --smtp-server="$(pwd)/fake.sendmail" \
 373        $patches \
 374        2>errors
 375'
 376
 377test_expect_success $PREREQ 'first message is compose text' '
 378        grep "^fake edit" msgtxt1
 379'
 380
 381test_expect_success $PREREQ 'second message is patch' '
 382        grep "Subject:.*Second" msgtxt2
 383'
 384
 385test_expect_success $PREREQ 'setup expect' "
 386cat >expected-suppress-sob <<\EOF
 3870001-Second.patch
 388(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 389(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 390(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 391Dry-OK. Log says:
 392Server: relay.example.com
 393MAIL FROM:<from@example.com>
 394RCPT TO:<to@example.com>
 395RCPT TO:<cc@example.com>
 396RCPT TO:<author@example.com>
 397RCPT TO:<one@example.com>
 398RCPT TO:<two@example.com>
 399From: Example <from@example.com>
 400To: to@example.com
 401Cc: cc@example.com,
 402        A <author@example.com>,
 403        One <one@example.com>,
 404        two@example.com
 405Subject: [PATCH 1/1] Second.
 406Date: DATE-STRING
 407Message-Id: MESSAGE-ID-STRING
 408X-Mailer: X-MAILER-STRING
 409
 410Result: OK
 411EOF
 412"
 413
 414test_suppression () {
 415        git send-email \
 416                --dry-run \
 417                --suppress-cc=$1 ${2+"--suppress-cc=$2"} \
 418                --from="Example <from@example.com>" \
 419                --to=to@example.com \
 420                --smtp-server relay.example.com \
 421                $patches |
 422        sed     -e "s/^\(Date:\).*/\1 DATE-STRING/" \
 423                -e "s/^\(Message-Id:\).*/\1 MESSAGE-ID-STRING/" \
 424                -e "s/^\(X-Mailer:\).*/\1 X-MAILER-STRING/" \
 425                >actual-suppress-$1${2+"-$2"} &&
 426        test_cmp expected-suppress-$1${2+"-$2"} actual-suppress-$1${2+"-$2"}
 427}
 428
 429test_expect_success $PREREQ 'sendemail.cc set' '
 430        git config sendemail.cc cc@example.com &&
 431        test_suppression sob
 432'
 433
 434test_expect_success $PREREQ 'setup expect' "
 435cat >expected-suppress-sob <<\EOF
 4360001-Second.patch
 437(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 438(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 439(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 440Dry-OK. Log says:
 441Server: relay.example.com
 442MAIL FROM:<from@example.com>
 443RCPT TO:<to@example.com>
 444RCPT TO:<author@example.com>
 445RCPT TO:<one@example.com>
 446RCPT TO:<two@example.com>
 447From: Example <from@example.com>
 448To: to@example.com
 449Cc: A <author@example.com>,
 450        One <one@example.com>,
 451        two@example.com
 452Subject: [PATCH 1/1] Second.
 453Date: DATE-STRING
 454Message-Id: MESSAGE-ID-STRING
 455X-Mailer: X-MAILER-STRING
 456
 457Result: OK
 458EOF
 459"
 460
 461test_expect_success $PREREQ 'sendemail.cc unset' '
 462        git config --unset sendemail.cc &&
 463        test_suppression sob
 464'
 465
 466test_expect_success $PREREQ 'setup expect' "
 467cat >expected-suppress-cccmd <<\EOF
 4680001-Second.patch
 469(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 470(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 471(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 472(body) Adding cc: C O Mitter <committer@example.com> from line 'Signed-off-by: C O Mitter <committer@example.com>'
 473Dry-OK. Log says:
 474Server: relay.example.com
 475MAIL FROM:<from@example.com>
 476RCPT TO:<to@example.com>
 477RCPT TO:<author@example.com>
 478RCPT TO:<one@example.com>
 479RCPT TO:<two@example.com>
 480RCPT TO:<committer@example.com>
 481From: Example <from@example.com>
 482To: to@example.com
 483Cc: A <author@example.com>,
 484        One <one@example.com>,
 485        two@example.com,
 486        C O Mitter <committer@example.com>
 487Subject: [PATCH 1/1] Second.
 488Date: DATE-STRING
 489Message-Id: MESSAGE-ID-STRING
 490X-Mailer: X-MAILER-STRING
 491
 492Result: OK
 493EOF
 494"
 495
 496test_expect_success $PREREQ 'sendemail.cccmd' '
 497        echo echo cc-cmd@example.com > cccmd &&
 498        chmod +x cccmd &&
 499        git config sendemail.cccmd ./cccmd &&
 500        test_suppression cccmd
 501'
 502
 503test_expect_success $PREREQ 'setup expect' '
 504cat >expected-suppress-all <<\EOF
 5050001-Second.patch
 506Dry-OK. Log says:
 507Server: relay.example.com
 508MAIL FROM:<from@example.com>
 509RCPT TO:<to@example.com>
 510From: Example <from@example.com>
 511To: to@example.com
 512Subject: [PATCH 1/1] Second.
 513Date: DATE-STRING
 514Message-Id: MESSAGE-ID-STRING
 515X-Mailer: X-MAILER-STRING
 516
 517Result: OK
 518EOF
 519'
 520
 521test_expect_success $PREREQ '--suppress-cc=all' '
 522        test_suppression all
 523'
 524
 525test_expect_success $PREREQ 'setup expect' "
 526cat >expected-suppress-body <<\EOF
 5270001-Second.patch
 528(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 529(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 530(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 531(cc-cmd) Adding cc: cc-cmd@example.com from: './cccmd'
 532Dry-OK. Log says:
 533Server: relay.example.com
 534MAIL FROM:<from@example.com>
 535RCPT TO:<to@example.com>
 536RCPT TO:<author@example.com>
 537RCPT TO:<one@example.com>
 538RCPT TO:<two@example.com>
 539RCPT TO:<cc-cmd@example.com>
 540From: Example <from@example.com>
 541To: to@example.com
 542Cc: A <author@example.com>,
 543        One <one@example.com>,
 544        two@example.com,
 545        cc-cmd@example.com
 546Subject: [PATCH 1/1] Second.
 547Date: DATE-STRING
 548Message-Id: MESSAGE-ID-STRING
 549X-Mailer: X-MAILER-STRING
 550
 551Result: OK
 552EOF
 553"
 554
 555test_expect_success $PREREQ '--suppress-cc=body' '
 556        test_suppression body
 557'
 558
 559test_expect_success $PREREQ 'setup expect' "
 560cat >expected-suppress-body-cccmd <<\EOF
 5610001-Second.patch
 562(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 563(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 564(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 565Dry-OK. Log says:
 566Server: relay.example.com
 567MAIL FROM:<from@example.com>
 568RCPT TO:<to@example.com>
 569RCPT TO:<author@example.com>
 570RCPT TO:<one@example.com>
 571RCPT TO:<two@example.com>
 572From: Example <from@example.com>
 573To: to@example.com
 574Cc: A <author@example.com>,
 575        One <one@example.com>,
 576        two@example.com
 577Subject: [PATCH 1/1] Second.
 578Date: DATE-STRING
 579Message-Id: MESSAGE-ID-STRING
 580X-Mailer: X-MAILER-STRING
 581
 582Result: OK
 583EOF
 584"
 585
 586test_expect_success $PREREQ '--suppress-cc=body --suppress-cc=cccmd' '
 587        test_suppression body cccmd
 588'
 589
 590test_expect_success $PREREQ 'setup expect' "
 591cat >expected-suppress-sob <<\EOF
 5920001-Second.patch
 593(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 594(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 595(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 596Dry-OK. Log says:
 597Server: relay.example.com
 598MAIL FROM:<from@example.com>
 599RCPT TO:<to@example.com>
 600RCPT TO:<author@example.com>
 601RCPT TO:<one@example.com>
 602RCPT TO:<two@example.com>
 603From: Example <from@example.com>
 604To: to@example.com
 605Cc: A <author@example.com>,
 606        One <one@example.com>,
 607        two@example.com
 608Subject: [PATCH 1/1] Second.
 609Date: DATE-STRING
 610Message-Id: MESSAGE-ID-STRING
 611X-Mailer: X-MAILER-STRING
 612
 613Result: OK
 614EOF
 615"
 616
 617test_expect_success $PREREQ '--suppress-cc=sob' '
 618        git config --unset sendemail.cccmd
 619        test_suppression sob
 620'
 621
 622test_expect_success $PREREQ 'setup expect' "
 623cat >expected-suppress-bodycc <<\EOF
 6240001-Second.patch
 625(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 626(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 627(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 628(body) Adding cc: C O Mitter <committer@example.com> from line 'Signed-off-by: C O Mitter <committer@example.com>'
 629Dry-OK. Log says:
 630Server: relay.example.com
 631MAIL FROM:<from@example.com>
 632RCPT TO:<to@example.com>
 633RCPT TO:<author@example.com>
 634RCPT TO:<one@example.com>
 635RCPT TO:<two@example.com>
 636RCPT TO:<committer@example.com>
 637From: Example <from@example.com>
 638To: to@example.com
 639Cc: A <author@example.com>,
 640        One <one@example.com>,
 641        two@example.com,
 642        C O Mitter <committer@example.com>
 643Subject: [PATCH 1/1] Second.
 644Date: DATE-STRING
 645Message-Id: MESSAGE-ID-STRING
 646X-Mailer: X-MAILER-STRING
 647
 648Result: OK
 649EOF
 650"
 651
 652test_expect_success $PREREQ '--suppress-cc=bodycc' '
 653        test_suppression bodycc
 654'
 655
 656test_expect_success $PREREQ 'setup expect' "
 657cat >expected-suppress-cc <<\EOF
 6580001-Second.patch
 659(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 660(body) Adding cc: C O Mitter <committer@example.com> from line 'Signed-off-by: C O Mitter <committer@example.com>'
 661Dry-OK. Log says:
 662Server: relay.example.com
 663MAIL FROM:<from@example.com>
 664RCPT TO:<to@example.com>
 665RCPT TO:<author@example.com>
 666RCPT TO:<committer@example.com>
 667From: Example <from@example.com>
 668To: to@example.com
 669Cc: A <author@example.com>,
 670        C O Mitter <committer@example.com>
 671Subject: [PATCH 1/1] Second.
 672Date: DATE-STRING
 673Message-Id: MESSAGE-ID-STRING
 674X-Mailer: X-MAILER-STRING
 675
 676Result: OK
 677EOF
 678"
 679
 680test_expect_success $PREREQ '--suppress-cc=cc' '
 681        test_suppression cc
 682'
 683
 684test_confirm () {
 685        echo y | \
 686                GIT_SEND_EMAIL_NOTTY=1 \
 687                git send-email \
 688                --from="Example <nobody@example.com>" \
 689                --to=nobody@example.com \
 690                --smtp-server="$(pwd)/fake.sendmail" \
 691                $@ $patches > stdout &&
 692        grep "Send this email" stdout
 693}
 694
 695test_expect_success $PREREQ '--confirm=always' '
 696        test_confirm --confirm=always --suppress-cc=all
 697'
 698
 699test_expect_success $PREREQ '--confirm=auto' '
 700        test_confirm --confirm=auto
 701'
 702
 703test_expect_success $PREREQ '--confirm=cc' '
 704        test_confirm --confirm=cc
 705'
 706
 707test_expect_success $PREREQ '--confirm=compose' '
 708        test_confirm --confirm=compose --compose
 709'
 710
 711test_expect_success $PREREQ 'confirm by default (due to cc)' '
 712        CONFIRM=$(git config --get sendemail.confirm) &&
 713        git config --unset sendemail.confirm &&
 714        test_confirm
 715        ret="$?"
 716        git config sendemail.confirm ${CONFIRM:-never}
 717        test $ret = "0"
 718'
 719
 720test_expect_success $PREREQ 'confirm by default (due to --compose)' '
 721        CONFIRM=$(git config --get sendemail.confirm) &&
 722        git config --unset sendemail.confirm &&
 723        test_confirm --suppress-cc=all --compose
 724        ret="$?"
 725        git config sendemail.confirm ${CONFIRM:-never}
 726        test $ret = "0"
 727'
 728
 729test_expect_success $PREREQ 'confirm detects EOF (inform assumes y)' '
 730        CONFIRM=$(git config --get sendemail.confirm) &&
 731        git config --unset sendemail.confirm &&
 732        rm -fr outdir &&
 733        git format-patch -2 -o outdir &&
 734        GIT_SEND_EMAIL_NOTTY=1 \
 735                git send-email \
 736                        --from="Example <nobody@example.com>" \
 737                        --to=nobody@example.com \
 738                        --smtp-server="$(pwd)/fake.sendmail" \
 739                        outdir/*.patch < /dev/null
 740        ret="$?"
 741        git config sendemail.confirm ${CONFIRM:-never}
 742        test $ret = "0"
 743'
 744
 745test_expect_success $PREREQ 'confirm detects EOF (auto causes failure)' '
 746        CONFIRM=$(git config --get sendemail.confirm) &&
 747        git config sendemail.confirm auto &&
 748        GIT_SEND_EMAIL_NOTTY=1 &&
 749        export GIT_SEND_EMAIL_NOTTY &&
 750                test_must_fail git send-email \
 751                        --from="Example <nobody@example.com>" \
 752                        --to=nobody@example.com \
 753                        --smtp-server="$(pwd)/fake.sendmail" \
 754                        $patches < /dev/null
 755        ret="$?"
 756        git config sendemail.confirm ${CONFIRM:-never}
 757        test $ret = "0"
 758'
 759
 760test_expect_success $PREREQ 'confirm doesnt loop forever' '
 761        CONFIRM=$(git config --get sendemail.confirm) &&
 762        git config sendemail.confirm auto &&
 763        GIT_SEND_EMAIL_NOTTY=1 &&
 764        export GIT_SEND_EMAIL_NOTTY &&
 765                yes "bogus" | test_must_fail git send-email \
 766                        --from="Example <nobody@example.com>" \
 767                        --to=nobody@example.com \
 768                        --smtp-server="$(pwd)/fake.sendmail" \
 769                        $patches
 770        ret="$?"
 771        git config sendemail.confirm ${CONFIRM:-never}
 772        test $ret = "0"
 773'
 774
 775test_expect_success $PREREQ 'utf8 Cc is rfc2047 encoded' '
 776        clean_fake_sendmail &&
 777        rm -fr outdir &&
 778        git format-patch -1 -o outdir --cc="àéìöú <utf8@example.com>" &&
 779        git send-email \
 780        --from="Example <nobody@example.com>" \
 781        --to=nobody@example.com \
 782        --smtp-server="$(pwd)/fake.sendmail" \
 783        outdir/*.patch &&
 784        grep "^ " msgtxt1 |
 785        grep "=?UTF-8?q?=C3=A0=C3=A9=C3=AC=C3=B6=C3=BA?= <utf8@example.com>"
 786'
 787
 788test_expect_success $PREREQ '--compose adds MIME for utf8 body' '
 789        clean_fake_sendmail &&
 790        (echo "#!$SHELL_PATH" &&
 791         echo "echo utf8 body: àéìöú >>\"\$1\""
 792        ) >fake-editor-utf8 &&
 793        chmod +x fake-editor-utf8 &&
 794          GIT_EDITOR="\"$(pwd)/fake-editor-utf8\"" \
 795          git send-email \
 796          --compose --subject foo \
 797          --from="Example <nobody@example.com>" \
 798          --to=nobody@example.com \
 799          --smtp-server="$(pwd)/fake.sendmail" \
 800          $patches &&
 801        grep "^utf8 body" msgtxt1 &&
 802        grep "^Content-Type: text/plain; charset=UTF-8" msgtxt1
 803'
 804
 805test_expect_success $PREREQ '--compose respects user mime type' '
 806        clean_fake_sendmail &&
 807        (echo "#!$SHELL_PATH" &&
 808         echo "(echo MIME-Version: 1.0"
 809         echo " echo Content-Type: text/plain\\; charset=iso-8859-1"
 810         echo " echo Content-Transfer-Encoding: 8bit"
 811         echo " echo Subject: foo"
 812         echo " echo "
 813         echo " echo utf8 body: àéìöú) >\"\$1\""
 814        ) >fake-editor-utf8-mime &&
 815        chmod +x fake-editor-utf8-mime &&
 816          GIT_EDITOR="\"$(pwd)/fake-editor-utf8-mime\"" \
 817          git send-email \
 818          --compose --subject foo \
 819          --from="Example <nobody@example.com>" \
 820          --to=nobody@example.com \
 821          --smtp-server="$(pwd)/fake.sendmail" \
 822          $patches &&
 823        grep "^utf8 body" msgtxt1 &&
 824        grep "^Content-Type: text/plain; charset=iso-8859-1" msgtxt1 &&
 825        ! grep "^Content-Type: text/plain; charset=UTF-8" msgtxt1
 826'
 827
 828test_expect_success $PREREQ '--compose adds MIME for utf8 subject' '
 829        clean_fake_sendmail &&
 830          GIT_EDITOR="\"$(pwd)/fake-editor\"" \
 831          git send-email \
 832          --compose --subject utf8-sübjëct \
 833          --from="Example <nobody@example.com>" \
 834          --to=nobody@example.com \
 835          --smtp-server="$(pwd)/fake.sendmail" \
 836          $patches &&
 837        grep "^fake edit" msgtxt1 &&
 838        grep "^Subject: =?UTF-8?q?utf8-s=C3=BCbj=C3=ABct?=" msgtxt1
 839'
 840
 841test_expect_success $PREREQ 'detects ambiguous reference/file conflict' '
 842        echo master > master &&
 843        git add master &&
 844        git commit -m"add master" &&
 845        test_must_fail git send-email --dry-run master 2>errors &&
 846        grep disambiguate errors
 847'
 848
 849test_expect_success $PREREQ 'feed two files' '
 850        rm -fr outdir &&
 851        git format-patch -2 -o outdir &&
 852        git send-email \
 853        --dry-run \
 854        --from="Example <nobody@example.com>" \
 855        --to=nobody@example.com \
 856        outdir/000?-*.patch 2>errors >out &&
 857        grep "^Subject: " out >subjects &&
 858        test "z$(sed -n -e 1p subjects)" = "zSubject: [PATCH 1/2] Second." &&
 859        test "z$(sed -n -e 2p subjects)" = "zSubject: [PATCH 2/2] add master"
 860'
 861
 862test_expect_success $PREREQ 'in-reply-to but no threading' '
 863        git send-email \
 864                --dry-run \
 865                --from="Example <nobody@example.com>" \
 866                --to=nobody@example.com \
 867                --in-reply-to="<in-reply-id@example.com>" \
 868                --nothread \
 869                $patches |
 870        grep "In-Reply-To: <in-reply-id@example.com>"
 871'
 872
 873test_expect_success $PREREQ 'no in-reply-to and no threading' '
 874        git send-email \
 875                --dry-run \
 876                --from="Example <nobody@example.com>" \
 877                --to=nobody@example.com \
 878                --nothread \
 879                $patches $patches >stdout &&
 880        ! grep "In-Reply-To: " stdout
 881'
 882
 883test_expect_success $PREREQ 'threading but no chain-reply-to' '
 884        git send-email \
 885                --dry-run \
 886                --from="Example <nobody@example.com>" \
 887                --to=nobody@example.com \
 888                --thread \
 889                --nochain-reply-to \
 890                $patches $patches >stdout &&
 891        grep "In-Reply-To: " stdout
 892'
 893
 894test_expect_success $PREREQ 'warning with an implicit --chain-reply-to' '
 895        git send-email \
 896        --dry-run \
 897        --from="Example <nobody@example.com>" \
 898        --to=nobody@example.com \
 899        outdir/000?-*.patch 2>errors >out &&
 900        grep "no-chain-reply-to" errors
 901'
 902
 903test_expect_success $PREREQ 'no warning with an explicit --chain-reply-to' '
 904        git send-email \
 905        --dry-run \
 906        --from="Example <nobody@example.com>" \
 907        --to=nobody@example.com \
 908        --chain-reply-to \
 909        outdir/000?-*.patch 2>errors >out &&
 910        ! grep "no-chain-reply-to" errors
 911'
 912
 913test_expect_success $PREREQ 'no warning with an explicit --no-chain-reply-to' '
 914        git send-email \
 915        --dry-run \
 916        --from="Example <nobody@example.com>" \
 917        --to=nobody@example.com \
 918        --nochain-reply-to \
 919        outdir/000?-*.patch 2>errors >out &&
 920        ! grep "no-chain-reply-to" errors
 921'
 922
 923test_expect_success $PREREQ 'no warning with sendemail.chainreplyto = false' '
 924        git config sendemail.chainreplyto false &&
 925        git send-email \
 926        --dry-run \
 927        --from="Example <nobody@example.com>" \
 928        --to=nobody@example.com \
 929        outdir/000?-*.patch 2>errors >out &&
 930        ! grep "no-chain-reply-to" errors
 931'
 932
 933test_expect_success $PREREQ 'no warning with sendemail.chainreplyto = true' '
 934        git config sendemail.chainreplyto true &&
 935        git send-email \
 936        --dry-run \
 937        --from="Example <nobody@example.com>" \
 938        --to=nobody@example.com \
 939        outdir/000?-*.patch 2>errors >out &&
 940        ! grep "no-chain-reply-to" errors
 941'
 942
 943test_expect_success $PREREQ 'sendemail.to works' '
 944        git config --replace-all sendemail.to "Somebody <somebody@ex.com>" &&
 945        git send-email \
 946                --dry-run \
 947                --from="Example <nobody@example.com>" \
 948                $patches $patches >stdout &&
 949        grep "To: Somebody <somebody@ex.com>" stdout
 950'
 951
 952test_expect_success $PREREQ '--no-to overrides sendemail.to' '
 953        git send-email \
 954                --dry-run \
 955                --from="Example <nobody@example.com>" \
 956                --no-to \
 957                --to=nobody@example.com \
 958                $patches $patches >stdout &&
 959        grep "To: nobody@example.com" stdout &&
 960        ! grep "To: Somebody <somebody@ex.com>" stdout
 961'
 962
 963test_expect_success $PREREQ 'sendemail.cc works' '
 964        git config --replace-all sendemail.cc "Somebody <somebody@ex.com>" &&
 965        git send-email \
 966                --dry-run \
 967                --from="Example <nobody@example.com>" \
 968                --to=nobody@example.com \
 969                $patches $patches >stdout &&
 970        grep "Cc: Somebody <somebody@ex.com>" stdout
 971'
 972
 973test_expect_success $PREREQ '--no-cc overrides sendemail.cc' '
 974        git send-email \
 975                --dry-run \
 976                --from="Example <nobody@example.com>" \
 977                --no-cc \
 978                --cc=bodies@example.com \
 979                --to=nobody@example.com \
 980                $patches $patches >stdout &&
 981        grep "Cc: bodies@example.com" stdout &&
 982        ! grep "Cc: Somebody <somebody@ex.com>" stdout
 983'
 984
 985test_expect_success $PREREQ 'sendemail.bcc works' '
 986        git config --replace-all sendemail.bcc "Other <other@ex.com>" &&
 987        git send-email \
 988                --dry-run \
 989                --from="Example <nobody@example.com>" \
 990                --to=nobody@example.com \
 991                --smtp-server relay.example.com \
 992                $patches $patches >stdout &&
 993        grep "RCPT TO:<other@ex.com>" stdout
 994'
 995
 996test_expect_success $PREREQ '--no-bcc overrides sendemail.bcc' '
 997        git send-email \
 998                --dry-run \
 999                --from="Example <nobody@example.com>" \
1000                --no-bcc \
1001                --bcc=bodies@example.com \
1002                --to=nobody@example.com \
1003                --smtp-server relay.example.com \
1004                $patches $patches >stdout &&
1005        grep "RCPT TO:<bodies@example.com>" stdout &&
1006        ! grep "RCPT TO:<other@ex.com>" stdout
1007'
1008
1009test_expect_success $PREREQ 'patches To headers are used by default' '
1010        patch=`git format-patch -1 --to="bodies@example.com"` &&
1011        test_when_finished "rm $patch" &&
1012        git send-email \
1013                --dry-run \
1014                --from="Example <nobody@example.com>" \
1015                --smtp-server relay.example.com \
1016                $patch >stdout &&
1017        grep "RCPT TO:<bodies@example.com>" stdout
1018'
1019
1020test_expect_success $PREREQ 'patches To headers are appended to' '
1021        patch=`git format-patch -1 --to="bodies@example.com"` &&
1022        test_when_finished "rm $patch" &&
1023        git send-email \
1024                --dry-run \
1025                --from="Example <nobody@example.com>" \
1026                --to=nobody@example.com \
1027                --smtp-server relay.example.com \
1028                $patch >stdout &&
1029        grep "RCPT TO:<bodies@example.com>" stdout &&
1030        grep "RCPT TO:<nobody@example.com>" stdout
1031'
1032
1033test_expect_success $PREREQ 'To headers from files reset each patch' '
1034        patch1=`git format-patch -1 --to="bodies@example.com"` &&
1035        patch2=`git format-patch -1 --to="other@example.com" HEAD~` &&
1036        test_when_finished "rm $patch1 && rm $patch2" &&
1037        git send-email \
1038                --dry-run \
1039                --from="Example <nobody@example.com>" \
1040                --to="nobody@example.com" \
1041                --smtp-server relay.example.com \
1042                $patch1 $patch2 >stdout &&
1043        test $(grep -c "RCPT TO:<bodies@example.com>" stdout) = 1 &&
1044        test $(grep -c "RCPT TO:<nobody@example.com>" stdout) = 2 &&
1045        test $(grep -c "RCPT TO:<other@example.com>" stdout) = 1
1046'
1047
1048test_expect_success $PREREQ 'setup expect' '
1049cat >email-using-8bit <<EOF
1050From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
1051Message-Id: <bogus-message-id@example.com>
1052From: author@example.com
1053Date: Sat, 12 Jun 2010 15:53:58 +0200
1054Subject: subject goes here
1055
1056Dieser deutsche Text enthält einen Umlaut!
1057EOF
1058'
1059
1060test_expect_success $PREREQ 'setup expect' '
1061cat >content-type-decl <<EOF
1062MIME-Version: 1.0
1063Content-Type: text/plain; charset=UTF-8
1064Content-Transfer-Encoding: 8bit
1065EOF
1066'
1067
1068test_expect_success $PREREQ 'asks about and fixes 8bit encodings' '
1069        clean_fake_sendmail &&
1070        echo |
1071        git send-email --from=author@example.com --to=nobody@example.com \
1072                        --smtp-server="$(pwd)/fake.sendmail" \
1073                        email-using-8bit >stdout &&
1074        grep "do not declare a Content-Transfer-Encoding" stdout &&
1075        grep email-using-8bit stdout &&
1076        grep "Which 8bit encoding" stdout &&
1077        egrep "Content|MIME" msgtxt1 >actual &&
1078        test_cmp actual content-type-decl
1079'
1080
1081test_expect_success $PREREQ 'sendemail.8bitEncoding works' '
1082        clean_fake_sendmail &&
1083        git config sendemail.assume8bitEncoding UTF-8 &&
1084        echo bogus |
1085        git send-email --from=author@example.com --to=nobody@example.com \
1086                        --smtp-server="$(pwd)/fake.sendmail" \
1087                        email-using-8bit >stdout &&
1088        egrep "Content|MIME" msgtxt1 >actual &&
1089        test_cmp actual content-type-decl
1090'
1091
1092test_expect_success $PREREQ '--8bit-encoding overrides sendemail.8bitEncoding' '
1093        clean_fake_sendmail &&
1094        git config sendemail.assume8bitEncoding "bogus too" &&
1095        echo bogus |
1096        git send-email --from=author@example.com --to=nobody@example.com \
1097                        --smtp-server="$(pwd)/fake.sendmail" \
1098                        --8bit-encoding=UTF-8 \
1099                        email-using-8bit >stdout &&
1100        egrep "Content|MIME" msgtxt1 >actual &&
1101        test_cmp actual content-type-decl
1102'
1103
1104test_expect_success $PREREQ 'setup expect' '
1105cat >email-using-8bit <<EOF
1106From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
1107Message-Id: <bogus-message-id@example.com>
1108From: author@example.com
1109Date: Sat, 12 Jun 2010 15:53:58 +0200
1110Subject: Dieser Betreff enthält auch einen Umlaut!
1111
1112Nothing to see here.
1113EOF
1114'
1115
1116test_expect_success $PREREQ 'setup expect' '
1117cat >expected <<EOF
1118Subject: =?UTF-8?q?Dieser=20Betreff=20enth=C3=A4lt=20auch=20einen=20Umlaut!?=
1119EOF
1120'
1121
1122test_expect_success $PREREQ '--8bit-encoding also treats subject' '
1123        clean_fake_sendmail &&
1124        echo bogus |
1125        git send-email --from=author@example.com --to=nobody@example.com \
1126                        --smtp-server="$(pwd)/fake.sendmail" \
1127                        --8bit-encoding=UTF-8 \
1128                        email-using-8bit >stdout &&
1129        grep "Subject" msgtxt1 >actual &&
1130        test_cmp expected actual
1131'
1132
1133# Note that the patches in this test are deliberately out of order; we
1134# want to make sure it works even if the cover-letter is not in the
1135# first mail.
1136test_expect_success 'refusing to send cover letter template' '
1137        clean_fake_sendmail &&
1138        rm -fr outdir &&
1139        git format-patch --cover-letter -2 -o outdir &&
1140        test_must_fail git send-email \
1141          --from="Example <nobody@example.com>" \
1142          --to=nobody@example.com \
1143          --smtp-server="$(pwd)/fake.sendmail" \
1144          outdir/0002-*.patch \
1145          outdir/0000-*.patch \
1146          outdir/0001-*.patch \
1147          2>errors >out &&
1148        grep "SUBJECT HERE" errors &&
1149        test -z "$(ls msgtxt*)"
1150'
1151
1152test_expect_success '--force sends cover letter template anyway' '
1153        clean_fake_sendmail &&
1154        rm -fr outdir &&
1155        git format-patch --cover-letter -2 -o outdir &&
1156        git send-email \
1157          --force \
1158          --from="Example <nobody@example.com>" \
1159          --to=nobody@example.com \
1160          --smtp-server="$(pwd)/fake.sendmail" \
1161          outdir/0002-*.patch \
1162          outdir/0000-*.patch \
1163          outdir/0001-*.patch \
1164          2>errors >out &&
1165        ! grep "SUBJECT HERE" errors &&
1166        test -n "$(ls msgtxt*)"
1167'
1168
1169test_done