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