notmuch-test: Add a basic test of "notmuch reply"
[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 EOF
167
168 printf "Testing \"notmuch new\" in several variations:\n"
169 printf " No new messages...\t\t"
170 execute_expecting new "No new mail."
171
172 printf " Single new message...\t\t"
173 generate_message
174 execute_expecting new "Added 1 new message to the database."
175
176 printf " Multiple new messages...\t"
177 generate_message
178 generate_message
179 execute_expecting new "Added 2 new messages to the database."
180
181 printf " No new messages (non-empty DB)... "
182 execute_expecting new "No new mail."
183
184 printf " New directories...\t\t"
185 rm -rf ${MAIL_DIR}/* ${MAIL_DIR}/.notmuch
186 mkdir ${MAIL_DIR}/def
187 mkdir ${MAIL_DIR}/ghi
188 generate_message [dir]=def
189
190 execute_expecting new "Added 1 new message to the database."
191
192 printf " Alternate inode order...\t"
193
194 rm -rf ${MAIL_DIR}/.notmuch
195 mv ${MAIL_DIR}/ghi ${MAIL_DIR}/abc
196 rm ${MAIL_DIR}/def/*
197 generate_message [dir]=abc
198
199 execute_expecting new "Added 1 new message to the database."
200
201 printf " Message moved in...\t\t"
202 rm -rf ${MAIL_DIR}/* ${MAIL_DIR}/.notmuch
203 generate_message
204 tmp_msg_filename=tmp/$gen_msg_filename
205 mkdir -p $(dirname $tmp_msg_filename)
206 mv $gen_msg_filename $tmp_msg_filename
207 increment_mtime ${MAIL_DIR}
208 $NOTMUCH new > /dev/null
209 mv $tmp_msg_filename $gen_msg_filename
210 increment_mtime ${MAIL_DIR}
211 execute_expecting new "Added 1 new message to the database."
212
213 printf " Renamed message...\t\t"
214
215 generate_message
216 $NOTMUCH new > /dev/null
217 mv $gen_msg_filename ${gen_msg_filename}-renamed
218 increment_mtime ${MAIL_DIR}
219 execute_expecting new "No new mail. Detected 1 file rename."
220
221 printf " Deleted message...\t\t"
222
223 rm ${gen_msg_filename}-renamed
224 increment_mtime ${MAIL_DIR}
225 execute_expecting new "No new mail. Removed 1 message."
226
227 printf " Renamed directory...\t\t"
228
229 generate_message [dir]=dir
230 generate_message [dir]=dir
231 generate_message [dir]=dir
232
233 $NOTMUCH new > /dev/null
234
235 mv ${MAIL_DIR}/dir ${MAIL_DIR}/dir-renamed
236 increment_mtime ${MAIL_DIR}
237
238 execute_expecting new "No new mail. Detected 3 file renames."
239
240 printf " Deleted directory...\t\t"
241
242 rm -rf ${MAIL_DIR}/dir-renamed
243 increment_mtime ${MAIL_DIR}
244
245 execute_expecting new "No new mail. Removed 3 messages."
246
247 printf " New directory (at end of list)... "
248
249 generate_message [dir]=zzz
250 generate_message [dir]=zzz
251 generate_message [dir]=zzz
252
253 execute_expecting new "Added 3 new messages to the database."
254
255 printf " Deleted directory (end of list)... "
256
257 rm -rf ${MAIL_DIR}/zzz
258 increment_mtime ${MAIL_DIR}
259
260 execute_expecting new "No new mail. Removed 3 messages."
261
262 printf " New symlink to directory...\t"
263
264 rm -rf ${MAIL_DIR}/.notmuch
265 mv ${MAIL_DIR} ${TEST_DIR}/actual_maildir
266
267 mkdir ${MAIL_DIR}
268 ln -s ${TEST_DIR}/actual_maildir ${MAIL_DIR}/symlink
269
270 execute_expecting new "Added 1 new message to the database."
271
272 printf " New symlink to a file...\t"
273 generate_message
274 external_msg_filename=${TEST_DIR}/external/$(basename $gen_msg_filename)
275 mkdir -p $(dirname $external_msg_filename)
276 mv $gen_msg_filename $external_msg_filename
277 ln -s $external_msg_filename $gen_msg_filename
278 increment_mtime ${MAIL_DIR}
279 execute_expecting new "Added 1 new message to the database."
280
281 printf " New two-level directory...\t"
282
283 generate_message [dir]=two/levels
284 generate_message [dir]=two/levels
285 generate_message [dir]=two/levels
286
287 execute_expecting new "Added 3 new messages to the database."
288
289 printf " Deleted two-level directory... "
290
291 rm -rf ${MAIL_DIR}/two
292 increment_mtime ${MAIL_DIR}
293
294 execute_expecting new "No new mail. Removed 3 messages."
295
296 printf "\nTesting \"notmuch reply\" in several variations:\n"
297
298 printf " Basic reply...\t\t\t"
299 generate_message '[from]="Sender <sender@example.com>"' \
300                  [to]=test_suite@notmuchmail.org \
301                  [subject]=notmuch-reply-test \
302                  '[date]="Tue, 05 Jan 2010 15:43:56 -0800"' \
303                  [body]=notmuch-reply-test    
304
305 $NOTMUCH new > /dev/null
306 execute_expecting "reply id:${gen_msg_id}" "From: Notmuch Test Suite <test_suite@notmuchmail.org>
307 Subject: Re: notmuch-reply-test
308 To: Sender <sender@example.com>
309 Bcc: test_suite@notmuchmail.org
310 In-Reply-To: <${gen_msg_id}>
311 References:  <${gen_msg_id}>
312
313 On Tue, 05 Jan 2010 15:43:56 -0800, Sender <sender@example.com> wrote:
314 > notmuch-reply-test"
315
316 cat <<EOF
317 Notmuch test suite complete.
318
319 Intermediate state can be examined in:
320         ${TEST_DIR}
321 EOF