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