]> git.notmuchmail.org Git - notmuch/blob - bindings/python/test/notmuch-test
Move everything down into a bindings/python directory.
[notmuch] / bindings / python / 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 execute_expecting "search 'body search (phrase)'" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; body search (phrase) (inbox unread)"
349
350 printf " Search by from: (address)...\t"
351 add_message '[subject]="search by from (address)"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' [from]=searchbyfrom@example.com
352 execute_expecting "search from:searchbyfrom@example.com" "thread:XXX   2000-01-01 [1/1] searchbyfrom@example.com; search by from (address) (inbox unread)"
353
354 printf " Search by from: (name)...\t"
355 add_message '[subject]="search by from (name)"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' '[from]="Search By From Name <test@example.com>"'
356 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)"
357
358 printf " Search by to: (address)...\t"
359 add_message '[subject]="search by to (address)"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' [to]=searchbyto@example.com
360 execute_expecting "search to:searchbyto@example.com" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; search by to (address) (inbox unread)"
361
362 printf " Search by to: (name)...\t"
363 add_message '[subject]="search by to (name)"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' '[to]="Search By To Name <test@example.com>"'
364 execute_expecting "search to:'Search By To Name'" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; search by to (name) (inbox unread)"
365
366 printf " Search by subject: (phrase)...\t"
367 add_message '[subject]="subject search test (phrase)"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
368 execute_expecting "search subject:'subject search test (phrase)'" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; subject search test (phrase) (inbox unread)"
369
370 printf "\nTesting \"notmuch reply\" in several variations:\n"
371
372 printf " Basic reply...\t\t\t"
373 add_message '[from]="Sender <sender@example.com>"' \
374              [to]=test_suite@notmuchmail.org \
375              [subject]=notmuch-reply-test \
376             '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
377             '[body]="basic reply test"'
378
379 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite@notmuchmail.org>
380 Subject: Re: notmuch-reply-test
381 To: Sender <sender@example.com>
382 Bcc: test_suite@notmuchmail.org
383 In-Reply-To: <${gen_msg_id}>
384 References:  <${gen_msg_id}>
385
386 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
387 > basic reply test"
388
389 printf " Multiple recipients...\t\t"
390 add_message '[from]="Sender <sender@example.com>"' \
391             '[to]="test_suite@notmuchmail.org, Someone Else <someone@example.com>"' \
392              [subject]=notmuch-reply-test \
393             '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
394             '[body]="Multiple recipients"'
395
396 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite@notmuchmail.org>
397 Subject: Re: notmuch-reply-test
398 To: Sender <sender@example.com>, Someone Else <someone@example.com>
399 Bcc: test_suite@notmuchmail.org
400 In-Reply-To: <${gen_msg_id}>
401 References:  <${gen_msg_id}>
402
403 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
404 > Multiple recipients"
405
406 printf " Reply with CC...\t\t"
407 add_message '[from]="Sender <sender@example.com>"' \
408              [to]=test_suite@notmuchmail.org \
409             '[cc]="Other Parties <cc@example.com>"' \
410              [subject]=notmuch-reply-test \
411             '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
412             '[body]="reply with CC"'
413
414 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite@notmuchmail.org>
415 Subject: Re: notmuch-reply-test
416 To: Sender <sender@example.com>
417 Cc: Other Parties <cc@example.com>
418 Bcc: test_suite@notmuchmail.org
419 In-Reply-To: <${gen_msg_id}>
420 References:  <${gen_msg_id}>
421
422 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
423 > reply with CC"
424
425 printf " Reply from alternate address..."
426 add_message '[from]="Sender <sender@example.com>"' \
427              [to]=test_suite_other@notmuchmail.org \
428              [subject]=notmuch-reply-test \
429             '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
430             '[body]="reply from alternate address"'
431
432 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite_other@notmuchmail.org>
433 Subject: Re: notmuch-reply-test
434 To: Sender <sender@example.com>
435 Bcc: test_suite@notmuchmail.org
436 In-Reply-To: <${gen_msg_id}>
437 References:  <${gen_msg_id}>
438
439 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
440 > reply from alternate address"
441
442 printf " Support for Reply-To...\t"
443 add_message '[from]="Sender <sender@example.com>"' \
444              [to]=test_suite@notmuchmail.org \
445              [subject]=notmuch-reply-test \
446             '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
447             '[body]="support for reply-to"' \
448             '[reply-to]="Sender <elsewhere@example.com>"'
449
450 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite@notmuchmail.org>
451 Subject: Re: notmuch-reply-test
452 To: Sender <elsewhere@example.com>
453 Bcc: test_suite@notmuchmail.org
454 In-Reply-To: <${gen_msg_id}>
455 References:  <${gen_msg_id}>
456
457 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
458 > support for reply-to"
459
460 printf " Un-munging Reply-To...\t\t"
461 add_message '[from]="Sender <sender@example.com>"' \
462             '[to]="Some List <list@example.com>"' \
463              [subject]=notmuch-reply-test \
464             '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
465             '[body]="Un-munging Reply-To"' \
466             '[reply-to]="Evil Munging List <list@example.com>"'
467
468 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite@notmuchmail.org>
469 Subject: Re: notmuch-reply-test
470 To: Sender <sender@example.com>, Some List <list@example.com>
471 Bcc: test_suite@notmuchmail.org
472 In-Reply-To: <${gen_msg_id}>
473 References:  <${gen_msg_id}>
474
475 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
476 > Un-munging Reply-To"
477
478 printf "\nTesting handling of uuencoded data:\n"
479
480 add_message [subject]=uuencodetest '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' \
481 '[body]="This message is used to ensure that notmuch correctly handles a
482 message containing a block of uuencoded data. First, we have a marker
483 this content beforeuudata . Then we beging the uunencoded data itself:
484
485 begin 644 bogus-uuencoded-data
486 M0123456789012345678901234567890123456789012345678901234567890
487 MOBVIOUSLY, THIS IS NOT ANY SORT OF USEFUL UUNECODED DATA.    
488 MINSTEAD THIS IS JUST A WAY TO ENSURE THAT THIS BLOCK OF DATA 
489 MIS CORRECTLY IGNORED WHEN NOTMUCH CREATES ITS INDEX. SO WE   
490 MINCLUDE A DURINGUUDATA MARKER THAT SHOULD NOT RESULT IN ANY  
491 MSEARCH RESULT.                                               
492 \`
493 end
494
495 Finally, we have our afteruudata marker as well."'
496
497 printf " Ensure content before uu data is indexed..."
498 execute_expecting "search beforeuudata" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; uuencodetest (inbox unread)"
499 printf " Ensure uu data is not indexed...\t"
500 execute_expecting "search DURINGUUDATA" ""
501 printf " Ensure content after uu data is indexed..."
502 execute_expecting "search afteruudata" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; uuencodetest (inbox unread)"
503
504 printf "\nTesting \"notmuch dump\" and \"notmuch restore\":\n"
505
506 printf " Dumping all tags...\t\t"
507 $NOTMUCH dump dump.expected
508 echo "  PASS"
509
510 printf " Clearing all tags...\t\t"
511 sed -e 's/(\([^(]*\))$/()/' < dump.expected > clear.expected
512 $NOTMUCH restore clear.expected
513 $NOTMUCH dump clear.actual
514 if diff clear.expected clear.actual > /dev/null; then
515     echo "      PASS"
516 else
517     echo "      FAIL"
518     echo "      Expected output: See file clear.expected"
519     echo "      Actual output:   See file clear.actual"
520 fi
521
522 printf " Restoring original tags...\t"
523 $NOTMUCH restore dump.expected
524 $NOTMUCH dump dump.actual
525 if diff dump.expected dump.actual > /dev/null; then
526     echo "      PASS"
527 else
528     echo "      FAIL"
529     echo "      Expected output: See file dump.expected"
530     echo "      Actual output:   See file dump.actual"
531 fi
532
533 printf " Restore with nothing to do...\t"
534 $NOTMUCH restore dump.expected
535 echo "  PASS"
536
537 cat <<EOF
538 Notmuch test suite complete.
539
540 Intermediate state can be examined in:
541         ${TEST_DIR}
542 EOF