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