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