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