notmuch-test: Use named-parameters for generate_message function
[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 gen_msg_cnt=0
38 gen_msg_filename=""
39 generate_message ()
40 {
41     # This is our (bash-specific) magic for doing named parameters
42     local -A template="($@)"
43
44     gen_msg_cnt=$((gen_msg_cnt + 1))
45     gen_msg_name=msg-$(printf "%03d" $gen_msg_cnt)
46
47     if [ -z "${template[dir]}" ]; then
48         gen_msg_filename="${MAIL_DIR}/$gen_msg_name"
49     else
50         gen_msg_filename="${MAIL_DIR}/${template[dir]}/$gen_msg_name"
51         mkdir -p $(dirname $gen_msg_filename)
52     fi
53
54 cat <<EOF >$gen_msg_filename
55 From: Notmuch Test Suite <test_suite@notmuchmail.org>
56 To: Notmuch Test Suite <test_suite@notmuchmail.org>
57 Message-Id: <msg-${gen_msg_cnt}@notmuch-test-suite>
58 Subject: Test message ${gen_msg_filename}
59 Date: Tue, 05 Jan 2010 15:43:57 -0800
60
61 This is just a test message at ${gen_msg_filename}
62 EOF
63 }
64
65 do_sleep ()
66 {
67     sleep 1
68 }
69
70 NOTMUCH_IGNORED_OUTPUT_REGEXP='^Processed [0-9]*( total)? file|Found [0-9]* total file'
71
72 execute_expecting ()
73 {
74     args=$1
75     expected=$2
76
77     output=$($NOTMUCH $args | grep -v -E -e "$NOTMUCH_IGNORED_OUTPUT_REGEXP")
78     if [ "$output" = "$expected" ]; then
79         echo "  PASS"
80     else
81         echo "  FAIL"
82         echo "  Expected output: $expected"
83         echo "  Actual output:   $output"
84     fi
85 }
86
87 TEST_DIR=$(pwd)/test.$$
88 MAIL_DIR=${TEST_DIR}/mail
89 export NOTMUCH_CONFIG=${TEST_DIR}/notmuch-config
90 NOTMUCH=$(find_notmuch_binary $(pwd))
91
92 rm -rf ${TEST_DIR}
93 mkdir ${TEST_DIR}
94 cd ${TEST_DIR}
95
96 mkdir ${MAIL_DIR}
97
98 cat <<EOF > ${NOTMUCH_CONFIG}
99 [database]
100 path=${MAIL_DIR}
101
102 [user]
103 name=Notmuch Test Suite
104 primary_email=test_suite@notmuchmail.org
105 EOF
106
107 printf "Testing \"notmuch new\" in several variations:\n"
108 printf " No new messages...\t\t"
109 execute_expecting new "No new mail."
110
111 printf " Single new message...\t\t"
112 do_sleep
113 generate_message
114 execute_expecting new "Added 1 new message to the database."
115
116 printf " Multiple new messages...\t"
117 do_sleep
118 generate_message
119 generate_message
120 execute_expecting new "Added 2 new messages to the database."
121
122 printf " No new messages (non-empty DB)... "
123 execute_expecting new "No new mail."
124
125 printf " New directories...\t\t"
126 rm -rf ${MAIL_DIR}/* ${MAIL_DIR}/.notmuch
127 mkdir ${MAIL_DIR}/def
128 mkdir ${MAIL_DIR}/ghi
129 generate_message [dir]=def
130
131 execute_expecting new "Added 1 new message to the database."
132
133 printf " Alternate inode order...\t"
134
135 rm -rf ${MAIL_DIR}/.notmuch
136 mv ${MAIL_DIR}/ghi ${MAIL_DIR}/abc
137 rm ${MAIL_DIR}/def/*
138 generate_message [dir]=abc
139
140 execute_expecting new "Added 1 new message to the database."
141
142 printf " Message moved in...\t\t"
143 rm -rf ${MAIL_DIR}/* ${MAIL_DIR}/.notmuch
144 generate_message
145 tmp_msg_filename=tmp/$gen_msg_filename
146 mkdir -p $(dirname $tmp_msg_filename)
147 mv $gen_msg_filename $tmp_msg_filename
148 do_sleep
149 $NOTMUCH new > /dev/null
150 do_sleep
151 mv $tmp_msg_filename $gen_msg_filename
152 execute_expecting new "Added 1 new message to the database."
153
154 printf " Renamed message...\t\t"
155
156 do_sleep
157 generate_message
158 $NOTMUCH new > /dev/null
159 do_sleep
160 mv $gen_msg_filename ${gen_msg_filename}-renamed
161 execute_expecting new "No new mail. Detected 1 file rename."
162
163 printf " Deleted message...\t\t"
164
165 do_sleep
166 rm ${gen_msg_filename}-renamed
167 execute_expecting new "No new mail. Removed 1 message."
168
169 printf " Renamed directory...\t\t"
170
171 do_sleep
172 generate_message [dir]=dir
173 generate_message [dir]=dir
174 generate_message [dir]=dir
175
176 $NOTMUCH new > /dev/null
177
178 do_sleep
179 mv ${MAIL_DIR}/dir ${MAIL_DIR}/dir-renamed
180
181 execute_expecting new "No new mail. Detected 3 file renames."
182
183 printf " Deleted directory...\t\t"
184
185 do_sleep
186 rm -rf ${MAIL_DIR}/dir-renamed
187
188 execute_expecting new "No new mail. Removed 3 messages."
189
190 printf " New directory (at end of list)... "
191
192 do_sleep
193 generate_message [dir]=zzz
194 generate_message [dir]=zzz
195 generate_message [dir]=zzz
196
197 execute_expecting new "Added 3 new messages to the database."
198
199 printf " Deleted directory (end of list)... "
200
201 do_sleep
202 rm -rf ${MAIL_DIR}/zzz
203
204 execute_expecting new "No new mail. Removed 3 messages."
205
206 printf " New symlink to directory...\t"
207
208 rm -rf ${MAIL_DIR}/.notmuch
209 mv ${MAIL_DIR} ${TEST_DIR}/actual_maildir
210
211 mkdir ${MAIL_DIR}
212 ln -s ${TEST_DIR}/actual_maildir ${MAIL_DIR}/symlink
213
214 execute_expecting new "Added 1 new message to the database."
215
216 printf " New symlink to a file...\t"
217 do_sleep
218 generate_message
219 external_msg_filename=${TEST_DIR}/external/$(basename $gen_msg_filename)
220 mkdir -p $(dirname $external_msg_filename)
221 mv $gen_msg_filename $external_msg_filename
222 ln -s $external_msg_filename $gen_msg_filename
223
224 execute_expecting new "Added 1 new message to the database."
225
226 printf " New two-level directory...\t"
227
228 do_sleep
229 generate_message [dir]=two/levels
230 generate_message [dir]=two/levels
231 generate_message [dir]=two/levels
232
233 execute_expecting new "Added 3 new messages to the database."
234
235 printf " Deleted two-level directory... "
236
237 do_sleep
238 rm -rf ${MAIL_DIR}/two
239
240 execute_expecting new "No new mail. Removed 3 messages."
241
242 cat <<EOF
243 Notmuch test suite complete.
244
245 Intermediate state can be examined in:
246         ${TEST_DIR}
247 EOF