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