]> git.notmuchmail.org Git - notmuch/blob - test/test-lib-common.sh
lib/database: delete stemmer on destroy
[notmuch] / test / test-lib-common.sh
1 #
2 # Copyright (c) 2005 Junio C Hamano
3 # Copyright (c) 2010 Notmuch Developers
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see https://www.gnu.org/licenses/ .
17
18 # This file contains common code to be used by both the regular
19 # (correctness) tests and the performance tests.
20
21 # test-lib.sh defines die() which echoes to nonstandard fd where
22 # output was redirected earlier in that file. If test-lib.sh is not
23 # loaded, neither this redirection nor die() function were defined.
24 #
25 type die >/dev/null 2>&1 || die () { echo "$@" >&2; exit 1; }
26
27 if [[ -z "$NOTMUCH_SRCDIR" ]] || [[ -z "$NOTMUCH_BUILDDIR" ]]; then
28         echo "internal: srcdir or builddir not set" >&2
29         exit 1
30 fi
31
32 backup_database () {
33     test_name=$(basename $0 .sh)
34     rm -rf $TMP_DIRECTORY/notmuch-dir-backup."$test_name"
35     cp -pR ${MAIL_DIR}/.notmuch $TMP_DIRECTORY/notmuch-dir-backup."${test_name}"
36 }
37
38 restore_database () {
39     test_name=$(basename $0 .sh)
40     rm -rf ${MAIL_DIR}/.notmuch
41     cp -pR $TMP_DIRECTORY/notmuch-dir-backup."${test_name}" ${MAIL_DIR}/.notmuch
42 }
43
44 # Prepend $TEST_DIRECTORY/../lib to LD_LIBRARY_PATH, to make tests work
45 # on systems where ../notmuch depends on LD_LIBRARY_PATH.
46 LD_LIBRARY_PATH=${TEST_DIRECTORY%/*}/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
47 export LD_LIBRARY_PATH
48
49 # configure output
50 . "$NOTMUCH_BUILDDIR/sh.config" || exit 1
51
52 # load OS specifics
53 if [[ -e "$NOTMUCH_SRCDIR/test/test-lib-$PLATFORM.sh" ]]; then
54     . "$NOTMUCH_SRCDIR/test/test-lib-$PLATFORM.sh" || exit 1
55 fi
56
57 # Generate a new message in the mail directory, with a unique message
58 # ID and subject. The message is not added to the index.
59 #
60 # After this function returns, the filename of the generated message
61 # is available as $gen_msg_filename and the message ID is available as
62 # $gen_msg_id .
63 #
64 # This function supports named parameters with the bash syntax for
65 # assigning a value to an associative array ([name]=value). The
66 # supported parameters are:
67 #
68 #  [dir]=directory/of/choice
69 #
70 #       Generate the message in directory 'directory/of/choice' within
71 #       the mail store. The directory will be created if necessary.
72 #
73 #  [filename]=name
74 #
75 #       Store the message in file 'name'. The default is to store it
76 #       in 'msg-<count>', where <count> is three-digit number of the
77 #       message.
78 #
79 #  [body]=text
80 #
81 #       Text to use as the body of the email message
82 #
83 #  '[from]="Some User <user@example.com>"'
84 #  '[to]="Some User <user@example.com>"'
85 #  '[subject]="Subject of email message"'
86 #  '[date]="RFC 822 Date"'
87 #
88 #       Values for email headers. If not provided, default values will
89 #       be generated instead.
90 #
91 #  '[cc]="Some User <user@example.com>"'
92 #  [reply-to]=some-address
93 #  [in-reply-to]=<message-id>
94 #  [references]=<message-id>
95 #  [content-type]=content-type-specification
96 #  '[header]=full header line, including keyword'
97 #
98 #       Additional values for email headers. If these are not provided
99 #       then the relevant headers will simply not appear in the
100 #       message.
101 #
102 #  '[id]=message-id'
103 #
104 #       Controls the message-id of the created message.
105 gen_msg_cnt=0
106 gen_msg_filename=""
107 gen_msg_id=""
108 generate_message () {
109     # This is our (bash-specific) magic for doing named parameters
110     local -A template="($@)"
111     local additional_headers
112
113     gen_msg_cnt=$((gen_msg_cnt + 1))
114     if [ -z "${template[filename]}" ]; then
115         gen_msg_name="msg-$(printf "%03d" $gen_msg_cnt)"
116     else
117         gen_msg_name=${template[filename]}
118     fi
119
120     if [ -z "${template[id]}" ]; then
121         gen_msg_id="${gen_msg_name%:2,*}@notmuch-test-suite"
122     else
123         gen_msg_id="${template[id]}"
124     fi
125
126     if [ -z "${template[dir]}" ]; then
127         gen_msg_filename="${MAIL_DIR}/$gen_msg_name"
128     else
129         gen_msg_filename="${MAIL_DIR}/${template[dir]}/$gen_msg_name"
130         mkdir -p "$(dirname "$gen_msg_filename")"
131     fi
132
133     if [ -z "${template[body]}" ]; then
134         template[body]="This is just a test message (#${gen_msg_cnt})"
135     fi
136
137     if [ -z "${template[from]}" ]; then
138         template[from]="Notmuch Test Suite <test_suite@notmuchmail.org>"
139     fi
140
141     if [ -z "${template[to]}" ]; then
142         template[to]="Notmuch Test Suite <test_suite@notmuchmail.org>"
143     fi
144
145     if [ -z "${template[subject]}" ]; then
146         if [ -n "$test_subtest_name" ]; then
147             template[subject]="$test_subtest_name"
148         else
149             template[subject]="Test message #${gen_msg_cnt}"
150         fi
151     elif [ "${template[subject]}" = "@FORCE_EMPTY" ]; then
152         template[subject]=""
153     fi
154
155     if [ -z "${template[date]}" ]; then
156         # we use decreasing timestamps here for historical reasons;
157         # the existing test suite when we converted to unique timestamps just
158         # happened to have signicantly fewer failures with that choice.
159         local date_secs=$((978709437 - gen_msg_cnt))
160         # printf %(..)T is bash 4.2+ feature. use perl fallback if needed...
161         TZ=UTC printf -v template[date] "%(%a, %d %b %Y %T %z)T" $date_secs 2>/dev/null ||
162             template[date]=`perl -le 'use POSIX "strftime";
163                                 @time = gmtime '"$date_secs"';
164                                 print strftime "%a, %d %b %Y %T +0000", @time'`
165     fi
166
167     additional_headers=""
168     if [ ! -z "${template[header]}" ]; then
169         additional_headers="${template[header]}
170 ${additional_headers}"
171     fi
172
173     if [ ! -z "${template[reply-to]}" ]; then
174         additional_headers="Reply-To: ${template[reply-to]}
175 ${additional_headers}"
176     fi
177
178     if [ ! -z "${template[in-reply-to]}" ]; then
179         additional_headers="In-Reply-To: ${template[in-reply-to]}
180 ${additional_headers}"
181     fi
182
183     if [ ! -z "${template[cc]}" ]; then
184         additional_headers="Cc: ${template[cc]}
185 ${additional_headers}"
186     fi
187
188     if [ ! -z "${template[bcc]}" ]; then
189         additional_headers="Bcc: ${template[bcc]}
190 ${additional_headers}"
191     fi
192
193     if [ ! -z "${template[references]}" ]; then
194         additional_headers="References: ${template[references]}
195 ${additional_headers}"
196     fi
197
198     if [ ! -z "${template[content-type]}" ]; then
199         additional_headers="Content-Type: ${template[content-type]}
200 ${additional_headers}"
201     fi
202
203     if [ ! -z "${template[content-transfer-encoding]}" ]; then
204         additional_headers="Content-Transfer-Encoding: ${template[content-transfer-encoding]}
205 ${additional_headers}"
206     fi
207
208     # Note that in the way we're setting it above and using it below,
209     # `additional_headers' will also serve as the header / body separator
210     # (empty line in between).
211
212     cat <<EOF >"$gen_msg_filename"
213 From: ${template[from]}
214 To: ${template[to]}
215 Message-Id: <${gen_msg_id}>
216 Subject: ${template[subject]}
217 Date: ${template[date]}
218 ${additional_headers}
219 ${template[body]}
220 EOF
221 }
222
223 # Generate a new message and add it to the database.
224 #
225 # All of the arguments and return values supported by generate_message
226 # are also supported here, so see that function for details.
227 add_message () {
228     generate_message "$@" &&
229     notmuch new > /dev/null
230 }
231
232 if test -n "$valgrind"
233 then
234         make_symlink () {
235                 test -h "$2" &&
236                 test "$1" = "$(readlink "$2")" || {
237                         # be super paranoid
238                         if mkdir "$2".lock
239                         then
240                                 rm -f "$2" &&
241                                 ln -s "$1" "$2" &&
242                                 rm -r "$2".lock
243                         else
244                                 while test -d "$2".lock
245                                 do
246                                         say "Waiting for lock on $2."
247                                         sleep 1
248                                 done
249                         fi
250                 }
251         }
252
253         make_valgrind_symlink () {
254                 # handle only executables
255                 test -x "$1" || return
256
257                 base=$(basename "$1")
258                 symlink_target=$TEST_DIRECTORY/../$base
259                 # do not override scripts
260                 if test -x "$symlink_target" &&
261                     test ! -d "$symlink_target" &&
262                     test "#!" != "$(head -c 2 < "$symlink_target")"
263                 then
264                         symlink_target=$TEST_DIRECTORY/valgrind.sh
265                 fi
266                 case "$base" in
267                 *.sh|*.perl)
268                         symlink_target=$TEST_DIRECTORY/unprocessed-script
269                 esac
270                 # create the link, or replace it if it is out of date
271                 make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
272         }
273
274         # override notmuch executable in TEST_DIRECTORY/..
275         GIT_VALGRIND=$TEST_DIRECTORY/valgrind
276         mkdir -p "$GIT_VALGRIND"/bin
277         make_valgrind_symlink $TEST_DIRECTORY/../notmuch
278         OLDIFS=$IFS
279         IFS=:
280         for path in $PATH
281         do
282                 ls "$path"/notmuch 2> /dev/null |
283                 while read file
284                 do
285                         make_valgrind_symlink "$file"
286                 done
287         done
288         IFS=$OLDIFS
289         PATH=$GIT_VALGRIND/bin:$PATH
290         GIT_EXEC_PATH=$GIT_VALGRIND/bin
291         export GIT_VALGRIND
292         test -n "$NOTMUCH_BUILDDIR" && MANPATH="$NOTMUCH_BUILDDIR/doc/_build/man"
293 else # normal case
294         if test -n "$NOTMUCH_BUILDDIR"
295                 then
296                         PATH="$NOTMUCH_BUILDDIR:$PATH"
297                         MANPATH="$NOTMUCH_BUILDDIR/doc/_build/man"
298                 fi
299 fi
300 export PATH MANPATH
301
302 # Test repository
303 test="tmp.$(basename "$0" .sh)"
304 TMP_DIRECTORY="$TEST_DIRECTORY/$test"
305 test ! -z "$debug" || remove_tmp=$TMP_DIRECTORY
306 rm -rf "$TMP_DIRECTORY" || {
307         GIT_EXIT_OK=t
308         echo >&6 "FATAL: Cannot prepare test area"
309         exit 1
310 }
311
312 # A temporary home directory is needed by at least:
313 # - emacs/"Sending a message via (fake) SMTP"
314 # - emacs/"Reply within emacs"
315 # - crypto/emacs_deliver_message
316 export HOME="${TMP_DIRECTORY}/home"
317 mkdir -p "${HOME}"
318
319 MAIL_DIR="${TMP_DIRECTORY}/mail"
320 export NOTMUCH_CONFIG="${TMP_DIRECTORY}/notmuch-config"
321
322 mkdir -p "${MAIL_DIR}"
323
324 cat <<EOF >"${NOTMUCH_CONFIG}"
325 [database]
326 path=${MAIL_DIR}
327
328 [user]
329 name=Notmuch Test Suite
330 primary_email=test_suite@notmuchmail.org
331 other_email=test_suite_other@notmuchmail.org;test_suite@otherdomain.org
332 EOF