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