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