t9001-send-email.sh 29.6 KB
Newer Older
1 2
#!/bin/sh

3
test_description='git send-email'
4 5
. ./test-lib.sh

6 7
# May be altered later in the test
PREREQ="PERL"
8

9
test_expect_success $PREREQ \
10 11 12
    'prepare reference tree' \
    'echo "1A quick brown fox jumps over the" >file &&
     echo "lazy dog" >>file &&
J
Jeff King 已提交
13
     git add file &&
14 15
     GIT_AUTHOR_NAME="A" git commit -a -m "Initial."'

16
test_expect_success $PREREQ \
17
    'Setup helper tool' \
18
    '(echo "#!$SHELL_PATH"
19
      echo shift
20 21
      echo output=1
      echo "while test -f commandline\$output; do output=\$((\$output+1)); done"
22 23 24
      echo for a
      echo do
      echo "  echo \"!\$a!\""
25 26
      echo "done >commandline\$output"
      echo "cat > msgtxt\$output"
J
Jeff King 已提交
27 28 29
      ) >fake.sendmail &&
     chmod +x ./fake.sendmail &&
     git add fake.sendmail &&
30 31
     GIT_AUTHOR_NAME="A" git commit -a -m "Second."'

32 33 34 35
clean_fake_sendmail() {
	rm -f commandline* msgtxt*
}

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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
# Test no confirm early to ensure remaining tests will not hang
test_no_confirm () {
	rm -f no_confirm_okay
	echo n | \
		GIT_SEND_EMAIL_NOTTY=1 \
		git send-email \
		--from="Example <from@example.com>" \
		--to=nobody@example.com \
		--smtp-server="$(pwd)/fake.sendmail" \
		$@ \
		$patches > stdout &&
		test_must_fail grep "Send this email" stdout &&
		> no_confirm_okay
}

# Exit immediately to prevent hang if a no-confirm test fails
check_no_confirm () {
57 58 59 60 61 62
	if ! test -f no_confirm_okay
	then
		say 'confirm test failed; skipping remaining tests to prevent hanging'
		PREREQ="$PREREQ,CHECK_NO_CONFIRM"
	fi
	return 0
63 64
}

65 66 67
test_expect_success $PREREQ 'No confirm with --suppress-cc' '
	test_no_confirm --suppress-cc=sob &&
	check_no_confirm
68 69
'

70 71 72 73

test_expect_success $PREREQ 'No confirm with --confirm=never' '
	test_no_confirm --confirm=never &&
	check_no_confirm
74 75 76 77
'

# leave sendemail.confirm set to never after this so that none of the
# remaining tests prompt unintentionally.
78
test_expect_success $PREREQ 'No confirm with sendemail.confirm=never' '
79
	git config sendemail.confirm never &&
80 81
	test_no_confirm --compose --subject=foo &&
	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 90 91
cat >expected <<\EOF
!nobody@example.com!
!author@example.com!
92 93
!one@example.com!
!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 103 104 105
    clean_fake_sendmail &&
     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
'

106
test_expect_success $PREREQ 'setup expect' '
107 108 109 110 111 112 113 114
cat >expected <<\EOF
!patch@example.com!
!-i!
!nobody@example.com!
!author@example.com!
!one@example.com!
!two@example.com!
EOF
115 116
'

117
test_expect_success $PREREQ \
118 119 120
    'Verify commandline' \
    'test_cmp expected commandline1'

121
test_expect_success $PREREQ 'Send patches with --envelope-sender=auto' '
122 123 124 125
    clean_fake_sendmail &&
     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
'

126
test_expect_success $PREREQ 'setup expect' '
127 128 129 130 131 132 133 134
cat >expected <<\EOF
!nobody@example.com!
!-i!
!nobody@example.com!
!author@example.com!
!one@example.com!
!two@example.com!
EOF
135 136
'

137
test_expect_success $PREREQ \
138 139 140
    'Verify commandline' \
    'test_cmp expected commandline1'

141
test_expect_success $PREREQ 'setup expect' "
142 143 144
cat >expected-show-all-headers <<\EOF
0001-Second.patch
(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
145 146
(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
147 148 149
Dry-OK. Log says:
Server: relay.example.com
MAIL FROM:<from@example.com>
150 151 152 153 154 155
RCPT TO:<to@example.com>
RCPT TO:<cc@example.com>
RCPT TO:<author@example.com>
RCPT TO:<one@example.com>
RCPT TO:<two@example.com>
RCPT TO:<bcc@example.com>
156 157
From: Example <from@example.com>
To: to@example.com
158 159 160 161
Cc: cc@example.com,
	A <author@example.com>,
	One <one@example.com>,
	two@example.com
162 163 164 165 166 167 168 169 170
Subject: [PATCH 1/1] Second.
Date: DATE-STRING
Message-Id: MESSAGE-ID-STRING
X-Mailer: X-MAILER-STRING
In-Reply-To: <unique-message-id@example.com>
References: <unique-message-id@example.com>

Result: OK
EOF
171
"
172

173
test_expect_success $PREREQ 'Show all headers' '
174 175
	git send-email \
		--dry-run \
176
		--suppress-cc=sob \
177 178 179 180 181 182 183 184 185 186 187
		--from="Example <from@example.com>" \
		--to=to@example.com \
		--cc=cc@example.com \
		--bcc=bcc@example.com \
		--in-reply-to="<unique-message-id@example.com>" \
		--smtp-server relay.example.com \
		$patches |
	sed	-e "s/^\(Date:\).*/\1 DATE-STRING/" \
		-e "s/^\(Message-Id:\).*/\1 MESSAGE-ID-STRING/" \
		-e "s/^\(X-Mailer:\).*/\1 X-MAILER-STRING/" \
		>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 193 194 195 196 197 198 199
	clean_fake_sendmail &&
	(echo "Example <from@example.com>"
	 echo "to@example.com"
	 echo ""
	) | GIT_SEND_EMAIL_NOTTY=1 git send-email \
		--smtp-server="$(pwd)/fake.sendmail" \
		$patches \
		2>errors &&
200 201
		grep "^From: Example <from@example.com>\$" msgtxt1 &&
		grep "^To: to@example.com\$" msgtxt1
202 203
'

204
test_expect_success $PREREQ 'cccmd works' '
205 206 207
	clean_fake_sendmail &&
	cp $patches cccmd.patch &&
	echo cccmd--cccmd@example.com >>cccmd.patch &&
208 209 210 211
	{
	  echo "#!$SHELL_PATH"
	  echo sed -n -e s/^cccmd--//p \"\$1\"
	} > cccmd-sed &&
212 213 214 215 216 217 218 219
	chmod +x cccmd-sed &&
	git send-email \
		--from="Example <nobody@example.com>" \
		--to=nobody@example.com \
		--cc-cmd=./cccmd-sed \
		--smtp-server="$(pwd)/fake.sendmail" \
		cccmd.patch \
		&&
220
	grep "^	cccmd@example.com" msgtxt1
221 222
'

223
test_expect_success $PREREQ 'reject long lines' '
224 225 226
	z8=zzzzzzzz &&
	z64=$z8$z8$z8$z8$z8$z8$z8$z8 &&
	z512=$z64$z64$z64$z64$z64$z64$z64$z64 &&
227
	clean_fake_sendmail &&
228 229
	cp $patches longline.patch &&
	echo $z512$z512 >>longline.patch &&
230
	test_must_fail git send-email \
231 232 233 234 235 236 237 238
		--from="Example <nobody@example.com>" \
		--to=nobody@example.com \
		--smtp-server="$(pwd)/fake.sendmail" \
		$patches longline.patch \
		2>errors &&
	grep longline.patch errors
'

239
test_expect_success $PREREQ 'no patch was sent' '
240
	! test -e commandline1
241 242
'

243
test_expect_success $PREREQ 'Author From: in message body' '
244 245 246 247 248 249
	clean_fake_sendmail &&
	git send-email \
		--from="Example <nobody@example.com>" \
		--to=nobody@example.com \
		--smtp-server="$(pwd)/fake.sendmail" \
		$patches &&
250
	sed "1,/^\$/d" < msgtxt1 > msgbody1
251 252 253
	grep "From: A <author@example.com>" msgbody1
'

254
test_expect_success $PREREQ 'Author From: not in message body' '
255 256 257 258 259 260
	clean_fake_sendmail &&
	git send-email \
		--from="A <author@example.com>" \
		--to=nobody@example.com \
		--smtp-server="$(pwd)/fake.sendmail" \
		$patches &&
261
	sed "1,/^\$/d" < msgtxt1 > msgbody1
262 263 264
	! grep "From: A <author@example.com>" msgbody1
'

265
test_expect_success $PREREQ 'allow long lines with --no-validate' '
J
Jeff King 已提交
266 267 268 269
	git send-email \
		--from="Example <nobody@example.com>" \
		--to=nobody@example.com \
		--smtp-server="$(pwd)/fake.sendmail" \
270
		--novalidate \
J
Jeff King 已提交
271 272 273 274
		$patches longline.patch \
		2>errors
'

275
test_expect_success $PREREQ 'Invalid In-Reply-To' '
276
	clean_fake_sendmail &&
277 278 279 280 281
	git send-email \
		--from="Example <nobody@example.com>" \
		--to=nobody@example.com \
		--in-reply-to=" " \
		--smtp-server="$(pwd)/fake.sendmail" \
282
		$patches \
283
		2>errors
284
	! grep "^In-Reply-To: < *>" msgtxt1
285 286
'

287
test_expect_success $PREREQ 'Valid In-Reply-To when prompting' '
288
	clean_fake_sendmail &&
289 290 291 292 293 294
	(echo "From Example <from@example.com>"
	 echo "To Example <to@example.com>"
	 echo ""
	) | env GIT_SEND_EMAIL_NOTTY=1 git send-email \
		--smtp-server="$(pwd)/fake.sendmail" \
		$patches 2>errors &&
295
	! grep "^In-Reply-To: < *>" msgtxt1
296 297
'

298
test_expect_success $PREREQ 'setup fake editor' '
299
	(echo "#!$SHELL_PATH" &&
300
	 echo "echo fake edit >>\"\$1\""
301 302 303 304
	) >fake-editor &&
	chmod +x fake-editor
'

305
test_set_editor "$(pwd)/fake-editor"
306

307
test_expect_success $PREREQ '--compose works' '
308
	clean_fake_sendmail &&
309 310 311 312 313 314 315
	git send-email \
	--compose --subject foo \
	--from="Example <nobody@example.com>" \
	--to=nobody@example.com \
	--smtp-server="$(pwd)/fake.sendmail" \
	$patches \
	2>errors
316 317
'

318
test_expect_success $PREREQ 'first message is compose text' '
319 320 321
	grep "^fake edit" msgtxt1
'

322
test_expect_success $PREREQ 'second message is patch' '
323 324 325
	grep "Subject:.*Second" msgtxt2
'

326
test_expect_success $PREREQ 'setup expect' "
327
cat >expected-suppress-sob <<\EOF
328 329
0001-Second.patch
(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
330 331
(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
332 333 334
Dry-OK. Log says:
Server: relay.example.com
MAIL FROM:<from@example.com>
335 336 337 338 339
RCPT TO:<to@example.com>
RCPT TO:<cc@example.com>
RCPT TO:<author@example.com>
RCPT TO:<one@example.com>
RCPT TO:<two@example.com>
340 341
From: Example <from@example.com>
To: to@example.com
342 343 344 345
Cc: cc@example.com,
	A <author@example.com>,
	One <one@example.com>,
	two@example.com
346 347 348 349 350 351 352
Subject: [PATCH 1/1] Second.
Date: DATE-STRING
Message-Id: MESSAGE-ID-STRING
X-Mailer: X-MAILER-STRING

Result: OK
EOF
353
"
354

355
test_suppression () {
356 357
	git send-email \
		--dry-run \
358
		--suppress-cc=$1 ${2+"--suppress-cc=$2"} \
359 360 361 362 363 364 365
		--from="Example <from@example.com>" \
		--to=to@example.com \
		--smtp-server relay.example.com \
		$patches |
	sed	-e "s/^\(Date:\).*/\1 DATE-STRING/" \
		-e "s/^\(Message-Id:\).*/\1 MESSAGE-ID-STRING/" \
		-e "s/^\(X-Mailer:\).*/\1 X-MAILER-STRING/" \
366 367
		>actual-suppress-$1${2+"-$2"} &&
	test_cmp expected-suppress-$1${2+"-$2"} actual-suppress-$1${2+"-$2"}
368 369
}

370
test_expect_success $PREREQ 'sendemail.cc set' '
371 372
	git config sendemail.cc cc@example.com &&
	test_suppression sob
373 374
'

375
test_expect_success $PREREQ 'setup expect' "
376
cat >expected-suppress-sob <<\EOF
377 378
0001-Second.patch
(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
379 380
(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
381 382 383
Dry-OK. Log says:
Server: relay.example.com
MAIL FROM:<from@example.com>
384 385 386 387
RCPT TO:<to@example.com>
RCPT TO:<author@example.com>
RCPT TO:<one@example.com>
RCPT TO:<two@example.com>
388 389
From: Example <from@example.com>
To: to@example.com
390 391 392
Cc: A <author@example.com>,
	One <one@example.com>,
	two@example.com
393 394 395 396 397 398 399
Subject: [PATCH 1/1] Second.
Date: DATE-STRING
Message-Id: MESSAGE-ID-STRING
X-Mailer: X-MAILER-STRING

Result: OK
EOF
400
"
401

402
test_expect_success $PREREQ 'sendemail.cc unset' '
403
	git config --unset sendemail.cc &&
404 405 406
	test_suppression sob
'

407
test_expect_success $PREREQ 'setup expect' "
408 409 410 411 412 413 414 415 416
cat >expected-suppress-cccmd <<\EOF
0001-Second.patch
(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
(body) Adding cc: C O Mitter <committer@example.com> from line 'Signed-off-by: C O Mitter <committer@example.com>'
Dry-OK. Log says:
Server: relay.example.com
MAIL FROM:<from@example.com>
417 418 419 420 421
RCPT TO:<to@example.com>
RCPT TO:<author@example.com>
RCPT TO:<one@example.com>
RCPT TO:<two@example.com>
RCPT TO:<committer@example.com>
422 423
From: Example <from@example.com>
To: to@example.com
424 425 426 427
Cc: A <author@example.com>,
	One <one@example.com>,
	two@example.com,
	C O Mitter <committer@example.com>
428 429 430 431 432 433 434
Subject: [PATCH 1/1] Second.
Date: DATE-STRING
Message-Id: MESSAGE-ID-STRING
X-Mailer: X-MAILER-STRING

Result: OK
EOF
435
"
436

437
test_expect_success $PREREQ 'sendemail.cccmd' '
438 439 440 441 442 443
	echo echo cc-cmd@example.com > cccmd &&
	chmod +x cccmd &&
	git config sendemail.cccmd ./cccmd &&
	test_suppression cccmd
'

444
test_expect_success $PREREQ 'setup expect' '
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
cat >expected-suppress-all <<\EOF
0001-Second.patch
Dry-OK. Log says:
Server: relay.example.com
MAIL FROM:<from@example.com>
RCPT TO:<to@example.com>
From: Example <from@example.com>
To: to@example.com
Subject: [PATCH 1/1] Second.
Date: DATE-STRING
Message-Id: MESSAGE-ID-STRING
X-Mailer: X-MAILER-STRING

Result: OK
EOF
460
'
461

462
test_expect_success $PREREQ '--suppress-cc=all' '
463 464 465
	test_suppression all
'

466
test_expect_success $PREREQ 'setup expect' "
467 468 469 470 471
cat >expected-suppress-body <<\EOF
0001-Second.patch
(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
472
(cc-cmd) Adding cc: cc-cmd@example.com from: './cccmd'
473 474 475
Dry-OK. Log says:
Server: relay.example.com
MAIL FROM:<from@example.com>
476 477 478 479 480
RCPT TO:<to@example.com>
RCPT TO:<author@example.com>
RCPT TO:<one@example.com>
RCPT TO:<two@example.com>
RCPT TO:<cc-cmd@example.com>
481 482
From: Example <from@example.com>
To: to@example.com
483 484 485 486
Cc: A <author@example.com>,
	One <one@example.com>,
	two@example.com,
	cc-cmd@example.com
487 488 489 490 491 492 493
Subject: [PATCH 1/1] Second.
Date: DATE-STRING
Message-Id: MESSAGE-ID-STRING
X-Mailer: X-MAILER-STRING

Result: OK
EOF
494
"
495

496
test_expect_success $PREREQ '--suppress-cc=body' '
497 498 499
	test_suppression body
'

500
test_expect_success $PREREQ 'setup expect' "
501 502 503 504 505 506 507 508
cat >expected-suppress-body-cccmd <<\EOF
0001-Second.patch
(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
Dry-OK. Log says:
Server: relay.example.com
MAIL FROM:<from@example.com>
509 510 511 512
RCPT TO:<to@example.com>
RCPT TO:<author@example.com>
RCPT TO:<one@example.com>
RCPT TO:<two@example.com>
513 514
From: Example <from@example.com>
To: to@example.com
515 516 517
Cc: A <author@example.com>,
	One <one@example.com>,
	two@example.com
518 519 520 521 522 523 524
Subject: [PATCH 1/1] Second.
Date: DATE-STRING
Message-Id: MESSAGE-ID-STRING
X-Mailer: X-MAILER-STRING

Result: OK
EOF
525
"
526

527
test_expect_success $PREREQ '--suppress-cc=body --suppress-cc=cccmd' '
528 529 530
	test_suppression body cccmd
'

531
test_expect_success $PREREQ 'setup expect' "
532 533 534 535 536 537 538 539
cat >expected-suppress-sob <<\EOF
0001-Second.patch
(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
Dry-OK. Log says:
Server: relay.example.com
MAIL FROM:<from@example.com>
540 541 542 543
RCPT TO:<to@example.com>
RCPT TO:<author@example.com>
RCPT TO:<one@example.com>
RCPT TO:<two@example.com>
544 545
From: Example <from@example.com>
To: to@example.com
546 547 548
Cc: A <author@example.com>,
	One <one@example.com>,
	two@example.com
549 550 551 552 553 554 555
Subject: [PATCH 1/1] Second.
Date: DATE-STRING
Message-Id: MESSAGE-ID-STRING
X-Mailer: X-MAILER-STRING

Result: OK
EOF
556
"
557

558
test_expect_success $PREREQ '--suppress-cc=sob' '
559
	git config --unset sendemail.cccmd
560 561 562
	test_suppression sob
'

563
test_expect_success $PREREQ 'setup expect' "
564 565 566 567 568 569 570 571 572
cat >expected-suppress-bodycc <<\EOF
0001-Second.patch
(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
(body) Adding cc: C O Mitter <committer@example.com> from line 'Signed-off-by: C O Mitter <committer@example.com>'
Dry-OK. Log says:
Server: relay.example.com
MAIL FROM:<from@example.com>
573 574 575 576 577
RCPT TO:<to@example.com>
RCPT TO:<author@example.com>
RCPT TO:<one@example.com>
RCPT TO:<two@example.com>
RCPT TO:<committer@example.com>
578 579
From: Example <from@example.com>
To: to@example.com
580 581 582 583
Cc: A <author@example.com>,
	One <one@example.com>,
	two@example.com,
	C O Mitter <committer@example.com>
584 585 586 587 588 589 590
Subject: [PATCH 1/1] Second.
Date: DATE-STRING
Message-Id: MESSAGE-ID-STRING
X-Mailer: X-MAILER-STRING

Result: OK
EOF
591
"
592

593
test_expect_success $PREREQ '--suppress-cc=bodycc' '
594 595 596
	test_suppression bodycc
'

597
test_expect_success $PREREQ 'setup expect' "
598 599 600 601 602 603 604
cat >expected-suppress-cc <<\EOF
0001-Second.patch
(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
(body) Adding cc: C O Mitter <committer@example.com> from line 'Signed-off-by: C O Mitter <committer@example.com>'
Dry-OK. Log says:
Server: relay.example.com
MAIL FROM:<from@example.com>
605 606 607
RCPT TO:<to@example.com>
RCPT TO:<author@example.com>
RCPT TO:<committer@example.com>
608 609
From: Example <from@example.com>
To: to@example.com
610 611
Cc: A <author@example.com>,
	C O Mitter <committer@example.com>
612 613 614 615 616 617 618
Subject: [PATCH 1/1] Second.
Date: DATE-STRING
Message-Id: MESSAGE-ID-STRING
X-Mailer: X-MAILER-STRING

Result: OK
EOF
619
"
620

621
test_expect_success $PREREQ '--suppress-cc=cc' '
622
	test_suppression cc
623 624
'

625 626 627 628 629 630 631
test_confirm () {
	echo y | \
		GIT_SEND_EMAIL_NOTTY=1 \
		git send-email \
		--from="Example <nobody@example.com>" \
		--to=nobody@example.com \
		--smtp-server="$(pwd)/fake.sendmail" \
632 633
		$@ $patches > stdout &&
	grep "Send this email" stdout
634 635
}

636
test_expect_success $PREREQ '--confirm=always' '
637 638 639
	test_confirm --confirm=always --suppress-cc=all
'

640
test_expect_success $PREREQ '--confirm=auto' '
641 642 643
	test_confirm --confirm=auto
'

644
test_expect_success $PREREQ '--confirm=cc' '
645 646 647
	test_confirm --confirm=cc
'

648
test_expect_success $PREREQ '--confirm=compose' '
649 650 651
	test_confirm --confirm=compose --compose
'

652
test_expect_success $PREREQ 'confirm by default (due to cc)' '
653 654
	CONFIRM=$(git config --get sendemail.confirm) &&
	git config --unset sendemail.confirm &&
655 656 657 658
	test_confirm
	ret="$?"
	git config sendemail.confirm ${CONFIRM:-never}
	test $ret = "0"
659 660
'

661
test_expect_success $PREREQ 'confirm by default (due to --compose)' '
662 663 664 665 666 667 668 669
	CONFIRM=$(git config --get sendemail.confirm) &&
	git config --unset sendemail.confirm &&
	test_confirm --suppress-cc=all --compose
	ret="$?"
	git config sendemail.confirm ${CONFIRM:-never}
	test $ret = "0"
'

670
test_expect_success $PREREQ 'confirm detects EOF (inform assumes y)' '
671 672
	CONFIRM=$(git config --get sendemail.confirm) &&
	git config --unset sendemail.confirm &&
673 674
	rm -fr outdir &&
	git format-patch -2 -o outdir &&
675 676 677 678 679
	GIT_SEND_EMAIL_NOTTY=1 \
		git send-email \
			--from="Example <nobody@example.com>" \
			--to=nobody@example.com \
			--smtp-server="$(pwd)/fake.sendmail" \
680
			outdir/*.patch < /dev/null
681 682 683 684 685
	ret="$?"
	git config sendemail.confirm ${CONFIRM:-never}
	test $ret = "0"
'

686
test_expect_success $PREREQ 'confirm detects EOF (auto causes failure)' '
687 688
	CONFIRM=$(git config --get sendemail.confirm) &&
	git config sendemail.confirm auto &&
689 690
	GIT_SEND_EMAIL_NOTTY=1 &&
	export GIT_SEND_EMAIL_NOTTY &&
691 692 693 694 695 696 697 698 699 700
		test_must_fail git send-email \
			--from="Example <nobody@example.com>" \
			--to=nobody@example.com \
			--smtp-server="$(pwd)/fake.sendmail" \
			$patches < /dev/null
	ret="$?"
	git config sendemail.confirm ${CONFIRM:-never}
	test $ret = "0"
'

701
test_expect_success $PREREQ 'confirm doesnt loop forever' '
702 703
	CONFIRM=$(git config --get sendemail.confirm) &&
	git config sendemail.confirm auto &&
704 705 706
	GIT_SEND_EMAIL_NOTTY=1 &&
	export GIT_SEND_EMAIL_NOTTY &&
		yes "bogus" | test_must_fail git send-email \
707 708 709 710 711 712 713 714 715
			--from="Example <nobody@example.com>" \
			--to=nobody@example.com \
			--smtp-server="$(pwd)/fake.sendmail" \
			$patches
	ret="$?"
	git config sendemail.confirm ${CONFIRM:-never}
	test $ret = "0"
'

716
test_expect_success $PREREQ 'utf8 Cc is rfc2047 encoded' '
717 718 719 720 721 722 723 724
	clean_fake_sendmail &&
	rm -fr outdir &&
	git format-patch -1 -o outdir --cc="àéìöú <utf8@example.com>" &&
	git send-email \
	--from="Example <nobody@example.com>" \
	--to=nobody@example.com \
	--smtp-server="$(pwd)/fake.sendmail" \
	outdir/*.patch &&
725
	grep "^	" msgtxt1 |
726
	grep "=?UTF-8?q?=C3=A0=C3=A9=C3=AC=C3=B6=C3=BA?= <utf8@example.com>"
727 728
'

729
test_expect_success $PREREQ '--compose adds MIME for utf8 body' '
730
	clean_fake_sendmail &&
731
	(echo "#!$SHELL_PATH" &&
732
	 echo "echo utf8 body: àéìöú >>\"\$1\""
733 734
	) >fake-editor-utf8 &&
	chmod +x fake-editor-utf8 &&
735
	  GIT_EDITOR="\"$(pwd)/fake-editor-utf8\"" \
736 737 738 739 740 741 742
	  git send-email \
	  --compose --subject foo \
	  --from="Example <nobody@example.com>" \
	  --to=nobody@example.com \
	  --smtp-server="$(pwd)/fake.sendmail" \
	  $patches &&
	grep "^utf8 body" msgtxt1 &&
743
	grep "^Content-Type: text/plain; charset=UTF-8" msgtxt1
744 745
'

746
test_expect_success $PREREQ '--compose respects user mime type' '
747
	clean_fake_sendmail &&
748
	(echo "#!$SHELL_PATH" &&
749 750 751 752 753
	 echo "(echo MIME-Version: 1.0"
	 echo " echo Content-Type: text/plain\\; charset=iso-8859-1"
	 echo " echo Content-Transfer-Encoding: 8bit"
	 echo " echo Subject: foo"
	 echo " echo "
754
	 echo " echo utf8 body: àéìöú) >\"\$1\""
755 756
	) >fake-editor-utf8-mime &&
	chmod +x fake-editor-utf8-mime &&
757
	  GIT_EDITOR="\"$(pwd)/fake-editor-utf8-mime\"" \
758 759 760 761 762 763 764 765
	  git send-email \
	  --compose --subject foo \
	  --from="Example <nobody@example.com>" \
	  --to=nobody@example.com \
	  --smtp-server="$(pwd)/fake.sendmail" \
	  $patches &&
	grep "^utf8 body" msgtxt1 &&
	grep "^Content-Type: text/plain; charset=iso-8859-1" msgtxt1 &&
766
	! grep "^Content-Type: text/plain; charset=UTF-8" msgtxt1
767 768
'

769
test_expect_success $PREREQ '--compose adds MIME for utf8 subject' '
770
	clean_fake_sendmail &&
771
	  GIT_EDITOR="\"$(pwd)/fake-editor\"" \
772 773 774 775 776 777 778
	  git send-email \
	  --compose --subject utf8-sübjëct \
	  --from="Example <nobody@example.com>" \
	  --to=nobody@example.com \
	  --smtp-server="$(pwd)/fake.sendmail" \
	  $patches &&
	grep "^fake edit" msgtxt1 &&
779
	grep "^Subject: =?UTF-8?q?utf8-s=C3=BCbj=C3=ABct?=" msgtxt1
780 781
'

782
test_expect_success $PREREQ 'detects ambiguous reference/file conflict' '
783 784 785 786 787 788 789
	echo master > master &&
	git add master &&
	git commit -m"add master" &&
	test_must_fail git send-email --dry-run master 2>errors &&
	grep disambiguate errors
'

790
test_expect_success $PREREQ 'feed two files' '
791 792
	rm -fr outdir &&
	git format-patch -2 -o outdir &&
793
	git send-email \
794 795 796 797 798 799 800 801 802
	--dry-run \
	--from="Example <nobody@example.com>" \
	--to=nobody@example.com \
	outdir/000?-*.patch 2>errors >out &&
	grep "^Subject: " out >subjects &&
	test "z$(sed -n -e 1p subjects)" = "zSubject: [PATCH 1/2] Second." &&
	test "z$(sed -n -e 2p subjects)" = "zSubject: [PATCH 2/2] add master"
'

803
test_expect_success $PREREQ 'in-reply-to but no threading' '
804 805 806 807 808
	git send-email \
		--dry-run \
		--from="Example <nobody@example.com>" \
		--to=nobody@example.com \
		--in-reply-to="<in-reply-id@example.com>" \
809
		--nothread \
810 811 812 813
		$patches |
	grep "In-Reply-To: <in-reply-id@example.com>"
'

814
test_expect_success $PREREQ 'no in-reply-to and no threading' '
815 816 817 818 819 820 821 822 823
	git send-email \
		--dry-run \
		--from="Example <nobody@example.com>" \
		--to=nobody@example.com \
		--nothread \
		$patches $patches >stdout &&
	! grep "In-Reply-To: " stdout
'

824
test_expect_success $PREREQ 'threading but no chain-reply-to' '
825 826 827 828 829 830 831 832 833 834
	git send-email \
		--dry-run \
		--from="Example <nobody@example.com>" \
		--to=nobody@example.com \
		--thread \
		--nochain-reply-to \
		$patches $patches >stdout &&
	grep "In-Reply-To: " stdout
'

835
test_expect_success $PREREQ 'warning with an implicit --chain-reply-to' '
836 837 838 839 840 841 842 843
	git send-email \
	--dry-run \
	--from="Example <nobody@example.com>" \
	--to=nobody@example.com \
	outdir/000?-*.patch 2>errors >out &&
	grep "no-chain-reply-to" errors
'

844
test_expect_success $PREREQ 'no warning with an explicit --chain-reply-to' '
845 846 847 848 849 850 851 852 853
	git send-email \
	--dry-run \
	--from="Example <nobody@example.com>" \
	--to=nobody@example.com \
	--chain-reply-to \
	outdir/000?-*.patch 2>errors >out &&
	! grep "no-chain-reply-to" errors
'

854
test_expect_success $PREREQ 'no warning with an explicit --no-chain-reply-to' '
855 856 857 858
	git send-email \
	--dry-run \
	--from="Example <nobody@example.com>" \
	--to=nobody@example.com \
859
	--nochain-reply-to \
860 861 862 863
	outdir/000?-*.patch 2>errors >out &&
	! grep "no-chain-reply-to" errors
'

864
test_expect_success $PREREQ 'no warning with sendemail.chainreplyto = false' '
865 866 867 868 869 870 871 872 873
	git config sendemail.chainreplyto false &&
	git send-email \
	--dry-run \
	--from="Example <nobody@example.com>" \
	--to=nobody@example.com \
	outdir/000?-*.patch 2>errors >out &&
	! grep "no-chain-reply-to" errors
'

874
test_expect_success $PREREQ 'no warning with sendemail.chainreplyto = true' '
875 876 877 878 879 880 881 882 883
	git config sendemail.chainreplyto true &&
	git send-email \
	--dry-run \
	--from="Example <nobody@example.com>" \
	--to=nobody@example.com \
	outdir/000?-*.patch 2>errors >out &&
	! grep "no-chain-reply-to" errors
'

884
test_expect_success $PREREQ 'sendemail.to works' '
885 886 887 888 889 890 891 892
	git config --replace-all sendemail.to "Somebody <somebody@ex.com>" &&
	git send-email \
		--dry-run \
		--from="Example <nobody@example.com>" \
		$patches $patches >stdout &&
	grep "To: Somebody <somebody@ex.com>" stdout
'

893
test_expect_success $PREREQ '--no-to overrides sendemail.to' '
894 895 896 897 898 899 900 901 902 903
	git send-email \
		--dry-run \
		--from="Example <nobody@example.com>" \
		--no-to \
		--to=nobody@example.com \
		$patches $patches >stdout &&
	grep "To: nobody@example.com" stdout &&
	! grep "To: Somebody <somebody@ex.com>" stdout
'

904
test_expect_success $PREREQ 'sendemail.cc works' '
905 906 907 908 909 910 911 912 913
	git config --replace-all sendemail.cc "Somebody <somebody@ex.com>" &&
	git send-email \
		--dry-run \
		--from="Example <nobody@example.com>" \
		--to=nobody@example.com \
		$patches $patches >stdout &&
	grep "Cc: Somebody <somebody@ex.com>" stdout
'

914
test_expect_success $PREREQ '--no-cc overrides sendemail.cc' '
915 916 917 918 919 920 921 922 923 924 925
	git send-email \
		--dry-run \
		--from="Example <nobody@example.com>" \
		--no-cc \
		--cc=bodies@example.com \
		--to=nobody@example.com \
		$patches $patches >stdout &&
	grep "Cc: bodies@example.com" stdout &&
	! grep "Cc: Somebody <somebody@ex.com>" stdout
'

926
test_expect_success $PREREQ 'sendemail.bcc works' '
927 928 929 930 931 932 933 934 935 936
	git config --replace-all sendemail.bcc "Other <other@ex.com>" &&
	git send-email \
		--dry-run \
		--from="Example <nobody@example.com>" \
		--to=nobody@example.com \
		--smtp-server relay.example.com \
		$patches $patches >stdout &&
	grep "RCPT TO:<other@ex.com>" stdout
'

937
test_expect_success $PREREQ '--no-bcc overrides sendemail.bcc' '
938 939 940 941 942 943 944 945 946 947 948 949
	git send-email \
		--dry-run \
		--from="Example <nobody@example.com>" \
		--no-bcc \
		--bcc=bodies@example.com \
		--to=nobody@example.com \
		--smtp-server relay.example.com \
		$patches $patches >stdout &&
	grep "RCPT TO:<bodies@example.com>" stdout &&
	! grep "RCPT TO:<other@ex.com>" stdout
'

950
test_expect_success $PREREQ 'setup expect' '
951 952 953 954 955 956 957 958 959
cat >email-using-8bit <<EOF
From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
Message-Id: <bogus-message-id@example.com>
From: author@example.com
Date: Sat, 12 Jun 2010 15:53:58 +0200
Subject: subject goes here

Dieser deutsche Text enthält einen Umlaut!
EOF
960
'
961

962
test_expect_success $PREREQ 'setup expect' '
963 964 965 966 967
cat >content-type-decl <<EOF
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
EOF
968
'
969

970
test_expect_success $PREREQ 'asks about and fixes 8bit encodings' '
971 972 973 974 975 976 977 978
	clean_fake_sendmail &&
	echo |
	git send-email --from=author@example.com --to=nobody@example.com \
			--smtp-server="$(pwd)/fake.sendmail" \
			email-using-8bit >stdout &&
	grep "do not declare a Content-Transfer-Encoding" stdout &&
	grep email-using-8bit stdout &&
	grep "Which 8bit encoding" stdout &&
979
	egrep "Content|MIME" msgtxt1 >actual &&
980 981 982
	test_cmp actual content-type-decl
'

983
test_expect_success $PREREQ 'sendemail.8bitEncoding works' '
984 985 986 987 988 989
	clean_fake_sendmail &&
	git config sendemail.assume8bitEncoding UTF-8 &&
	echo bogus |
	git send-email --from=author@example.com --to=nobody@example.com \
			--smtp-server="$(pwd)/fake.sendmail" \
			email-using-8bit >stdout &&
990
	egrep "Content|MIME" msgtxt1 >actual &&
991 992 993
	test_cmp actual content-type-decl
'

994
test_expect_success $PREREQ '--8bit-encoding overrides sendemail.8bitEncoding' '
995 996 997 998 999 1000 1001
	clean_fake_sendmail &&
	git config sendemail.assume8bitEncoding "bogus too" &&
	echo bogus |
	git send-email --from=author@example.com --to=nobody@example.com \
			--smtp-server="$(pwd)/fake.sendmail" \
			--8bit-encoding=UTF-8 \
			email-using-8bit >stdout &&
1002
	egrep "Content|MIME" msgtxt1 >actual &&
1003 1004 1005
	test_cmp actual content-type-decl
'

1006
test_expect_success $PREREQ 'setup expect' '
1007 1008 1009 1010 1011 1012 1013 1014 1015
cat >email-using-8bit <<EOF
From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
Message-Id: <bogus-message-id@example.com>
From: author@example.com
Date: Sat, 12 Jun 2010 15:53:58 +0200
Subject: Dieser Betreff enthält auch einen Umlaut!

Nothing to see here.
EOF
1016
'
1017

1018
test_expect_success $PREREQ 'setup expect' '
1019 1020 1021
cat >expected <<EOF
Subject: =?UTF-8?q?Dieser=20Betreff=20enth=C3=A4lt=20auch=20einen=20Umlaut!?=
EOF
1022
'
1023

1024
test_expect_success $PREREQ '--8bit-encoding also treats subject' '
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
	clean_fake_sendmail &&
	echo bogus |
	git send-email --from=author@example.com --to=nobody@example.com \
			--smtp-server="$(pwd)/fake.sendmail" \
			--8bit-encoding=UTF-8 \
			email-using-8bit >stdout &&
	grep "Subject" msgtxt1 >actual &&
	test_cmp expected actual
'

1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
# Note that the patches in this test are deliberately out of order; we
# want to make sure it works even if the cover-letter is not in the
# first mail.
test_expect_success 'refusing to send cover letter template' '
	clean_fake_sendmail &&
	rm -fr outdir &&
	git format-patch --cover-letter -2 -o outdir &&
	test_must_fail git send-email \
	  --from="Example <nobody@example.com>" \
	  --to=nobody@example.com \
	  --smtp-server="$(pwd)/fake.sendmail" \
	  outdir/0002-*.patch \
	  outdir/0000-*.patch \
	  outdir/0001-*.patch \
	  2>errors >out &&
	grep "SUBJECT HERE" errors &&
	test -z "$(ls msgtxt*)"
'

test_expect_success '--force sends cover letter template anyway' '
	clean_fake_sendmail &&
	rm -fr outdir &&
	git format-patch --cover-letter -2 -o outdir &&
	git send-email \
	  --force \
	  --from="Example <nobody@example.com>" \
	  --to=nobody@example.com \
	  --smtp-server="$(pwd)/fake.sendmail" \
	  outdir/0002-*.patch \
	  outdir/0000-*.patch \
	  outdir/0001-*.patch \
	  2>errors >out &&
	! grep "SUBJECT HERE" errors &&
	test -n "$(ls msgtxt*)"
'

1071
test_done