Merge branch 'rw/apply-does-not-take-ignore-date' into maint
[git] / t / test-lib.sh
1 # Test framework for git.  See t/README for usage.
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 # Keep the original TERM for say_color
19 ORIGINAL_TERM=$TERM
20
21 # Test the binaries we have just built.  The tests are kept in
22 # t/ subdirectory and are run in 'trash directory' subdirectory.
23 if test -z "$TEST_DIRECTORY"
24 then
25         # We allow tests to override this, in case they want to run tests
26         # outside of t/, e.g. for running tests on the test library
27         # itself.
28         TEST_DIRECTORY=$(pwd)
29 else
30         # ensure that TEST_DIRECTORY is an absolute path so that it
31         # is valid even if the current working directory is changed
32         TEST_DIRECTORY=$(cd "$TEST_DIRECTORY" && pwd) || exit 1
33 fi
34 if test -z "$TEST_OUTPUT_DIRECTORY"
35 then
36         # Similarly, override this to store the test-results subdir
37         # elsewhere
38         TEST_OUTPUT_DIRECTORY=$TEST_DIRECTORY
39 fi
40 GIT_BUILD_DIR="$TEST_DIRECTORY"/..
41
42 ################################################################
43 # It appears that people try to run tests without building...
44 "$GIT_BUILD_DIR/git" >/dev/null
45 if test $? != 1
46 then
47         echo >&2 'error: you do not seem to have built git yet.'
48         exit 1
49 fi
50
51 . "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS
52 export PERL_PATH SHELL_PATH
53
54 # if --tee was passed, write the output not only to the terminal, but
55 # additionally to the file test-results/$BASENAME.out, too.
56 case "$GIT_TEST_TEE_STARTED, $* " in
57 done,*)
58         # do not redirect again
59         ;;
60 *' --tee '*|*' --va'*)
61         mkdir -p "$TEST_OUTPUT_DIRECTORY/test-results"
62         BASE="$TEST_OUTPUT_DIRECTORY/test-results/$(basename "$0" .sh)"
63         (GIT_TEST_TEE_STARTED=done ${SHELL_PATH} "$0" "$@" 2>&1;
64          echo $? > $BASE.exit) | tee $BASE.out
65         test "$(cat $BASE.exit)" = 0
66         exit
67         ;;
68 esac
69
70 # For repeatability, reset the environment to known value.
71 LANG=C
72 LC_ALL=C
73 PAGER=cat
74 TZ=UTC
75 TERM=dumb
76 export LANG LC_ALL PAGER TERM TZ
77 EDITOR=:
78 # A call to "unset" with no arguments causes at least Solaris 10
79 # /usr/xpg4/bin/sh and /bin/ksh to bail out.  So keep the unsets
80 # deriving from the command substitution clustered with the other
81 # ones.
82 unset VISUAL EMAIL LANGUAGE COLUMNS $("$PERL_PATH" -e '
83         my @env = keys %ENV;
84         my $ok = join("|", qw(
85                 TRACE
86                 DEBUG
87                 USE_LOOKUP
88                 TEST
89                 .*_TEST
90                 PROVE
91                 VALGRIND
92                 UNZIP
93                 PERF_
94                 CURL_VERBOSE
95         ));
96         my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
97         print join("\n", @vars);
98 ')
99 unset XDG_CONFIG_HOME
100 unset GITPERLLIB
101 GIT_AUTHOR_EMAIL=author@example.com
102 GIT_AUTHOR_NAME='A U Thor'
103 GIT_COMMITTER_EMAIL=committer@example.com
104 GIT_COMMITTER_NAME='C O Mitter'
105 GIT_MERGE_VERBOSITY=5
106 GIT_MERGE_AUTOEDIT=no
107 export GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT
108 export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
109 export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
110 export EDITOR
111
112 # Tests using GIT_TRACE typically don't want <timestamp> <file>:<line> output
113 GIT_TRACE_BARE=1
114 export GIT_TRACE_BARE
115
116 if test -n "${TEST_GIT_INDEX_VERSION:+isset}"
117 then
118         GIT_INDEX_VERSION="$TEST_GIT_INDEX_VERSION"
119         export GIT_INDEX_VERSION
120 fi
121
122 # Add libc MALLOC and MALLOC_PERTURB test
123 # only if we are not executing the test with valgrind
124 if expr " $GIT_TEST_OPTS " : ".* --valgrind " >/dev/null ||
125    test -n "$TEST_NO_MALLOC_CHECK"
126 then
127         setup_malloc_check () {
128                 : nothing
129         }
130         teardown_malloc_check () {
131                 : nothing
132         }
133 else
134         setup_malloc_check () {
135                 MALLOC_CHECK_=3 MALLOC_PERTURB_=165
136                 export MALLOC_CHECK_ MALLOC_PERTURB_
137         }
138         teardown_malloc_check () {
139                 unset MALLOC_CHECK_ MALLOC_PERTURB_
140         }
141 fi
142
143 # Protect ourselves from common misconfiguration to export
144 # CDPATH into the environment
145 unset CDPATH
146
147 unset GREP_OPTIONS
148 unset UNZIP
149
150 case $(echo $GIT_TRACE |tr "[A-Z]" "[a-z]") in
151 1|2|true)
152         echo "* warning: Some tests will not work if GIT_TRACE" \
153                 "is set as to trace on STDERR ! *"
154         echo "* warning: Please set GIT_TRACE to something" \
155                 "other than 1, 2 or true ! *"
156         ;;
157 esac
158
159 # Convenience
160 #
161 # A regexp to match 5 and 40 hexdigits
162 _x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
163 _x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
164
165 # Zero SHA-1
166 _z40=0000000000000000000000000000000000000000
167
168 # Line feed
169 LF='
170 '
171
172 # UTF-8 ZERO WIDTH NON-JOINER, which HFS+ ignores
173 # when case-folding filenames
174 u200c=$(printf '\342\200\214')
175
176 export _x05 _x40 _z40 LF u200c
177
178 # Each test should start with something like this, after copyright notices:
179 #
180 # test_description='Description of this test...
181 # This test checks if command xyzzy does the right thing...
182 # '
183 # . ./test-lib.sh
184 [ "x$ORIGINAL_TERM" != "xdumb" ] && (
185                 TERM=$ORIGINAL_TERM &&
186                 export TERM &&
187                 [ -t 1 ] &&
188                 tput bold >/dev/null 2>&1 &&
189                 tput setaf 1 >/dev/null 2>&1 &&
190                 tput sgr0 >/dev/null 2>&1
191         ) &&
192         color=t
193
194 while test "$#" -ne 0
195 do
196         case "$1" in
197         -d|--d|--de|--deb|--debu|--debug)
198                 debug=t; shift ;;
199         -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
200                 immediate=t; shift ;;
201         -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
202                 GIT_TEST_LONG=t; export GIT_TEST_LONG; shift ;;
203         -r)
204                 shift; test "$#" -ne 0 || {
205                         echo 'error: -r requires an argument' >&2;
206                         exit 1;
207                 }
208                 run_list=$1; shift ;;
209         --run=*)
210                 run_list=$(expr "z$1" : 'z[^=]*=\(.*\)'); shift ;;
211         -h|--h|--he|--hel|--help)
212                 help=t; shift ;;
213         -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
214                 verbose=t; shift ;;
215         --verbose-only=*)
216                 verbose_only=$(expr "z$1" : 'z[^=]*=\(.*\)')
217                 shift ;;
218         -q|--q|--qu|--qui|--quie|--quiet)
219                 # Ignore --quiet under a TAP::Harness. Saying how many tests
220                 # passed without the ok/not ok details is always an error.
221                 test -z "$HARNESS_ACTIVE" && quiet=t; shift ;;
222         --with-dashes)
223                 with_dashes=t; shift ;;
224         --no-color)
225                 color=; shift ;;
226         --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
227                 valgrind=memcheck
228                 shift ;;
229         --valgrind=*)
230                 valgrind=$(expr "z$1" : 'z[^=]*=\(.*\)')
231                 shift ;;
232         --valgrind-only=*)
233                 valgrind_only=$(expr "z$1" : 'z[^=]*=\(.*\)')
234                 shift ;;
235         --tee)
236                 shift ;; # was handled already
237         --root=*)
238                 root=$(expr "z$1" : 'z[^=]*=\(.*\)')
239                 shift ;;
240         -x)
241                 trace=t
242                 verbose=t
243                 shift ;;
244         *)
245                 echo "error: unknown test option '$1'" >&2; exit 1 ;;
246         esac
247 done
248
249 if test -n "$valgrind_only"
250 then
251         test -z "$valgrind" && valgrind=memcheck
252         test -z "$verbose" && verbose_only="$valgrind_only"
253 elif test -n "$valgrind"
254 then
255         verbose=t
256 fi
257
258 if test -n "$color"
259 then
260         say_color () {
261                 (
262                 TERM=$ORIGINAL_TERM
263                 export TERM
264                 case "$1" in
265                 error)
266                         tput bold; tput setaf 1;; # bold red
267                 skip)
268                         tput setaf 4;; # blue
269                 warn)
270                         tput setaf 3;; # brown/yellow
271                 pass)
272                         tput setaf 2;; # green
273                 info)
274                         tput setaf 6;; # cyan
275                 *)
276                         test -n "$quiet" && return;;
277                 esac
278                 shift
279                 printf "%s" "$*"
280                 tput sgr0
281                 echo
282                 )
283         }
284 else
285         say_color() {
286                 test -z "$1" && test -n "$quiet" && return
287                 shift
288                 printf "%s\n" "$*"
289         }
290 fi
291
292 error () {
293         say_color error "error: $*"
294         GIT_EXIT_OK=t
295         exit 1
296 }
297
298 say () {
299         say_color info "$*"
300 }
301
302 test "${test_description}" != "" ||
303 error "Test script did not set test_description."
304
305 if test "$help" = "t"
306 then
307         printf '%s\n' "$test_description"
308         exit 0
309 fi
310
311 exec 5>&1
312 exec 6<&0
313 if test "$verbose" = "t"
314 then
315         exec 4>&2 3>&1
316 else
317         exec 4>/dev/null 3>/dev/null
318 fi
319
320 test_failure=0
321 test_count=0
322 test_fixed=0
323 test_broken=0
324 test_success=0
325
326 test_external_has_tap=0
327
328 die () {
329         code=$?
330         if test -n "$GIT_EXIT_OK"
331         then
332                 exit $code
333         else
334                 echo >&5 "FATAL: Unexpected exit with code $code"
335                 exit 1
336         fi
337 }
338
339 GIT_EXIT_OK=
340 trap 'die' EXIT
341
342 # The user-facing functions are loaded from a separate file so that
343 # test_perf subshells can have them too
344 . "$TEST_DIRECTORY/test-lib-functions.sh"
345
346 # You are not expected to call test_ok_ and test_failure_ directly, use
347 # the test_expect_* functions instead.
348
349 test_ok_ () {
350         test_success=$(($test_success + 1))
351         say_color "" "ok $test_count - $@"
352 }
353
354 test_failure_ () {
355         test_failure=$(($test_failure + 1))
356         say_color error "not ok $test_count - $1"
357         shift
358         printf '%s\n' "$*" | sed -e 's/^/#      /'
359         test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
360 }
361
362 test_known_broken_ok_ () {
363         test_fixed=$(($test_fixed+1))
364         say_color error "ok $test_count - $@ # TODO known breakage vanished"
365 }
366
367 test_known_broken_failure_ () {
368         test_broken=$(($test_broken+1))
369         say_color warn "not ok $test_count - $@ # TODO known breakage"
370 }
371
372 test_debug () {
373         test "$debug" = "" || eval "$1"
374 }
375
376 match_pattern_list () {
377         arg="$1"
378         shift
379         test -z "$*" && return 1
380         for pattern_
381         do
382                 case "$arg" in
383                 $pattern_)
384                         return 0
385                 esac
386         done
387         return 1
388 }
389
390 match_test_selector_list () {
391         title="$1"
392         shift
393         arg="$1"
394         shift
395         test -z "$1" && return 0
396
397         # Both commas and whitespace are accepted as separators.
398         OLDIFS=$IFS
399         IFS='   ,'
400         set -- $1
401         IFS=$OLDIFS
402
403         # If the first selector is negative we include by default.
404         include=
405         case "$1" in
406                 !*) include=t ;;
407         esac
408
409         for selector
410         do
411                 orig_selector=$selector
412
413                 positive=t
414                 case "$selector" in
415                         !*)
416                                 positive=
417                                 selector=${selector##?}
418                                 ;;
419                 esac
420
421                 test -z "$selector" && continue
422
423                 case "$selector" in
424                         *-*)
425                                 if expr "z${selector%%-*}" : "z[0-9]*[^0-9]" >/dev/null
426                                 then
427                                         echo "error: $title: invalid non-numeric in range" \
428                                                 "start: '$orig_selector'" >&2
429                                         exit 1
430                                 fi
431                                 if expr "z${selector#*-}" : "z[0-9]*[^0-9]" >/dev/null
432                                 then
433                                         echo "error: $title: invalid non-numeric in range" \
434                                                 "end: '$orig_selector'" >&2
435                                         exit 1
436                                 fi
437                                 ;;
438                         *)
439                                 if expr "z$selector" : "z[0-9]*[^0-9]" >/dev/null
440                                 then
441                                         echo "error: $title: invalid non-numeric in test" \
442                                                 "selector: '$orig_selector'" >&2
443                                         exit 1
444                                 fi
445                 esac
446
447                 # Short cut for "obvious" cases
448                 test -z "$include" && test -z "$positive" && continue
449                 test -n "$include" && test -n "$positive" && continue
450
451                 case "$selector" in
452                         -*)
453                                 if test $arg -le ${selector#-}
454                                 then
455                                         include=$positive
456                                 fi
457                                 ;;
458                         *-)
459                                 if test $arg -ge ${selector%-}
460                                 then
461                                         include=$positive
462                                 fi
463                                 ;;
464                         *-*)
465                                 if test ${selector%%-*} -le $arg \
466                                         && test $arg -le ${selector#*-}
467                                 then
468                                         include=$positive
469                                 fi
470                                 ;;
471                         *)
472                                 if test $arg -eq $selector
473                                 then
474                                         include=$positive
475                                 fi
476                                 ;;
477                 esac
478         done
479
480         test -n "$include"
481 }
482
483 maybe_teardown_verbose () {
484         test -z "$verbose_only" && return
485         exec 4>/dev/null 3>/dev/null
486         verbose=
487 }
488
489 last_verbose=t
490 maybe_setup_verbose () {
491         test -z "$verbose_only" && return
492         if match_pattern_list $test_count $verbose_only
493         then
494                 exec 4>&2 3>&1
495                 # Emit a delimiting blank line when going from
496                 # non-verbose to verbose.  Within verbose mode the
497                 # delimiter is printed by test_expect_*.  The choice
498                 # of the initial $last_verbose is such that before
499                 # test 1, we do not print it.
500                 test -z "$last_verbose" && echo >&3 ""
501                 verbose=t
502         else
503                 exec 4>/dev/null 3>/dev/null
504                 verbose=
505         fi
506         last_verbose=$verbose
507 }
508
509 maybe_teardown_valgrind () {
510         test -z "$GIT_VALGRIND" && return
511         GIT_VALGRIND_ENABLED=
512 }
513
514 maybe_setup_valgrind () {
515         test -z "$GIT_VALGRIND" && return
516         if test -z "$valgrind_only"
517         then
518                 GIT_VALGRIND_ENABLED=t
519                 return
520         fi
521         GIT_VALGRIND_ENABLED=
522         if match_pattern_list $test_count $valgrind_only
523         then
524                 GIT_VALGRIND_ENABLED=t
525         fi
526 }
527
528 # This is a separate function because some tests use
529 # "return" to end a test_expect_success block early
530 # (and we want to make sure we run any cleanup like
531 # "set +x").
532 test_eval_inner_ () {
533         # Do not add anything extra (including LF) after '$*'
534         eval "
535                 test \"$trace\" = t && set -x
536                 $*"
537 }
538
539 test_eval_ () {
540         # We run this block with stderr redirected to avoid extra cruft
541         # during a "-x" trace. Once in "set -x" mode, we cannot prevent
542         # the shell from printing the "set +x" to turn it off (nor the saving
543         # of $? before that). But we can make sure that the output goes to
544         # /dev/null.
545         #
546         # The test itself is run with stderr put back to &4 (so either to
547         # /dev/null, or to the original stderr if --verbose was used).
548         {
549                 test_eval_inner_ "$@" </dev/null >&3 2>&4
550                 test_eval_ret_=$?
551                 if test "$trace" = t
552                 then
553                         set +x
554                         if test "$test_eval_ret_" != 0
555                         then
556                                 say_color error >&4 "error: last command exited with \$?=$test_eval_ret_"
557                         fi
558                 fi
559         } 2>/dev/null
560         return $test_eval_ret_
561 }
562
563 test_run_ () {
564         test_cleanup=:
565         expecting_failure=$2
566         setup_malloc_check
567         test_eval_ "$1"
568         eval_ret=$?
569         teardown_malloc_check
570
571         if test -z "$immediate" || test $eval_ret = 0 ||
572            test -n "$expecting_failure" && test "$test_cleanup" != ":"
573         then
574                 setup_malloc_check
575                 test_eval_ "$test_cleanup"
576                 teardown_malloc_check
577         fi
578         if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"
579         then
580                 echo ""
581         fi
582         return "$eval_ret"
583 }
584
585 test_start_ () {
586         test_count=$(($test_count+1))
587         maybe_setup_verbose
588         maybe_setup_valgrind
589 }
590
591 test_finish_ () {
592         echo >&3 ""
593         maybe_teardown_valgrind
594         maybe_teardown_verbose
595 }
596
597 test_skip () {
598         to_skip=
599         skipped_reason=
600         if match_pattern_list $this_test.$test_count $GIT_SKIP_TESTS
601         then
602                 to_skip=t
603                 skipped_reason="GIT_SKIP_TESTS"
604         fi
605         if test -z "$to_skip" && test -n "$test_prereq" &&
606            ! test_have_prereq "$test_prereq"
607         then
608                 to_skip=t
609
610                 of_prereq=
611                 if test "$missing_prereq" != "$test_prereq"
612                 then
613                         of_prereq=" of $test_prereq"
614                 fi
615                 skipped_reason="missing $missing_prereq${of_prereq}"
616         fi
617         if test -z "$to_skip" && test -n "$run_list" &&
618                 ! match_test_selector_list '--run' $test_count "$run_list"
619         then
620                 to_skip=t
621                 skipped_reason="--run"
622         fi
623
624         case "$to_skip" in
625         t)
626                 say_color skip >&3 "skipping test: $@"
627                 say_color skip "ok $test_count # skip $1 ($skipped_reason)"
628                 : true
629                 ;;
630         *)
631                 false
632                 ;;
633         esac
634 }
635
636 # stub; perf-lib overrides it
637 test_at_end_hook_ () {
638         :
639 }
640
641 test_done () {
642         GIT_EXIT_OK=t
643
644         if test -z "$HARNESS_ACTIVE"
645         then
646                 test_results_dir="$TEST_OUTPUT_DIRECTORY/test-results"
647                 mkdir -p "$test_results_dir"
648                 base=${0##*/}
649                 test_results_path="$test_results_dir/${base%.sh}-$$.counts"
650
651                 cat >>"$test_results_path" <<-EOF
652                 total $test_count
653                 success $test_success
654                 fixed $test_fixed
655                 broken $test_broken
656                 failed $test_failure
657
658                 EOF
659         fi
660
661         if test "$test_fixed" != 0
662         then
663                 say_color error "# $test_fixed known breakage(s) vanished; please update test(s)"
664         fi
665         if test "$test_broken" != 0
666         then
667                 say_color warn "# still have $test_broken known breakage(s)"
668         fi
669         if test "$test_broken" != 0 || test "$test_fixed" != 0
670         then
671                 test_remaining=$(( $test_count - $test_broken - $test_fixed ))
672                 msg="remaining $test_remaining test(s)"
673         else
674                 test_remaining=$test_count
675                 msg="$test_count test(s)"
676         fi
677         case "$test_failure" in
678         0)
679                 # Maybe print SKIP message
680                 if test -n "$skip_all" && test $test_count -gt 0
681                 then
682                         error "Can't use skip_all after running some tests"
683                 fi
684                 [ -z "$skip_all" ] || skip_all=" # SKIP $skip_all"
685
686                 if test $test_external_has_tap -eq 0
687                 then
688                         if test $test_remaining -gt 0
689                         then
690                                 say_color pass "# passed all $msg"
691                         fi
692                         say "1..$test_count$skip_all"
693                 fi
694
695                 test -d "$remove_trash" &&
696                 cd "$(dirname "$remove_trash")" &&
697                 rm -rf "$(basename "$remove_trash")"
698
699                 test_at_end_hook_
700
701                 exit 0 ;;
702
703         *)
704                 if test $test_external_has_tap -eq 0
705                 then
706                         say_color error "# failed $test_failure among $msg"
707                         say "1..$test_count"
708                 fi
709
710                 exit 1 ;;
711
712         esac
713 }
714
715 if test -n "$valgrind"
716 then
717         make_symlink () {
718                 test -h "$2" &&
719                 test "$1" = "$(readlink "$2")" || {
720                         # be super paranoid
721                         if mkdir "$2".lock
722                         then
723                                 rm -f "$2" &&
724                                 ln -s "$1" "$2" &&
725                                 rm -r "$2".lock
726                         else
727                                 while test -d "$2".lock
728                                 do
729                                         say "Waiting for lock on $2."
730                                         sleep 1
731                                 done
732                         fi
733                 }
734         }
735
736         make_valgrind_symlink () {
737                 # handle only executables, unless they are shell libraries that
738                 # need to be in the exec-path.
739                 test -x "$1" ||
740                 test "# " = "$(head -c 2 <"$1")" ||
741                 return;
742
743                 base=$(basename "$1")
744                 symlink_target=$GIT_BUILD_DIR/$base
745                 # do not override scripts
746                 if test -x "$symlink_target" &&
747                     test ! -d "$symlink_target" &&
748                     test "#!" != "$(head -c 2 < "$symlink_target")"
749                 then
750                         symlink_target=../valgrind.sh
751                 fi
752                 case "$base" in
753                 *.sh|*.perl)
754                         symlink_target=../unprocessed-script
755                 esac
756                 # create the link, or replace it if it is out of date
757                 make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
758         }
759
760         # override all git executables in TEST_DIRECTORY/..
761         GIT_VALGRIND=$TEST_DIRECTORY/valgrind
762         mkdir -p "$GIT_VALGRIND"/bin
763         for file in $GIT_BUILD_DIR/git* $GIT_BUILD_DIR/test-*
764         do
765                 make_valgrind_symlink $file
766         done
767         # special-case the mergetools loadables
768         make_symlink "$GIT_BUILD_DIR"/mergetools "$GIT_VALGRIND/bin/mergetools"
769         OLDIFS=$IFS
770         IFS=:
771         for path in $PATH
772         do
773                 ls "$path"/git-* 2> /dev/null |
774                 while read file
775                 do
776                         make_valgrind_symlink "$file"
777                 done
778         done
779         IFS=$OLDIFS
780         PATH=$GIT_VALGRIND/bin:$PATH
781         GIT_EXEC_PATH=$GIT_VALGRIND/bin
782         export GIT_VALGRIND
783         GIT_VALGRIND_MODE="$valgrind"
784         export GIT_VALGRIND_MODE
785         GIT_VALGRIND_ENABLED=t
786         test -n "$valgrind_only" && GIT_VALGRIND_ENABLED=
787         export GIT_VALGRIND_ENABLED
788 elif test -n "$GIT_TEST_INSTALLED"
789 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:$GIT_BUILD_DIR:$PATH
793         GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH}
794 else # normal case, use ../bin-wrappers only unless $with_dashes:
795         git_bin_dir="$GIT_BUILD_DIR/bin-wrappers"
796         if ! test -x "$git_bin_dir/git"
797         then
798                 if test -z "$with_dashes"
799                 then
800                         say "$git_bin_dir/git is not executable; using GIT_EXEC_PATH"
801                 fi
802                 with_dashes=t
803         fi
804         PATH="$git_bin_dir:$PATH"
805         GIT_EXEC_PATH=$GIT_BUILD_DIR
806         if test -n "$with_dashes"
807         then
808                 PATH="$GIT_BUILD_DIR:$PATH"
809         fi
810 fi
811 GIT_TEMPLATE_DIR="$GIT_BUILD_DIR"/templates/blt
812 GIT_CONFIG_NOSYSTEM=1
813 GIT_ATTR_NOSYSTEM=1
814 export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM
815
816 if test -z "$GIT_TEST_CMP"
817 then
818         if test -n "$GIT_TEST_CMP_USE_COPIED_CONTEXT"
819         then
820                 GIT_TEST_CMP="$DIFF -c"
821         else
822                 GIT_TEST_CMP="$DIFF -u"
823         fi
824 fi
825
826 GITPERLLIB="$GIT_BUILD_DIR"/perl/blib/lib:"$GIT_BUILD_DIR"/perl/blib/arch/auto/Git
827 export GITPERLLIB
828 test -d "$GIT_BUILD_DIR"/templates/blt || {
829         error "You haven't built things yet, have you?"
830 }
831
832 if ! test -x "$GIT_BUILD_DIR"/test-chmtime
833 then
834         echo >&2 'You need to build test-chmtime:'
835         echo >&2 'Run "make test-chmtime" in the source (toplevel) directory'
836         exit 1
837 fi
838
839 # Test repository
840 TRASH_DIRECTORY="trash directory.$(basename "$0" .sh)"
841 test -n "$root" && TRASH_DIRECTORY="$root/$TRASH_DIRECTORY"
842 case "$TRASH_DIRECTORY" in
843 /*) ;; # absolute path is good
844  *) TRASH_DIRECTORY="$TEST_OUTPUT_DIRECTORY/$TRASH_DIRECTORY" ;;
845 esac
846 test ! -z "$debug" || remove_trash=$TRASH_DIRECTORY
847 rm -fr "$TRASH_DIRECTORY" || {
848         GIT_EXIT_OK=t
849         echo >&5 "FATAL: Cannot prepare test area"
850         exit 1
851 }
852
853 HOME="$TRASH_DIRECTORY"
854 GNUPGHOME="$HOME/gnupg-home-not-used"
855 export HOME GNUPGHOME
856
857 if test -z "$TEST_NO_CREATE_REPO"
858 then
859         test_create_repo "$TRASH_DIRECTORY"
860 else
861         mkdir -p "$TRASH_DIRECTORY"
862 fi
863 # Use -P to resolve symlinks in our working directory so that the cwd
864 # in subprocesses like git equals our $PWD (for pathname comparisons).
865 cd -P "$TRASH_DIRECTORY" || exit 1
866
867 this_test=${0##*/}
868 this_test=${this_test%%-*}
869 if match_pattern_list "$this_test" $GIT_SKIP_TESTS
870 then
871         say_color info >&3 "skipping test $this_test altogether"
872         skip_all="skip all tests in $this_test"
873         test_done
874 fi
875
876 # Provide an implementation of the 'yes' utility
877 yes () {
878         if test $# = 0
879         then
880                 y=y
881         else
882                 y="$*"
883         fi
884
885         while echo "$y"
886         do
887                 :
888         done
889 }
890
891 # Fix some commands on Windows
892 case $(uname -s) in
893 *MINGW*)
894         # Windows has its own (incompatible) sort and find
895         sort () {
896                 /usr/bin/sort "$@"
897         }
898         find () {
899                 /usr/bin/find "$@"
900         }
901         sum () {
902                 md5sum "$@"
903         }
904         # git sees Windows-style pwd
905         pwd () {
906                 builtin pwd -W
907         }
908         # no POSIX permissions
909         # backslashes in pathspec are converted to '/'
910         # exec does not inherit the PID
911         test_set_prereq MINGW
912         test_set_prereq NATIVE_CRLF
913         test_set_prereq SED_STRIPS_CR
914         test_set_prereq GREP_STRIPS_CR
915         GIT_TEST_CMP=mingw_test_cmp
916         ;;
917 *CYGWIN*)
918         test_set_prereq POSIXPERM
919         test_set_prereq EXECKEEPSPID
920         test_set_prereq CYGWIN
921         test_set_prereq SED_STRIPS_CR
922         test_set_prereq GREP_STRIPS_CR
923         ;;
924 *)
925         test_set_prereq POSIXPERM
926         test_set_prereq BSLASHPSPEC
927         test_set_prereq EXECKEEPSPID
928         ;;
929 esac
930
931 ( COLUMNS=1 && test $COLUMNS = 1 ) && test_set_prereq COLUMNS_CAN_BE_1
932 test -z "$NO_PERL" && test_set_prereq PERL
933 test -z "$NO_PYTHON" && test_set_prereq PYTHON
934 test -n "$USE_LIBPCRE" && test_set_prereq LIBPCRE
935 test -z "$NO_GETTEXT" && test_set_prereq GETTEXT
936
937 # Can we rely on git's output in the C locale?
938 if test -n "$GETTEXT_POISON"
939 then
940         GIT_GETTEXT_POISON=YesPlease
941         export GIT_GETTEXT_POISON
942         test_set_prereq GETTEXT_POISON
943 else
944         test_set_prereq C_LOCALE_OUTPUT
945 fi
946
947 # Use this instead of test_cmp to compare files that contain expected and
948 # actual output from git commands that can be translated.  When running
949 # under GETTEXT_POISON this pretends that the command produced expected
950 # results.
951 test_i18ncmp () {
952         test -n "$GETTEXT_POISON" || test_cmp "$@"
953 }
954
955 # Use this instead of "grep expected-string actual" to see if the
956 # output from a git command that can be translated either contains an
957 # expected string, or does not contain an unwanted one.  When running
958 # under GETTEXT_POISON this pretends that the command produced expected
959 # results.
960 test_i18ngrep () {
961         if test -n "$GETTEXT_POISON"
962         then
963             : # pretend success
964         elif test "x!" = "x$1"
965         then
966                 shift
967                 ! grep "$@"
968         else
969                 grep "$@"
970         fi
971 }
972
973 test_lazy_prereq PIPE '
974         # test whether the filesystem supports FIFOs
975         case $(uname -s) in
976         CYGWIN*)
977                 false
978                 ;;
979         *)
980                 rm -f testfifo && mkfifo testfifo
981                 ;;
982         esac
983 '
984
985 test_lazy_prereq SYMLINKS '
986         # test whether the filesystem supports symbolic links
987         ln -s x y && test -h y
988 '
989
990 test_lazy_prereq FILEMODE '
991         test "$(git config --bool core.filemode)" = true
992 '
993
994 test_lazy_prereq CASE_INSENSITIVE_FS '
995         echo good >CamelCase &&
996         echo bad >camelcase &&
997         test "$(cat CamelCase)" != good
998 '
999
1000 test_lazy_prereq UTF8_NFD_TO_NFC '
1001         # check whether FS converts nfd unicode to nfc
1002         auml=$(printf "\303\244")
1003         aumlcdiar=$(printf "\141\314\210")
1004         >"$auml" &&
1005         case "$(echo *)" in
1006         "$aumlcdiar")
1007                 true ;;
1008         *)
1009                 false ;;
1010         esac
1011 '
1012
1013 test_lazy_prereq AUTOIDENT '
1014         sane_unset GIT_AUTHOR_NAME &&
1015         sane_unset GIT_AUTHOR_EMAIL &&
1016         git var GIT_AUTHOR_IDENT
1017 '
1018
1019 test_lazy_prereq EXPENSIVE '
1020         test -n "$GIT_TEST_LONG"
1021 '
1022
1023 test_lazy_prereq USR_BIN_TIME '
1024         test -x /usr/bin/time
1025 '
1026
1027 # When the tests are run as root, permission tests will report that
1028 # things are writable when they shouldn't be.
1029 test -w / || test_set_prereq SANITY
1030
1031 GIT_UNZIP=${GIT_UNZIP:-unzip}
1032 test_lazy_prereq UNZIP '
1033         "$GIT_UNZIP" -v
1034         test $? -ne 127
1035 '