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