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