03ef1d2d02467674f4f096fcd872a409fe7421fe
[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 if test -n "$valgrind"
70 then
71         make_symlink () {
72                 test -h "$2" &&
73                 test "$1" = "$(readlink "$2")" || {
74                         # be super paranoid
75                         if mkdir "$2".lock
76                         then
77                                 rm -f "$2" &&
78                                 ln -s "$1" "$2" &&
79                                 rm -r "$2".lock
80                         else
81                                 while test -d "$2".lock
82                                 do
83                                         say "Waiting for lock on $2."
84                                         sleep 1
85                                 done
86                         fi
87                 }
88         }
89
90         make_valgrind_symlink () {
91                 # handle only executables
92                 test -x "$1" || return
93
94                 base=$(basename "$1")
95                 symlink_target=$TEST_DIRECTORY/../$base
96                 # do not override scripts
97                 if test -x "$symlink_target" &&
98                     test ! -d "$symlink_target" &&
99                     test "#!" != "$(head -c 2 < "$symlink_target")"
100                 then
101                         symlink_target=$TEST_DIRECTORY/valgrind.sh
102                 fi
103                 case "$base" in
104                 *.sh|*.perl)
105                         symlink_target=$TEST_DIRECTORY/unprocessed-script
106                 esac
107                 # create the link, or replace it if it is out of date
108                 make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
109         }
110
111         # override notmuch executable in TEST_DIRECTORY/..
112         GIT_VALGRIND=$TEST_DIRECTORY/valgrind
113         mkdir -p "$GIT_VALGRIND"/bin
114         make_valgrind_symlink $TEST_DIRECTORY/../notmuch
115         OLDIFS=$IFS
116         IFS=:
117         for path in $PATH
118         do
119                 ls "$path"/notmuch 2> /dev/null |
120                 while read file
121                 do
122                         make_valgrind_symlink "$file"
123                 done
124         done
125         IFS=$OLDIFS
126         PATH=$GIT_VALGRIND/bin:$PATH
127         GIT_EXEC_PATH=$GIT_VALGRIND/bin
128         export GIT_VALGRIND
129         test -n "$notmuch_path" && MANPATH="$notmuch_path/doc/_build/man"
130 else # normal case
131         if test -n "$notmuch_path"
132                 then
133                         PATH="$notmuch_path:$PATH"
134                         MANPATH="$notmuch_path/doc/_build/man"
135                 fi
136 fi
137 export PATH MANPATH
138
139 # Test repository
140 test="tmp.$(basename "$0" .sh)"
141 test -n "$root" && test="$root/$test"
142 case "$test" in
143 /*) TMP_DIRECTORY="$test" ;;
144  *) TMP_DIRECTORY="$TEST_DIRECTORY/$test" ;;
145 esac
146 test ! -z "$debug" || remove_tmp=$TMP_DIRECTORY
147 rm -fr "$test" || {
148         GIT_EXIT_OK=t
149         echo >&6 "FATAL: Cannot prepare test area"
150         exit 1
151 }
152
153 # A temporary home directory is needed by at least:
154 # - emacs/"Sending a message via (fake) SMTP"
155 # - emacs/"Reply within emacs"
156 # - crypto/emacs_deliver_message
157 export HOME="${TMP_DIRECTORY}/home"
158 mkdir -p "${HOME}"
159
160 MAIL_DIR="${TMP_DIRECTORY}/mail"
161 export GNUPGHOME="${TMP_DIRECTORY}/gnupg"
162 export NOTMUCH_CONFIG="${TMP_DIRECTORY}/notmuch-config"
163
164 mkdir -p "${test}"
165 mkdir -p "${MAIL_DIR}"
166
167 cat <<EOF >"${NOTMUCH_CONFIG}"
168 [database]
169 path=${MAIL_DIR}
170
171 [user]
172 name=Notmuch Test Suite
173 primary_email=test_suite@notmuchmail.org
174 other_email=test_suite_other@notmuchmail.org;test_suite@otherdomain.org
175 EOF