configure: Fix installation of library to work with alternate --prefix
[notmuch] / test / notmuch-test
1 #!/bin/bash
2 set -e
3
4 find_notmuch_binary ()
5 {
6     dir=$1
7
8     while [ -n "$dir" ]; do
9         bin=$dir/notmuch
10         if [ -x $bin ]; then
11             echo $bin
12             return
13         fi
14         dir=$(dirname $dir)
15         if [ "$dir" = "/" ]; then
16             break
17         fi
18     done
19
20     echo notmuch
21 }
22
23 increment_mtime_amount=0
24 increment_mtime ()
25 {
26     dir=$1
27
28     increment_mtime_amount=$((increment_mtime_amount + 1))
29     touch -d "+${increment_mtime_amount} seconds" $dir
30 }
31
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.
34 #
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
37 # $gen_msg_id .
38 #
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:
42 #
43 #  [dir]=directory/of/choice
44 #
45 #       Generate the message in directory 'directory/of/choice' within
46 #       the mail store. The directory will be created if necessary.
47 #
48 #  [body]=text
49 #
50 #       Text to use as the body of the email message
51 #
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"'
56 #
57 #       Values for email headers. If not provided, default values will
58 #       be generated instead.
59 #
60 #  '[cc]="Some User <user@example.com>"'
61 #  [reply-to]=some-address
62 #  [in-reply-to]=<message-id>
63 #
64 #       Additional values for email headers. If these are not provided
65 #       then the relevant headers will simply not appear in the
66 #       message.
67 gen_msg_cnt=0
68 gen_msg_filename=""
69 gen_msg_id=""
70 generate_message ()
71 {
72     # This is our (bash-specific) magic for doing named parameters
73     local -A template="($@)"
74     local additional_headers
75
76     gen_msg_cnt=$((gen_msg_cnt + 1))
77     gen_msg_name=msg-$(printf "%03d" $gen_msg_cnt)
78     gen_msg_id="${gen_msg_name}@notmuch-test-suite"
79
80     if [ -z "${template[dir]}" ]; then
81         gen_msg_filename="${MAIL_DIR}/$gen_msg_name"
82     else
83         gen_msg_filename="${MAIL_DIR}/${template[dir]}/$gen_msg_name"
84         mkdir -p $(dirname $gen_msg_filename)
85     fi
86
87     if [ -z "${template[body]}" ]; then
88         template[body]="This is just a test message at ${gen_msg_filename}"
89     fi
90
91     if [ -z "${template[from]}" ]; then
92         template[from]="Notmuch Test Suite <test_suite@notmuchmail.org>"
93     fi
94
95     if [ -z "${template[to]}" ]; then
96         template[to]="Notmuch Test Suite <test_suite@notmuchmail.org>"
97     fi
98
99     if [ -z "${template[subject]}" ]; then
100         template[subject]="Test message ${gen_msg_filename}"
101     fi
102
103     if [ -z "${template[date]}" ]; then
104         template[date]="Tue, 05 Jan 2010 15:43:57 -0800"
105     fi
106
107     additional_headers=""
108     if [ ! -z "${template[reply-to]}" ]; then
109         additional_headers="Reply-To: ${template[reply-to]}
110 ${additional_headers}"
111     fi
112
113     if [ ! -z "${template[in-reply-to]}" ]; then
114         additional_headers="In-Reply-To: ${template[in-reply-to]}
115 ${additional_headers}"
116     fi
117
118     if [ ! -z "${template[cc]}" ]; then
119         additional_headers="Cc: ${template[cc]}
120 ${additional_headers}"
121     fi
122
123 cat <<EOF >$gen_msg_filename
124 From: ${template[from]}
125 To: ${template[to]}
126 Message-Id: <${gen_msg_id}>
127 Subject: ${template[subject]}
128 Date: ${template[date]}
129 ${additional_headers}
130 ${template[body]}
131 EOF
132
133     # Ensure that the mtime of the containing directory is updated
134     increment_mtime $(dirname ${gen_msg_filename})
135 }
136
137 # Generate a new message and add it to the index.
138 #
139 # All of the arguments and return values supported by generate_message
140 # are alos supported here, so see that function for details.
141 add_message ()
142 {
143     generate_message "$@"
144
145     $NOTMUCH new > /dev/null
146 }
147
148 NOTMUCH_IGNORED_OUTPUT_REGEXP='^Processed [0-9]*( total)? file|Found [0-9]* total file'
149 NOTMUCH_THREAD_ID_SQUELCH='s/thread:................/thread:XXX/'
150 execute_expecting ()
151 {
152     args=$1
153     expected=$2
154
155     output=$($NOTMUCH $args | grep -v -E -e "$NOTMUCH_IGNORED_OUTPUT_REGEXP" | sed -e "$NOTMUCH_THREAD_ID_SQUELCH" || true)
156     if [ "$output" = "$expected" ]; then
157         echo "  PASS"
158     else
159         echo "  FAIL"
160         echo "  Expected output: $expected"
161         echo "  Actual output:   $output"
162     fi
163 }
164
165 TEST_DIR=$(pwd)/test.$$
166 MAIL_DIR=${TEST_DIR}/mail
167 export NOTMUCH_CONFIG=${TEST_DIR}/notmuch-config
168 NOTMUCH=$(find_notmuch_binary $(pwd))
169
170 rm -rf ${TEST_DIR}
171 mkdir ${TEST_DIR}
172 cd ${TEST_DIR}
173
174 mkdir ${MAIL_DIR}
175
176 cat <<EOF > ${NOTMUCH_CONFIG}
177 [database]
178 path=${MAIL_DIR}
179
180 [user]
181 name=Notmuch Test Suite
182 primary_email=test_suite@notmuchmail.org
183 other_email=test_suite_other@notmuchmail.org
184 EOF
185
186 printf "Testing \"notmuch new\" in several variations:\n"
187 printf " No new messages...\t\t"
188 execute_expecting new "No new mail."
189
190 printf " Single new message...\t\t"
191 generate_message
192 execute_expecting new "Added 1 new message to the database."
193
194 printf " Multiple new messages...\t"
195 generate_message
196 generate_message
197 execute_expecting new "Added 2 new messages to the database."
198
199 printf " No new messages (non-empty DB)... "
200 execute_expecting new "No new mail."
201
202 printf " New directories...\t\t"
203 rm -rf ${MAIL_DIR}/* ${MAIL_DIR}/.notmuch
204 mkdir ${MAIL_DIR}/def
205 mkdir ${MAIL_DIR}/ghi
206 generate_message [dir]=def
207
208 execute_expecting new "Added 1 new message to the database."
209
210 printf " Alternate inode order...\t"
211
212 rm -rf ${MAIL_DIR}/.notmuch
213 mv ${MAIL_DIR}/ghi ${MAIL_DIR}/abc
214 rm ${MAIL_DIR}/def/*
215 generate_message [dir]=abc
216
217 execute_expecting new "Added 1 new message to the database."
218
219 printf " Message moved in...\t\t"
220 rm -rf ${MAIL_DIR}/* ${MAIL_DIR}/.notmuch
221 generate_message
222 tmp_msg_filename=tmp/$gen_msg_filename
223 mkdir -p $(dirname $tmp_msg_filename)
224 mv $gen_msg_filename $tmp_msg_filename
225 increment_mtime ${MAIL_DIR}
226 $NOTMUCH new > /dev/null
227 mv $tmp_msg_filename $gen_msg_filename
228 increment_mtime ${MAIL_DIR}
229 execute_expecting new "Added 1 new message to the database."
230
231 printf " Renamed message...\t\t"
232
233 generate_message
234 $NOTMUCH new > /dev/null
235 mv $gen_msg_filename ${gen_msg_filename}-renamed
236 increment_mtime ${MAIL_DIR}
237 execute_expecting new "No new mail. Detected 1 file rename."
238
239 printf " Deleted message...\t\t"
240
241 rm ${gen_msg_filename}-renamed
242 increment_mtime ${MAIL_DIR}
243 execute_expecting new "No new mail. Removed 1 message."
244
245 printf " Renamed directory...\t\t"
246
247 generate_message [dir]=dir
248 generate_message [dir]=dir
249 generate_message [dir]=dir
250
251 $NOTMUCH new > /dev/null
252
253 mv ${MAIL_DIR}/dir ${MAIL_DIR}/dir-renamed
254 increment_mtime ${MAIL_DIR}
255
256 execute_expecting new "No new mail. Detected 3 file renames."
257
258 printf " Deleted directory...\t\t"
259
260 rm -rf ${MAIL_DIR}/dir-renamed
261 increment_mtime ${MAIL_DIR}
262
263 execute_expecting new "No new mail. Removed 3 messages."
264
265 printf " New directory (at end of list)... "
266
267 generate_message [dir]=zzz
268 generate_message [dir]=zzz
269 generate_message [dir]=zzz
270
271 execute_expecting new "Added 3 new messages to the database."
272
273 printf " Deleted directory (end of list)... "
274
275 rm -rf ${MAIL_DIR}/zzz
276 increment_mtime ${MAIL_DIR}
277
278 execute_expecting new "No new mail. Removed 3 messages."
279
280 printf " New symlink to directory...\t"
281
282 rm -rf ${MAIL_DIR}/.notmuch
283 mv ${MAIL_DIR} ${TEST_DIR}/actual_maildir
284
285 mkdir ${MAIL_DIR}
286 ln -s ${TEST_DIR}/actual_maildir ${MAIL_DIR}/symlink
287
288 execute_expecting new "Added 1 new message to the database."
289
290 printf " New symlink to a file...\t"
291 generate_message
292 external_msg_filename=${TEST_DIR}/external/$(basename $gen_msg_filename)
293 mkdir -p $(dirname $external_msg_filename)
294 mv $gen_msg_filename $external_msg_filename
295 ln -s $external_msg_filename $gen_msg_filename
296 increment_mtime ${MAIL_DIR}
297 execute_expecting new "Added 1 new message to the database."
298
299 printf " New two-level directory...\t"
300
301 generate_message [dir]=two/levels
302 generate_message [dir]=two/levels
303 generate_message [dir]=two/levels
304
305 execute_expecting new "Added 3 new messages to the database."
306
307 printf " Deleted two-level directory... "
308
309 rm -rf ${MAIL_DIR}/two
310 increment_mtime ${MAIL_DIR}
311
312 execute_expecting new "No new mail. Removed 3 messages."
313
314 printf "\nTesting \"notmuch search\" in several variations:\n"
315
316 printf " Search body...\t\t\t"
317 add_message '[subject]="body search"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' [body]=bodysearchtest
318 execute_expecting "search bodysearchtest" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; body search (inbox unread)"
319
320 printf " Search by from:...\t\t"
321 add_message '[subject]="search by from"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' [from]=searchbyfrom
322 execute_expecting "search from:searchbyfrom" "thread:XXX   2000-01-01 [1/1] searchbyfrom; search by from (inbox unread)"
323
324 printf " Search by to:...\t\t"
325 add_message '[subject]="search by to"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' [to]=searchbyto
326 execute_expecting "search to:searchbyto" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; search by to (inbox unread)"
327
328 printf " Search by subject:...\t\t"
329 add_message [subject]=subjectsearchtest '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
330 execute_expecting "search subject:subjectsearchtest" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; subjectsearchtest (inbox unread)"
331
332 printf " Search by id:...\t\t"
333 add_message '[subject]="search by id"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
334 execute_expecting "search id:${gen_msg_id}" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; search by id (inbox unread)"
335
336 printf " Search by tag:...\t\t"
337 add_message '[subject]="search by tag"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
338 $NOTMUCH tag +searchbytag id:${gen_msg_id}
339 execute_expecting "search tag:searchbytag" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; search by tag (inbox searchbytag unread)"
340
341 printf " Search by thread:...\t\t"
342 add_message '[subject]="search by thread"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
343 thread_id=$($NOTMUCH search id:${gen_msg_id} | sed -e 's/thread:\([a-f0-9]*\).*/\1/')
344 execute_expecting "search thread:${thread_id}" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; search by thread (inbox unread)"
345
346 printf " Search body (phrase)...\t"
347 add_message '[subject]="body search (phrase)"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' '[body]="body search (phrase)"'
348 add_message '[subject]="negative result"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' '[body]="This phrase should not match the body search"'
349 execute_expecting "search '\"body search (phrase)\"'" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; body search (phrase) (inbox unread)"
350
351 printf " Search by from: (address)...\t"
352 add_message '[subject]="search by from (address)"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' [from]=searchbyfrom@example.com
353 execute_expecting "search from:searchbyfrom@example.com" "thread:XXX   2000-01-01 [1/1] searchbyfrom@example.com; search by from (address) (inbox unread)"
354
355 printf " Search by from: (name)...\t"
356 add_message '[subject]="search by from (name)"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' '[from]="Search By From Name <test@example.com>"'
357 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)"
358
359 printf " Search by to: (address)...\t"
360 add_message '[subject]="search by to (address)"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' [to]=searchbyto@example.com
361 execute_expecting "search to:searchbyto@example.com" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; search by to (address) (inbox unread)"
362
363 printf " Search by to: (name)...\t"
364 add_message '[subject]="search by to (name)"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' '[to]="Search By To Name <test@example.com>"'
365 execute_expecting "search to:'Search By To Name'" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; search by to (name) (inbox unread)"
366
367 printf " Search by subject: (phrase)...\t"
368 add_message '[subject]="subject search test (phrase)"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
369 add_message '[subject]="this phrase should not match the subject search test"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
370 execute_expecting "search 'subject:\"subject search test (phrase)\"'" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; subject search test (phrase) (inbox unread)"
371
372 printf "\nTesting \"notmuch reply\" in several variations:\n"
373
374 printf " Basic reply...\t\t\t"
375 add_message '[from]="Sender <sender@example.com>"' \
376              [to]=test_suite@notmuchmail.org \
377              [subject]=notmuch-reply-test \
378             '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
379             '[body]="basic reply test"'
380
381 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite@notmuchmail.org>
382 Subject: Re: notmuch-reply-test
383 To: Sender <sender@example.com>
384 Bcc: test_suite@notmuchmail.org
385 In-Reply-To: <${gen_msg_id}>
386 References:  <${gen_msg_id}>
387
388 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
389 > basic reply test"
390
391 printf " Multiple recipients...\t\t"
392 add_message '[from]="Sender <sender@example.com>"' \
393             '[to]="test_suite@notmuchmail.org, Someone Else <someone@example.com>"' \
394              [subject]=notmuch-reply-test \
395             '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
396             '[body]="Multiple recipients"'
397
398 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite@notmuchmail.org>
399 Subject: Re: notmuch-reply-test
400 To: Sender <sender@example.com>, Someone Else <someone@example.com>
401 Bcc: test_suite@notmuchmail.org
402 In-Reply-To: <${gen_msg_id}>
403 References:  <${gen_msg_id}>
404
405 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
406 > Multiple recipients"
407
408 printf " Reply with CC...\t\t"
409 add_message '[from]="Sender <sender@example.com>"' \
410              [to]=test_suite@notmuchmail.org \
411             '[cc]="Other Parties <cc@example.com>"' \
412              [subject]=notmuch-reply-test \
413             '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
414             '[body]="reply with CC"'
415
416 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite@notmuchmail.org>
417 Subject: Re: notmuch-reply-test
418 To: Sender <sender@example.com>
419 Cc: Other Parties <cc@example.com>
420 Bcc: test_suite@notmuchmail.org
421 In-Reply-To: <${gen_msg_id}>
422 References:  <${gen_msg_id}>
423
424 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
425 > reply with CC"
426
427 printf " Reply from alternate address..."
428 add_message '[from]="Sender <sender@example.com>"' \
429              [to]=test_suite_other@notmuchmail.org \
430              [subject]=notmuch-reply-test \
431             '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
432             '[body]="reply from alternate address"'
433
434 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite_other@notmuchmail.org>
435 Subject: Re: notmuch-reply-test
436 To: Sender <sender@example.com>
437 Bcc: test_suite@notmuchmail.org
438 In-Reply-To: <${gen_msg_id}>
439 References:  <${gen_msg_id}>
440
441 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
442 > reply from alternate address"
443
444 printf " Support for Reply-To...\t"
445 add_message '[from]="Sender <sender@example.com>"' \
446              [to]=test_suite@notmuchmail.org \
447              [subject]=notmuch-reply-test \
448             '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
449             '[body]="support for reply-to"' \
450             '[reply-to]="Sender <elsewhere@example.com>"'
451
452 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite@notmuchmail.org>
453 Subject: Re: notmuch-reply-test
454 To: Sender <elsewhere@example.com>
455 Bcc: test_suite@notmuchmail.org
456 In-Reply-To: <${gen_msg_id}>
457 References:  <${gen_msg_id}>
458
459 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
460 > support for reply-to"
461
462 printf " Un-munging Reply-To...\t\t"
463 add_message '[from]="Sender <sender@example.com>"' \
464             '[to]="Some List <list@example.com>"' \
465              [subject]=notmuch-reply-test \
466             '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
467             '[body]="Un-munging Reply-To"' \
468             '[reply-to]="Evil Munging List <list@example.com>"'
469
470 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite@notmuchmail.org>
471 Subject: Re: notmuch-reply-test
472 To: Sender <sender@example.com>, Some List <list@example.com>
473 Bcc: test_suite@notmuchmail.org
474 In-Reply-To: <${gen_msg_id}>
475 References:  <${gen_msg_id}>
476
477 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
478 > Un-munging Reply-To"
479
480 printf "\nTesting handling of uuencoded data:\n"
481
482 add_message [subject]=uuencodetest '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' \
483 '[body]="This message is used to ensure that notmuch correctly handles a
484 message containing a block of uuencoded data. First, we have a marker
485 this content beforeuudata . Then we beging the uunencoded data itself:
486
487 begin 644 bogus-uuencoded-data
488 M0123456789012345678901234567890123456789012345678901234567890
489 MOBVIOUSLY, THIS IS NOT ANY SORT OF USEFUL UUNECODED DATA.    
490 MINSTEAD THIS IS JUST A WAY TO ENSURE THAT THIS BLOCK OF DATA 
491 MIS CORRECTLY IGNORED WHEN NOTMUCH CREATES ITS INDEX. SO WE   
492 MINCLUDE A DURINGUUDATA MARKER THAT SHOULD NOT RESULT IN ANY  
493 MSEARCH RESULT.                                               
494 \`
495 end
496
497 Finally, we have our afteruudata marker as well."'
498
499 printf " Ensure content before uu data is indexed..."
500 execute_expecting "search beforeuudata" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; uuencodetest (inbox unread)"
501 printf " Ensure uu data is not indexed...\t"
502 execute_expecting "search DURINGUUDATA" ""
503 printf " Ensure content after uu data is indexed..."
504 execute_expecting "search afteruudata" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; uuencodetest (inbox unread)"
505
506 printf "\nTesting \"notmuch dump\" and \"notmuch restore\":\n"
507
508 printf " Dumping all tags...\t\t"
509 $NOTMUCH dump dump.expected
510 echo "  PASS"
511
512 printf " Clearing all tags...\t\t"
513 sed -e 's/(\([^(]*\))$/()/' < dump.expected > clear.expected
514 $NOTMUCH restore clear.expected
515 $NOTMUCH dump clear.actual
516 if diff clear.expected clear.actual > /dev/null; then
517     echo "      PASS"
518 else
519     echo "      FAIL"
520     echo "      Expected output: See file clear.expected"
521     echo "      Actual output:   See file clear.actual"
522 fi
523
524 printf " Restoring original tags...\t"
525 $NOTMUCH restore dump.expected
526 $NOTMUCH dump dump.actual
527 if diff dump.expected dump.actual > /dev/null; then
528     echo "      PASS"
529 else
530     echo "      FAIL"
531     echo "      Expected output: See file dump.expected"
532     echo "      Actual output:   See file dump.actual"
533 fi
534
535 printf " Restore with nothing to do...\t"
536 $NOTMUCH restore dump.expected
537 echo "  PASS"
538
539 cat <<EOF
540 Notmuch test suite complete.
541
542 Intermediate state can be examined in:
543         ${TEST_DIR}
544 EOF