]> git.cworth.org Git - notmuch-old/blob - test/test-lib-common.sh
notmuch (0.27-2) unstable; urgency=medium
[notmuch-old] / 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 {
110     # This is our (bash-specific) magic for doing named parameters
111     local -A template="($@)"
112     local additional_headers
113
114     gen_msg_cnt=$((gen_msg_cnt + 1))
115     if [ -z "${template[filename]}" ]; then
116         gen_msg_name="msg-$(printf "%03d" $gen_msg_cnt)"
117     else
118         gen_msg_name=${template[filename]}
119     fi
120
121     if [ -z "${template[id]}" ]; then
122         gen_msg_id="${gen_msg_name%:2,*}@notmuch-test-suite"
123     else
124         gen_msg_id="${template[id]}"
125     fi
126
127     if [ -z "${template[dir]}" ]; then
128         gen_msg_filename="${MAIL_DIR}/$gen_msg_name"
129     else
130         gen_msg_filename="${MAIL_DIR}/${template[dir]}/$gen_msg_name"
131         mkdir -p "$(dirname "$gen_msg_filename")"
132     fi
133
134     if [ -z "${template[body]}" ]; then
135         template[body]="This is just a test message (#${gen_msg_cnt})"
136     fi
137
138     if [ -z "${template[from]}" ]; then
139         template[from]="Notmuch Test Suite <test_suite@notmuchmail.org>"
140     fi
141
142     if [ -z "${template[to]}" ]; then
143         template[to]="Notmuch Test Suite <test_suite@notmuchmail.org>"
144     fi
145
146     if [ -z "${template[subject]}" ]; then
147         if [ -n "$test_subtest_name" ]; then
148             template[subject]="$test_subtest_name"
149         else
150             template[subject]="Test message #${gen_msg_cnt}"
151         fi
152     elif [ "${template[subject]}" = "@FORCE_EMPTY" ]; then
153         template[subject]=""
154     fi
155
156     if [ -z "${template[date]}" ]; then
157         # we use decreasing timestamps here for historical reasons;
158         # the existing test suite when we converted to unique timestamps just
159         # happened to have signicantly fewer failures with that choice.
160         local date_secs=$((978709437 - gen_msg_cnt))
161         # printf %(..)T is bash 4.2+ feature. use perl fallback if needed...
162         TZ=UTC printf -v template[date] "%(%a, %d %b %Y %T %z)T" $date_secs 2>/dev/null ||
163             template[date]=`perl -le 'use POSIX "strftime";
164                                 @time = gmtime '"$date_secs"';
165                                 print strftime "%a, %d %b %Y %T +0000", @time'`
166     fi
167
168     additional_headers=""
169     if [ ! -z "${template[header]}" ]; then
170         additional_headers="${template[header]}
171 ${additional_headers}"
172     fi
173
174     if [ ! -z "${template[reply-to]}" ]; then
175         additional_headers="Reply-To: ${template[reply-to]}
176 ${additional_headers}"
177     fi
178
179     if [ ! -z "${template[in-reply-to]}" ]; then
180         additional_headers="In-Reply-To: ${template[in-reply-to]}
181 ${additional_headers}"
182     fi
183
184     if [ ! -z "${template[cc]}" ]; then
185         additional_headers="Cc: ${template[cc]}
186 ${additional_headers}"
187     fi
188
189     if [ ! -z "${template[bcc]}" ]; then
190         additional_headers="Bcc: ${template[bcc]}
191 ${additional_headers}"
192     fi
193
194     if [ ! -z "${template[references]}" ]; then
195         additional_headers="References: ${template[references]}
196 ${additional_headers}"
197     fi
198
199     if [ ! -z "${template[content-type]}" ]; then
200         additional_headers="Content-Type: ${template[content-type]}
201 ${additional_headers}"
202     fi
203
204     if [ ! -z "${template[content-transfer-encoding]}" ]; then
205         additional_headers="Content-Transfer-Encoding: ${template[content-transfer-encoding]}
206 ${additional_headers}"
207     fi
208
209     # Note that in the way we're setting it above and using it below,
210     # `additional_headers' will also serve as the header / body separator
211     # (empty line in between).
212
213     cat <<EOF >"$gen_msg_filename"
214 From: ${template[from]}
215 To: ${template[to]}
216 Message-Id: <${gen_msg_id}>
217 Subject: ${template[subject]}
218 Date: ${template[date]}
219 ${additional_headers}
220 ${template[body]}
221 EOF
222 }
223
224 # Generate a new message and add it to the database.
225 #
226 # All of the arguments and return values supported by generate_message
227 # are also supported here, so see that function for details.
228 add_message ()
229 {
230     generate_message "$@" &&
231     notmuch new > /dev/null
232 }
233
234 if test -n "$valgrind"
235 then
236         make_symlink () {
237                 test -h "$2" &&
238                 test "$1" = "$(readlink "$2")" || {
239                         # be super paranoid
240                         if mkdir "$2".lock
241                         then
242                                 rm -f "$2" &&
243                                 ln -s "$1" "$2" &&
244                                 rm -r "$2".lock
245                         else
246                                 while test -d "$2".lock
247                                 do
248                                         say "Waiting for lock on $2."
249                                         sleep 1
250                                 done
251                         fi
252                 }
253         }
254
255         make_valgrind_symlink () {
256                 # handle only executables
257                 test -x "$1" || return
258
259                 base=$(basename "$1")
260                 symlink_target=$TEST_DIRECTORY/../$base
261                 # do not override scripts
262                 if test -x "$symlink_target" &&
263                     test ! -d "$symlink_target" &&
264                     test "#!" != "$(head -c 2 < "$symlink_target")"
265                 then
266                         symlink_target=$TEST_DIRECTORY/valgrind.sh
267                 fi
268                 case "$base" in
269                 *.sh|*.perl)
270                         symlink_target=$TEST_DIRECTORY/unprocessed-script
271                 esac
272                 # create the link, or replace it if it is out of date
273                 make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
274         }
275
276         # override notmuch executable in TEST_DIRECTORY/..
277         GIT_VALGRIND=$TEST_DIRECTORY/valgrind
278         mkdir -p "$GIT_VALGRIND"/bin
279         make_valgrind_symlink $TEST_DIRECTORY/../notmuch
280         OLDIFS=$IFS
281         IFS=:
282         for path in $PATH
283         do
284                 ls "$path"/notmuch 2> /dev/null |
285                 while read file
286                 do
287                         make_valgrind_symlink "$file"
288                 done
289         done
290         IFS=$OLDIFS
291         PATH=$GIT_VALGRIND/bin:$PATH
292         GIT_EXEC_PATH=$GIT_VALGRIND/bin
293         export GIT_VALGRIND
294         test -n "$NOTMUCH_BUILDDIR" && MANPATH="$NOTMUCH_BUILDDIR/doc/_build/man"
295 else # normal case
296         if test -n "$NOTMUCH_BUILDDIR"
297                 then
298                         PATH="$NOTMUCH_BUILDDIR:$PATH"
299                         MANPATH="$NOTMUCH_BUILDDIR/doc/_build/man"
300                 fi
301 fi
302 export PATH MANPATH
303
304 # Test repository
305 test="tmp.$(basename "$0" .sh)"
306 TMP_DIRECTORY="$TEST_DIRECTORY/$test"
307 test ! -z "$debug" || remove_tmp=$TMP_DIRECTORY
308 rm -rf "$TMP_DIRECTORY" || {
309         GIT_EXIT_OK=t
310         echo >&6 "FATAL: Cannot prepare test area"
311         exit 1
312 }
313
314 # A temporary home directory is needed by at least:
315 # - emacs/"Sending a message via (fake) SMTP"
316 # - emacs/"Reply within emacs"
317 # - crypto/emacs_deliver_message
318 export HOME="${TMP_DIRECTORY}/home"
319 mkdir -p "${HOME}"
320
321 MAIL_DIR="${TMP_DIRECTORY}/mail"
322 export NOTMUCH_CONFIG="${TMP_DIRECTORY}/notmuch-config"
323
324 mkdir -p "${MAIL_DIR}"
325
326 cat <<EOF >"${NOTMUCH_CONFIG}"
327 [database]
328 path=${MAIL_DIR}
329
330 [user]
331 name=Notmuch Test Suite
332 primary_email=test_suite@notmuchmail.org
333 other_email=test_suite_other@notmuchmail.org;test_suite@otherdomain.org
334 EOF