2 # Copyright (c) 2005 Junio C Hamano
3 # Copyright (c) 2010 Notmuch Developers
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.
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.
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/ .
18 # This file contains common code to be used by both the regular
19 # (correctness) tests and the performance tests.
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.
25 type die >/dev/null 2>&1 || die () { echo "$@" >&2; exit 1; }
31 while [ -n "$dir" ]; do
33 if [ -x "$bin" ]; then
37 dir="$(dirname "$dir")"
38 if [ "$dir" = "/" ]; then
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}"
51 test_name=$(basename $0 .sh)
52 rm -rf ${MAIL_DIR}/.notmuch
53 cp -pR notmuch-dir-backup."${test_name}" ${MAIL_DIR}/.notmuch
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"`
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
67 . $notmuch_path/sh.config || exit 1
70 if [ -e ./test-lib-$PLATFORM.sh ]; then
71 . ./test-lib-$PLATFORM.sh || exit 1
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.
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
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:
85 # [dir]=directory/of/choice
87 # Generate the message in directory 'directory/of/choice' within
88 # the mail store. The directory will be created if necessary.
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
98 # Text to use as the body of the email message
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"'
105 # Values for email headers. If not provided, default values will
106 # be generated instead.
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'
115 # Additional values for email headers. If these are not provided
116 # then the relevant headers will simply not appear in the
121 # Controls the message-id of the created message.
127 # This is our (bash-specific) magic for doing named parameters
128 local -A template="($@)"
129 local additional_headers
131 gen_msg_cnt=$((gen_msg_cnt + 1))
132 if [ -z "${template[filename]}" ]; then
133 gen_msg_name="msg-$(printf "%03d" $gen_msg_cnt)"
135 gen_msg_name=${template[filename]}
138 if [ -z "${template[id]}" ]; then
139 gen_msg_id="${gen_msg_name%:2,*}@notmuch-test-suite"
141 gen_msg_id="${template[id]}"
144 if [ -z "${template[dir]}" ]; then
145 gen_msg_filename="${MAIL_DIR}/$gen_msg_name"
147 gen_msg_filename="${MAIL_DIR}/${template[dir]}/$gen_msg_name"
148 mkdir -p "$(dirname "$gen_msg_filename")"
151 if [ -z "${template[body]}" ]; then
152 template[body]="This is just a test message (#${gen_msg_cnt})"
155 if [ -z "${template[from]}" ]; then
156 template[from]="Notmuch Test Suite <test_suite@notmuchmail.org>"
159 if [ -z "${template[to]}" ]; then
160 template[to]="Notmuch Test Suite <test_suite@notmuchmail.org>"
163 if [ -z "${template[subject]}" ]; then
164 if [ -n "$test_subtest_name" ]; then
165 template[subject]="$test_subtest_name"
167 template[subject]="Test message #${gen_msg_cnt}"
169 elif [ "${template[subject]}" = "@FORCE_EMPTY" ]; then
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'`
185 additional_headers=""
186 if [ ! -z "${template[header]}" ]; then
187 additional_headers="${template[header]}
188 ${additional_headers}"
191 if [ ! -z "${template[reply-to]}" ]; then
192 additional_headers="Reply-To: ${template[reply-to]}
193 ${additional_headers}"
196 if [ ! -z "${template[in-reply-to]}" ]; then
197 additional_headers="In-Reply-To: ${template[in-reply-to]}
198 ${additional_headers}"
201 if [ ! -z "${template[cc]}" ]; then
202 additional_headers="Cc: ${template[cc]}
203 ${additional_headers}"
206 if [ ! -z "${template[bcc]}" ]; then
207 additional_headers="Bcc: ${template[bcc]}
208 ${additional_headers}"
211 if [ ! -z "${template[references]}" ]; then
212 additional_headers="References: ${template[references]}
213 ${additional_headers}"
216 if [ ! -z "${template[content-type]}" ]; then
217 additional_headers="Content-Type: ${template[content-type]}
218 ${additional_headers}"
221 if [ ! -z "${template[content-transfer-encoding]}" ]; then
222 additional_headers="Content-Transfer-Encoding: ${template[content-transfer-encoding]}
223 ${additional_headers}"
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).
230 cat <<EOF >"$gen_msg_filename"
231 From: ${template[from]}
233 Message-Id: <${gen_msg_id}>
234 Subject: ${template[subject]}
235 Date: ${template[date]}
236 ${additional_headers}
241 # Generate a new message and add it to the database.
243 # All of the arguments and return values supported by generate_message
244 # are also supported here, so see that function for details.
247 generate_message "$@" &&
248 notmuch new > /dev/null
251 if test -n "$valgrind"
255 test "$1" = "$(readlink "$2")" || {
263 while test -d "$2".lock
265 say "Waiting for lock on $2."
272 make_valgrind_symlink () {
273 # handle only executables
274 test -x "$1" || return
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")"
283 symlink_target=$TEST_DIRECTORY/valgrind.sh
287 symlink_target=$TEST_DIRECTORY/unprocessed-script
289 # create the link, or replace it if it is out of date
290 make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
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
301 ls "$path"/notmuch 2> /dev/null |
304 make_valgrind_symlink "$file"
308 PATH=$GIT_VALGRIND/bin:$PATH
309 GIT_EXEC_PATH=$GIT_VALGRIND/bin
311 test -n "$notmuch_path" && MANPATH="$notmuch_path/doc/_build/man"
313 if test -n "$notmuch_path"
315 PATH="$notmuch_path:$PATH"
316 MANPATH="$notmuch_path/doc/_build/man"
322 test="tmp.$(basename "$0" .sh)"
323 test -n "$root" && test="$root/$test"
325 /*) TMP_DIRECTORY="$test" ;;
326 *) TMP_DIRECTORY="$TEST_DIRECTORY/$test" ;;
328 test ! -z "$debug" || remove_tmp=$TMP_DIRECTORY
331 echo >&6 "FATAL: Cannot prepare test area"
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"
342 MAIL_DIR="${TMP_DIRECTORY}/mail"
343 export NOTMUCH_CONFIG="${TMP_DIRECTORY}/notmuch-config"
346 mkdir -p "${MAIL_DIR}"
348 cat <<EOF >"${NOTMUCH_CONFIG}"
353 name=Notmuch Test Suite
354 primary_email=test_suite@notmuchmail.org
355 other_email=test_suite_other@notmuchmail.org;test_suite@otherdomain.org