8 while [ -n "$dir" ]; do
15 if [ "$dir" = "/" ]; then
23 increment_mtime_amount=0
28 increment_mtime_amount=$((increment_mtime_amount + 1))
29 touch -d "+${increment_mtime_amount} seconds" $dir
32 # Generate a new message in the mail directory, with a unique message
33 # ID and subject. The message is not added to the index.
35 # After this function returns, the filename of the generated message
36 # is available as $gen_msg_filename and the message ID is available as
39 # This function supports named parameters with the bash syntax for
40 # assigning a value to an associative array ([name]=value). The
41 # supported parameters are:
43 # [dir]=directory/of/choice
45 # Generate the message in directory 'directory/of/choice' within
46 # the mail store. The directory will be created if necessary.
50 # Text to use as the body of the email message
52 # '[from]="Some User <user@example.com>"'
53 # '[to]="Some User <user@example.com>"'
54 # '[subject]="Subject of email message"'
55 # '[date]="RFC 822 Date"'
57 # Values for email headers. If not provided, default values will
58 # be generated instead.
60 # '[cc]="Some User <user@example.com>"'
61 # [reply-to]=some-address
62 # [in-reply-to]=<message-id>
64 # Additional values for email headers. If these are not provided
65 # then the relevant headers will simply not appear in the
70 # Controls the message-id of the created message.
76 # This is our (bash-specific) magic for doing named parameters
77 local -A template="($@)"
78 local additional_headers
80 if [ -z "${template[id]}" ]; then
81 gen_msg_cnt=$((gen_msg_cnt + 1))
82 gen_msg_name=msg-$(printf "%03d" $gen_msg_cnt)
83 gen_msg_id="${gen_msg_name}@notmuch-test-suite"
85 gen_msg_name="msg-${template[id]}"
86 gen_msg_id="${template[id]}"
89 if [ -z "${template[dir]}" ]; then
90 gen_msg_filename="${MAIL_DIR}/$gen_msg_name"
92 gen_msg_filename="${MAIL_DIR}/${template[dir]}/$gen_msg_name"
93 mkdir -p $(dirname $gen_msg_filename)
96 if [ -z "${template[body]}" ]; then
97 template[body]="This is just a test message at ${gen_msg_filename}"
100 if [ -z "${template[from]}" ]; then
101 template[from]="Notmuch Test Suite <test_suite@notmuchmail.org>"
104 if [ -z "${template[to]}" ]; then
105 template[to]="Notmuch Test Suite <test_suite@notmuchmail.org>"
108 if [ -z "${template[subject]}" ]; then
109 template[subject]="Test message ${gen_msg_filename}"
112 if [ -z "${template[date]}" ]; then
113 template[date]="Tue, 05 Jan 2010 15:43:57 -0800"
116 additional_headers=""
117 if [ ! -z "${template[reply-to]}" ]; then
118 additional_headers="Reply-To: ${template[reply-to]}
119 ${additional_headers}"
122 if [ ! -z "${template[in-reply-to]}" ]; then
123 additional_headers="In-Reply-To: ${template[in-reply-to]}
124 ${additional_headers}"
127 if [ ! -z "${template[cc]}" ]; then
128 additional_headers="Cc: ${template[cc]}
129 ${additional_headers}"
132 cat <<EOF >$gen_msg_filename
133 From: ${template[from]}
135 Message-Id: <${gen_msg_id}>
136 Subject: ${template[subject]}
137 Date: ${template[date]}
138 ${additional_headers}
142 # Ensure that the mtime of the containing directory is updated
143 increment_mtime $(dirname ${gen_msg_filename})
146 # Generate a new message and add it to the index.
148 # All of the arguments and return values supported by generate_message
149 # are alos supported here, so see that function for details.
152 generate_message "$@"
154 $NOTMUCH new > /dev/null
157 NOTMUCH_IGNORED_OUTPUT_REGEXP='^Processed [0-9]*( total)? file|Found [0-9]* total file'
158 NOTMUCH_THREAD_ID_SQUELCH='s/thread:................/thread:XXX/'
164 output=$($NOTMUCH $args | grep -v -E -e "$NOTMUCH_IGNORED_OUTPUT_REGEXP" | sed -e "$NOTMUCH_THREAD_ID_SQUELCH" || true)
165 if [ "$output" = "$expected" ]; then
169 echo " Expected output: $expected"
170 echo " Actual output: $output"
174 TEST_DIR=$(pwd)/test.$$
175 MAIL_DIR=${TEST_DIR}/mail
176 export NOTMUCH_CONFIG=${TEST_DIR}/notmuch-config
177 NOTMUCH=$(find_notmuch_binary $(pwd))
185 cat <<EOF > ${NOTMUCH_CONFIG}
190 name=Notmuch Test Suite
191 primary_email=test_suite@notmuchmail.org
192 other_email=test_suite_other@notmuchmail.org
195 printf "Testing \"notmuch new\" in several variations:\n"
196 printf " No new messages...\t\t"
197 execute_expecting new "No new mail."
199 printf " Single new message...\t\t"
201 execute_expecting new "Added 1 new message to the database."
203 printf " Multiple new messages...\t"
206 execute_expecting new "Added 2 new messages to the database."
208 printf " No new messages (non-empty DB)... "
209 execute_expecting new "No new mail."
211 printf " New directories...\t\t"
212 rm -rf ${MAIL_DIR}/* ${MAIL_DIR}/.notmuch
213 mkdir ${MAIL_DIR}/def
214 mkdir ${MAIL_DIR}/ghi
215 generate_message [dir]=def
217 execute_expecting new "Added 1 new message to the database."
219 printf " Alternate inode order...\t"
221 rm -rf ${MAIL_DIR}/.notmuch
222 mv ${MAIL_DIR}/ghi ${MAIL_DIR}/abc
224 generate_message [dir]=abc
226 execute_expecting new "Added 1 new message to the database."
228 printf " Message moved in...\t\t"
229 rm -rf ${MAIL_DIR}/* ${MAIL_DIR}/.notmuch
231 tmp_msg_filename=tmp/$gen_msg_filename
232 mkdir -p $(dirname $tmp_msg_filename)
233 mv $gen_msg_filename $tmp_msg_filename
234 increment_mtime ${MAIL_DIR}
235 $NOTMUCH new > /dev/null
236 mv $tmp_msg_filename $gen_msg_filename
237 increment_mtime ${MAIL_DIR}
238 execute_expecting new "Added 1 new message to the database."
240 printf " Renamed message...\t\t"
243 $NOTMUCH new > /dev/null
244 mv $gen_msg_filename ${gen_msg_filename}-renamed
245 increment_mtime ${MAIL_DIR}
246 execute_expecting new "No new mail. Detected 1 file rename."
248 printf " Deleted message...\t\t"
250 rm ${gen_msg_filename}-renamed
251 increment_mtime ${MAIL_DIR}
252 execute_expecting new "No new mail. Removed 1 message."
254 printf " Renamed directory...\t\t"
256 generate_message [dir]=dir
257 generate_message [dir]=dir
258 generate_message [dir]=dir
260 $NOTMUCH new > /dev/null
262 mv ${MAIL_DIR}/dir ${MAIL_DIR}/dir-renamed
263 increment_mtime ${MAIL_DIR}
265 execute_expecting new "No new mail. Detected 3 file renames."
267 printf " Deleted directory...\t\t"
269 rm -rf ${MAIL_DIR}/dir-renamed
270 increment_mtime ${MAIL_DIR}
272 execute_expecting new "No new mail. Removed 3 messages."
274 printf " New directory (at end of list)... "
276 generate_message [dir]=zzz
277 generate_message [dir]=zzz
278 generate_message [dir]=zzz
280 execute_expecting new "Added 3 new messages to the database."
282 printf " Deleted directory (end of list)... "
284 rm -rf ${MAIL_DIR}/zzz
285 increment_mtime ${MAIL_DIR}
287 execute_expecting new "No new mail. Removed 3 messages."
289 printf " New symlink to directory...\t"
291 rm -rf ${MAIL_DIR}/.notmuch
292 mv ${MAIL_DIR} ${TEST_DIR}/actual_maildir
295 ln -s ${TEST_DIR}/actual_maildir ${MAIL_DIR}/symlink
297 execute_expecting new "Added 1 new message to the database."
299 printf " New symlink to a file...\t"
301 external_msg_filename=${TEST_DIR}/external/$(basename $gen_msg_filename)
302 mkdir -p $(dirname $external_msg_filename)
303 mv $gen_msg_filename $external_msg_filename
304 ln -s $external_msg_filename $gen_msg_filename
305 increment_mtime ${MAIL_DIR}
306 execute_expecting new "Added 1 new message to the database."
308 printf " New two-level directory...\t"
310 generate_message [dir]=two/levels
311 generate_message [dir]=two/levels
312 generate_message [dir]=two/levels
314 execute_expecting new "Added 3 new messages to the database."
316 printf " Deleted two-level directory... "
318 rm -rf ${MAIL_DIR}/two
319 increment_mtime ${MAIL_DIR}
321 execute_expecting new "No new mail. Removed 3 messages."
323 printf "\nTesting \"notmuch search\" in several variations:\n"
325 printf " Search body...\t\t\t"
326 add_message '[subject]="body search"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' [body]=bodysearchtest
327 execute_expecting "search bodysearchtest" "thread:XXX 2000-01-01 [1/1] Notmuch Test Suite; body search (inbox unread)"
329 printf " Search by from:...\t\t"
330 add_message '[subject]="search by from"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' [from]=searchbyfrom
331 execute_expecting "search from:searchbyfrom" "thread:XXX 2000-01-01 [1/1] searchbyfrom; search by from (inbox unread)"
333 printf " Search by to:...\t\t"
334 add_message '[subject]="search by to"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' [to]=searchbyto
335 execute_expecting "search to:searchbyto" "thread:XXX 2000-01-01 [1/1] Notmuch Test Suite; search by to (inbox unread)"
337 printf " Search by subject:...\t\t"
338 add_message [subject]=subjectsearchtest '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
339 execute_expecting "search subject:subjectsearchtest" "thread:XXX 2000-01-01 [1/1] Notmuch Test Suite; subjectsearchtest (inbox unread)"
341 printf " Search by id:...\t\t"
342 add_message '[subject]="search by id"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
343 execute_expecting "search id:${gen_msg_id}" "thread:XXX 2000-01-01 [1/1] Notmuch Test Suite; search by id (inbox unread)"
345 printf " Search by tag:...\t\t"
346 add_message '[subject]="search by tag"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
347 $NOTMUCH tag +searchbytag id:${gen_msg_id}
348 execute_expecting "search tag:searchbytag" "thread:XXX 2000-01-01 [1/1] Notmuch Test Suite; search by tag (inbox searchbytag unread)"
350 printf " Search by thread:...\t\t"
351 add_message '[subject]="search by thread"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
352 thread_id=$($NOTMUCH search id:${gen_msg_id} | sed -e 's/thread:\([a-f0-9]*\).*/\1/')
353 execute_expecting "search thread:${thread_id}" "thread:XXX 2000-01-01 [1/1] Notmuch Test Suite; search by thread (inbox unread)"
355 printf " Search body (phrase)...\t"
356 add_message '[subject]="body search (phrase)"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' '[body]="body search (phrase)"'
357 add_message '[subject]="negative result"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' '[body]="This phrase should not match the body search"'
358 execute_expecting "search '\"body search (phrase)\"'" "thread:XXX 2000-01-01 [1/1] Notmuch Test Suite; body search (phrase) (inbox unread)"
360 printf " Search by from: (address)...\t"
361 add_message '[subject]="search by from (address)"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' [from]=searchbyfrom@example.com
362 execute_expecting "search from:searchbyfrom@example.com" "thread:XXX 2000-01-01 [1/1] searchbyfrom@example.com; search by from (address) (inbox unread)"
364 printf " Search by from: (name)...\t"
365 add_message '[subject]="search by from (name)"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' '[from]="Search By From Name <test@example.com>"'
366 execute_expecting "search from:'Search By From Name'" "thread:XXX 2000-01-01 [1/1] Search By From Name; search by from (name) (inbox unread)"
368 printf " Search by to: (address)...\t"
369 add_message '[subject]="search by to (address)"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' [to]=searchbyto@example.com
370 execute_expecting "search to:searchbyto@example.com" "thread:XXX 2000-01-01 [1/1] Notmuch Test Suite; search by to (address) (inbox unread)"
372 printf " Search by to: (name)...\t"
373 add_message '[subject]="search by to (name)"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' '[to]="Search By To Name <test@example.com>"'
374 execute_expecting "search to:'Search By To Name'" "thread:XXX 2000-01-01 [1/1] Notmuch Test Suite; search by to (name) (inbox unread)"
376 printf " Search by subject: (phrase)...\t"
377 add_message '[subject]="subject search test (phrase)"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
378 add_message '[subject]="this phrase should not match the subject search test"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
379 execute_expecting "search 'subject:\"subject search test (phrase)\"'" "thread:XXX 2000-01-01 [1/1] Notmuch Test Suite; subject search test (phrase) (inbox unread)"
381 printf "\nTesting \"notmuch reply\" in several variations:\n"
383 printf " Basic reply...\t\t\t"
384 add_message '[from]="Sender <sender@example.com>"' \
385 [to]=test_suite@notmuchmail.org \
386 [subject]=notmuch-reply-test \
387 '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
388 '[body]="basic reply test"'
390 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite@notmuchmail.org>
391 Subject: Re: notmuch-reply-test
392 To: Sender <sender@example.com>
393 Bcc: test_suite@notmuchmail.org
394 In-Reply-To: <${gen_msg_id}>
395 References: <${gen_msg_id}>
397 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
400 printf " Multiple recipients...\t\t"
401 add_message '[from]="Sender <sender@example.com>"' \
402 '[to]="test_suite@notmuchmail.org, Someone Else <someone@example.com>"' \
403 [subject]=notmuch-reply-test \
404 '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
405 '[body]="Multiple recipients"'
407 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite@notmuchmail.org>
408 Subject: Re: notmuch-reply-test
409 To: Sender <sender@example.com>, Someone Else <someone@example.com>
410 Bcc: test_suite@notmuchmail.org
411 In-Reply-To: <${gen_msg_id}>
412 References: <${gen_msg_id}>
414 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
415 > Multiple recipients"
417 printf " Reply with CC...\t\t"
418 add_message '[from]="Sender <sender@example.com>"' \
419 [to]=test_suite@notmuchmail.org \
420 '[cc]="Other Parties <cc@example.com>"' \
421 [subject]=notmuch-reply-test \
422 '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
423 '[body]="reply with CC"'
425 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite@notmuchmail.org>
426 Subject: Re: notmuch-reply-test
427 To: Sender <sender@example.com>
428 Cc: Other Parties <cc@example.com>
429 Bcc: test_suite@notmuchmail.org
430 In-Reply-To: <${gen_msg_id}>
431 References: <${gen_msg_id}>
433 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
436 printf " Reply from alternate address..."
437 add_message '[from]="Sender <sender@example.com>"' \
438 [to]=test_suite_other@notmuchmail.org \
439 [subject]=notmuch-reply-test \
440 '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
441 '[body]="reply from alternate address"'
443 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite_other@notmuchmail.org>
444 Subject: Re: notmuch-reply-test
445 To: Sender <sender@example.com>
446 Bcc: test_suite@notmuchmail.org
447 In-Reply-To: <${gen_msg_id}>
448 References: <${gen_msg_id}>
450 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
451 > reply from alternate address"
453 printf " Support for Reply-To...\t"
454 add_message '[from]="Sender <sender@example.com>"' \
455 [to]=test_suite@notmuchmail.org \
456 [subject]=notmuch-reply-test \
457 '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
458 '[body]="support for reply-to"' \
459 '[reply-to]="Sender <elsewhere@example.com>"'
461 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite@notmuchmail.org>
462 Subject: Re: notmuch-reply-test
463 To: Sender <elsewhere@example.com>
464 Bcc: test_suite@notmuchmail.org
465 In-Reply-To: <${gen_msg_id}>
466 References: <${gen_msg_id}>
468 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
469 > support for reply-to"
471 printf " Un-munging Reply-To...\t\t"
472 add_message '[from]="Sender <sender@example.com>"' \
473 '[to]="Some List <list@example.com>"' \
474 [subject]=notmuch-reply-test \
475 '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
476 '[body]="Un-munging Reply-To"' \
477 '[reply-to]="Evil Munging List <list@example.com>"'
479 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite@notmuchmail.org>
480 Subject: Re: notmuch-reply-test
481 To: Sender <sender@example.com>, Some List <list@example.com>
482 Bcc: test_suite@notmuchmail.org
483 In-Reply-To: <${gen_msg_id}>
484 References: <${gen_msg_id}>
486 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
487 > Un-munging Reply-To"
489 printf "\nTesting handling of uuencoded data:\n"
491 add_message [subject]=uuencodetest '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' \
492 '[body]="This message is used to ensure that notmuch correctly handles a
493 message containing a block of uuencoded data. First, we have a marker
494 this content beforeuudata . Then we beging the uunencoded data itself:
496 begin 644 bogus-uuencoded-data
497 M0123456789012345678901234567890123456789012345678901234567890
498 MOBVIOUSLY, THIS IS NOT ANY SORT OF USEFUL UUNECODED DATA.
499 MINSTEAD THIS IS JUST A WAY TO ENSURE THAT THIS BLOCK OF DATA
500 MIS CORRECTLY IGNORED WHEN NOTMUCH CREATES ITS INDEX. SO WE
501 MINCLUDE A DURINGUUDATA MARKER THAT SHOULD NOT RESULT IN ANY
506 Finally, we have our afteruudata marker as well."'
508 printf " Ensure content before uu data is indexed..."
509 execute_expecting "search beforeuudata" "thread:XXX 2000-01-01 [1/1] Notmuch Test Suite; uuencodetest (inbox unread)"
510 printf " Ensure uu data is not indexed...\t"
511 execute_expecting "search DURINGUUDATA" ""
512 printf " Ensure content after uu data is indexed..."
513 execute_expecting "search afteruudata" "thread:XXX 2000-01-01 [1/1] Notmuch Test Suite; uuencodetest (inbox unread)"
515 printf "\nTesting \"notmuch dump\" and \"notmuch restore\":\n"
517 printf " Dumping all tags...\t\t"
518 $NOTMUCH dump dump.expected
521 printf " Clearing all tags...\t\t"
522 sed -e 's/(\([^(]*\))$/()/' < dump.expected > clear.expected
523 $NOTMUCH restore clear.expected
524 $NOTMUCH dump clear.actual
525 if diff clear.expected clear.actual > /dev/null; then
529 echo " Expected output: See file clear.expected"
530 echo " Actual output: See file clear.actual"
533 printf " Restoring original tags...\t"
534 $NOTMUCH restore dump.expected
535 $NOTMUCH dump dump.actual
536 if diff dump.expected dump.actual > /dev/null; then
540 echo " Expected output: See file dump.expected"
541 echo " Actual output: See file dump.actual"
544 printf " Restore with nothing to do...\t"
545 $NOTMUCH restore dump.expected
548 printf "\nTesting threading when messages received out of order:\n"
549 printf " Adding initial child message...\t\t"
550 generate_message [body]=foo '[in-reply-to]=\<parent-id\>' [subject]=brokenthreadtest '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
551 execute_expecting new "Added 1 new message to the database."
552 printf " Searching returns the message...\t\t"
553 execute_expecting "search foo" "thread:XXX 2000-01-01 [1/1] Notmuch Test Suite; brokenthreadtest (inbox unread)"
554 printf " Adding second child message...\t\t\t"
555 generate_message [body]=foo '[in-reply-to]=\<parent-id\>' [subject]=brokenthreadtest '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
556 execute_expecting new "Added 1 new message to the database."
557 printf " Searching returns both messages in one thread..."
558 execute_expecting "search foo" "thread:XXX 2000-01-01 [2/2] Notmuch Test Suite; brokenthreadtest (inbox unread)"
559 printf " Adding parent message...\t\t\t"
560 generate_message [body]=foo [id]=parent-id [subject]=brokenthreadtest '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
561 execute_expecting new "Added 1 new message to the database."
562 printf " Searching returns all three messages in one thread..."
563 execute_expecting "search foo" "thread:XXX 2000-01-01 [3/3] Notmuch Test Suite; brokenthreadtest (inbox unread)"
566 Notmuch test suite complete.
568 Intermediate state can be examined in: