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