]> git.notmuchmail.org Git - notmuch/blob - test/test-lib.sh
test: Cleanup the test output
[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 pass_if_equal ()
377 {
378     output=$1
379     expected=$2
380
381     if [ "$output" = "$expected" ]; then
382         true
383     else
384         testname=$this_test.$test_count
385         echo "$expected" > $testname.expected
386         echo "$output" > $testname.output
387         diff -u $testname.expected $testname.output
388         false
389     fi
390 }
391
392 TEST_DIR=$(pwd)/test.$$
393 MAIL_DIR=${TEST_DIR}/mail
394 export NOTMUCH_CONFIG=${TEST_DIR}/notmuch-config
395 NOTMUCH=notmuch
396
397 NOTMUCH_NEW ()
398 {
399     notmuch new | grep -v -E -e '^Processed [0-9]*( total)? file|Found [0-9]* total file'
400 }
401
402 notmuch_search_sanitize ()
403 {
404     sed -r -e 's/("?thread"?: ?)("?)................("?)/\1\2XXX\3/'
405 }
406
407 NOTMUCH_SHOW_FILENAME_SQUELCH='s,filename:.*/mail,filename:/XXX/mail,'
408 notmuch_show_sanitize ()
409 {
410     sed -e "$NOTMUCH_SHOW_FILENAME_SQUELCH"
411 }
412
413 # End of notmuch helper functions
414
415 # Use test_set_prereq to tell that a particular prerequisite is available.
416 # The prerequisite can later be checked for in two ways:
417 #
418 # - Explicitly using test_have_prereq.
419 #
420 # - Implicitly by specifying the prerequisite tag in the calls to
421 #   test_expect_{success,failure,code}.
422 #
423 # The single parameter is the prerequisite tag (a simple word, in all
424 # capital letters by convention).
425
426 test_set_prereq () {
427         satisfied="$satisfied$1 "
428 }
429 satisfied=" "
430
431 test_have_prereq () {
432         case $satisfied in
433         *" $1 "*)
434                 : yes, have it ;;
435         *)
436                 ! : nope ;;
437         esac
438 }
439
440 # You are not expected to call test_ok_ and test_failure_ directly, use
441 # the text_expect_* functions instead.
442
443 test_ok_ () {
444         test_success=$(($test_success + 1))
445         say_color pass "%-6s" "PASS"
446         echo " $@"
447 }
448
449 test_failure_ () {
450         test_failure=$(($test_failure + 1))
451         say_color error "%-6s" "FAIL"
452         echo " $1"
453         shift
454         echo "$@" | sed -e 's/^/        /'
455         test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
456 }
457
458 test_known_broken_ok_ () {
459         test_fixed=$(($test_fixed+1))
460         say_color pass "%-6s" "FIXED"
461         echo " $@"
462 }
463
464 test_known_broken_failure_ () {
465         test_broken=$(($test_broken+1))
466         say_color pass "%-6s" "BROKEN"
467         echo " $@"
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 "%-6s" "SKIP"
501                 echo " $1"
502                 : true
503                 ;;
504         *)
505                 false
506                 ;;
507         esac
508 }
509
510 test_expect_failure () {
511         test "$#" = 3 && { prereq=$1; shift; } || prereq=
512         test "$#" = 2 ||
513         error "bug in the test script: not 2 or 3 parameters to test-expect-failure"
514         if ! test_skip "$@"
515         then
516                 say >&3 "checking known breakage: $2"
517                 test_run_ "$2"
518                 if [ "$?" = 0 -a "$eval_ret" = 0 ]
519                 then
520                         test_known_broken_ok_ "$1"
521                 else
522                         test_known_broken_failure_ "$1"
523                 fi
524         fi
525         echo >&3 ""
526 }
527
528 test_expect_success () {
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-success"
532         if ! test_skip "$@"
533         then
534                 say >&3 "expecting success: $2"
535                 test_run_ "$2"
536                 if [ "$?" = 0 -a "$eval_ret" = 0 ]
537                 then
538                         test_ok_ "$1"
539                 else
540                         test_failure_ "$@"
541                 fi
542         fi
543         echo >&3 ""
544 }
545
546 test_expect_code () {
547         test "$#" = 4 && { prereq=$1; shift; } || prereq=
548         test "$#" = 3 ||
549         error "bug in the test script: not 3 or 4 parameters to test-expect-code"
550         if ! test_skip "$@"
551         then
552                 say >&3 "expecting exit code $1: $3"
553                 test_run_ "$3"
554                 if [ "$?" = 0 -a "$eval_ret" = "$1" ]
555                 then
556                         test_ok_ "$2"
557                 else
558                         test_failure_ "$@"
559                 fi
560         fi
561         echo >&3 ""
562 }
563
564 # test_external runs external test scripts that provide continuous
565 # test output about their progress, and succeeds/fails on
566 # zero/non-zero exit code.  It outputs the test output on stdout even
567 # in non-verbose mode, and announces the external script with "* run
568 # <n>: ..." before running it.  When providing relative paths, keep in
569 # mind that all scripts run in "trash directory".
570 # Usage: test_external description command arguments...
571 # Example: test_external 'Perl API' perl ../path/to/test.pl
572 test_external () {
573         test "$#" = 4 && { prereq=$1; shift; } || prereq=
574         test "$#" = 3 ||
575         error >&5 "bug in the test script: not 3 or 4 parameters to test_external"
576         descr="$1"
577         shift
578         if ! test_skip "$descr" "$@"
579         then
580                 # Announce the script to reduce confusion about the
581                 # test output that follows.
582                 say_color "" " run $test_count: $descr ($*)"
583                 # Run command; redirect its stderr to &4 as in
584                 # test_run_, but keep its stdout on our stdout even in
585                 # non-verbose mode.
586                 "$@" 2>&4
587                 if [ "$?" = 0 ]
588                 then
589                         test_ok_ "$descr"
590                 else
591                         test_failure_ "$descr" "$@"
592                 fi
593         fi
594 }
595
596 # Like test_external, but in addition tests that the command generated
597 # no output on stderr.
598 test_external_without_stderr () {
599         # The temporary file has no (and must have no) security
600         # implications.
601         tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
602         stderr="$tmp/git-external-stderr.$$.tmp"
603         test_external "$@" 4> "$stderr"
604         [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
605         descr="no stderr: $1"
606         shift
607         say >&3 "expecting no stderr from previous command"
608         if [ ! -s "$stderr" ]; then
609                 rm "$stderr"
610                 test_ok_ "$descr"
611         else
612                 if [ "$verbose" = t ]; then
613                         output=`echo; echo Stderr is:; cat "$stderr"`
614                 else
615                         output=
616                 fi
617                 # rm first in case test_failure exits.
618                 rm "$stderr"
619                 test_failure_ "$descr" "$@" "$output"
620         fi
621 }
622
623 # This is not among top-level (test_expect_success | test_expect_failure)
624 # but is a prefix that can be used in the test script, like:
625 #
626 #       test_expect_success 'complain and die' '
627 #           do something &&
628 #           do something else &&
629 #           test_must_fail git checkout ../outerspace
630 #       '
631 #
632 # Writing this as "! git checkout ../outerspace" is wrong, because
633 # the failure could be due to a segv.  We want a controlled failure.
634
635 test_must_fail () {
636         "$@"
637         test $? -gt 0 -a $? -le 129 -o $? -gt 192
638 }
639
640 # test_cmp is a helper function to compare actual and expected output.
641 # You can use it like:
642 #
643 #       test_expect_success 'foo works' '
644 #               echo expected >expected &&
645 #               foo >actual &&
646 #               test_cmp expected actual
647 #       '
648 #
649 # This could be written as either "cmp" or "diff -u", but:
650 # - cmp's output is not nearly as easy to read as diff -u
651 # - not all diff versions understand "-u"
652
653 test_cmp() {
654         $GIT_TEST_CMP "$@"
655 }
656
657 # This function can be used to schedule some commands to be run
658 # unconditionally at the end of the test to restore sanity:
659 #
660 #       test_expect_success 'test core.capslock' '
661 #               git config core.capslock true &&
662 #               test_when_finished "git config --unset core.capslock" &&
663 #               hello world
664 #       '
665 #
666 # That would be roughly equivalent to
667 #
668 #       test_expect_success 'test core.capslock' '
669 #               git config core.capslock true &&
670 #               hello world
671 #               git config --unset core.capslock
672 #       '
673 #
674 # except that the greeting and config --unset must both succeed for
675 # the test to pass.
676
677 test_when_finished () {
678         test_cleanup="{ $*
679                 } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
680 }
681
682 test_done () {
683         GIT_EXIT_OK=t
684         test_results_dir="$TEST_DIRECTORY/test-results"
685         mkdir -p "$test_results_dir"
686         test_results_path="$test_results_dir/${0%.sh}-$$"
687
688         echo "total $test_count" >> $test_results_path
689         echo "success $test_success" >> $test_results_path
690         echo "fixed $test_fixed" >> $test_results_path
691         echo "broken $test_broken" >> $test_results_path
692         echo "failed $test_failure" >> $test_results_path
693         echo "" >> $test_results_path
694
695         echo
696
697         if [ "$test_failure" = "0" ]; then
698             rm -rf "$remove_tmp"
699             exit 0
700         else
701             exit 1
702         fi
703 }
704
705 find_notmuch_path ()
706 {
707     dir="$1"
708
709     while [ -n "$dir" ]; do
710         bin="$dir/notmuch"
711         if [ -x "$bin" ]; then
712             echo "$dir"
713             return
714         fi
715         dir="$(dirname "$dir")"
716         if [ "$dir" = "/" ]; then
717             break
718         fi
719     done
720 }
721
722 # Test the binaries we have just built.  The tests are kept in
723 # test/ subdirectory and are run in 'trash directory' subdirectory.
724 TEST_DIRECTORY=$(pwd)
725 # FIXME: Only the normal case bellow is updated to notmuch
726 if test -n "$valgrind"
727 then
728         make_symlink () {
729                 test -h "$2" &&
730                 test "$1" = "$(readlink "$2")" || {
731                         # be super paranoid
732                         if mkdir "$2".lock
733                         then
734                                 rm -f "$2" &&
735                                 ln -s "$1" "$2" &&
736                                 rm -r "$2".lock
737                         else
738                                 while test -d "$2".lock
739                                 do
740                                         say "Waiting for lock on $2."
741                                         sleep 1
742                                 done
743                         fi
744                 }
745         }
746
747         make_valgrind_symlink () {
748                 # handle only executables
749                 test -x "$1" || return
750
751                 base=$(basename "$1")
752                 symlink_target=$TEST_DIRECTORY/../$base
753                 # do not override scripts
754                 if test -x "$symlink_target" &&
755                     test ! -d "$symlink_target" &&
756                     test "#!" != "$(head -c 2 < "$symlink_target")"
757                 then
758                         symlink_target=../valgrind.sh
759                 fi
760                 case "$base" in
761                 *.sh|*.perl)
762                         symlink_target=../unprocessed-script
763                 esac
764                 # create the link, or replace it if it is out of date
765                 make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
766         }
767
768         # override all git executables in TEST_DIRECTORY/..
769         GIT_VALGRIND=$TEST_DIRECTORY/valgrind
770         mkdir -p "$GIT_VALGRIND"/bin
771         for file in $TEST_DIRECTORY/../git* $TEST_DIRECTORY/../test-*
772         do
773                 make_valgrind_symlink $file
774         done
775         OLDIFS=$IFS
776         IFS=:
777         for path in $PATH
778         do
779                 ls "$path"/git-* 2> /dev/null |
780                 while read file
781                 do
782                         make_valgrind_symlink "$file"
783                 done
784         done
785         IFS=$OLDIFS
786         PATH=$GIT_VALGRIND/bin:$PATH
787         GIT_EXEC_PATH=$GIT_VALGRIND/bin
788         export GIT_VALGRIND
789 elif test -n "$GIT_TEST_INSTALLED" ; then
790         GIT_EXEC_PATH=$($GIT_TEST_INSTALLED/git --exec-path)  ||
791         error "Cannot run git from $GIT_TEST_INSTALLED."
792         PATH=$GIT_TEST_INSTALLED:$TEST_DIRECTORY/..:$PATH
793         GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH}
794 else # normal case
795         notmuch_path=`find_notmuch_path "$TEST_DIRECTORY"`
796         test -n "$notmuch_path" && PATH="$notmuch_path:$PATH"
797 fi
798 export PATH
799
800 # Test repository
801 test="tmp.$(basename "$0" .sh)"
802 test -n "$root" && test="$root/$test"
803 case "$test" in
804 /*) TMP_DIRECTORY="$test" ;;
805  *) TMP_DIRECTORY="$TEST_DIRECTORY/$test" ;;
806 esac
807 test ! -z "$debug" || remove_tmp=$TMP_DIRECTORY
808 rm -fr "$test" || {
809         GIT_EXIT_OK=t
810         echo >&5 "FATAL: Cannot prepare test area"
811         exit 1
812 }
813
814 MAIL_DIR="${TMP_DIRECTORY}/mail"
815 export NOTMUCH_CONFIG="${TMP_DIRECTORY}/notmuch-config"
816
817 mkdir -p "${test}"
818 mkdir "$MAIL_DIR"
819
820 cat <<EOF >"${NOTMUCH_CONFIG}"
821 [database]
822 path=${MAIL_DIR}
823
824 [user]
825 name=Notmuch Test Suite
826 primary_email=test_suite@notmuchmail.org
827 other_email=test_suite_other@notmuchmail.org;test_suite@otherdomain.org
828 EOF
829
830
831 # Use -P to resolve symlinks in our working directory so that the cwd
832 # in subprocesses like git equals our $PWD (for pathname comparisons).
833 cd -P "$test" || error "Cannot setup test environment"
834
835 this_test=${0##*/}
836 this_test=${this_test%%-*}
837 for skp in $GIT_SKIP_TESTS
838 do
839         to_skip=
840         for skp in $GIT_SKIP_TESTS
841         do
842                 case "$this_test" in
843                 $skp)
844                         to_skip=t
845                 esac
846         done
847         case "$to_skip" in
848         t)
849                 say_color skip >&3 "skipping test $this_test altogether"
850                 say_color skip "skip all tests in $this_test"
851                 test_done
852         esac
853 done
854
855 # Provide an implementation of the 'yes' utility
856 yes () {
857         if test $# = 0
858         then
859                 y=y
860         else
861                 y="$*"
862         fi
863
864         while echo "$y"
865         do
866                 :
867         done
868 }
869
870 # Fix some commands on Windows
871 case $(uname -s) in
872 *MINGW*)
873         # Windows has its own (incompatible) sort and find
874         sort () {
875                 /usr/bin/sort "$@"
876         }
877         find () {
878                 /usr/bin/find "$@"
879         }
880         sum () {
881                 md5sum "$@"
882         }
883         # git sees Windows-style pwd
884         pwd () {
885                 builtin pwd -W
886         }
887         # no POSIX permissions
888         # backslashes in pathspec are converted to '/'
889         # exec does not inherit the PID
890         ;;
891 *)
892         test_set_prereq POSIXPERM
893         test_set_prereq BSLASHPSPEC
894         test_set_prereq EXECKEEPSPID
895         ;;
896 esac
897
898 test -z "$NO_PERL" && test_set_prereq PERL
899 test -z "$NO_PYTHON" && test_set_prereq PYTHON
900
901 # test whether the filesystem supports symbolic links
902 ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS
903 rm -f y