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