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