notmuch-test: Allow custom headers when generating messages
[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 # Generate a new message in the mail directory, with
24 # a unique message ID and subject.
25 #
26 # The filename of the message generated is available as
27 # $gen_msg_filename
28 #
29 # This function supports named parameters with the bash syntax for
30 # assigning a value to an associative array ([name]=value). The
31 # supported parameters are:
32 #
33 #  [dir]=directory/of/choice
34 #
35 #       Generate the message in directory 'directory/of/choice' within
36 #       the mail store. The directory will be created if necessary.
37 #
38 #  [body]=text
39 #
40 #       Text to use as the body of the email message
41 #
42 #  '[from]="Some User <user@example.com>"'
43 #  '[to]="Some User <user@example.com>"'
44 #  '[subject]="Subject of email message"'
45 #
46 #       Values for email headers. If not provided, default values will
47 #       be generated instead.
48 #
49 #  '[cc]="Some User <user@example.com>"'
50 #  [in-reply-to]=<message-id>
51 #
52 #       Additional values for email headers. If these are not provided
53 #       then the relevant headers will simply not appear in the
54 #       message.
55 gen_msg_cnt=0
56 gen_msg_filename=""
57 generate_message ()
58 {
59     # This is our (bash-specific) magic for doing named parameters
60     local -A template="($@)"
61     local additional_headers
62
63     gen_msg_cnt=$((gen_msg_cnt + 1))
64     gen_msg_name=msg-$(printf "%03d" $gen_msg_cnt)
65
66     if [ -z "${template[dir]}" ]; then
67         gen_msg_filename="${MAIL_DIR}/$gen_msg_name"
68     else
69         gen_msg_filename="${MAIL_DIR}/${template[dir]}/$gen_msg_name"
70         mkdir -p $(dirname $gen_msg_filename)
71     fi
72
73     if [ -z "${template[body]}" ]; then
74         template[body]="This is just a test message at ${gen_msg_filename}"
75     fi
76
77     if [ -z "${template[from]}" ]; then
78         template[from]="Notmuch Test Suite <test_suite@notmuchmail.org>"
79     fi
80
81     if [ -z "${template[to]}" ]; then
82         template[to]="Notmuch Test Suite <test_suite@notmuchmail.org>"
83     fi
84
85     if [ -z "${template[subject]}" ]; then
86         template[subject]="Test message ${gen_msg_filename}"
87     fi
88
89     additional_headers=""
90     if [ ! -z "${template[cc]}" ]; then
91         additional_headers="Cc: ${template[cc]}
92 ${additional_headers}"
93     fi
94
95     if [ ! -z "${template[in-reply-to]}" ]; then
96         additional_headers="In-Reply-To: ${template[in-reply-to]}
97 ${additional_headers}"
98     fi
99
100 cat <<EOF >$gen_msg_filename
101 From: ${template[from]}
102 To: ${template[to]}
103 Message-Id: <msg-${gen_msg_cnt}@notmuch-test-suite>
104 Subject: ${template[subject]}
105 Date: Tue, 05 Jan 2010 15:43:57 -0800
106 ${additional_headers}
107 ${template[body]}
108 EOF
109 }
110
111 do_sleep ()
112 {
113     sleep 1
114 }
115
116 NOTMUCH_IGNORED_OUTPUT_REGEXP='^Processed [0-9]*( total)? file|Found [0-9]* total file'
117
118 execute_expecting ()
119 {
120     args=$1
121     expected=$2
122
123     output=$($NOTMUCH $args | grep -v -E -e "$NOTMUCH_IGNORED_OUTPUT_REGEXP")
124     if [ "$output" = "$expected" ]; then
125         echo "  PASS"
126     else
127         echo "  FAIL"
128         echo "  Expected output: $expected"
129         echo "  Actual output:   $output"
130     fi
131 }
132
133 TEST_DIR=$(pwd)/test.$$
134 MAIL_DIR=${TEST_DIR}/mail
135 export NOTMUCH_CONFIG=${TEST_DIR}/notmuch-config
136 NOTMUCH=$(find_notmuch_binary $(pwd))
137
138 rm -rf ${TEST_DIR}
139 mkdir ${TEST_DIR}
140 cd ${TEST_DIR}
141
142 mkdir ${MAIL_DIR}
143
144 cat <<EOF > ${NOTMUCH_CONFIG}
145 [database]
146 path=${MAIL_DIR}
147
148 [user]
149 name=Notmuch Test Suite
150 primary_email=test_suite@notmuchmail.org
151 EOF
152
153 printf "Testing \"notmuch new\" in several variations:\n"
154 printf " No new messages...\t\t"
155 execute_expecting new "No new mail."
156
157 printf " Single new message...\t\t"
158 do_sleep
159 generate_message
160 execute_expecting new "Added 1 new message to the database."
161
162 printf " Multiple new messages...\t"
163 do_sleep
164 generate_message
165 generate_message
166 execute_expecting new "Added 2 new messages to the database."
167
168 printf " No new messages (non-empty DB)... "
169 execute_expecting new "No new mail."
170
171 printf " New directories...\t\t"
172 rm -rf ${MAIL_DIR}/* ${MAIL_DIR}/.notmuch
173 mkdir ${MAIL_DIR}/def
174 mkdir ${MAIL_DIR}/ghi
175 generate_message [dir]=def
176
177 execute_expecting new "Added 1 new message to the database."
178
179 printf " Alternate inode order...\t"
180
181 rm -rf ${MAIL_DIR}/.notmuch
182 mv ${MAIL_DIR}/ghi ${MAIL_DIR}/abc
183 rm ${MAIL_DIR}/def/*
184 generate_message [dir]=abc
185
186 execute_expecting new "Added 1 new message to the database."
187
188 printf " Message moved in...\t\t"
189 rm -rf ${MAIL_DIR}/* ${MAIL_DIR}/.notmuch
190 generate_message
191 tmp_msg_filename=tmp/$gen_msg_filename
192 mkdir -p $(dirname $tmp_msg_filename)
193 mv $gen_msg_filename $tmp_msg_filename
194 do_sleep
195 $NOTMUCH new > /dev/null
196 do_sleep
197 mv $tmp_msg_filename $gen_msg_filename
198 execute_expecting new "Added 1 new message to the database."
199
200 printf " Renamed message...\t\t"
201
202 do_sleep
203 generate_message
204 $NOTMUCH new > /dev/null
205 do_sleep
206 mv $gen_msg_filename ${gen_msg_filename}-renamed
207 execute_expecting new "No new mail. Detected 1 file rename."
208
209 printf " Deleted message...\t\t"
210
211 do_sleep
212 rm ${gen_msg_filename}-renamed
213 execute_expecting new "No new mail. Removed 1 message."
214
215 printf " Renamed directory...\t\t"
216
217 do_sleep
218 generate_message [dir]=dir
219 generate_message [dir]=dir
220 generate_message [dir]=dir
221
222 $NOTMUCH new > /dev/null
223
224 do_sleep
225 mv ${MAIL_DIR}/dir ${MAIL_DIR}/dir-renamed
226
227 execute_expecting new "No new mail. Detected 3 file renames."
228
229 printf " Deleted directory...\t\t"
230
231 do_sleep
232 rm -rf ${MAIL_DIR}/dir-renamed
233
234 execute_expecting new "No new mail. Removed 3 messages."
235
236 printf " New directory (at end of list)... "
237
238 do_sleep
239 generate_message [dir]=zzz
240 generate_message [dir]=zzz
241 generate_message [dir]=zzz
242
243 execute_expecting new "Added 3 new messages to the database."
244
245 printf " Deleted directory (end of list)... "
246
247 do_sleep
248 rm -rf ${MAIL_DIR}/zzz
249
250 execute_expecting new "No new mail. Removed 3 messages."
251
252 printf " New symlink to directory...\t"
253
254 rm -rf ${MAIL_DIR}/.notmuch
255 mv ${MAIL_DIR} ${TEST_DIR}/actual_maildir
256
257 mkdir ${MAIL_DIR}
258 ln -s ${TEST_DIR}/actual_maildir ${MAIL_DIR}/symlink
259
260 execute_expecting new "Added 1 new message to the database."
261
262 printf " New symlink to a file...\t"
263 do_sleep
264 generate_message
265 external_msg_filename=${TEST_DIR}/external/$(basename $gen_msg_filename)
266 mkdir -p $(dirname $external_msg_filename)
267 mv $gen_msg_filename $external_msg_filename
268 ln -s $external_msg_filename $gen_msg_filename
269
270 execute_expecting new "Added 1 new message to the database."
271
272 printf " New two-level directory...\t"
273
274 do_sleep
275 generate_message [dir]=two/levels
276 generate_message [dir]=two/levels
277 generate_message [dir]=two/levels
278
279 execute_expecting new "Added 3 new messages to the database."
280
281 printf " Deleted two-level directory... "
282
283 do_sleep
284 rm -rf ${MAIL_DIR}/two
285
286 execute_expecting new "No new mail. Removed 3 messages."
287
288 cat <<EOF
289 Notmuch test suite complete.
290
291 Intermediate state can be examined in:
292         ${TEST_DIR}
293 EOF