63ab88bfc832b91edaf50d829c94385c5b32f2d2
   1#!/bin/sh
   2
   3test_description='git send-email'
   4. ./test-lib.sh
   5
   6PROG='git send-email'
   7test_expect_success \
   8    'prepare reference tree' \
   9    'echo "1A quick brown fox jumps over the" >file &&
  10     echo "lazy dog" >>file &&
  11     git add file &&
  12     GIT_AUTHOR_NAME="A" git commit -a -m "Initial."'
  13
  14test_expect_success \
  15    'Setup helper tool' \
  16    '(echo "#!$SHELL_PATH"
  17      echo shift
  18      echo output=1
  19      echo "while test -f commandline\$output; do output=\$((\$output+1)); done"
  20      echo for a
  21      echo do
  22      echo "  echo \"!\$a!\""
  23      echo "done >commandline\$output"
  24      echo "cat > msgtxt\$output"
  25      ) >fake.sendmail &&
  26     chmod +x ./fake.sendmail &&
  27     git add fake.sendmail &&
  28     GIT_AUTHOR_NAME="A" git commit -a -m "Second."'
  29
  30clean_fake_sendmail() {
  31        rm -f commandline* msgtxt*
  32}
  33
  34test_expect_success 'Extract patches' '
  35    patches=`git format-patch --cc="One <one@example.com>" --cc=two@example.com -n HEAD^1`
  36'
  37
  38test_expect_success 'Send patches' '
  39     git send-email --from="Example <nobody@example.com>" --to=nobody@example.com --smtp-server="$(pwd)/fake.sendmail" $patches 2>errors
  40'
  41
  42cat >expected <<\EOF
  43!nobody@example.com!
  44!author@example.com!
  45!one@example.com!
  46!two@example.com!
  47EOF
  48test_expect_success \
  49    'Verify commandline' \
  50    'diff commandline1 expected'
  51
  52cat >expected-show-all-headers <<\EOF
  530001-Second.patch
  54(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
  55(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
  56(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
  57Dry-OK. Log says:
  58Server: relay.example.com
  59MAIL FROM:<from@example.com>
  60RCPT TO:<to@example.com>,<cc@example.com>,<author@example.com>,<one@example.com>,<two@example.com>,<bcc@example.com>
  61From: Example <from@example.com>
  62To: to@example.com
  63Cc: cc@example.com, A <author@example.com>, One <one@example.com>, two@example.com
  64Subject: [PATCH 1/1] Second.
  65Date: DATE-STRING
  66Message-Id: MESSAGE-ID-STRING
  67X-Mailer: X-MAILER-STRING
  68In-Reply-To: <unique-message-id@example.com>
  69References: <unique-message-id@example.com>
  70
  71Result: OK
  72EOF
  73
  74test_expect_success 'Show all headers' '
  75        git send-email \
  76                --dry-run \
  77                --from="Example <from@example.com>" \
  78                --to=to@example.com \
  79                --cc=cc@example.com \
  80                --bcc=bcc@example.com \
  81                --in-reply-to="<unique-message-id@example.com>" \
  82                --smtp-server relay.example.com \
  83                $patches |
  84        sed     -e "s/^\(Date:\).*/\1 DATE-STRING/" \
  85                -e "s/^\(Message-Id:\).*/\1 MESSAGE-ID-STRING/" \
  86                -e "s/^\(X-Mailer:\).*/\1 X-MAILER-STRING/" \
  87                >actual-show-all-headers &&
  88        test_cmp expected-show-all-headers actual-show-all-headers
  89'
  90
  91z8=zzzzzzzz
  92z64=$z8$z8$z8$z8$z8$z8$z8$z8
  93z512=$z64$z64$z64$z64$z64$z64$z64$z64
  94test_expect_success 'reject long lines' '
  95        clean_fake_sendmail &&
  96        cp $patches longline.patch &&
  97        echo $z512$z512 >>longline.patch &&
  98        test_must_fail git send-email \
  99                --from="Example <nobody@example.com>" \
 100                --to=nobody@example.com \
 101                --smtp-server="$(pwd)/fake.sendmail" \
 102                $patches longline.patch \
 103                2>errors &&
 104        grep longline.patch errors
 105'
 106
 107test_expect_success 'no patch was sent' '
 108        ! test -e commandline1
 109'
 110
 111test_expect_success 'Author From: in message body' '
 112        clean_fake_sendmail &&
 113        git send-email \
 114                --from="Example <nobody@example.com>" \
 115                --to=nobody@example.com \
 116                --smtp-server="$(pwd)/fake.sendmail" \
 117                $patches &&
 118        sed "1,/^$/d" < msgtxt1 > msgbody1
 119        grep "From: A <author@example.com>" msgbody1
 120'
 121
 122test_expect_success 'Author From: not in message body' '
 123        clean_fake_sendmail &&
 124        git send-email \
 125                --from="A <author@example.com>" \
 126                --to=nobody@example.com \
 127                --smtp-server="$(pwd)/fake.sendmail" \
 128                $patches &&
 129        sed "1,/^$/d" < msgtxt1 > msgbody1
 130        ! grep "From: A <author@example.com>" msgbody1
 131'
 132
 133test_expect_success 'allow long lines with --no-validate' '
 134        git send-email \
 135                --from="Example <nobody@example.com>" \
 136                --to=nobody@example.com \
 137                --smtp-server="$(pwd)/fake.sendmail" \
 138                --novalidate \
 139                $patches longline.patch \
 140                2>errors
 141'
 142
 143test_expect_success 'Invalid In-Reply-To' '
 144        clean_fake_sendmail &&
 145        git send-email \
 146                --from="Example <nobody@example.com>" \
 147                --to=nobody@example.com \
 148                --in-reply-to=" " \
 149                --smtp-server="$(pwd)/fake.sendmail" \
 150                $patches
 151                2>errors
 152        ! grep "^In-Reply-To: < *>" msgtxt1
 153'
 154
 155test_expect_success 'Valid In-Reply-To when prompting' '
 156        clean_fake_sendmail &&
 157        (echo "From Example <from@example.com>"
 158         echo "To Example <to@example.com>"
 159         echo ""
 160        ) | env GIT_SEND_EMAIL_NOTTY=1 git send-email \
 161                --smtp-server="$(pwd)/fake.sendmail" \
 162                $patches 2>errors &&
 163        ! grep "^In-Reply-To: < *>" msgtxt1
 164'
 165
 166test_expect_success 'setup fake editor' '
 167        (echo "#!$SHELL_PATH" &&
 168         echo "echo fake edit >>\"\$1\""
 169        ) >fake-editor &&
 170        chmod +x fake-editor
 171'
 172
 173test_set_editor "$(pwd)/fake-editor"
 174
 175test_expect_success '--compose works' '
 176        clean_fake_sendmail &&
 177        echo y | \
 178                GIT_SEND_EMAIL_NOTTY=1 \
 179                git send-email \
 180                --compose --subject foo \
 181                --from="Example <nobody@example.com>" \
 182                --to=nobody@example.com \
 183                --smtp-server="$(pwd)/fake.sendmail" \
 184                $patches \
 185                2>errors
 186'
 187
 188test_expect_success 'first message is compose text' '
 189        grep "^fake edit" msgtxt1
 190'
 191
 192test_expect_success 'second message is patch' '
 193        grep "Subject:.*Second" msgtxt2
 194'
 195
 196cat >expected-show-all-headers <<\EOF
 1970001-Second.patch
 198(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 199(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 200(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 201Dry-OK. Log says:
 202Server: relay.example.com
 203MAIL FROM:<from@example.com>
 204RCPT TO:<to@example.com>,<cc@example.com>,<author@example.com>,<one@example.com>,<two@example.com>
 205From: Example <from@example.com>
 206To: to@example.com
 207Cc: cc@example.com, A <author@example.com>, One <one@example.com>, two@example.com
 208Subject: [PATCH 1/1] Second.
 209Date: DATE-STRING
 210Message-Id: MESSAGE-ID-STRING
 211X-Mailer: X-MAILER-STRING
 212
 213Result: OK
 214EOF
 215
 216test_expect_success 'sendemail.cc set' '
 217        git config sendemail.cc cc@example.com &&
 218        git send-email \
 219                --dry-run \
 220                --from="Example <from@example.com>" \
 221                --to=to@example.com \
 222                --smtp-server relay.example.com \
 223                $patches |
 224        sed     -e "s/^\(Date:\).*/\1 DATE-STRING/" \
 225                -e "s/^\(Message-Id:\).*/\1 MESSAGE-ID-STRING/" \
 226                -e "s/^\(X-Mailer:\).*/\1 X-MAILER-STRING/" \
 227                >actual-show-all-headers &&
 228        test_cmp expected-show-all-headers actual-show-all-headers
 229'
 230
 231cat >expected-show-all-headers <<\EOF
 2320001-Second.patch
 233(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
 234(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
 235(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
 236Dry-OK. Log says:
 237Server: relay.example.com
 238MAIL FROM:<from@example.com>
 239RCPT TO:<to@example.com>,<author@example.com>,<one@example.com>,<two@example.com>
 240From: Example <from@example.com>
 241To: to@example.com
 242Cc: A <author@example.com>, One <one@example.com>, two@example.com
 243Subject: [PATCH 1/1] Second.
 244Date: DATE-STRING
 245Message-Id: MESSAGE-ID-STRING
 246X-Mailer: X-MAILER-STRING
 247
 248Result: OK
 249EOF
 250
 251test_expect_success 'sendemail.cc unset' '
 252        git config --unset sendemail.cc &&
 253        git send-email \
 254                --dry-run \
 255                --from="Example <from@example.com>" \
 256                --to=to@example.com \
 257                --smtp-server relay.example.com \
 258                $patches |
 259        sed     -e "s/^\(Date:\).*/\1 DATE-STRING/" \
 260                -e "s/^\(Message-Id:\).*/\1 MESSAGE-ID-STRING/" \
 261                -e "s/^\(X-Mailer:\).*/\1 X-MAILER-STRING/" \
 262                >actual-show-all-headers &&
 263        test_cmp expected-show-all-headers actual-show-all-headers
 264'
 265
 266test_expect_success '--compose adds MIME for utf8 body' '
 267        clean_fake_sendmail &&
 268        (echo "#!$SHELL_PATH" &&
 269         echo "echo utf8 body: àéìöú >>\"\$1\""
 270        ) >fake-editor-utf8 &&
 271        chmod +x fake-editor-utf8 &&
 272        echo y | \
 273          GIT_EDITOR="\"$(pwd)/fake-editor-utf8\"" \
 274          GIT_SEND_EMAIL_NOTTY=1 \
 275          git send-email \
 276          --compose --subject foo \
 277          --from="Example <nobody@example.com>" \
 278          --to=nobody@example.com \
 279          --smtp-server="$(pwd)/fake.sendmail" \
 280          $patches &&
 281        grep "^utf8 body" msgtxt1 &&
 282        grep "^Content-Type: text/plain; charset=utf-8" msgtxt1
 283'
 284
 285test_expect_success '--compose respects user mime type' '
 286        clean_fake_sendmail &&
 287        (echo "#!$SHELL_PATH" &&
 288         echo "(echo MIME-Version: 1.0"
 289         echo " echo Content-Type: text/plain\\; charset=iso-8859-1"
 290         echo " echo Content-Transfer-Encoding: 8bit"
 291         echo " echo Subject: foo"
 292         echo " echo "
 293         echo " echo utf8 body: àéìöú) >\"\$1\""
 294        ) >fake-editor-utf8-mime &&
 295        chmod +x fake-editor-utf8-mime &&
 296        echo y | \
 297          GIT_EDITOR="\"$(pwd)/fake-editor-utf8-mime\"" \
 298          GIT_SEND_EMAIL_NOTTY=1 \
 299          git send-email \
 300          --compose --subject foo \
 301          --from="Example <nobody@example.com>" \
 302          --to=nobody@example.com \
 303          --smtp-server="$(pwd)/fake.sendmail" \
 304          $patches &&
 305        grep "^utf8 body" msgtxt1 &&
 306        grep "^Content-Type: text/plain; charset=iso-8859-1" msgtxt1 &&
 307        ! grep "^Content-Type: text/plain; charset=utf-8" msgtxt1
 308'
 309
 310test_expect_success '--compose adds MIME for utf8 subject' '
 311        clean_fake_sendmail &&
 312        echo y | \
 313          GIT_EDITOR="\"$(pwd)/fake-editor\"" \
 314          GIT_SEND_EMAIL_NOTTY=1 \
 315          git send-email \
 316          --compose --subject utf8-sübjëct \
 317          --from="Example <nobody@example.com>" \
 318          --to=nobody@example.com \
 319          --smtp-server="$(pwd)/fake.sendmail" \
 320          $patches &&
 321        grep "^fake edit" msgtxt1 &&
 322        grep "^Subject: =?utf-8?q?utf8-s=C3=BCbj=C3=ABct?=" msgtxt1
 323'
 324
 325test_expect_success 'detects ambiguous reference/file conflict' '
 326        echo master > master &&
 327        git add master &&
 328        git commit -m"add master" &&
 329        test_must_fail git send-email --dry-run master 2>errors &&
 330        grep disambiguate errors
 331'
 332
 333test_expect_success 'feed two files' '
 334        rm -fr outdir &&
 335        git format-patch -2 -o outdir &&
 336        GIT_SEND_EMAIL_NOTTY=1 git send-email \
 337        --dry-run \
 338        --from="Example <nobody@example.com>" \
 339        --to=nobody@example.com \
 340        outdir/000?-*.patch 2>errors >out &&
 341        grep "^Subject: " out >subjects &&
 342        test "z$(sed -n -e 1p subjects)" = "zSubject: [PATCH 1/2] Second." &&
 343        test "z$(sed -n -e 2p subjects)" = "zSubject: [PATCH 2/2] add master"
 344'
 345
 346test_done