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; }
27 if [[ -z "$NOTMUCH_SRCDIR" ]] || [[ -z "$NOTMUCH_BUILDDIR" ]]; then
28 echo "internal: srcdir or builddir not set" >&2
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 () {
38 if [[ ${test_missing_external_prereq_["${binary}"]} == t ]]; then
39 # dependency is missing, call the replacement function to note it
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}"
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
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
64 . "$NOTMUCH_BUILDDIR/sh.config" || exit 1
67 if [[ -e "$NOTMUCH_SRCDIR/test/test-lib-$PLATFORM.sh" ]]; then
68 . "$NOTMUCH_SRCDIR/test/test-lib-$PLATFORM.sh" || exit 1
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.
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
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:
82 # [dir]=directory/of/choice
84 # Generate the message in directory 'directory/of/choice' within
85 # the mail store. The directory will be created if necessary.
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
95 # Text to use as the body of the email message
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"'
102 # Values for email headers. If not provided, default values will
103 # be generated instead.
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'
112 # Additional values for email headers. If these are not provided
113 # then the relevant headers will simply not appear in the
118 # Controls the message-id of the created message.
122 generate_message () {
123 # This is our (bash-specific) magic for doing named parameters
124 local -A template="($@)"
125 local additional_headers
127 gen_msg_cnt=$((gen_msg_cnt + 1))
128 if [ -z "${template[filename]}" ]; then
129 gen_msg_name="msg-$(printf "%03d" $gen_msg_cnt)"
131 gen_msg_name=${template[filename]}
134 if [ -z "${template[id]}" ]; then
135 gen_msg_id="${gen_msg_name%:2,*}@notmuch-test-suite"
137 gen_msg_id="${template[id]}"
140 if [ -z "${template[dir]}" ]; then
141 gen_msg_filename="${MAIL_DIR}/$gen_msg_name"
143 gen_msg_filename="${MAIL_DIR}/${template[dir]}/$gen_msg_name"
144 mkdir -p "$(dirname "$gen_msg_filename")"
147 if [ -z "${template[body]}" ]; then
148 template[body]="This is just a test message (#${gen_msg_cnt})"
151 if [ -z "${template[from]}" ]; then
152 template[from]="Notmuch Test Suite <test_suite@notmuchmail.org>"
155 if [ -z "${template[to]}" ]; then
156 template[to]="Notmuch Test Suite <test_suite@notmuchmail.org>"
159 if [ -z "${template[subject]}" ]; then
160 if [ -n "$test_subtest_name" ]; then
161 template[subject]="$test_subtest_name"
163 template[subject]="Test message #${gen_msg_cnt}"
165 elif [ "${template[subject]}" = "@FORCE_EMPTY" ]; then
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'`
181 additional_headers=""
182 if [ ! -z "${template[header]}" ]; then
183 additional_headers="${template[header]}
184 ${additional_headers}"
187 if [ ! -z "${template[reply-to]}" ]; then
188 additional_headers="Reply-To: ${template[reply-to]}
189 ${additional_headers}"
192 if [ ! -z "${template[in-reply-to]}" ]; then
193 additional_headers="In-Reply-To: ${template[in-reply-to]}
194 ${additional_headers}"
197 if [ ! -z "${template[cc]}" ]; then
198 additional_headers="Cc: ${template[cc]}
199 ${additional_headers}"
202 if [ ! -z "${template[bcc]}" ]; then
203 additional_headers="Bcc: ${template[bcc]}
204 ${additional_headers}"
207 if [ ! -z "${template[references]}" ]; then
208 additional_headers="References: ${template[references]}
209 ${additional_headers}"
212 if [ ! -z "${template[content-type]}" ]; then
213 additional_headers="Content-Type: ${template[content-type]}
214 ${additional_headers}"
217 if [ ! -z "${template[content-transfer-encoding]}" ]; then
218 additional_headers="Content-Transfer-Encoding: ${template[content-transfer-encoding]}
219 ${additional_headers}"
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).
226 cat <<EOF >"$gen_msg_filename"
227 From: ${template[from]}
229 Message-Id: <${gen_msg_id}>
230 Subject: ${template[subject]}
231 Date: ${template[date]}
232 ${additional_headers}
237 # Generate a new message and add it to the database.
239 # All of the arguments and return values supported by generate_message
240 # are also supported here, so see that function for details.
242 generate_message "$@" &&
243 notmuch new > /dev/null
246 if test -n "$valgrind"
250 test "$1" = "$(readlink "$2")" || {
258 while test -d "$2".lock
260 say "Waiting for lock on $2."
267 make_valgrind_symlink () {
268 # handle only executables
269 test -x "$1" || return
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")"
278 symlink_target=$TEST_DIRECTORY/valgrind.sh
282 symlink_target=$TEST_DIRECTORY/unprocessed-script
284 # create the link, or replace it if it is out of date
285 make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
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
296 ls "$path"/notmuch 2> /dev/null |
299 make_valgrind_symlink "$file"
303 PATH=$GIT_VALGRIND/bin:$PATH
304 GIT_EXEC_PATH=$GIT_VALGRIND/bin
306 test -n "$NOTMUCH_BUILDDIR" && MANPATH="$NOTMUCH_BUILDDIR/doc/_build/man"
308 if test -n "$NOTMUCH_BUILDDIR"
310 PATH="$NOTMUCH_BUILDDIR:$PATH"
311 MANPATH="$NOTMUCH_BUILDDIR/doc/_build/man"
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" || {
322 echo >&6 "FATAL: Cannot prepare test area"
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"
333 MAIL_DIR="${TMP_DIRECTORY}/mail"
334 export NOTMUCH_CONFIG="${TMP_DIRECTORY}/notmuch-config"
336 mkdir -p "${MAIL_DIR}"
338 cat <<EOF >"${NOTMUCH_CONFIG}"
343 name=Notmuch Test Suite
344 primary_email=test_suite@notmuchmail.org
345 other_email=test_suite_other@notmuchmail.org;test_suite@otherdomain.org