]> git.notmuchmail.org Git - notmuch/blob - test/test-lib.sh
Update test framework for use with notmuch
[notmuch] / test / test-lib.sh
1 #!/bin/bash
2 #
3 # Copyright (c) 2005 Junio C Hamano
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 # if --tee was passed, write the output not only to the terminal, but
19 # additionally to the file test-results/$BASENAME.out, too.
20 case "$GIT_TEST_TEE_STARTED, $* " in
21 done,*)
22         # do not redirect again
23         ;;
24 *' --tee '*|*' --va'*)
25         mkdir -p test-results
26         BASE=test-results/$(basename "$0" .sh)
27         (GIT_TEST_TEE_STARTED=done ${SHELL-sh} "$0" "$@" 2>&1;
28          echo $? > $BASE.exit) | tee $BASE.out
29         test "$(cat $BASE.exit)" = 0
30         exit
31         ;;
32 esac
33
34 # Keep the original TERM for say_color
35 ORIGINAL_TERM=$TERM
36
37 # For repeatability, reset the environment to known value.
38 LANG=C
39 LC_ALL=C
40 PAGER=cat
41 TZ=UTC
42 TERM=dumb
43 export LANG LC_ALL PAGER TERM TZ
44 GIT_TEST_CMP=${GIT_TEST_CMP:-diff -u}
45
46 # Protect ourselves from common misconfiguration to export
47 # CDPATH into the environment
48 unset CDPATH
49
50 unset GREP_OPTIONS
51
52 case $(echo $GIT_TRACE |tr "[A-Z]" "[a-z]") in
53         1|2|true)
54                 echo "* warning: Some tests will not work if GIT_TRACE" \
55                         "is set as to trace on STDERR ! *"
56                 echo "* warning: Please set GIT_TRACE to something" \
57                         "other than 1, 2 or true ! *"
58                 ;;
59 esac
60
61 # Convenience
62 #
63 # A regexp to match 5 and 40 hexdigits
64 _x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
65 _x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
66
67 _x04='[0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
68 _x32="$_x04$_x04$_x04$_x04$_x04$_x04$_x04$_x04"
69
70 # Each test should start with something like this, after copyright notices:
71 #
72 # test_description='Description of this test...
73 # This test checks if command xyzzy does the right thing...
74 # '
75 # . ./test-lib.sh
76 [ "x$ORIGINAL_TERM" != "xdumb" ] && (
77                 TERM=$ORIGINAL_TERM &&
78                 export TERM &&
79                 [ -t 1 ] &&
80                 tput bold >/dev/null 2>&1 &&
81                 tput setaf 1 >/dev/null 2>&1 &&
82                 tput sgr0 >/dev/null 2>&1
83         ) &&
84         color=t
85
86 while test "$#" -ne 0
87 do
88         case "$1" in
89         -d|--d|--de|--deb|--debu|--debug)
90                 debug=t; shift ;;
91         -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
92                 immediate=t; shift ;;
93         -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
94                 GIT_TEST_LONG=t; export GIT_TEST_LONG; shift ;;
95         -h|--h|--he|--hel|--help)
96                 help=t; shift ;;
97         -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
98                 verbose=t; shift ;;
99         -q|--q|--qu|--qui|--quie|--quiet)
100                 quiet=t; shift ;;
101         --with-dashes)
102                 with_dashes=t; shift ;;
103         --no-color)
104                 color=; shift ;;
105         --no-python)
106                 # noop now...
107                 shift ;;
108         --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
109                 valgrind=t; verbose=t; shift ;;
110         --tee)
111                 shift ;; # was handled already
112         --root=*)
113                 root=$(expr "z$1" : 'z[^=]*=\(.*\)')
114                 shift ;;
115         *)
116                 echo "error: unknown test option '$1'" >&2; exit 1 ;;
117         esac
118 done
119
120 if test -n "$color"; then
121         say_color () {
122                 (
123                 TERM=$ORIGINAL_TERM
124                 export TERM
125                 case "$1" in
126                         error) tput bold; tput setaf 1;; # bold red
127                         skip)  tput bold; tput setaf 2;; # bold green
128                         pass)  tput setaf 2;;            # green
129                         info)  tput setaf 3;;            # brown
130                         *) test -n "$quiet" && return;;
131                 esac
132                 shift
133                 printf "* %s" "$*"
134                 tput sgr0
135                 echo
136                 )
137         }
138 else
139         say_color() {
140                 test -z "$1" && test -n "$quiet" && return
141                 shift
142                 echo "* $*"
143         }
144 fi
145
146 error () {
147         say_color error "error: $*"
148         GIT_EXIT_OK=t
149         exit 1
150 }
151
152 say () {
153         say_color info "$*"
154 }
155
156 test "${test_description}" != "" ||
157 error "Test script did not set test_description."
158
159 if test "$help" = "t"
160 then
161         echo "$test_description"
162         exit 0
163 fi
164
165 exec 5>&1
166 if test "$verbose" = "t"
167 then
168         exec 4>&2 3>&1
169 else
170         exec 4>/dev/null 3>/dev/null
171 fi
172
173 test_failure=0
174 test_count=0
175 test_fixed=0
176 test_broken=0
177 test_success=0
178
179 die () {
180         code=$?
181         if test -n "$GIT_EXIT_OK"
182         then
183                 exit $code
184         else
185                 echo >&5 "FATAL: Unexpected exit with code $code"
186                 exit 1
187         fi
188 }
189
190 GIT_EXIT_OK=
191 trap 'die' EXIT
192
193 test_decode_color () {
194         sed     -e 's/.\[1m/<WHITE>/g' \
195                 -e 's/.\[31m/<RED>/g' \
196                 -e 's/.\[32m/<GREEN>/g' \
197                 -e 's/.\[33m/<YELLOW>/g' \
198                 -e 's/.\[34m/<BLUE>/g' \
199                 -e 's/.\[35m/<MAGENTA>/g' \
200                 -e 's/.\[36m/<CYAN>/g' \
201                 -e 's/.\[m/<RESET>/g'
202 }
203
204 q_to_nul () {
205         perl -pe 'y/Q/\000/'
206 }
207
208 q_to_cr () {
209         tr Q '\015'
210 }
211
212 append_cr () {
213         sed -e 's/$/Q/' | tr Q '\015'
214 }
215
216 remove_cr () {
217         tr '\015' Q | sed -e 's/Q$//'
218 }
219
220 # Notmuch helper functions
221 increment_mtime_amount=0
222 increment_mtime ()
223 {
224     dir=$1
225
226     increment_mtime_amount=$((increment_mtime_amount + 1))
227     touch -d "+${increment_mtime_amount} seconds" $dir
228 }
229
230 # Generate a new message in the mail directory, with a unique message
231 # ID and subject. The message is not added to the index.
232 #
233 # After this function returns, the filename of the generated message
234 # is available as $gen_msg_filename and the message ID is available as
235 # $gen_msg_id .
236 #
237 # This function supports named parameters with the bash syntax for
238 # assigning a value to an associative array ([name]=value). The
239 # supported parameters are:
240 #
241 #  [dir]=directory/of/choice
242 #
243 #       Generate the message in directory 'directory/of/choice' within
244 #       the mail store. The directory will be created if necessary.
245 #
246 #  [body]=text
247 #
248 #       Text to use as the body of the email message
249 #
250 #  '[from]="Some User <user@example.com>"'
251 #  '[to]="Some User <user@example.com>"'
252 #  '[subject]="Subject of email message"'
253 #  '[date]="RFC 822 Date"'
254 #
255 #       Values for email headers. If not provided, default values will
256 #       be generated instead.
257 #
258 #  '[cc]="Some User <user@example.com>"'
259 #  [reply-to]=some-address
260 #  [in-reply-to]=<message-id>
261 #  [references]=<message-id>
262 #  [content-type]=content-type-specification
263 #  '[header]=full header line, including keyword'
264 #
265 #       Additional values for email headers. If these are not provided
266 #       then the relevant headers will simply not appear in the
267 #       message.
268 #
269 #  '[id]=message-id'
270 #
271 #       Controls the message-id of the created message.
272 gen_msg_cnt=0
273 gen_msg_filename=""
274 gen_msg_id=""
275 generate_message ()
276 {
277     # This is our (bash-specific) magic for doing named parameters
278     local -A template="($@)"
279     local additional_headers
280
281     gen_msg_cnt=$((gen_msg_cnt + 1))
282     gen_msg_name=msg-$(printf "%03d" $gen_msg_cnt)
283
284     if [ -z "${template[id]}" ]; then
285         gen_msg_id="${gen_msg_name}@notmuch-test-suite"
286     else
287         gen_msg_id="${template[id]}"
288     fi
289
290     if [ -z "${template[dir]}" ]; then
291         gen_msg_filename="${MAIL_DIR}/$gen_msg_name"
292     else
293         gen_msg_filename="${MAIL_DIR}/${template[dir]}/$gen_msg_name"
294         mkdir -p $(dirname $gen_msg_filename)
295     fi
296
297     if [ -z "${template[body]}" ]; then
298         template[body]="This is just a test message (#${gen_msg_cnt})"
299     fi
300
301     if [ -z "${template[from]}" ]; then
302         template[from]="Notmuch Test Suite <test_suite@notmuchmail.org>"
303     fi
304
305     if [ -z "${template[to]}" ]; then
306         template[to]="Notmuch Test Suite <test_suite@notmuchmail.org>"
307     fi
308
309     if [ -z "${template[subject]}" ]; then
310         template[subject]="Test message #${gen_msg_cnt}"
311     fi
312
313     if [ -z "${template[date]}" ]; then
314         template[date]="Tue, 05 Jan 2001 15:43:57 -0800"
315     fi
316
317     additional_headers=""
318     if [ ! -z "${template[header]}" ]; then
319         additional_headers="${template[header]}
320 ${additional_headers}"
321     fi
322
323     if [ ! -z "${template[reply-to]}" ]; then
324         additional_headers="Reply-To: ${template[reply-to]}
325 ${additional_headers}"
326     fi
327
328     if [ ! -z "${template[in-reply-to]}" ]; then
329         additional_headers="In-Reply-To: ${template[in-reply-to]}
330 ${additional_headers}"
331     fi
332
333     if [ ! -z "${template[cc]}" ]; then
334         additional_headers="Cc: ${template[cc]}
335 ${additional_headers}"
336     fi
337
338     if [ ! -z "${template[references]}" ]; then
339         additional_headers="References: ${template[references]}
340 ${additional_headers}"
341     fi
342
343     if [ ! -z "${template[content-type]}" ]; then
344         additional_headers="Content-Type: ${template[content-type]}
345 ${additional_headers}"
346     fi
347
348
349 cat <<EOF >$gen_msg_filename
350 From: ${template[from]}
351 To: ${template[to]}
352 Message-Id: <${gen_msg_id}>
353 Subject: ${template[subject]}
354 Date: ${template[date]}
355 ${additional_headers}
356 ${template[body]}
357 EOF
358
359     # Ensure that the mtime of the containing directory is updated
360     increment_mtime $(dirname ${gen_msg_filename})
361 }
362
363 # Generate a new message and add it to the index.
364 #
365 # All of the arguments and return values supported by generate_message
366 # are also supported here, so see that function for details.
367 add_message ()
368 {
369     generate_message "$@"
370
371     $NOTMUCH new > /dev/null
372 }
373
374 tests=0
375 test_failures=0
376
377 pass_if_equal ()
378 {
379     output=$1
380     expected=$2
381
382     tests=$((tests + 1))
383
384     if [ "$output" = "$expected" ]; then
385         echo "  PASS"
386     else
387         echo "  FAIL"
388         testname=test-$(printf "%03d" $tests)
389         echo "$expected" > $testname.expected
390         echo "$output" > $testname.output
391         diff -u $testname.expected $testname.output || true
392         test_failures=$((test_failures + 1))
393     fi
394 }
395
396 TEST_DIR=$(pwd)/test.$$
397 MAIL_DIR=${TEST_DIR}/mail
398 export NOTMUCH_CONFIG=${TEST_DIR}/notmuch-config
399 NOTMUCH=$(find_notmuch_binary $(pwd))
400
401 NOTMUCH_NEW ()
402 {
403     $NOTMUCH new | grep -v -E -e '^Processed [0-9]*( total)? file|Found [0-9]* total file'
404 }
405
406 notmuch_search_sanitize ()
407 {
408     sed -r -e 's/("?thread"?: ?)("?)................("?)/\1\2XXX\3/'
409 }
410
411 NOTMUCH_SHOW_FILENAME_SQUELCH='s,filename:.*/mail,filename:/XXX/mail,'
412 notmuch_show_sanitize ()
413 {
414     sed -e "$NOTMUCH_SHOW_FILENAME_SQUELCH"
415 }
416
417 # End of notmuch helper functions
418
419 # Use test_set_prereq to tell that a particular prerequisite is available.
420 # The prerequisite can later be checked for in two ways:
421 #
422 # - Explicitly using test_have_prereq.
423 #
424 # - Implicitly by specifying the prerequisite tag in the calls to
425 #   test_expect_{success,failure,code}.
426 #
427 # The single parameter is the prerequisite tag (a simple word, in all
428 # capital letters by convention).
429
430 test_set_prereq () {
431         satisfied="$satisfied$1 "
432 }
433 satisfied=" "
434
435 test_have_prereq () {
436         case $satisfied in
437         *" $1 "*)
438                 : yes, have it ;;
439         *)
440                 ! : nope ;;
441         esac
442 }
443
444 # You are not expected to call test_ok_ and test_failure_ directly, use
445 # the text_expect_* functions instead.
446
447 test_ok_ () {
448         test_success=$(($test_success + 1))
449         say_color "" "  ok $test_count: $@"
450 }
451
452 test_failure_ () {
453         test_failure=$(($test_failure + 1))
454         say_color error "FAIL $test_count: $1"
455         shift
456         echo "$@" | sed -e 's/^/        /'
457         test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
458 }
459
460 test_known_broken_ok_ () {
461         test_fixed=$(($test_fixed+1))
462         say_color "" "  FIXED $test_count: $@"
463 }
464
465 test_known_broken_failure_ () {
466         test_broken=$(($test_broken+1))
467         say_color skip "  still broken $test_count: $@"
468 }
469
470 test_debug () {
471         test "$debug" = "" || eval "$1"
472 }
473
474 test_run_ () {
475         test_cleanup=:
476         eval >&3 2>&4 "$1"
477         eval_ret=$?
478         eval >&3 2>&4 "$test_cleanup"
479         return 0
480 }
481
482 test_skip () {
483         test_count=$(($test_count+1))
484         to_skip=
485         for skp in $GIT_SKIP_TESTS
486         do
487                 case $this_test.$test_count in
488                 $skp)
489                         to_skip=t
490                 esac
491         done
492         if test -z "$to_skip" && test -n "$prereq" &&
493            ! test_have_prereq "$prereq"
494         then
495                 to_skip=t
496         fi
497         case "$to_skip" in
498         t)
499                 say_color skip >&3 "skipping test: $@"
500                 say_color skip "skip $test_count: $1"
501                 : true
502                 ;;
503         *)
504                 false
505                 ;;
506         esac
507 }
508
509 test_expect_failure () {
510         test "$#" = 3 && { prereq=$1; shift; } || prereq=
511         test "$#" = 2 ||
512         error "bug in the test script: not 2 or 3 parameters to test-expect-failure"
513         if ! test_skip "$@"
514         then
515                 say >&3 "checking known breakage: $2"
516                 test_run_ "$2"
517                 if [ "$?" = 0 -a "$eval_ret" = 0 ]
518                 then
519                         test_known_broken_ok_ "$1"
520                 else
521                         test_known_broken_failure_ "$1"
522                 fi
523         fi
524         echo >&3 ""
525 }
526
527 test_expect_success () {
528         test "$#" = 3 && { prereq=$1; shift; } || prereq=
529         test "$#" = 2 ||
530         error "bug in the test script: not 2 or 3 parameters to test-expect-success"
531         if ! test_skip "$@"
532         then
533                 say >&3 "expecting success: $2"
534                 test_run_ "$2"
535                 if [ "$?" = 0 -a "$eval_ret" = 0 ]
536                 then
537                         test_ok_ "$1"
538                 else
539                         test_failure_ "$@"
540                 fi
541         fi
542         echo >&3 ""
543 }
544
545 test_expect_code () {
546         test "$#" = 4 && { prereq=$1; shift; } || prereq=
547         test "$#" = 3 ||
548         error "bug in the test script: not 3 or 4 parameters to test-expect-code"
549         if ! test_skip "$@"
550         then
551                 say >&3 "expecting exit code $1: $3"
552                 test_run_ "$3"
553                 if [ "$?" = 0 -a "$eval_ret" = "$1" ]
554                 then
555                         test_ok_ "$2"
556                 else
557                         test_failure_ "$@"
558                 fi
559         fi
560         echo >&3 ""
561 }
562
563 # test_external runs external test scripts that provide continuous
564 # test output about their progress, and succeeds/fails on
565 # zero/non-zero exit code.  It outputs the test output on stdout even
566 # in non-verbose mode, and announces the external script with "* run
567 # <n>: ..." before running it.  When providing relative paths, keep in
568 # mind that all scripts run in "trash directory".
569 # Usage: test_external description command arguments...
570 # Example: test_external 'Perl API' perl ../path/to/test.pl
571 test_external () {
572         test "$#" = 4 && { prereq=$1; shift; } || prereq=
573         test "$#" = 3 ||
574         error >&5 "bug in the test script: not 3 or 4 parameters to test_external"
575         descr="$1"
576         shift
577         if ! test_skip "$descr" "$@"
578         then
579                 # Announce the script to reduce confusion about the
580                 # test output that follows.
581                 say_color "" " run $test_count: $descr ($*)"
582                 # Run command; redirect its stderr to &4 as in
583                 # test_run_, but keep its stdout on our stdout even in
584                 # non-verbose mode.
585                 "$@" 2>&4
586                 if [ "$?" = 0 ]
587                 then
588                         test_ok_ "$descr"
589                 else
590                         test_failure_ "$descr" "$@"
591                 fi
592         fi
593 }
594
595 # Like test_external, but in addition tests that the command generated
596 # no output on stderr.
597 test_external_without_stderr () {
598         # The temporary file has no (and must have no) security
599         # implications.
600         tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
601         stderr="$tmp/git-external-stderr.$$.tmp"
602         test_external "$@" 4> "$stderr"
603         [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
604         descr="no stderr: $1"
605         shift
606         say >&3 "expecting no stderr from previous command"
607         if [ ! -s "$stderr" ]; then
608                 rm "$stderr"
609                 test_ok_ "$descr"
610         else
611                 if [ "$verbose" = t ]; then
612                         output=`echo; echo Stderr is:; cat "$stderr"`
613                 else
614                         output=
615                 fi
616                 # rm first in case test_failure exits.
617                 rm "$stderr"
618                 test_failure_ "$descr" "$@" "$output"
619         fi
620 }
621
622 # This is not among top-level (test_expect_success | test_expect_failure)
623 # but is a prefix that can be used in the test script, like:
624 #
625 #       test_expect_success 'complain and die' '
626 #           do something &&
627 #           do something else &&
628 #           test_must_fail git checkout ../outerspace
629 #       '
630 #
631 # Writing this as "! git checkout ../outerspace" is wrong, because
632 # the failure could be due to a segv.  We want a controlled failure.
633
634 test_must_fail () {
635         "$@"
636         test $? -gt 0 -a $? -le 129 -o $? -gt 192
637 }
638
639 # test_cmp is a helper function to compare actual and expected output.
640 # You can use it like:
641 #
642 #       test_expect_success 'foo works' '
643 #               echo expected >expected &&
644 #               foo >actual &&
645 #               test_cmp expected actual
646 #       '
647 #
648 # This could be written as either "cmp" or "diff -u", but:
649 # - cmp's output is not nearly as easy to read as diff -u
650 # - not all diff versions understand "-u"
651
652 test_cmp() {
653         $GIT_TEST_CMP "$@"
654 }
655
656 # This function can be used to schedule some commands to be run
657 # unconditionally at the end of the test to restore sanity:
658 #
659 #       test_expect_success 'test core.capslock' '
660 #               git config core.capslock true &&
661 #               test_when_finished "git config --unset core.capslock" &&
662 #               hello world
663 #       '
664 #
665 # That would be roughly equivalent to
666 #
667 #       test_expect_success 'test core.capslock' '
668 #               git config core.capslock true &&
669 #               hello world
670 #               git config --unset core.capslock
671 #       '
672 #
673 # except that the greeting and config --unset must both succeed for
674 # the test to pass.
675
676 test_when_finished () {
677         test_cleanup="{ $*
678                 } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
679 }
680
681 test_done () {
682         GIT_EXIT_OK=t
683         test_results_dir="$TEST_DIRECTORY/test-results"
684         mkdir -p "$test_results_dir"
685         test_results_path="$test_results_dir/${0%.sh}-$$"
686
687         echo "total $test_count" >> $test_results_path
688         echo "success $test_success" >> $test_results_path
689         echo "fixed $test_fixed" >> $test_results_path
690         echo "broken $test_broken" >> $test_results_path
691         echo "failed $test_failure" >> $test_results_path
692         echo "" >> $test_results_path
693
694         if test "$test_fixed" != 0
695         then
696                 say_color pass "fixed $test_fixed known breakage(s)"
697         fi
698         if test "$test_broken" != 0
699         then
700                 say_color error "still have $test_broken known breakage(s)"
701                 msg="remaining $(($test_count-$test_broken)) test(s)"
702         else
703                 msg="$test_count test(s)"
704         fi
705         case "$test_failure" in
706         0)
707                 say_color pass "passed all $msg"
708
709                 test -d "$remove_trash" &&
710                 cd "$(dirname "$remove_trash")" &&
711                 rm -rf "$(basename "$remove_trash")"
712
713                 exit 0 ;;
714
715         *)
716                 say_color error "failed $test_failure among $msg"
717                 exit 1 ;;
718
719         esac
720 }
721
722 find_notmuch_path ()
723 {
724     dir="$1"
725
726     while [ -n "$dir" ]; do
727         bin="$dir/notmuch"
728         if [ -x "$bin" ]; then
729             echo "$dir"
730             return
731         fi
732         dir="$(dirname "$dir")"
733         if [ "$dir" = "/" ]; then
734             break
735         fi
736     done
737 }
738
739 # Test the binaries we have just built.  The tests are kept in
740 # test/ subdirectory and are run in 'trash directory' subdirectory.
741 TEST_DIRECTORY=$(pwd)
742 # FIXME: Only the normal case bellow is updated to notmuch
743 if test -n "$valgrind"
744 then
745         make_symlink () {
746                 test -h "$2" &&
747                 test "$1" = "$(readlink "$2")" || {
748                         # be super paranoid
749                         if mkdir "$2".lock
750                         then
751                                 rm -f "$2" &&
752                                 ln -s "$1" "$2" &&
753                                 rm -r "$2".lock
754                         else
755                                 while test -d "$2".lock
756                                 do
757                                         say "Waiting for lock on $2."
758                                         sleep 1
759                                 done
760                         fi
761                 }
762         }
763
764         make_valgrind_symlink () {
765                 # handle only executables
766                 test -x "$1" || return
767
768                 base=$(basename "$1")
769                 symlink_target=$TEST_DIRECTORY/../$base
770                 # do not override scripts
771                 if test -x "$symlink_target" &&
772                     test ! -d "$symlink_target" &&
773                     test "#!" != "$(head -c 2 < "$symlink_target")"
774                 then
775                         symlink_target=../valgrind.sh
776                 fi
777                 case "$base" in
778                 *.sh|*.perl)
779                         symlink_target=../unprocessed-script
780                 esac
781                 # create the link, or replace it if it is out of date
782                 make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
783         }
784
785         # override all git executables in TEST_DIRECTORY/..
786         GIT_VALGRIND=$TEST_DIRECTORY/valgrind
787         mkdir -p "$GIT_VALGRIND"/bin
788         for file in $TEST_DIRECTORY/../git* $TEST_DIRECTORY/../test-*
789         do
790                 make_valgrind_symlink $file
791         done
792         OLDIFS=$IFS
793         IFS=:
794         for path in $PATH
795         do
796                 ls "$path"/git-* 2> /dev/null |
797                 while read file
798                 do
799                         make_valgrind_symlink "$file"
800                 done
801         done
802         IFS=$OLDIFS
803         PATH=$GIT_VALGRIND/bin:$PATH
804         GIT_EXEC_PATH=$GIT_VALGRIND/bin
805         export GIT_VALGRIND
806 elif test -n "$GIT_TEST_INSTALLED" ; then
807         GIT_EXEC_PATH=$($GIT_TEST_INSTALLED/git --exec-path)  ||
808         error "Cannot run git from $GIT_TEST_INSTALLED."
809         PATH=$GIT_TEST_INSTALLED:$TEST_DIRECTORY/..:$PATH
810         GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH}
811 else # normal case
812         notmuch_path=`find_notmuch_path "$TEST_DIRECTORY"`
813         test -n "$notmuch_path" && PATH="$notmuch_path:$PATH"
814 fi
815 export PATH
816
817 # Test repository
818 test="trash directory.$(basename "$0" .sh)"
819 test -n "$root" && test="$root/$test"
820 case "$test" in
821 /*) TRASH_DIRECTORY="$test" ;;
822  *) TRASH_DIRECTORY="$TEST_DIRECTORY/$test" ;;
823 esac
824 test ! -z "$debug" || remove_trash=$TRASH_DIRECTORY
825 rm -fr "$test" || {
826         GIT_EXIT_OK=t
827         echo >&5 "FATAL: Cannot prepare test area"
828         exit 1
829 }
830
831 MAIL_DIR="${TRASH_DIRECTORY}/mail"
832 export NOTMUCH_CONFIG="${TRASH_DIRECTORY}/notmuch-config"
833
834 mkdir -p "${test}"
835 mkdir "$MAIL_DIR"
836
837 cat <<EOF >"${NOTMUCH_CONFIG}"
838 [database]
839 path=${MAIL_DIR}
840
841 [user]
842 name=Notmuch Test Suite
843 primary_email=test_suite@notmuchmail.org
844 other_email=test_suite_other@notmuchmail.org;test_suite@otherdomain.org
845 EOF
846
847
848 # Use -P to resolve symlinks in our working directory so that the cwd
849 # in subprocesses like git equals our $PWD (for pathname comparisons).
850 cd -P "$test" || error "Cannot setup test environment"
851
852 this_test=${0##*/}
853 this_test=${this_test%%-*}
854 for skp in $GIT_SKIP_TESTS
855 do
856         to_skip=
857         for skp in $GIT_SKIP_TESTS
858         do
859                 case "$this_test" in
860                 $skp)
861                         to_skip=t
862                 esac
863         done
864         case "$to_skip" in
865         t)
866                 say_color skip >&3 "skipping test $this_test altogether"
867                 say_color skip "skip all tests in $this_test"
868                 test_done
869         esac
870 done
871
872 # Provide an implementation of the 'yes' utility
873 yes () {
874         if test $# = 0
875         then
876                 y=y
877         else
878                 y="$*"
879         fi
880
881         while echo "$y"
882         do
883                 :
884         done
885 }
886
887 # Fix some commands on Windows
888 case $(uname -s) in
889 *MINGW*)
890         # Windows has its own (incompatible) sort and find
891         sort () {
892                 /usr/bin/sort "$@"
893         }
894         find () {
895                 /usr/bin/find "$@"
896         }
897         sum () {
898                 md5sum "$@"
899         }
900         # git sees Windows-style pwd
901         pwd () {
902                 builtin pwd -W
903         }
904         # no POSIX permissions
905         # backslashes in pathspec are converted to '/'
906         # exec does not inherit the PID
907         ;;
908 *)
909         test_set_prereq POSIXPERM
910         test_set_prereq BSLASHPSPEC
911         test_set_prereq EXECKEEPSPID
912         ;;
913 esac
914
915 test -z "$NO_PERL" && test_set_prereq PERL
916 test -z "$NO_PYTHON" && test_set_prereq PYTHON
917
918 # test whether the filesystem supports symbolic links
919 ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS
920 rm -f y