Code

tests: move convenience regexp to match object names to test-lib.sh
[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 # Convenience
78 #
79 # A regexp to match 5 and 40 hexdigits
80 _x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
81 _x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
83 # Each test should start with something like this, after copyright notices:
84 #
85 # test_description='Description of this test...
86 # This test checks if command xyzzy does the right thing...
87 # '
88 # . ./test-lib.sh
89 [ "x$ORIGINAL_TERM" != "xdumb" ] && (
90                 TERM=$ORIGINAL_TERM &&
91                 export TERM &&
92                 [ -t 1 ] &&
93                 tput bold >/dev/null 2>&1 &&
94                 tput setaf 1 >/dev/null 2>&1 &&
95                 tput sgr0 >/dev/null 2>&1
96         ) &&
97         color=t
99 while test "$#" -ne 0
100 do
101         case "$1" in
102         -d|--d|--de|--deb|--debu|--debug)
103                 debug=t; shift ;;
104         -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
105                 immediate=t; shift ;;
106         -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
107                 GIT_TEST_LONG=t; export GIT_TEST_LONG; shift ;;
108         -h|--h|--he|--hel|--help)
109                 help=t; shift ;;
110         -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
111                 verbose=t; shift ;;
112         -q|--q|--qu|--qui|--quie|--quiet)
113                 quiet=t; shift ;;
114         --no-color)
115                 color=; shift ;;
116         --no-python)
117                 # noop now...
118                 shift ;;
119         --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
120                 valgrind=t; verbose=t; shift ;;
121         --tee)
122                 shift ;; # was handled already
123         --root=*)
124                 root=$(expr "z$1" : 'z[^=]*=\(.*\)')
125                 shift ;;
126         *)
127                 echo "error: unknown test option '$1'" >&2; exit 1 ;;
128         esac
129 done
131 if test -n "$color"; then
132         say_color () {
133                 (
134                 TERM=$ORIGINAL_TERM
135                 export TERM
136                 case "$1" in
137                         error) tput bold; tput setaf 1;; # bold red
138                         skip)  tput bold; tput setaf 2;; # bold green
139                         pass)  tput setaf 2;;            # green
140                         info)  tput setaf 3;;            # brown
141                         *) test -n "$quiet" && return;;
142                 esac
143                 shift
144                 printf "* %s" "$*"
145                 tput sgr0
146                 echo
147                 )
148         }
149 else
150         say_color() {
151                 test -z "$1" && test -n "$quiet" && return
152                 shift
153                 echo "* $*"
154         }
155 fi
157 error () {
158         say_color error "error: $*"
159         GIT_EXIT_OK=t
160         exit 1
163 say () {
164         say_color info "$*"
167 test "${test_description}" != "" ||
168 error "Test script did not set test_description."
170 if test "$help" = "t"
171 then
172         echo "$test_description"
173         exit 0
174 fi
176 exec 5>&1
177 if test "$verbose" = "t"
178 then
179         exec 4>&2 3>&1
180 else
181         exec 4>/dev/null 3>/dev/null
182 fi
184 test_failure=0
185 test_count=0
186 test_fixed=0
187 test_broken=0
188 test_success=0
190 die () {
191         code=$?
192         if test -n "$GIT_EXIT_OK"
193         then
194                 exit $code
195         else
196                 echo >&5 "FATAL: Unexpected exit with code $code"
197                 exit 1
198         fi
201 GIT_EXIT_OK=
202 trap 'die' EXIT
204 # The semantics of the editor variables are that of invoking
205 # sh -c "$EDITOR \"$@\"" files ...
207 # If our trash directory contains shell metacharacters, they will be
208 # interpreted if we just set $EDITOR directly, so do a little dance with
209 # environment variables to work around this.
211 # In particular, quoting isn't enough, as the path may contain the same quote
212 # that we're using.
213 test_set_editor () {
214         FAKE_EDITOR="$1"
215         export FAKE_EDITOR
216         EDITOR='"$FAKE_EDITOR"'
217         export EDITOR
220 test_tick () {
221         if test -z "${test_tick+set}"
222         then
223                 test_tick=1112911993
224         else
225                 test_tick=$(($test_tick + 60))
226         fi
227         GIT_COMMITTER_DATE="$test_tick -0700"
228         GIT_AUTHOR_DATE="$test_tick -0700"
229         export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
232 # Call test_commit with the arguments "<message> [<file> [<contents>]]"
234 # This will commit a file with the given contents and the given commit
235 # message.  It will also add a tag with <message> as name.
237 # Both <file> and <contents> default to <message>.
239 test_commit () {
240         file=${2:-"$1.t"}
241         echo "${3-$1}" > "$file" &&
242         git add "$file" &&
243         test_tick &&
244         git commit -m "$1" &&
245         git tag "$1"
248 # Call test_merge with the arguments "<message> <commit>", where <commit>
249 # can be a tag pointing to the commit-to-merge.
251 test_merge () {
252         test_tick &&
253         git merge -m "$1" "$2" &&
254         git tag "$1"
257 # This function helps systems where core.filemode=false is set.
258 # Use it instead of plain 'chmod +x' to set or unset the executable bit
259 # of a file in the working directory and add it to the index.
261 test_chmod () {
262         chmod "$@" &&
263         git update-index --add "--chmod=$@"
266 # Use test_set_prereq to tell that a particular prerequisite is available.
267 # The prerequisite can later be checked for in two ways:
269 # - Explicitly using test_have_prereq.
271 # - Implicitly by specifying the prerequisite tag in the calls to
272 #   test_expect_{success,failure,code}.
274 # The single parameter is the prerequisite tag (a simple word, in all
275 # capital letters by convention).
277 test_set_prereq () {
278         satisfied="$satisfied$1 "
280 satisfied=" "
282 test_have_prereq () {
283         case $satisfied in
284         *" $1 "*)
285                 : yes, have it ;;
286         *)
287                 ! : nope ;;
288         esac
291 # You are not expected to call test_ok_ and test_failure_ directly, use
292 # the text_expect_* functions instead.
294 test_ok_ () {
295         test_success=$(($test_success + 1))
296         say_color "" "  ok $test_count: $@"
299 test_failure_ () {
300         test_failure=$(($test_failure + 1))
301         say_color error "FAIL $test_count: $1"
302         shift
303         echo "$@" | sed -e 's/^/        /'
304         test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
307 test_known_broken_ok_ () {
308         test_fixed=$(($test_fixed+1))
309         say_color "" "  FIXED $test_count: $@"
312 test_known_broken_failure_ () {
313         test_broken=$(($test_broken+1))
314         say_color skip "  still broken $test_count: $@"
317 test_debug () {
318         test "$debug" = "" || eval "$1"
321 test_run_ () {
322         eval >&3 2>&4 "$1"
323         eval_ret="$?"
324         return 0
327 test_skip () {
328         test_count=$(($test_count+1))
329         to_skip=
330         for skp in $GIT_SKIP_TESTS
331         do
332                 case $this_test.$test_count in
333                 $skp)
334                         to_skip=t
335                 esac
336         done
337         if test -z "$to_skip" && test -n "$prereq" &&
338            ! test_have_prereq "$prereq"
339         then
340                 to_skip=t
341         fi
342         case "$to_skip" in
343         t)
344                 say_color skip >&3 "skipping test: $@"
345                 say_color skip "skip $test_count: $1"
346                 : true
347                 ;;
348         *)
349                 false
350                 ;;
351         esac
354 test_expect_failure () {
355         test "$#" = 3 && { prereq=$1; shift; } || prereq=
356         test "$#" = 2 ||
357         error "bug in the test script: not 2 or 3 parameters to test-expect-failure"
358         if ! test_skip "$@"
359         then
360                 say >&3 "checking known breakage: $2"
361                 test_run_ "$2"
362                 if [ "$?" = 0 -a "$eval_ret" = 0 ]
363                 then
364                         test_known_broken_ok_ "$1"
365                 else
366                         test_known_broken_failure_ "$1"
367                 fi
368         fi
369         echo >&3 ""
372 test_expect_success () {
373         test "$#" = 3 && { prereq=$1; shift; } || prereq=
374         test "$#" = 2 ||
375         error "bug in the test script: not 2 or 3 parameters to test-expect-success"
376         if ! test_skip "$@"
377         then
378                 say >&3 "expecting success: $2"
379                 test_run_ "$2"
380                 if [ "$?" = 0 -a "$eval_ret" = 0 ]
381                 then
382                         test_ok_ "$1"
383                 else
384                         test_failure_ "$@"
385                 fi
386         fi
387         echo >&3 ""
390 test_expect_code () {
391         test "$#" = 4 && { prereq=$1; shift; } || prereq=
392         test "$#" = 3 ||
393         error "bug in the test script: not 3 or 4 parameters to test-expect-code"
394         if ! test_skip "$@"
395         then
396                 say >&3 "expecting exit code $1: $3"
397                 test_run_ "$3"
398                 if [ "$?" = 0 -a "$eval_ret" = "$1" ]
399                 then
400                         test_ok_ "$2"
401                 else
402                         test_failure_ "$@"
403                 fi
404         fi
405         echo >&3 ""
408 # test_external runs external test scripts that provide continuous
409 # test output about their progress, and succeeds/fails on
410 # zero/non-zero exit code.  It outputs the test output on stdout even
411 # in non-verbose mode, and announces the external script with "* run
412 # <n>: ..." before running it.  When providing relative paths, keep in
413 # mind that all scripts run in "trash directory".
414 # Usage: test_external description command arguments...
415 # Example: test_external 'Perl API' perl ../path/to/test.pl
416 test_external () {
417         test "$#" = 4 && { prereq=$1; shift; } || prereq=
418         test "$#" = 3 ||
419         error >&5 "bug in the test script: not 3 or 4 parameters to test_external"
420         descr="$1"
421         shift
422         if ! test_skip "$descr" "$@"
423         then
424                 # Announce the script to reduce confusion about the
425                 # test output that follows.
426                 say_color "" " run $test_count: $descr ($*)"
427                 # Run command; redirect its stderr to &4 as in
428                 # test_run_, but keep its stdout on our stdout even in
429                 # non-verbose mode.
430                 "$@" 2>&4
431                 if [ "$?" = 0 ]
432                 then
433                         test_ok_ "$descr"
434                 else
435                         test_failure_ "$descr" "$@"
436                 fi
437         fi
440 # Like test_external, but in addition tests that the command generated
441 # no output on stderr.
442 test_external_without_stderr () {
443         # The temporary file has no (and must have no) security
444         # implications.
445         tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
446         stderr="$tmp/git-external-stderr.$$.tmp"
447         test_external "$@" 4> "$stderr"
448         [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
449         descr="no stderr: $1"
450         shift
451         say >&3 "expecting no stderr from previous command"
452         if [ ! -s "$stderr" ]; then
453                 rm "$stderr"
454                 test_ok_ "$descr"
455         else
456                 if [ "$verbose" = t ]; then
457                         output=`echo; echo Stderr is:; cat "$stderr"`
458                 else
459                         output=
460                 fi
461                 # rm first in case test_failure exits.
462                 rm "$stderr"
463                 test_failure_ "$descr" "$@" "$output"
464         fi
467 # This is not among top-level (test_expect_success | test_expect_failure)
468 # but is a prefix that can be used in the test script, like:
470 #       test_expect_success 'complain and die' '
471 #           do something &&
472 #           do something else &&
473 #           test_must_fail git checkout ../outerspace
474 #       '
476 # Writing this as "! git checkout ../outerspace" is wrong, because
477 # the failure could be due to a segv.  We want a controlled failure.
479 test_must_fail () {
480         "$@"
481         test $? -gt 0 -a $? -le 129 -o $? -gt 192
484 # test_cmp is a helper function to compare actual and expected output.
485 # You can use it like:
487 #       test_expect_success 'foo works' '
488 #               echo expected >expected &&
489 #               foo >actual &&
490 #               test_cmp expected actual
491 #       '
493 # This could be written as either "cmp" or "diff -u", but:
494 # - cmp's output is not nearly as easy to read as diff -u
495 # - not all diff versions understand "-u"
497 test_cmp() {
498         $GIT_TEST_CMP "$@"
501 # Most tests can use the created repository, but some may need to create more.
502 # Usage: test_create_repo <directory>
503 test_create_repo () {
504         test "$#" = 1 ||
505         error "bug in the test script: not 1 parameter to test-create-repo"
506         owd=`pwd`
507         repo="$1"
508         mkdir -p "$repo"
509         cd "$repo" || error "Cannot setup test environment"
510         "$GIT_EXEC_PATH/git-init" "--template=$TEST_DIRECTORY/../templates/blt/" >&3 2>&4 ||
511         error "cannot run git init -- have you built things yet?"
512         mv .git/hooks .git/hooks-disabled
513         cd "$owd"
516 test_done () {
517         GIT_EXIT_OK=t
518         test_results_dir="$TEST_DIRECTORY/test-results"
519         mkdir -p "$test_results_dir"
520         test_results_path="$test_results_dir/${0%.sh}-$$"
522         echo "total $test_count" >> $test_results_path
523         echo "success $test_success" >> $test_results_path
524         echo "fixed $test_fixed" >> $test_results_path
525         echo "broken $test_broken" >> $test_results_path
526         echo "failed $test_failure" >> $test_results_path
527         echo "" >> $test_results_path
529         if test "$test_fixed" != 0
530         then
531                 say_color pass "fixed $test_fixed known breakage(s)"
532         fi
533         if test "$test_broken" != 0
534         then
535                 say_color error "still have $test_broken known breakage(s)"
536                 msg="remaining $(($test_count-$test_broken)) test(s)"
537         else
538                 msg="$test_count test(s)"
539         fi
540         case "$test_failure" in
541         0)
542                 say_color pass "passed all $msg"
544                 test -d "$remove_trash" &&
545                 cd "$(dirname "$remove_trash")" &&
546                 rm -rf "$(basename "$remove_trash")"
548                 exit 0 ;;
550         *)
551                 say_color error "failed $test_failure among $msg"
552                 exit 1 ;;
554         esac
557 # Test the binaries we have just built.  The tests are kept in
558 # t/ subdirectory and are run in 'trash directory' subdirectory.
559 TEST_DIRECTORY=$(pwd)
560 if test -z "$valgrind"
561 then
562         if test -z "$GIT_TEST_INSTALLED"
563         then
564                 PATH=$TEST_DIRECTORY/..:$PATH
565                 GIT_EXEC_PATH=$TEST_DIRECTORY/..
566         else
567                 GIT_EXEC_PATH=$($GIT_TEST_INSTALLED/git --exec-path)  ||
568                 error "Cannot run git from $GIT_TEST_INSTALLED."
569                 PATH=$GIT_TEST_INSTALLED:$TEST_DIRECTORY/..:$PATH
570                 GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH}
571         fi
572 else
573         make_symlink () {
574                 test -h "$2" &&
575                 test "$1" = "$(readlink "$2")" || {
576                         # be super paranoid
577                         if mkdir "$2".lock
578                         then
579                                 rm -f "$2" &&
580                                 ln -s "$1" "$2" &&
581                                 rm -r "$2".lock
582                         else
583                                 while test -d "$2".lock
584                                 do
585                                         say "Waiting for lock on $2."
586                                         sleep 1
587                                 done
588                         fi
589                 }
590         }
592         make_valgrind_symlink () {
593                 # handle only executables
594                 test -x "$1" || return
596                 base=$(basename "$1")
597                 symlink_target=$TEST_DIRECTORY/../$base
598                 # do not override scripts
599                 if test -x "$symlink_target" &&
600                     test ! -d "$symlink_target" &&
601                     test "#!" != "$(head -c 2 < "$symlink_target")"
602                 then
603                         symlink_target=../valgrind.sh
604                 fi
605                 case "$base" in
606                 *.sh|*.perl)
607                         symlink_target=../unprocessed-script
608                 esac
609                 # create the link, or replace it if it is out of date
610                 make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
611         }
613         # override all git executables in TEST_DIRECTORY/..
614         GIT_VALGRIND=$TEST_DIRECTORY/valgrind
615         mkdir -p "$GIT_VALGRIND"/bin
616         for file in $TEST_DIRECTORY/../git* $TEST_DIRECTORY/../test-*
617         do
618                 make_valgrind_symlink $file
619         done
620         OLDIFS=$IFS
621         IFS=:
622         for path in $PATH
623         do
624                 ls "$path"/git-* 2> /dev/null |
625                 while read file
626                 do
627                         make_valgrind_symlink "$file"
628                 done
629         done
630         IFS=$OLDIFS
631         PATH=$GIT_VALGRIND/bin:$PATH
632         GIT_EXEC_PATH=$GIT_VALGRIND/bin
633         export GIT_VALGRIND
634 fi
635 GIT_TEMPLATE_DIR=$(pwd)/../templates/blt
636 unset GIT_CONFIG
637 GIT_CONFIG_NOSYSTEM=1
638 GIT_CONFIG_NOGLOBAL=1
639 export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_CONFIG_NOGLOBAL
641 GITPERLLIB=$(pwd)/../perl/blib/lib:$(pwd)/../perl/blib/arch/auto/Git
642 export GITPERLLIB
643 test -d ../templates/blt || {
644         error "You haven't built things yet, have you?"
647 if ! test -x ../test-chmtime; then
648         echo >&2 'You need to build test-chmtime:'
649         echo >&2 'Run "make test-chmtime" in the source (toplevel) directory'
650         exit 1
651 fi
653 . ../GIT-BUILD-OPTIONS
655 # Test repository
656 test="trash directory.$(basename "$0" .sh)"
657 test -n "$root" && test="$root/$test"
658 case "$test" in
659 /*) TRASH_DIRECTORY="$test" ;;
660  *) TRASH_DIRECTORY="$TEST_DIRECTORY/$test" ;;
661 esac
662 test ! -z "$debug" || remove_trash=$TRASH_DIRECTORY
663 rm -fr "$test" || {
664         GIT_EXIT_OK=t
665         echo >&5 "FATAL: Cannot prepare test area"
666         exit 1
669 test_create_repo "$test"
670 # Use -P to resolve symlinks in our working directory so that the cwd
671 # in subprocesses like git equals our $PWD (for pathname comparisons).
672 cd -P "$test" || exit 1
674 this_test=${0##*/}
675 this_test=${this_test%%-*}
676 for skp in $GIT_SKIP_TESTS
677 do
678         to_skip=
679         for skp in $GIT_SKIP_TESTS
680         do
681                 case "$this_test" in
682                 $skp)
683                         to_skip=t
684                 esac
685         done
686         case "$to_skip" in
687         t)
688                 say_color skip >&3 "skipping test $this_test altogether"
689                 say_color skip "skip all tests in $this_test"
690                 test_done
691         esac
692 done
694 # Provide an implementation of the 'yes' utility
695 yes () {
696         if test $# = 0
697         then
698                 y=y
699         else
700                 y="$*"
701         fi
703         while echo "$y"
704         do
705                 :
706         done
709 # Fix some commands on Windows
710 case $(uname -s) in
711 *MINGW*)
712         # Windows has its own (incompatible) sort and find
713         sort () {
714                 /usr/bin/sort "$@"
715         }
716         find () {
717                 /usr/bin/find "$@"
718         }
719         sum () {
720                 md5sum "$@"
721         }
722         # git sees Windows-style pwd
723         pwd () {
724                 builtin pwd -W
725         }
726         # no POSIX permissions
727         # backslashes in pathspec are converted to '/'
728         # exec does not inherit the PID
729         ;;
730 *)
731         test_set_prereq POSIXPERM
732         test_set_prereq BSLASHPSPEC
733         test_set_prereq EXECKEEPSPID
734         ;;
735 esac
737 test -z "$NO_PERL" && test_set_prereq PERL
739 # test whether the filesystem supports symbolic links
740 ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS
741 rm -f y