4e17b781e26298c3abefec9ec7d2c1e7e9e28644
[notmuch] / test / test-lib-common.sh
1 #
2 # Copyright (c) 2005 Junio C Hamano
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see http://www.gnu.org/licenses/ .
16
17 # This file contains common code to be used by both the regular
18 # (correctness) tests and the performance tests.
19
20 find_notmuch_path ()
21 {
22     dir="$1"
23
24     while [ -n "$dir" ]; do
25         bin="$dir/notmuch"
26         if [ -x "$bin" ]; then
27             echo "$dir"
28             return
29         fi
30         dir="$(dirname "$dir")"
31         if [ "$dir" = "/" ]; then
32             break
33         fi
34     done
35 }
36
37 backup_database () {
38     test_name=$(basename $0 .sh)
39     rm -rf notmuch-dir-backup."$test_name"
40     cp -pR ${MAIL_DIR}/.notmuch notmuch-dir-backup."${test_name}"
41 }
42
43 restore_database () {
44     test_name=$(basename $0 .sh)
45     rm -rf ${MAIL_DIR}/.notmuch
46     cp -pR notmuch-dir-backup."${test_name}" ${MAIL_DIR}/.notmuch
47 }
48
49 # Test the binaries we have just built.  The tests are kept in
50 # test/ subdirectory and are run in 'trash directory' subdirectory.
51 TEST_DIRECTORY=$(pwd -P)
52 notmuch_path=`find_notmuch_path "$TEST_DIRECTORY"`
53
54 # configure output
55 . $notmuch_path/sh.config || exit 1
56
57 if test -n "$valgrind"
58 then
59         make_symlink () {
60                 test -h "$2" &&
61                 test "$1" = "$(readlink "$2")" || {
62                         # be super paranoid
63                         if mkdir "$2".lock
64                         then
65                                 rm -f "$2" &&
66                                 ln -s "$1" "$2" &&
67                                 rm -r "$2".lock
68                         else
69                                 while test -d "$2".lock
70                                 do
71                                         say "Waiting for lock on $2."
72                                         sleep 1
73                                 done
74                         fi
75                 }
76         }
77
78         make_valgrind_symlink () {
79                 # handle only executables
80                 test -x "$1" || return
81
82                 base=$(basename "$1")
83                 symlink_target=$TEST_DIRECTORY/../$base
84                 # do not override scripts
85                 if test -x "$symlink_target" &&
86                     test ! -d "$symlink_target" &&
87                     test "#!" != "$(head -c 2 < "$symlink_target")"
88                 then
89                         symlink_target=$TEST_DIRECTORY/valgrind.sh
90                 fi
91                 case "$base" in
92                 *.sh|*.perl)
93                         symlink_target=$TEST_DIRECTORY/unprocessed-script
94                 esac
95                 # create the link, or replace it if it is out of date
96                 make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
97         }
98
99         # override notmuch executable in TEST_DIRECTORY/..
100         GIT_VALGRIND=$TEST_DIRECTORY/valgrind
101         mkdir -p "$GIT_VALGRIND"/bin
102         make_valgrind_symlink $TEST_DIRECTORY/../notmuch
103         OLDIFS=$IFS
104         IFS=:
105         for path in $PATH
106         do
107                 ls "$path"/notmuch 2> /dev/null |
108                 while read file
109                 do
110                         make_valgrind_symlink "$file"
111                 done
112         done
113         IFS=$OLDIFS
114         PATH=$GIT_VALGRIND/bin:$PATH
115         GIT_EXEC_PATH=$GIT_VALGRIND/bin
116         export GIT_VALGRIND
117         test -n "$notmuch_path" && MANPATH="$notmuch_path/doc/_build/man"
118 else # normal case
119         if test -n "$notmuch_path"
120                 then
121                         PATH="$notmuch_path:$PATH"
122                         MANPATH="$notmuch_path/doc/_build/man"
123                 fi
124 fi
125 export PATH MANPATH
126
127 # Test repository
128 test="tmp.$(basename "$0" .sh)"
129 test -n "$root" && test="$root/$test"
130 case "$test" in
131 /*) TMP_DIRECTORY="$test" ;;
132  *) TMP_DIRECTORY="$TEST_DIRECTORY/$test" ;;
133 esac
134 test ! -z "$debug" || remove_tmp=$TMP_DIRECTORY
135 rm -fr "$test" || {
136         GIT_EXIT_OK=t
137         echo >&6 "FATAL: Cannot prepare test area"
138         exit 1
139 }
140
141 # A temporary home directory is needed by at least:
142 # - emacs/"Sending a message via (fake) SMTP"
143 # - emacs/"Reply within emacs"
144 # - crypto/emacs_deliver_message
145 export HOME="${TMP_DIRECTORY}/home"
146 mkdir -p "${HOME}"
147
148 MAIL_DIR="${TMP_DIRECTORY}/mail"
149 export GNUPGHOME="${TMP_DIRECTORY}/gnupg"
150 export NOTMUCH_CONFIG="${TMP_DIRECTORY}/notmuch-config"
151
152 mkdir -p "${test}"
153 mkdir -p "${MAIL_DIR}"
154
155 cat <<EOF >"${NOTMUCH_CONFIG}"
156 [database]
157 path=${MAIL_DIR}
158
159 [user]
160 name=Notmuch Test Suite
161 primary_email=test_suite@notmuchmail.org
162 other_email=test_suite_other@notmuchmail.org;test_suite@otherdomain.org
163 EOF