Code

run test suite without dashed git-commands in PATH
[git.git] / t / test-lib.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Junio C Hamano
4 #
6 # if --tee was passed, write the output not only to the terminal, but
7 # additionally to the file test-results/$BASENAME.out, too.
8 case "$GIT_TEST_TEE_STARTED, $* " in
9 done,*)
10         # do not redirect again
11         ;;
12 *' --tee '*|*' --va'*)
13         mkdir -p test-results
14         BASE=test-results/$(basename "$0" .sh)
15         (GIT_TEST_TEE_STARTED=done ${SHELL-sh} "$0" "$@" 2>&1;
16          echo $? > $BASE.exit) | tee $BASE.out
17         test "$(cat $BASE.exit)" = 0
18         exit
19         ;;
20 esac
22 # Keep the original TERM for say_color
23 ORIGINAL_TERM=$TERM
25 # For repeatability, reset the environment to known value.
26 LANG=C
27 LC_ALL=C
28 PAGER=cat
29 TZ=UTC
30 TERM=dumb
31 export LANG LC_ALL PAGER TERM TZ
32 EDITOR=:
33 unset VISUAL
34 unset GIT_EDITOR
35 unset AUTHOR_DATE
36 unset AUTHOR_EMAIL
37 unset AUTHOR_NAME
38 unset COMMIT_AUTHOR_EMAIL
39 unset COMMIT_AUTHOR_NAME
40 unset EMAIL
41 unset GIT_ALTERNATE_OBJECT_DIRECTORIES
42 unset GIT_AUTHOR_DATE
43 GIT_AUTHOR_EMAIL=author@example.com
44 GIT_AUTHOR_NAME='A U Thor'
45 unset GIT_COMMITTER_DATE
46 GIT_COMMITTER_EMAIL=committer@example.com
47 GIT_COMMITTER_NAME='C O Mitter'
48 unset GIT_DIFF_OPTS
49 unset GIT_DIR
50 unset GIT_WORK_TREE
51 unset GIT_EXTERNAL_DIFF
52 unset GIT_INDEX_FILE
53 unset GIT_OBJECT_DIRECTORY
54 unset GIT_CEILING_DIRECTORIES
55 unset SHA1_FILE_DIRECTORIES
56 unset SHA1_FILE_DIRECTORY
57 GIT_MERGE_VERBOSITY=5
58 export GIT_MERGE_VERBOSITY
59 export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
60 export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
61 export EDITOR
62 GIT_TEST_CMP=${GIT_TEST_CMP:-diff -u}
64 # Protect ourselves from common misconfiguration to export
65 # CDPATH into the environment
66 unset CDPATH
68 case $(echo $GIT_TRACE |tr "[A-Z]" "[a-z]") in
69         1|2|true)
70                 echo "* warning: Some tests will not work if GIT_TRACE" \
71                         "is set as to trace on STDERR ! *"
72                 echo "* warning: Please set GIT_TRACE to something" \
73                         "other than 1, 2 or true ! *"
74                 ;;
75 esac
77 # Each test should start with something like this, after copyright notices:
78 #
79 # test_description='Description of this test...
80 # This test checks if command xyzzy does the right thing...
81 # '
82 # . ./test-lib.sh
83 [ "x$ORIGINAL_TERM" != "xdumb" ] && (
84                 TERM=$ORIGINAL_TERM &&
85                 export TERM &&
86                 [ -t 1 ] &&
87                 tput bold >/dev/null 2>&1 &&
88                 tput setaf 1 >/dev/null 2>&1 &&
89                 tput sgr0 >/dev/null 2>&1
90         ) &&
91         color=t
93 while test "$#" -ne 0
94 do
95         case "$1" in
96         -d|--d|--de|--deb|--debu|--debug)
97                 debug=t; shift ;;
98         -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
99                 immediate=t; shift ;;
100         -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
101                 GIT_TEST_LONG=t; export GIT_TEST_LONG; shift ;;
102         -h|--h|--he|--hel|--help)
103                 help=t; shift ;;
104         -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
105                 verbose=t; shift ;;
106         -q|--q|--qu|--qui|--quie|--quiet)
107                 quiet=t; shift ;;
108         --with-dashes)
109                 with_dashes=t; shift ;;
110         --no-color)
111                 color=; shift ;;
112         --no-python)
113                 # noop now...
114                 shift ;;
115         --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
116                 valgrind=t; verbose=t; shift ;;
117         --tee)
118                 shift ;; # was handled already
119         --root=*)
120                 root=$(expr "z$1" : 'z[^=]*=\(.*\)')
121                 shift ;;
122         *)
123                 echo "error: unknown test option '$1'" >&2; exit 1 ;;
124         esac
125 done
127 if test -n "$color"; then
128         say_color () {
129                 (
130                 TERM=$ORIGINAL_TERM
131                 export TERM
132                 case "$1" in
133                         error) tput bold; tput setaf 1;; # bold red
134                         skip)  tput bold; tput setaf 2;; # bold green
135                         pass)  tput setaf 2;;            # green
136                         info)  tput setaf 3;;            # brown
137                         *) test -n "$quiet" && return;;
138                 esac
139                 shift
140                 printf "* %s" "$*"
141                 tput sgr0
142                 echo
143                 )
144         }
145 else
146         say_color() {
147                 test -z "$1" && test -n "$quiet" && return
148                 shift
149                 echo "* $*"
150         }
151 fi
153 error () {
154         say_color error "error: $*"
155         GIT_EXIT_OK=t
156         exit 1
159 say () {
160         say_color info "$*"
163 test "${test_description}" != "" ||
164 error "Test script did not set test_description."
166 if test "$help" = "t"
167 then
168         echo "$test_description"
169         exit 0
170 fi
172 exec 5>&1
173 if test "$verbose" = "t"
174 then
175         exec 4>&2 3>&1
176 else
177         exec 4>/dev/null 3>/dev/null
178 fi
180 test_failure=0
181 test_count=0
182 test_fixed=0
183 test_broken=0
184 test_success=0
186 die () {
187         code=$?
188         if test -n "$GIT_EXIT_OK"
189         then
190                 exit $code
191         else
192                 echo >&5 "FATAL: Unexpected exit with code $code"
193                 exit 1
194         fi
197 GIT_EXIT_OK=
198 trap 'die' EXIT
200 # The semantics of the editor variables are that of invoking
201 # sh -c "$EDITOR \"$@\"" files ...
203 # If our trash directory contains shell metacharacters, they will be
204 # interpreted if we just set $EDITOR directly, so do a little dance with
205 # environment variables to work around this.
207 # In particular, quoting isn't enough, as the path may contain the same quote
208 # that we're using.
209 test_set_editor () {
210         FAKE_EDITOR="$1"
211         export FAKE_EDITOR
212         EDITOR='"$FAKE_EDITOR"'
213         export EDITOR
216 test_tick () {
217         if test -z "${test_tick+set}"
218         then
219                 test_tick=1112911993
220         else
221                 test_tick=$(($test_tick + 60))
222         fi
223         GIT_COMMITTER_DATE="$test_tick -0700"
224         GIT_AUTHOR_DATE="$test_tick -0700"
225         export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
228 # Call test_commit with the arguments "<message> [<file> [<contents>]]"
230 # This will commit a file with the given contents and the given commit
231 # message.  It will also add a tag with <message> as name.
233 # Both <file> and <contents> default to <message>.
235 test_commit () {
236         file=${2:-"$1.t"}
237         echo "${3-$1}" > "$file" &&
238         git add "$file" &&
239         test_tick &&
240         git commit -m "$1" &&
241         git tag "$1"
244 # Call test_merge with the arguments "<message> <commit>", where <commit>
245 # can be a tag pointing to the commit-to-merge.
247 test_merge () {
248         test_tick &&
249         git merge -m "$1" "$2" &&
250         git tag "$1"
253 # This function helps systems where core.filemode=false is set.
254 # Use it instead of plain 'chmod +x' to set or unset the executable bit
255 # of a file in the working directory and add it to the index.
257 test_chmod () {
258         chmod "$@" &&
259         git update-index --add "--chmod=$@"
262 # Use test_set_prereq to tell that a particular prerequisite is available.
263 # The prerequisite can later be checked for in two ways:
265 # - Explicitly using test_have_prereq.
267 # - Implicitly by specifying the prerequisite tag in the calls to
268 #   test_expect_{success,failure,code}.
270 # The single parameter is the prerequisite tag (a simple word, in all
271 # capital letters by convention).
273 test_set_prereq () {
274         satisfied="$satisfied$1 "
276 satisfied=" "
278 test_have_prereq () {
279         case $satisfied in
280         *" $1 "*)
281                 : yes, have it ;;
282         *)
283                 ! : nope ;;
284         esac
287 # You are not expected to call test_ok_ and test_failure_ directly, use
288 # the text_expect_* functions instead.
290 test_ok_ () {
291         test_success=$(($test_success + 1))
292         say_color "" "  ok $test_count: $@"
295 test_failure_ () {
296         test_failure=$(($test_failure + 1))
297         say_color error "FAIL $test_count: $1"
298         shift
299         echo "$@" | sed -e 's/^/        /'
300         test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
303 test_known_broken_ok_ () {
304         test_fixed=$(($test_fixed+1))
305         say_color "" "  FIXED $test_count: $@"
308 test_known_broken_failure_ () {
309         test_broken=$(($test_broken+1))
310         say_color skip "  still broken $test_count: $@"
313 test_debug () {
314         test "$debug" = "" || eval "$1"
317 test_run_ () {
318         eval >&3 2>&4 "$1"
319         eval_ret="$?"
320         return 0
323 test_skip () {
324         test_count=$(($test_count+1))
325         to_skip=
326         for skp in $GIT_SKIP_TESTS
327         do
328                 case $this_test.$test_count in
329                 $skp)
330                         to_skip=t
331                 esac
332         done
333         if test -z "$to_skip" && test -n "$prereq" &&
334            ! test_have_prereq "$prereq"
335         then
336                 to_skip=t
337         fi
338         case "$to_skip" in
339         t)
340                 say_color skip >&3 "skipping test: $@"
341                 say_color skip "skip $test_count: $1"
342                 : true
343                 ;;
344         *)
345                 false
346                 ;;
347         esac
350 test_expect_failure () {
351         test "$#" = 3 && { prereq=$1; shift; } || prereq=
352         test "$#" = 2 ||
353         error "bug in the test script: not 2 or 3 parameters to test-expect-failure"
354         if ! test_skip "$@"
355         then
356                 say >&3 "checking known breakage: $2"
357                 test_run_ "$2"
358                 if [ "$?" = 0 -a "$eval_ret" = 0 ]
359                 then
360                         test_known_broken_ok_ "$1"
361                 else
362                         test_known_broken_failure_ "$1"
363                 fi
364         fi
365         echo >&3 ""
368 test_expect_success () {
369         test "$#" = 3 && { prereq=$1; shift; } || prereq=
370         test "$#" = 2 ||
371         error "bug in the test script: not 2 or 3 parameters to test-expect-success"
372         if ! test_skip "$@"
373         then
374                 say >&3 "expecting success: $2"
375                 test_run_ "$2"
376                 if [ "$?" = 0 -a "$eval_ret" = 0 ]
377                 then
378                         test_ok_ "$1"
379                 else
380                         test_failure_ "$@"
381                 fi
382         fi
383         echo >&3 ""
386 test_expect_code () {
387         test "$#" = 4 && { prereq=$1; shift; } || prereq=
388         test "$#" = 3 ||
389         error "bug in the test script: not 3 or 4 parameters to test-expect-code"
390         if ! test_skip "$@"
391         then
392                 say >&3 "expecting exit code $1: $3"
393                 test_run_ "$3"
394                 if [ "$?" = 0 -a "$eval_ret" = "$1" ]
395                 then
396                         test_ok_ "$2"
397                 else
398                         test_failure_ "$@"
399                 fi
400         fi
401         echo >&3 ""
404 # test_external runs external test scripts that provide continuous
405 # test output about their progress, and succeeds/fails on
406 # zero/non-zero exit code.  It outputs the test output on stdout even
407 # in non-verbose mode, and announces the external script with "* run
408 # <n>: ..." before running it.  When providing relative paths, keep in
409 # mind that all scripts run in "trash directory".
410 # Usage: test_external description command arguments...
411 # Example: test_external 'Perl API' perl ../path/to/test.pl
412 test_external () {
413         test "$#" = 4 && { prereq=$1; shift; } || prereq=
414         test "$#" = 3 ||
415         error >&5 "bug in the test script: not 3 or 4 parameters to test_external"
416         descr="$1"
417         shift
418         if ! test_skip "$descr" "$@"
419         then
420                 # Announce the script to reduce confusion about the
421                 # test output that follows.
422                 say_color "" " run $test_count: $descr ($*)"
423                 # Run command; redirect its stderr to &4 as in
424                 # test_run_, but keep its stdout on our stdout even in
425                 # non-verbose mode.
426                 "$@" 2>&4
427                 if [ "$?" = 0 ]
428                 then
429                         test_ok_ "$descr"
430                 else
431                         test_failure_ "$descr" "$@"
432                 fi
433         fi
436 # Like test_external, but in addition tests that the command generated
437 # no output on stderr.
438 test_external_without_stderr () {
439         # The temporary file has no (and must have no) security
440         # implications.
441         tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
442         stderr="$tmp/git-external-stderr.$$.tmp"
443         test_external "$@" 4> "$stderr"
444         [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
445         descr="no stderr: $1"
446         shift
447         say >&3 "expecting no stderr from previous command"
448         if [ ! -s "$stderr" ]; then
449                 rm "$stderr"
450                 test_ok_ "$descr"
451         else
452                 if [ "$verbose" = t ]; then
453                         output=`echo; echo Stderr is:; cat "$stderr"`
454                 else
455                         output=
456                 fi
457                 # rm first in case test_failure exits.
458                 rm "$stderr"
459                 test_failure_ "$descr" "$@" "$output"
460         fi
463 # This is not among top-level (test_expect_success | test_expect_failure)
464 # but is a prefix that can be used in the test script, like:
466 #       test_expect_success 'complain and die' '
467 #           do something &&
468 #           do something else &&
469 #           test_must_fail git checkout ../outerspace
470 #       '
472 # Writing this as "! git checkout ../outerspace" is wrong, because
473 # the failure could be due to a segv.  We want a controlled failure.
475 test_must_fail () {
476         "$@"
477         test $? -gt 0 -a $? -le 129 -o $? -gt 192
480 # test_cmp is a helper function to compare actual and expected output.
481 # You can use it like:
483 #       test_expect_success 'foo works' '
484 #               echo expected >expected &&
485 #               foo >actual &&
486 #               test_cmp expected actual
487 #       '
489 # This could be written as either "cmp" or "diff -u", but:
490 # - cmp's output is not nearly as easy to read as diff -u
491 # - not all diff versions understand "-u"
493 test_cmp() {
494         $GIT_TEST_CMP "$@"
497 # Most tests can use the created repository, but some may need to create more.
498 # Usage: test_create_repo <directory>
499 test_create_repo () {
500         test "$#" = 1 ||
501         error "bug in the test script: not 1 parameter to test-create-repo"
502         owd=`pwd`
503         repo="$1"
504         mkdir -p "$repo"
505         cd "$repo" || error "Cannot setup test environment"
506         "$GIT_EXEC_PATH/git-init" "--template=$TEST_DIRECTORY/../templates/blt/" >&3 2>&4 ||
507         error "cannot run git init -- have you built things yet?"
508         mv .git/hooks .git/hooks-disabled
509         cd "$owd"
512 test_done () {
513         GIT_EXIT_OK=t
514         test_results_dir="$TEST_DIRECTORY/test-results"
515         mkdir -p "$test_results_dir"
516         test_results_path="$test_results_dir/${0%.sh}-$$"
518         echo "total $test_count" >> $test_results_path
519         echo "success $test_success" >> $test_results_path
520         echo "fixed $test_fixed" >> $test_results_path
521         echo "broken $test_broken" >> $test_results_path
522         echo "failed $test_failure" >> $test_results_path
523         echo "" >> $test_results_path
525         if test "$test_fixed" != 0
526         then
527                 say_color pass "fixed $test_fixed known breakage(s)"
528         fi
529         if test "$test_broken" != 0
530         then
531                 say_color error "still have $test_broken known breakage(s)"
532                 msg="remaining $(($test_count-$test_broken)) test(s)"
533         else
534                 msg="$test_count test(s)"
535         fi
536         case "$test_failure" in
537         0)
538                 say_color pass "passed all $msg"
540                 test -d "$remove_trash" &&
541                 cd "$(dirname "$remove_trash")" &&
542                 rm -rf "$(basename "$remove_trash")"
544                 exit 0 ;;
546         *)
547                 say_color error "failed $test_failure among $msg"
548                 exit 1 ;;
550         esac
553 # Test the binaries we have just built.  The tests are kept in
554 # t/ subdirectory and are run in 'trash directory' subdirectory.
555 TEST_DIRECTORY=$(pwd)
556 if test -n "$valgrind"
557 then
558         make_symlink () {
559                 test -h "$2" &&
560                 test "$1" = "$(readlink "$2")" || {
561                         # be super paranoid
562                         if mkdir "$2".lock
563                         then
564                                 rm -f "$2" &&
565                                 ln -s "$1" "$2" &&
566                                 rm -r "$2".lock
567                         else
568                                 while test -d "$2".lock
569                                 do
570                                         say "Waiting for lock on $2."
571                                         sleep 1
572                                 done
573                         fi
574                 }
575         }
577         make_valgrind_symlink () {
578                 # handle only executables
579                 test -x "$1" || return
581                 base=$(basename "$1")
582                 symlink_target=$TEST_DIRECTORY/../$base
583                 # do not override scripts
584                 if test -x "$symlink_target" &&
585                     test ! -d "$symlink_target" &&
586                     test "#!" != "$(head -c 2 < "$symlink_target")"
587                 then
588                         symlink_target=../valgrind.sh
589                 fi
590                 case "$base" in
591                 *.sh|*.perl)
592                         symlink_target=../unprocessed-script
593                 esac
594                 # create the link, or replace it if it is out of date
595                 make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
596         }
598         # override all git executables in TEST_DIRECTORY/..
599         GIT_VALGRIND=$TEST_DIRECTORY/valgrind
600         mkdir -p "$GIT_VALGRIND"/bin
601         for file in $TEST_DIRECTORY/../git* $TEST_DIRECTORY/../test-*
602         do
603                 make_valgrind_symlink $file
604         done
605         OLDIFS=$IFS
606         IFS=:
607         for path in $PATH
608         do
609                 ls "$path"/git-* 2> /dev/null |
610                 while read file
611                 do
612                         make_valgrind_symlink "$file"
613                 done
614         done
615         IFS=$OLDIFS
616         PATH=$GIT_VALGRIND/bin:$PATH
617         GIT_EXEC_PATH=$GIT_VALGRIND/bin
618         export GIT_VALGRIND
619 elif test -n "$GIT_TEST_INSTALLED" ; then
620         GIT_EXEC_PATH=$($GIT_TEST_INSTALLED/git --exec-path)  ||
621         error "Cannot run git from $GIT_TEST_INSTALLED."
622         PATH=$GIT_TEST_INSTALLED:$TEST_DIRECTORY/..:$PATH
623         GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH}
624 else # normal case, use ../bin-wrappers only unless $with_dashes:
625         git_bin_dir="$TEST_DIRECTORY/../bin-wrappers"
626         if ! test -x "$git_bin_dir/git" ; then
627                 if test -z "$with_dashes" ; then
628                         say "$git_bin_dir/git is not executable; using GIT_EXEC_PATH"
629                 fi
630                 with_dashes=t
631         fi
632         PATH="$git_bin_dir:$PATH"
633         GIT_EXEC_PATH=$TEST_DIRECTORY/..
634         if test -n "$with_dashes" ; then
635                 PATH="$TEST_DIRECTORY/..:$PATH"
636         fi
637 fi
638 GIT_TEMPLATE_DIR=$(pwd)/../templates/blt
639 unset GIT_CONFIG
640 GIT_CONFIG_NOSYSTEM=1
641 GIT_CONFIG_NOGLOBAL=1
642 export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_CONFIG_NOGLOBAL
644 GITPERLLIB=$(pwd)/../perl/blib/lib:$(pwd)/../perl/blib/arch/auto/Git
645 export GITPERLLIB
646 test -d ../templates/blt || {
647         error "You haven't built things yet, have you?"
650 if ! test -x ../test-chmtime; then
651         echo >&2 'You need to build test-chmtime:'
652         echo >&2 'Run "make test-chmtime" in the source (toplevel) directory'
653         exit 1
654 fi
656 . ../GIT-BUILD-OPTIONS
658 # Test repository
659 test="trash directory.$(basename "$0" .sh)"
660 test -n "$root" && test="$root/$test"
661 case "$test" in
662 /*) TRASH_DIRECTORY="$test" ;;
663  *) TRASH_DIRECTORY="$TEST_DIRECTORY/$test" ;;
664 esac
665 test ! -z "$debug" || remove_trash=$TRASH_DIRECTORY
666 rm -fr "$test" || {
667         GIT_EXIT_OK=t
668         echo >&5 "FATAL: Cannot prepare test area"
669         exit 1
672 test_create_repo "$test"
673 # Use -P to resolve symlinks in our working directory so that the cwd
674 # in subprocesses like git equals our $PWD (for pathname comparisons).
675 cd -P "$test" || exit 1
677 this_test=${0##*/}
678 this_test=${this_test%%-*}
679 for skp in $GIT_SKIP_TESTS
680 do
681         to_skip=
682         for skp in $GIT_SKIP_TESTS
683         do
684                 case "$this_test" in
685                 $skp)
686                         to_skip=t
687                 esac
688         done
689         case "$to_skip" in
690         t)
691                 say_color skip >&3 "skipping test $this_test altogether"
692                 say_color skip "skip all tests in $this_test"
693                 test_done
694         esac
695 done
697 # Provide an implementation of the 'yes' utility
698 yes () {
699         if test $# = 0
700         then
701                 y=y
702         else
703                 y="$*"
704         fi
706         while echo "$y"
707         do
708                 :
709         done
712 # Fix some commands on Windows
713 case $(uname -s) in
714 *MINGW*)
715         # Windows has its own (incompatible) sort and find
716         sort () {
717                 /usr/bin/sort "$@"
718         }
719         find () {
720                 /usr/bin/find "$@"
721         }
722         sum () {
723                 md5sum "$@"
724         }
725         # git sees Windows-style pwd
726         pwd () {
727                 builtin pwd -W
728         }
729         # no POSIX permissions
730         # backslashes in pathspec are converted to '/'
731         # exec does not inherit the PID
732         ;;
733 *)
734         test_set_prereq POSIXPERM
735         test_set_prereq BSLASHPSPEC
736         test_set_prereq EXECKEEPSPID
737         ;;
738 esac
740 test -z "$NO_PERL" && test_set_prereq PERL
742 # test whether the filesystem supports symbolic links
743 ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS
744 rm -f y