]> git.notmuchmail.org Git - notmuch/blob - test/notmuch-test
af63d39afa011e4ba0bb9ba653523eb0061a1f5f
[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
150 execute_expecting ()
151 {
152     args=$1
153     expected=$2
154
155     output=$($NOTMUCH $args | grep -v -E -e "$NOTMUCH_IGNORED_OUTPUT_REGEXP")
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 reply\" in several variations:\n"
315
316 printf " Basic reply...\t\t\t"
317 add_message '[from]="Sender <sender@example.com>"' \
318              [to]=test_suite@notmuchmail.org \
319              [subject]=notmuch-reply-test \
320             '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
321             '[body]="basic reply test"'
322
323 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite@notmuchmail.org>
324 Subject: Re: notmuch-reply-test
325 To: Sender <sender@example.com>
326 Bcc: test_suite@notmuchmail.org
327 In-Reply-To: <${gen_msg_id}>
328 References:  <${gen_msg_id}>
329
330 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
331 > basic reply test"
332
333 printf " Multiple recipients...\t\t"
334 add_message '[from]="Sender <sender@example.com>"' \
335             '[to]="test_suite@notmuchmail.org, Someone Else <someone@example.com>"' \
336              [subject]=notmuch-reply-test \
337             '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
338             '[body]="Multiple recipients"'
339
340 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite@notmuchmail.org>
341 Subject: Re: notmuch-reply-test
342 To: Sender <sender@example.com>, Someone Else <someone@example.com>
343 Bcc: test_suite@notmuchmail.org
344 In-Reply-To: <${gen_msg_id}>
345 References:  <${gen_msg_id}>
346
347 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
348 > Multiple recipients"
349
350 printf " Reply with CC...\t\t"
351 add_message '[from]="Sender <sender@example.com>"' \
352              [to]=test_suite@notmuchmail.org \
353             '[cc]="Other Parties <cc@example.com>"' \
354              [subject]=notmuch-reply-test \
355             '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
356             '[body]="reply with CC"'
357
358 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite@notmuchmail.org>
359 Subject: Re: notmuch-reply-test
360 To: Sender <sender@example.com>
361 Cc: Other Parties <cc@example.com>
362 Bcc: test_suite@notmuchmail.org
363 In-Reply-To: <${gen_msg_id}>
364 References:  <${gen_msg_id}>
365
366 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
367 > reply with CC"
368
369 printf " Reply from alternate address..."
370 add_message '[from]="Sender <sender@example.com>"' \
371              [to]=test_suite_other@notmuchmail.org \
372              [subject]=notmuch-reply-test \
373             '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
374             '[body]="reply from alternate address"'
375
376 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite_other@notmuchmail.org>
377 Subject: Re: notmuch-reply-test
378 To: Sender <sender@example.com>
379 Bcc: test_suite@notmuchmail.org
380 In-Reply-To: <${gen_msg_id}>
381 References:  <${gen_msg_id}>
382
383 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
384 > reply from alternate address"
385
386 printf " Support for Reply-To...\t"
387 add_message '[from]="Sender <sender@example.com>"' \
388              [to]=test_suite@notmuchmail.org \
389              [subject]=notmuch-reply-test \
390             '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
391             '[body]="support for reply-to"' \
392             '[reply-to]="Sender <elsewhere@example.com>"'
393
394 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite@notmuchmail.org>
395 Subject: Re: notmuch-reply-test
396 To: Sender <elsewhere@example.com>
397 Bcc: test_suite@notmuchmail.org
398 In-Reply-To: <${gen_msg_id}>
399 References:  <${gen_msg_id}>
400
401 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
402 > support for reply-to"
403
404 printf " Un-munging Reply-To...\t\t"
405 add_message '[from]="Sender <sender@example.com>"' \
406             '[to]="Some List <list@example.com>"' \
407              [subject]=notmuch-reply-test \
408             '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
409             '[body]="Un-munging Reply-To"' \
410             '[reply-to]="Evil Munging List <list@example.com>"'
411
412 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite@notmuchmail.org>
413 Subject: Re: notmuch-reply-test
414 To: Sender <sender@example.com>, Some List <list@example.com>
415 Bcc: test_suite@notmuchmail.org
416 In-Reply-To: <${gen_msg_id}>
417 References:  <${gen_msg_id}>
418
419 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
420 > Un-munging Reply-To"
421
422 cat <<EOF
423 Notmuch test suite complete.
424
425 Intermediate state can be examined in:
426         ${TEST_DIR}
427 EOF