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