work around misdetection of stdin attached to a tty
[git/dscho.git] / t / test-lib.sh
blobde0a5d5fd87c9ae5ba8aa28a48134488183eec7e
1 #!/bin/sh
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 # for git on windows so stdin will not be misdetected as attached to a
19 # terminal
20 exec < /dev/null
22 # if --tee was passed, write the output not only to the terminal, but
23 # additionally to the file test-results/$BASENAME.out, too.
24 case "$GIT_TEST_TEE_STARTED, $* " in
25 done,*)
26 # do not redirect again
28 *' --tee '*|*' --va'*)
29 mkdir -p test-results
30 BASE=test-results/$(basename "$0" .sh)
31 (GIT_TEST_TEE_STARTED=done ${SHELL-sh} "$0" "$@" 2>&1;
32 echo $? > $BASE.exit) | tee $BASE.out
33 test "$(cat $BASE.exit)" = 0
34 exit
36 esac
38 # Keep the original TERM for say_color
39 ORIGINAL_TERM=$TERM
41 # For repeatability, reset the environment to known value.
42 LANG=C
43 LC_ALL=C
44 PAGER=cat
45 TZ=UTC
46 TERM=dumb
47 export LANG LC_ALL PAGER TERM TZ
48 EDITOR=:
49 unset VISUAL
50 unset EMAIL
51 unset LANGUAGE
52 unset $(perl -e '
53 my @env = keys %ENV;
54 my $ok = join("|", qw(
55 TRACE
56 DEBUG
57 USE_LOOKUP
58 TEST
59 .*_TEST
60 PROVE
61 VALGRIND
62 ));
63 my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
64 print join("\n", @vars);
66 GIT_AUTHOR_EMAIL=author@example.com
67 GIT_AUTHOR_NAME='A U Thor'
68 GIT_COMMITTER_EMAIL=committer@example.com
69 GIT_COMMITTER_NAME='C O Mitter'
70 GIT_MERGE_VERBOSITY=5
71 export GIT_MERGE_VERBOSITY
72 export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
73 export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
74 export EDITOR
76 # Protect ourselves from common misconfiguration to export
77 # CDPATH into the environment
78 unset CDPATH
80 unset GREP_OPTIONS
82 case $(echo $GIT_TRACE |tr "[A-Z]" "[a-z]") in
83 1|2|true)
84 echo "* warning: Some tests will not work if GIT_TRACE" \
85 "is set as to trace on STDERR ! *"
86 echo "* warning: Please set GIT_TRACE to something" \
87 "other than 1, 2 or true ! *"
89 esac
91 # Convenience
93 # A regexp to match 5 and 40 hexdigits
94 _x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
95 _x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
97 # Zero SHA-1
98 _z40=0000000000000000000000000000000000000000
100 # Line feed
101 LF='
104 # Each test should start with something like this, after copyright notices:
106 # test_description='Description of this test...
107 # This test checks if command xyzzy does the right thing...
109 # . ./test-lib.sh
110 [ "x$ORIGINAL_TERM" != "xdumb" ] && (
111 TERM=$ORIGINAL_TERM &&
112 export TERM &&
113 [ -t 1 ] &&
114 tput bold >/dev/null 2>&1 &&
115 tput setaf 1 >/dev/null 2>&1 &&
116 tput sgr0 >/dev/null 2>&1
117 ) &&
118 color=t
120 while test "$#" -ne 0
122 case "$1" in
123 -d|--d|--de|--deb|--debu|--debug)
124 debug=t; shift ;;
125 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
126 immediate=t; shift ;;
127 -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
128 GIT_TEST_LONG=t; export GIT_TEST_LONG; shift ;;
129 -h|--h|--he|--hel|--help)
130 help=t; shift ;;
131 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
132 verbose=t; shift ;;
133 -q|--q|--qu|--qui|--quie|--quiet)
134 # Ignore --quiet under a TAP::Harness. Saying how many tests
135 # passed without the ok/not ok details is always an error.
136 test -z "$HARNESS_ACTIVE" && quiet=t; shift ;;
137 --with-dashes)
138 with_dashes=t; shift ;;
139 --no-color)
140 color=; shift ;;
141 --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
142 valgrind=t; verbose=t; shift ;;
143 --tee)
144 shift ;; # was handled already
145 --root=*)
146 root=$(expr "z$1" : 'z[^=]*=\(.*\)')
147 shift ;;
149 echo "error: unknown test option '$1'" >&2; exit 1 ;;
150 esac
151 done
153 if test -n "$color"; then
154 say_color () {
156 TERM=$ORIGINAL_TERM
157 export TERM
158 case "$1" in
159 error) tput bold; tput setaf 1;; # bold red
160 skip) tput bold; tput setaf 2;; # bold green
161 pass) tput setaf 2;; # green
162 info) tput setaf 3;; # brown
163 *) test -n "$quiet" && return;;
164 esac
165 shift
166 printf "%s" "$*"
167 tput sgr0
168 echo
171 else
172 say_color() {
173 test -z "$1" && test -n "$quiet" && return
174 shift
175 echo "$*"
179 error () {
180 say_color error "error: $*"
181 GIT_EXIT_OK=t
182 exit 1
185 say () {
186 say_color info "$*"
189 test "${test_description}" != "" ||
190 error "Test script did not set test_description."
192 if test "$help" = "t"
193 then
194 echo "$test_description"
195 exit 0
198 exec 5>&1
199 exec 6<&0
200 if test "$verbose" = "t"
201 then
202 exec 4>&2 3>&1
203 else
204 exec 4>/dev/null 3>/dev/null
207 test_failure=0
208 test_count=0
209 test_fixed=0
210 test_broken=0
211 test_success=0
213 test_external_has_tap=0
215 die () {
216 code=$?
217 if test -n "$GIT_EXIT_OK"
218 then
219 exit $code
220 else
221 echo >&5 "FATAL: Unexpected exit with code $code"
222 exit 1
226 GIT_EXIT_OK=
227 trap 'die' EXIT
229 # The semantics of the editor variables are that of invoking
230 # sh -c "$EDITOR \"$@\"" files ...
232 # If our trash directory contains shell metacharacters, they will be
233 # interpreted if we just set $EDITOR directly, so do a little dance with
234 # environment variables to work around this.
236 # In particular, quoting isn't enough, as the path may contain the same quote
237 # that we're using.
238 test_set_editor () {
239 FAKE_EDITOR="$1"
240 export FAKE_EDITOR
241 EDITOR='"$FAKE_EDITOR"'
242 export EDITOR
245 test_decode_color () {
246 awk '
247 function name(n) {
248 if (n == 0) return "RESET";
249 if (n == 1) return "BOLD";
250 if (n == 30) return "BLACK";
251 if (n == 31) return "RED";
252 if (n == 32) return "GREEN";
253 if (n == 33) return "YELLOW";
254 if (n == 34) return "BLUE";
255 if (n == 35) return "MAGENTA";
256 if (n == 36) return "CYAN";
257 if (n == 37) return "WHITE";
258 if (n == 40) return "BLACK";
259 if (n == 41) return "BRED";
260 if (n == 42) return "BGREEN";
261 if (n == 43) return "BYELLOW";
262 if (n == 44) return "BBLUE";
263 if (n == 45) return "BMAGENTA";
264 if (n == 46) return "BCYAN";
265 if (n == 47) return "BWHITE";
268 while (match($0, /\033\[[0-9;]*m/) != 0) {
269 printf "%s<", substr($0, 1, RSTART-1);
270 codes = substr($0, RSTART+2, RLENGTH-3);
271 if (length(codes) == 0)
272 printf "%s", name(0)
273 else {
274 n = split(codes, ary, ";");
275 sep = "";
276 for (i = 1; i <= n; i++) {
277 printf "%s%s", sep, name(ary[i]);
278 sep = ";"
281 printf ">";
282 $0 = substr($0, RSTART + RLENGTH, length($0) - RSTART - RLENGTH + 1);
284 print
289 nul_to_q () {
290 perl -pe 'y/\000/Q/'
293 q_to_nul () {
294 perl -pe 'y/Q/\000/'
297 q_to_cr () {
298 tr Q '\015'
301 q_to_tab () {
302 tr Q '\011'
305 append_cr () {
306 sed -e 's/$/Q/' | tr Q '\015'
309 remove_cr () {
310 tr '\015' Q | sed -e 's/Q$//'
313 # In some bourne shell implementations, the "unset" builtin returns
314 # nonzero status when a variable to be unset was not set in the first
315 # place.
317 # Use sane_unset when that should not be considered an error.
319 sane_unset () {
320 unset "$@"
321 return 0
324 test_tick () {
325 if test -z "${test_tick+set}"
326 then
327 test_tick=1112911993
328 else
329 test_tick=$(($test_tick + 60))
331 GIT_COMMITTER_DATE="$test_tick -0700"
332 GIT_AUTHOR_DATE="$test_tick -0700"
333 export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
336 # Call test_commit with the arguments "<message> [<file> [<contents>]]"
338 # This will commit a file with the given contents and the given commit
339 # message. It will also add a tag with <message> as name.
341 # Both <file> and <contents> default to <message>.
343 test_commit () {
344 file=${2:-"$1.t"}
345 echo "${3-$1}" > "$file" &&
346 git add "$file" &&
347 test_tick &&
348 git commit -m "$1" &&
349 git tag "$1"
352 # Call test_merge with the arguments "<message> <commit>", where <commit>
353 # can be a tag pointing to the commit-to-merge.
355 test_merge () {
356 test_tick &&
357 git merge -m "$1" "$2" &&
358 git tag "$1"
361 # This function helps systems where core.filemode=false is set.
362 # Use it instead of plain 'chmod +x' to set or unset the executable bit
363 # of a file in the working directory and add it to the index.
365 test_chmod () {
366 chmod "$@" &&
367 git update-index --add "--chmod=$@"
370 # Unset a configuration variable, but don't fail if it doesn't exist.
371 test_unconfig () {
372 git config --unset-all "$@"
373 config_status=$?
374 case "$config_status" in
375 5) # ok, nothing to unset
376 config_status=0
378 esac
379 return $config_status
382 # Set git config, automatically unsetting it after the test is over.
383 test_config () {
384 test_when_finished "test_unconfig '$1'" &&
385 git config "$@"
388 test_config_global () {
389 test_when_finished "test_unconfig --global '$1'" &&
390 git config --global "$@"
393 # Use test_set_prereq to tell that a particular prerequisite is available.
394 # The prerequisite can later be checked for in two ways:
396 # - Explicitly using test_have_prereq.
398 # - Implicitly by specifying the prerequisite tag in the calls to
399 # test_expect_{success,failure,code}.
401 # The single parameter is the prerequisite tag (a simple word, in all
402 # capital letters by convention).
404 test_set_prereq () {
405 satisfied="$satisfied$1 "
407 satisfied=" "
409 test_have_prereq () {
410 # prerequisites can be concatenated with ','
411 save_IFS=$IFS
412 IFS=,
413 set -- $*
414 IFS=$save_IFS
416 total_prereq=0
417 ok_prereq=0
418 missing_prereq=
420 for prerequisite
422 total_prereq=$(($total_prereq + 1))
423 case $satisfied in
424 *" $prerequisite "*)
425 ok_prereq=$(($ok_prereq + 1))
428 # Keep a list of missing prerequisites
429 if test -z "$missing_prereq"
430 then
431 missing_prereq=$prerequisite
432 else
433 missing_prereq="$prerequisite,$missing_prereq"
435 esac
436 done
438 test $total_prereq = $ok_prereq
441 test_declared_prereq () {
442 case ",$test_prereq," in
443 *,$1,*)
444 return 0
446 esac
447 return 1
450 # You are not expected to call test_ok_ and test_failure_ directly, use
451 # the text_expect_* functions instead.
453 test_ok_ () {
454 test_success=$(($test_success + 1))
455 say_color "" "ok $test_count - $@"
458 test_failure_ () {
459 test_failure=$(($test_failure + 1))
460 say_color error "not ok - $test_count $1"
461 shift
462 echo "$@" | sed -e 's/^/# /'
463 test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
466 test_known_broken_ok_ () {
467 test_fixed=$(($test_fixed+1))
468 say_color "" "ok $test_count - $@ # TODO known breakage"
471 test_known_broken_failure_ () {
472 test_broken=$(($test_broken+1))
473 say_color skip "not ok $test_count - $@ # TODO known breakage"
476 test_debug () {
477 test "$debug" = "" || eval "$1"
480 test_eval_ () {
481 # This is a separate function because some tests use
482 # "return" to end a test_expect_success block early.
483 eval </dev/null >&3 2>&4 "$*"
486 test_run_ () {
487 test_cleanup=:
488 expecting_failure=$2
489 test_eval_ "$1"
490 eval_ret=$?
492 if test -z "$immediate" || test $eval_ret = 0 || test -n "$expecting_failure"
493 then
494 test_eval_ "$test_cleanup"
496 if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"; then
497 echo ""
499 return "$eval_ret"
502 test_skip () {
503 test_count=$(($test_count+1))
504 to_skip=
505 for skp in $GIT_SKIP_TESTS
507 case $this_test.$test_count in
508 $skp)
509 to_skip=t
510 break
511 esac
512 done
513 if test -z "$to_skip" && test -n "$test_prereq" &&
514 ! test_have_prereq "$test_prereq"
515 then
516 to_skip=t
518 case "$to_skip" in
520 of_prereq=
521 if test "$missing_prereq" != "$test_prereq"
522 then
523 of_prereq=" of $test_prereq"
526 say_color skip >&3 "skipping test: $@"
527 say_color skip "ok $test_count # skip $1 (missing $missing_prereq${of_prereq})"
528 : true
531 false
533 esac
536 test_expect_failure () {
537 test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
538 test "$#" = 2 ||
539 error "bug in the test script: not 2 or 3 parameters to test-expect-failure"
540 export test_prereq
541 if ! test_skip "$@"
542 then
543 say >&3 "checking known breakage: $2"
544 if test_run_ "$2" expecting_failure
545 then
546 test_known_broken_ok_ "$1"
547 else
548 test_known_broken_failure_ "$1"
551 echo >&3 ""
554 test_expect_success () {
555 test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
556 test "$#" = 2 ||
557 error "bug in the test script: not 2 or 3 parameters to test-expect-success"
558 export test_prereq
559 if ! test_skip "$@"
560 then
561 say >&3 "expecting success: $2"
562 if test_run_ "$2"
563 then
564 test_ok_ "$1"
565 else
566 test_failure_ "$@"
569 echo >&3 ""
572 # test_external runs external test scripts that provide continuous
573 # test output about their progress, and succeeds/fails on
574 # zero/non-zero exit code. It outputs the test output on stdout even
575 # in non-verbose mode, and announces the external script with "# run
576 # <n>: ..." before running it. When providing relative paths, keep in
577 # mind that all scripts run in "trash directory".
578 # Usage: test_external description command arguments...
579 # Example: test_external 'Perl API' perl ../path/to/test.pl
580 test_external () {
581 test "$#" = 4 && { test_prereq=$1; shift; } || test_prereq=
582 test "$#" = 3 ||
583 error >&5 "bug in the test script: not 3 or 4 parameters to test_external"
584 descr="$1"
585 shift
586 export test_prereq
587 if ! test_skip "$descr" "$@"
588 then
589 # Announce the script to reduce confusion about the
590 # test output that follows.
591 say_color "" "# run $test_count: $descr ($*)"
592 # Export TEST_DIRECTORY, TRASH_DIRECTORY and GIT_TEST_LONG
593 # to be able to use them in script
594 export TEST_DIRECTORY TRASH_DIRECTORY GIT_TEST_LONG
595 # Run command; redirect its stderr to &4 as in
596 # test_run_, but keep its stdout on our stdout even in
597 # non-verbose mode.
598 "$@" 2>&4
599 if [ "$?" = 0 ]
600 then
601 if test $test_external_has_tap -eq 0; then
602 test_ok_ "$descr"
603 else
604 say_color "" "# test_external test $descr was ok"
605 test_success=$(($test_success + 1))
607 else
608 if test $test_external_has_tap -eq 0; then
609 test_failure_ "$descr" "$@"
610 else
611 say_color error "# test_external test $descr failed: $@"
612 test_failure=$(($test_failure + 1))
618 # Like test_external, but in addition tests that the command generated
619 # no output on stderr.
620 test_external_without_stderr () {
621 # The temporary file has no (and must have no) security
622 # implications.
623 tmp=${TMPDIR:-/tmp}
624 stderr="$tmp/git-external-stderr.$$.tmp"
625 test_external "$@" 4> "$stderr"
626 [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
627 descr="no stderr: $1"
628 shift
629 say >&3 "# expecting no stderr from previous command"
630 if [ ! -s "$stderr" ]; then
631 rm "$stderr"
633 if test $test_external_has_tap -eq 0; then
634 test_ok_ "$descr"
635 else
636 say_color "" "# test_external_without_stderr test $descr was ok"
637 test_success=$(($test_success + 1))
639 else
640 if [ "$verbose" = t ]; then
641 output=`echo; echo "# Stderr is:"; cat "$stderr"`
642 else
643 output=
645 # rm first in case test_failure exits.
646 rm "$stderr"
647 if test $test_external_has_tap -eq 0; then
648 test_failure_ "$descr" "$@" "$output"
649 else
650 say_color error "# test_external_without_stderr test $descr failed: $@: $output"
651 test_failure=$(($test_failure + 1))
656 # debugging-friendly alternatives to "test [-f|-d|-e]"
657 # The commands test the existence or non-existence of $1. $2 can be
658 # given to provide a more precise diagnosis.
659 test_path_is_file () {
660 if ! [ -f "$1" ]
661 then
662 echo "File $1 doesn't exist. $*"
663 false
667 test_path_is_dir () {
668 if ! [ -d "$1" ]
669 then
670 echo "Directory $1 doesn't exist. $*"
671 false
675 test_path_is_missing () {
676 if [ -e "$1" ]
677 then
678 echo "Path exists:"
679 ls -ld "$1"
680 if [ $# -ge 1 ]; then
681 echo "$*"
683 false
687 # test_line_count checks that a file has the number of lines it
688 # ought to. For example:
690 # test_expect_success 'produce exactly one line of output' '
691 # do something >output &&
692 # test_line_count = 1 output
695 # is like "test $(wc -l <output) = 1" except that it passes the
696 # output through when the number of lines is wrong.
698 test_line_count () {
699 if test $# != 3
700 then
701 error "bug in the test script: not 3 parameters to test_line_count"
702 elif ! test $(wc -l <"$3") "$1" "$2"
703 then
704 echo "test_line_count: line count for $3 !$1 $2"
705 cat "$3"
706 return 1
710 # This is not among top-level (test_expect_success | test_expect_failure)
711 # but is a prefix that can be used in the test script, like:
713 # test_expect_success 'complain and die' '
714 # do something &&
715 # do something else &&
716 # test_must_fail git checkout ../outerspace
719 # Writing this as "! git checkout ../outerspace" is wrong, because
720 # the failure could be due to a segv. We want a controlled failure.
722 test_must_fail () {
723 "$@"
724 exit_code=$?
725 if test $exit_code = 0; then
726 echo >&2 "test_must_fail: command succeeded: $*"
727 return 1
728 elif test $exit_code -gt 129 -a $exit_code -le 192; then
729 echo >&2 "test_must_fail: died by signal: $*"
730 return 1
731 elif test $exit_code = 127; then
732 echo >&2 "test_must_fail: command not found: $*"
733 return 1
735 return 0
738 # Similar to test_must_fail, but tolerates success, too. This is
739 # meant to be used in contexts like:
741 # test_expect_success 'some command works without configuration' '
742 # test_might_fail git config --unset all.configuration &&
743 # do something
746 # Writing "git config --unset all.configuration || :" would be wrong,
747 # because we want to notice if it fails due to segv.
749 test_might_fail () {
750 "$@"
751 exit_code=$?
752 if test $exit_code -gt 129 -a $exit_code -le 192; then
753 echo >&2 "test_might_fail: died by signal: $*"
754 return 1
755 elif test $exit_code = 127; then
756 echo >&2 "test_might_fail: command not found: $*"
757 return 1
759 return 0
762 # Similar to test_must_fail and test_might_fail, but check that a
763 # given command exited with a given exit code. Meant to be used as:
765 # test_expect_success 'Merge with d/f conflicts' '
766 # test_expect_code 1 git merge "merge msg" B master
769 test_expect_code () {
770 want_code=$1
771 shift
772 "$@"
773 exit_code=$?
774 if test $exit_code = $want_code
775 then
776 return 0
779 echo >&2 "test_expect_code: command exited with $exit_code, we wanted $want_code $*"
780 return 1
783 # test_cmp is a helper function to compare actual and expected output.
784 # You can use it like:
786 # test_expect_success 'foo works' '
787 # echo expected >expected &&
788 # foo >actual &&
789 # test_cmp expected actual
792 # This could be written as either "cmp" or "diff -u", but:
793 # - cmp's output is not nearly as easy to read as diff -u
794 # - not all diff versions understand "-u"
796 test_cmp() {
797 $GIT_TEST_CMP "$@"
800 # This function can be used to schedule some commands to be run
801 # unconditionally at the end of the test to restore sanity:
803 # test_expect_success 'test core.capslock' '
804 # git config core.capslock true &&
805 # test_when_finished "git config --unset core.capslock" &&
806 # hello world
809 # That would be roughly equivalent to
811 # test_expect_success 'test core.capslock' '
812 # git config core.capslock true &&
813 # hello world
814 # git config --unset core.capslock
817 # except that the greeting and config --unset must both succeed for
818 # the test to pass.
820 # Note that under --immediate mode, no clean-up is done to help diagnose
821 # what went wrong.
823 test_when_finished () {
824 test_cleanup="{ $*
825 } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
828 # Most tests can use the created repository, but some may need to create more.
829 # Usage: test_create_repo <directory>
830 test_create_repo () {
831 test "$#" = 1 ||
832 error "bug in the test script: not 1 parameter to test-create-repo"
833 repo="$1"
834 mkdir -p "$repo"
836 cd "$repo" || error "Cannot setup test environment"
837 "$GIT_EXEC_PATH/git-init" "--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
838 error "cannot run git init -- have you built things yet?"
839 mv .git/hooks .git/hooks-disabled
840 ) || exit
843 test_done () {
844 GIT_EXIT_OK=t
846 if test -z "$HARNESS_ACTIVE"; then
847 test_results_dir="$TEST_DIRECTORY/test-results"
848 mkdir -p "$test_results_dir"
849 test_results_path="$test_results_dir/${0%.sh}-$$.counts"
851 cat >>"$test_results_path" <<-EOF
852 total $test_count
853 success $test_success
854 fixed $test_fixed
855 broken $test_broken
856 failed $test_failure
861 if test "$test_fixed" != 0
862 then
863 say_color pass "# fixed $test_fixed known breakage(s)"
865 if test "$test_broken" != 0
866 then
867 say_color error "# still have $test_broken known breakage(s)"
868 msg="remaining $(($test_count-$test_broken)) test(s)"
869 else
870 msg="$test_count test(s)"
872 case "$test_failure" in
874 # Maybe print SKIP message
875 [ -z "$skip_all" ] || skip_all=" # SKIP $skip_all"
877 if test $test_external_has_tap -eq 0; then
878 say_color pass "# passed all $msg"
879 say "1..$test_count$skip_all"
882 test -d "$remove_trash" &&
883 cd "$(dirname "$remove_trash")" &&
884 rm -rf "$(basename "$remove_trash")"
886 exit 0 ;;
889 if test $test_external_has_tap -eq 0; then
890 say_color error "# failed $test_failure among $msg"
891 say "1..$test_count"
894 exit 1 ;;
896 esac
899 # Test the binaries we have just built. The tests are kept in
900 # t/ subdirectory and are run in 'trash directory' subdirectory.
901 if test -z "$TEST_DIRECTORY"
902 then
903 # We allow tests to override this, in case they want to run tests
904 # outside of t/, e.g. for running tests on the test library
905 # itself.
906 TEST_DIRECTORY=$(pwd)
908 GIT_BUILD_DIR="$TEST_DIRECTORY"/..
910 if test -n "$valgrind"
911 then
912 make_symlink () {
913 test -h "$2" &&
914 test "$1" = "$(readlink "$2")" || {
915 # be super paranoid
916 if mkdir "$2".lock
917 then
918 rm -f "$2" &&
919 ln -s "$1" "$2" &&
920 rm -r "$2".lock
921 else
922 while test -d "$2".lock
924 say "Waiting for lock on $2."
925 sleep 1
926 done
931 make_valgrind_symlink () {
932 # handle only executables, unless they are shell libraries that
933 # need to be in the exec-path. We will just use "#!" as a
934 # guess for a shell-script, since we have no idea what the user
935 # may have configured as the shell path.
936 test -x "$1" ||
937 test "#!" = "$(head -c 2 <"$1")" ||
938 return;
940 base=$(basename "$1")
941 symlink_target=$GIT_BUILD_DIR/$base
942 # do not override scripts
943 if test -x "$symlink_target" &&
944 test ! -d "$symlink_target" &&
945 test "#!" != "$(head -c 2 < "$symlink_target")"
946 then
947 symlink_target=../valgrind.sh
949 case "$base" in
950 *.sh|*.perl)
951 symlink_target=../unprocessed-script
952 esac
953 # create the link, or replace it if it is out of date
954 make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
957 # override all git executables in TEST_DIRECTORY/..
958 GIT_VALGRIND=$TEST_DIRECTORY/valgrind
959 mkdir -p "$GIT_VALGRIND"/bin
960 for file in $GIT_BUILD_DIR/git* $GIT_BUILD_DIR/test-*
962 make_valgrind_symlink $file
963 done
964 # special-case the mergetools loadables
965 make_symlink "$GIT_BUILD_DIR"/mergetools "$GIT_VALGRIND/bin/mergetools"
966 OLDIFS=$IFS
967 IFS=:
968 for path in $PATH
970 ls "$path"/git-* 2> /dev/null |
971 while read file
973 make_valgrind_symlink "$file"
974 done
975 done
976 IFS=$OLDIFS
977 PATH=$GIT_VALGRIND/bin:$PATH
978 GIT_EXEC_PATH=$GIT_VALGRIND/bin
979 export GIT_VALGRIND
980 elif test -n "$GIT_TEST_INSTALLED" ; then
981 GIT_EXEC_PATH=$($GIT_TEST_INSTALLED/git --exec-path) ||
982 error "Cannot run git from $GIT_TEST_INSTALLED."
983 PATH=$GIT_TEST_INSTALLED:$GIT_BUILD_DIR:$PATH
984 GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH}
985 else # normal case, use ../bin-wrappers only unless $with_dashes:
986 git_bin_dir="$GIT_BUILD_DIR/bin-wrappers"
987 if ! test -x "$git_bin_dir/git" ; then
988 if test -z "$with_dashes" ; then
989 say "$git_bin_dir/git is not executable; using GIT_EXEC_PATH"
991 with_dashes=t
993 PATH="$git_bin_dir:$PATH"
994 GIT_EXEC_PATH=$GIT_BUILD_DIR
995 if test -n "$with_dashes" ; then
996 PATH="$GIT_BUILD_DIR:$PATH"
999 GIT_TEMPLATE_DIR="$GIT_BUILD_DIR"/templates/blt
1000 unset GIT_CONFIG
1001 GIT_CONFIG_NOSYSTEM=1
1002 GIT_ATTR_NOSYSTEM=1
1003 export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM
1005 . "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS
1007 if test -z "$GIT_TEST_CMP"
1008 then
1009 if test -n "$GIT_TEST_CMP_USE_COPIED_CONTEXT"
1010 then
1011 GIT_TEST_CMP="$DIFF -c"
1012 else
1013 GIT_TEST_CMP="$DIFF -u"
1017 GITPERLLIB="$GIT_BUILD_DIR"/perl/blib/lib:"$GIT_BUILD_DIR"/perl/blib/arch/auto/Git
1018 export GITPERLLIB
1019 test -d "$GIT_BUILD_DIR"/templates/blt || {
1020 error "You haven't built things yet, have you?"
1023 if test -z "$GIT_TEST_INSTALLED" && test -z "$NO_PYTHON"
1024 then
1025 GITPYTHONLIB="$GIT_BUILD_DIR/git_remote_helpers/build/lib"
1026 export GITPYTHONLIB
1027 test -d "$GIT_BUILD_DIR"/git_remote_helpers/build || {
1028 error "You haven't built git_remote_helpers yet, have you?"
1032 if ! test -x "$GIT_BUILD_DIR"/test-chmtime; then
1033 echo >&2 'You need to build test-chmtime:'
1034 echo >&2 'Run "make test-chmtime" in the source (toplevel) directory'
1035 exit 1
1038 # Test repository
1039 test="trash directory.$(basename "$0" .sh)"
1040 test -n "$root" && test="$root/$test"
1041 case "$test" in
1042 /*) TRASH_DIRECTORY="$test" ;;
1043 *) TRASH_DIRECTORY="$TEST_DIRECTORY/$test" ;;
1044 esac
1045 test ! -z "$debug" || remove_trash=$TRASH_DIRECTORY
1046 rm -fr "$test" || {
1047 GIT_EXIT_OK=t
1048 echo >&5 "FATAL: Cannot prepare test area"
1049 exit 1
1052 HOME="$TRASH_DIRECTORY"
1053 export HOME
1055 test_create_repo "$test"
1056 # Use -P to resolve symlinks in our working directory so that the cwd
1057 # in subprocesses like git equals our $PWD (for pathname comparisons).
1058 cd -P "$test" || exit 1
1060 this_test=${0##*/}
1061 this_test=${this_test%%-*}
1062 for skp in $GIT_SKIP_TESTS
1064 case "$this_test" in
1065 $skp)
1066 say_color skip >&3 "skipping test $this_test altogether"
1067 skip_all="skip all tests in $this_test"
1068 test_done
1069 esac
1070 done
1072 # Provide an implementation of the 'yes' utility
1073 yes () {
1074 if test $# = 0
1075 then
1077 else
1078 y="$*"
1081 while echo "$y"
1084 done
1087 # Fix some commands on Windows
1088 case $(uname -s) in
1089 *MINGW*)
1090 # Windows has its own (incompatible) sort and find
1091 sort () {
1092 /usr/bin/sort "$@"
1094 find () {
1095 /usr/bin/find "$@"
1097 sum () {
1098 md5sum "$@"
1100 # git sees Windows-style pwd
1101 pwd () {
1102 builtin pwd -W
1104 # no POSIX permissions
1105 # backslashes in pathspec are converted to '/'
1106 # exec does not inherit the PID
1107 test_set_prereq MINGW
1108 test_set_prereq SED_STRIPS_CR
1110 *CYGWIN*)
1111 test_set_prereq POSIXPERM
1112 test_set_prereq EXECKEEPSPID
1113 test_set_prereq NOT_MINGW
1114 test_set_prereq SED_STRIPS_CR
1117 test_set_prereq POSIXPERM
1118 test_set_prereq BSLASHPSPEC
1119 test_set_prereq EXECKEEPSPID
1120 test_set_prereq NOT_MINGW
1122 esac
1124 test -z "$NO_PERL" && test_set_prereq PERL
1125 test -z "$NO_PYTHON" && test_set_prereq PYTHON
1126 test -n "$USE_LIBPCRE" && test_set_prereq LIBPCRE
1127 test -z "$NO_GETTEXT" && test_set_prereq GETTEXT
1129 # Can we rely on git's output in the C locale?
1130 if test -n "$GETTEXT_POISON"
1131 then
1132 GIT_GETTEXT_POISON=YesPlease
1133 export GIT_GETTEXT_POISON
1134 test_set_prereq GETTEXT_POISON
1135 else
1136 test_set_prereq C_LOCALE_OUTPUT
1139 # Use this instead of test_cmp to compare files that contain expected and
1140 # actual output from git commands that can be translated. When running
1141 # under GETTEXT_POISON this pretends that the command produced expected
1142 # results.
1143 test_i18ncmp () {
1144 test -n "$GETTEXT_POISON" || test_cmp "$@"
1147 # Use this instead of "grep expected-string actual" to see if the
1148 # output from a git command that can be translated either contains an
1149 # expected string, or does not contain an unwanted one. When running
1150 # under GETTEXT_POISON this pretends that the command produced expected
1151 # results.
1152 test_i18ngrep () {
1153 if test -n "$GETTEXT_POISON"
1154 then
1155 : # pretend success
1156 elif test "x!" = "x$1"
1157 then
1158 shift
1159 ! grep "$@"
1160 else
1161 grep "$@"
1165 # test whether the filesystem supports symbolic links
1166 ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS
1167 rm -f y
1169 # When the tests are run as root, permission tests will report that
1170 # things are writable when they shouldn't be.
1171 test -w / || test_set_prereq SANITY