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