notmuch-test: Test "notmuch reply" from alternate address.
[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
33 # a unique message ID and subject.
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 #  [in-reply-to]=<message-id>
62 #
63 #       Additional values for email headers. If these are not provided
64 #       then the relevant headers will simply not appear in the
65 #       message.
66 gen_msg_cnt=0
67 gen_msg_filename=""
68 gen_msg_id=""
69 generate_message ()
70 {
71     # This is our (bash-specific) magic for doing named parameters
72     local -A template="($@)"
73     local additional_headers
74
75     gen_msg_cnt=$((gen_msg_cnt + 1))
76     gen_msg_name=msg-$(printf "%03d" $gen_msg_cnt)
77     gen_msg_id="${gen_msg_name}@notmuch-test-suite"
78
79     if [ -z "${template[dir]}" ]; then
80         gen_msg_filename="${MAIL_DIR}/$gen_msg_name"
81     else
82         gen_msg_filename="${MAIL_DIR}/${template[dir]}/$gen_msg_name"
83         mkdir -p $(dirname $gen_msg_filename)
84     fi
85
86     if [ -z "${template[body]}" ]; then
87         template[body]="This is just a test message at ${gen_msg_filename}"
88     fi
89
90     if [ -z "${template[from]}" ]; then
91         template[from]="Notmuch Test Suite <test_suite@notmuchmail.org>"
92     fi
93
94     if [ -z "${template[to]}" ]; then
95         template[to]="Notmuch Test Suite <test_suite@notmuchmail.org>"
96     fi
97
98     if [ -z "${template[subject]}" ]; then
99         template[subject]="Test message ${gen_msg_filename}"
100     fi
101
102     if [ -z "${template[date]}" ]; then
103         template[date]="Tue, 05 Jan 2010 15:43:57 -0800"
104     fi
105
106     additional_headers=""
107     if [ ! -z "${template[cc]}" ]; then
108         additional_headers="Cc: ${template[cc]}
109 ${additional_headers}"
110     fi
111
112     if [ ! -z "${template[in-reply-to]}" ]; then
113         additional_headers="In-Reply-To: ${template[in-reply-to]}
114 ${additional_headers}"
115     fi
116
117 cat <<EOF >$gen_msg_filename
118 From: ${template[from]}
119 To: ${template[to]}
120 Message-Id: <${gen_msg_id}>
121 Subject: ${template[subject]}
122 Date: ${template[date]}
123 ${additional_headers}
124 ${template[body]}
125 EOF
126
127     # Ensure that the mtime of the containing directory is updated
128     increment_mtime $(dirname ${gen_msg_filename})
129 }
130
131 NOTMUCH_IGNORED_OUTPUT_REGEXP='^Processed [0-9]*( total)? file|Found [0-9]* total file'
132
133 execute_expecting ()
134 {
135     args=$1
136     expected=$2
137
138     output=$($NOTMUCH $args | grep -v -E -e "$NOTMUCH_IGNORED_OUTPUT_REGEXP")
139     if [ "$output" = "$expected" ]; then
140         echo "  PASS"
141     else
142         echo "  FAIL"
143         echo "  Expected output: $expected"
144         echo "  Actual output:   $output"
145     fi
146 }
147
148 TEST_DIR=$(pwd)/test.$$
149 MAIL_DIR=${TEST_DIR}/mail
150 export NOTMUCH_CONFIG=${TEST_DIR}/notmuch-config
151 NOTMUCH=$(find_notmuch_binary $(pwd))
152
153 rm -rf ${TEST_DIR}
154 mkdir ${TEST_DIR}
155 cd ${TEST_DIR}
156
157 mkdir ${MAIL_DIR}
158
159 cat <<EOF > ${NOTMUCH_CONFIG}
160 [database]
161 path=${MAIL_DIR}
162
163 [user]
164 name=Notmuch Test Suite
165 primary_email=test_suite@notmuchmail.org
166 other_email=test_suite_other@notmuchmail.org
167 EOF
168
169 printf "Testing \"notmuch new\" in several variations:\n"
170 printf " No new messages...\t\t"
171 execute_expecting new "No new mail."
172
173 printf " Single new message...\t\t"
174 generate_message
175 execute_expecting new "Added 1 new message to the database."
176
177 printf " Multiple new messages...\t"
178 generate_message
179 generate_message
180 execute_expecting new "Added 2 new messages to the database."
181
182 printf " No new messages (non-empty DB)... "
183 execute_expecting new "No new mail."
184
185 printf " New directories...\t\t"
186 rm -rf ${MAIL_DIR}/* ${MAIL_DIR}/.notmuch
187 mkdir ${MAIL_DIR}/def
188 mkdir ${MAIL_DIR}/ghi
189 generate_message [dir]=def
190
191 execute_expecting new "Added 1 new message to the database."
192
193 printf " Alternate inode order...\t"
194
195 rm -rf ${MAIL_DIR}/.notmuch
196 mv ${MAIL_DIR}/ghi ${MAIL_DIR}/abc
197 rm ${MAIL_DIR}/def/*
198 generate_message [dir]=abc
199
200 execute_expecting new "Added 1 new message to the database."
201
202 printf " Message moved in...\t\t"
203 rm -rf ${MAIL_DIR}/* ${MAIL_DIR}/.notmuch
204 generate_message
205 tmp_msg_filename=tmp/$gen_msg_filename
206 mkdir -p $(dirname $tmp_msg_filename)
207 mv $gen_msg_filename $tmp_msg_filename
208 increment_mtime ${MAIL_DIR}
209 $NOTMUCH new > /dev/null
210 mv $tmp_msg_filename $gen_msg_filename
211 increment_mtime ${MAIL_DIR}
212 execute_expecting new "Added 1 new message to the database."
213
214 printf " Renamed message...\t\t"
215
216 generate_message
217 $NOTMUCH new > /dev/null
218 mv $gen_msg_filename ${gen_msg_filename}-renamed
219 increment_mtime ${MAIL_DIR}
220 execute_expecting new "No new mail. Detected 1 file rename."
221
222 printf " Deleted message...\t\t"
223
224 rm ${gen_msg_filename}-renamed
225 increment_mtime ${MAIL_DIR}
226 execute_expecting new "No new mail. Removed 1 message."
227
228 printf " Renamed directory...\t\t"
229
230 generate_message [dir]=dir
231 generate_message [dir]=dir
232 generate_message [dir]=dir
233
234 $NOTMUCH new > /dev/null
235
236 mv ${MAIL_DIR}/dir ${MAIL_DIR}/dir-renamed
237 increment_mtime ${MAIL_DIR}
238
239 execute_expecting new "No new mail. Detected 3 file renames."
240
241 printf " Deleted directory...\t\t"
242
243 rm -rf ${MAIL_DIR}/dir-renamed
244 increment_mtime ${MAIL_DIR}
245
246 execute_expecting new "No new mail. Removed 3 messages."
247
248 printf " New directory (at end of list)... "
249
250 generate_message [dir]=zzz
251 generate_message [dir]=zzz
252 generate_message [dir]=zzz
253
254 execute_expecting new "Added 3 new messages to the database."
255
256 printf " Deleted directory (end of list)... "
257
258 rm -rf ${MAIL_DIR}/zzz
259 increment_mtime ${MAIL_DIR}
260
261 execute_expecting new "No new mail. Removed 3 messages."
262
263 printf " New symlink to directory...\t"
264
265 rm -rf ${MAIL_DIR}/.notmuch
266 mv ${MAIL_DIR} ${TEST_DIR}/actual_maildir
267
268 mkdir ${MAIL_DIR}
269 ln -s ${TEST_DIR}/actual_maildir ${MAIL_DIR}/symlink
270
271 execute_expecting new "Added 1 new message to the database."
272
273 printf " New symlink to a file...\t"
274 generate_message
275 external_msg_filename=${TEST_DIR}/external/$(basename $gen_msg_filename)
276 mkdir -p $(dirname $external_msg_filename)
277 mv $gen_msg_filename $external_msg_filename
278 ln -s $external_msg_filename $gen_msg_filename
279 increment_mtime ${MAIL_DIR}
280 execute_expecting new "Added 1 new message to the database."
281
282 printf " New two-level directory...\t"
283
284 generate_message [dir]=two/levels
285 generate_message [dir]=two/levels
286 generate_message [dir]=two/levels
287
288 execute_expecting new "Added 3 new messages to the database."
289
290 printf " Deleted two-level directory... "
291
292 rm -rf ${MAIL_DIR}/two
293 increment_mtime ${MAIL_DIR}
294
295 execute_expecting new "No new mail. Removed 3 messages."
296
297 printf "\nTesting \"notmuch reply\" in several variations:\n"
298
299 printf " Basic reply...\t\t\t"
300 generate_message '[from]="Sender <sender@example.com>"' \
301                  [to]=test_suite@notmuchmail.org \
302                  [subject]=notmuch-reply-test \
303                  '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
304                  [body]=notmuch-reply-test    
305
306 $NOTMUCH new > /dev/null
307 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite@notmuchmail.org>
308 Subject: Re: notmuch-reply-test
309 To: Sender <sender@example.com>
310 Bcc: test_suite@notmuchmail.org
311 In-Reply-To: <${gen_msg_id}>
312 References:  <${gen_msg_id}>
313
314 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
315 > notmuch-reply-test"
316
317 printf " Reply from alternate address...\t"
318 generate_message '[from]="Sender <sender@example.com>"' \
319                  [to]=test_suite_other@notmuchmail.org \
320                  [subject]=notmuch-reply-test \
321                  '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
322                  [body]=notmuch-reply-test    
323
324 $NOTMUCH new > /dev/null
325 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite_other@notmuchmail.org>
326 Subject: Re: notmuch-reply-test
327 To: Sender <sender@example.com>
328 Bcc: test_suite@notmuchmail.org
329 In-Reply-To: <${gen_msg_id}>
330 References:  <${gen_msg_id}>
331
332 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
333 > notmuch-reply-test"
334
335 cat <<EOF
336 Notmuch test suite complete.
337
338 Intermediate state can be examined in:
339         ${TEST_DIR}
340 EOF