test-lib: Simplify test counting.
[git] / t / test-lib.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Junio C Hamano
4 #
5
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
21
22 # Keep the original TERM for say_color
23 ORIGINAL_TERM=$TERM
24
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 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 VISUAL
62 GIT_TEST_CMP=${GIT_TEST_CMP:-diff -u}
63
64 # Protect ourselves from common misconfiguration to export
65 # CDPATH into the environment
66 unset CDPATH
67
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
76
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
92
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         --no-color)
109                 color=; shift ;;
110         --no-python)
111                 # noop now...
112                 shift ;;
113         --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
114                 valgrind=t; verbose=t; shift ;;
115         --tee)
116                 shift ;; # was handled already
117         *)
118                 break ;;
119         esac
120 done
121
122 if test -n "$color"; then
123         say_color () {
124                 (
125                 TERM=$ORIGINAL_TERM
126                 export TERM
127                 case "$1" in
128                         error) tput bold; tput setaf 1;; # bold red
129                         skip)  tput bold; tput setaf 2;; # bold green
130                         pass)  tput setaf 2;;            # green
131                         info)  tput setaf 3;;            # brown
132                         *) test -n "$quiet" && return;;
133                 esac
134                 shift
135                 printf "* %s" "$*"
136                 tput sgr0
137                 echo
138                 )
139         }
140 else
141         say_color() {
142                 test -z "$1" && test -n "$quiet" && return
143                 shift
144                 echo "* $*"
145         }
146 fi
147
148 error () {
149         say_color error "error: $*"
150         trap - EXIT
151         exit 1
152 }
153
154 say () {
155         say_color info "$*"
156 }
157
158 test "${test_description}" != "" ||
159 error "Test script did not set test_description."
160
161 if test "$help" = "t"
162 then
163         echo "$test_description"
164         exit 0
165 fi
166
167 exec 5>&1
168 if test "$verbose" = "t"
169 then
170         exec 4>&2 3>&1
171 else
172         exec 4>/dev/null 3>/dev/null
173 fi
174
175 test_failure=0
176 test_count=0
177 test_fixed=0
178 test_broken=0
179 test_success=0
180
181 die () {
182         echo >&5 "FATAL: Unexpected exit with code $?"
183         exit 1
184 }
185
186 trap 'die' EXIT
187
188 # The semantics of the editor variables are that of invoking
189 # sh -c "$EDITOR \"$@\"" files ...
190 #
191 # If our trash directory contains shell metacharacters, they will be
192 # interpreted if we just set $EDITOR directly, so do a little dance with
193 # environment variables to work around this.
194 #
195 # In particular, quoting isn't enough, as the path may contain the same quote
196 # that we're using.
197 test_set_editor () {
198         FAKE_EDITOR="$1"
199         export FAKE_EDITOR
200         VISUAL='"$FAKE_EDITOR"'
201         export VISUAL
202 }
203
204 test_tick () {
205         if test -z "${test_tick+set}"
206         then
207                 test_tick=1112911993
208         else
209                 test_tick=$(($test_tick + 60))
210         fi
211         GIT_COMMITTER_DATE="$test_tick -0700"
212         GIT_AUTHOR_DATE="$test_tick -0700"
213         export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
214 }
215
216 # Call test_commit with the arguments "<message> [<file> [<contents>]]"
217 #
218 # This will commit a file with the given contents and the given commit
219 # message.  It will also add a tag with <message> as name.
220 #
221 # Both <file> and <contents> default to <message>.
222
223 test_commit () {
224         file=${2:-"$1.t"}
225         echo "${3-$1}" > "$file" &&
226         git add "$file" &&
227         test_tick &&
228         git commit -m "$1" &&
229         git tag "$1"
230 }
231
232 # Call test_merge with the arguments "<message> <commit>", where <commit>
233 # can be a tag pointing to the commit-to-merge.
234
235 test_merge () {
236         test_tick &&
237         git merge -m "$1" "$2" &&
238         git tag "$1"
239 }
240
241 # You are not expected to call test_ok_ and test_failure_ directly, use
242 # the text_expect_* functions instead.
243
244 test_ok_ () {
245         test_success=$(($test_success + 1))
246         say_color "" "  ok $test_count: $@"
247 }
248
249 test_failure_ () {
250         test_failure=$(($test_failure + 1))
251         say_color error "FAIL $test_count: $1"
252         shift
253         echo "$@" | sed -e 's/^/        /'
254         test "$immediate" = "" || { trap - EXIT; exit 1; }
255 }
256
257 test_known_broken_ok_ () {
258         test_fixed=$(($test_fixed+1))
259         say_color "" "  FIXED $test_count: $@"
260 }
261
262 test_known_broken_failure_ () {
263         test_broken=$(($test_broken+1))
264         say_color skip "  still broken $test_count: $@"
265 }
266
267 test_debug () {
268         test "$debug" = "" || eval "$1"
269 }
270
271 test_run_ () {
272         eval >&3 2>&4 "$1"
273         eval_ret="$?"
274         return 0
275 }
276
277 test_skip () {
278         test_count=$(($test_count+1))
279         to_skip=
280         for skp in $GIT_SKIP_TESTS
281         do
282                 case $this_test.$test_count in
283                 $skp)
284                         to_skip=t
285                 esac
286         done
287         case "$to_skip" in
288         t)
289                 say_color skip >&3 "skipping test: $@"
290                 say_color skip "skip $test_count: $1"
291                 : true
292                 ;;
293         *)
294                 false
295                 ;;
296         esac
297 }
298
299 test_expect_failure () {
300         test "$#" = 2 ||
301         error "bug in the test script: not 2 parameters to test-expect-failure"
302         if ! test_skip "$@"
303         then
304                 say >&3 "checking known breakage: $2"
305                 test_run_ "$2"
306                 if [ "$?" = 0 -a "$eval_ret" = 0 ]
307                 then
308                         test_known_broken_ok_ "$1"
309                 else
310                     test_known_broken_failure_ "$1"
311                 fi
312         fi
313         echo >&3 ""
314 }
315
316 test_expect_success () {
317         test "$#" = 2 ||
318         error "bug in the test script: not 2 parameters to test-expect-success"
319         if ! test_skip "$@"
320         then
321                 say >&3 "expecting success: $2"
322                 test_run_ "$2"
323                 if [ "$?" = 0 -a "$eval_ret" = 0 ]
324                 then
325                         test_ok_ "$1"
326                 else
327                         test_failure_ "$@"
328                 fi
329         fi
330         echo >&3 ""
331 }
332
333 test_expect_code () {
334         test "$#" = 3 ||
335         error "bug in the test script: not 3 parameters to test-expect-code"
336         if ! test_skip "$@"
337         then
338                 say >&3 "expecting exit code $1: $3"
339                 test_run_ "$3"
340                 if [ "$?" = 0 -a "$eval_ret" = "$1" ]
341                 then
342                         test_ok_ "$2"
343                 else
344                         test_failure_ "$@"
345                 fi
346         fi
347         echo >&3 ""
348 }
349
350 # test_external runs external test scripts that provide continuous
351 # test output about their progress, and succeeds/fails on
352 # zero/non-zero exit code.  It outputs the test output on stdout even
353 # in non-verbose mode, and announces the external script with "* run
354 # <n>: ..." before running it.  When providing relative paths, keep in
355 # mind that all scripts run in "trash directory".
356 # Usage: test_external description command arguments...
357 # Example: test_external 'Perl API' perl ../path/to/test.pl
358 test_external () {
359         test "$#" -eq 3 ||
360         error >&5 "bug in the test script: not 3 parameters to test_external"
361         descr="$1"
362         shift
363         if ! test_skip "$descr" "$@"
364         then
365                 # Announce the script to reduce confusion about the
366                 # test output that follows.
367                 say_color "" " run $test_count: $descr ($*)"
368                 # Run command; redirect its stderr to &4 as in
369                 # test_run_, but keep its stdout on our stdout even in
370                 # non-verbose mode.
371                 "$@" 2>&4
372                 if [ "$?" = 0 ]
373                 then
374                         test_ok_ "$descr"
375                 else
376                         test_failure_ "$descr" "$@"
377                 fi
378         fi
379 }
380
381 # Like test_external, but in addition tests that the command generated
382 # no output on stderr.
383 test_external_without_stderr () {
384         # The temporary file has no (and must have no) security
385         # implications.
386         tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
387         stderr="$tmp/git-external-stderr.$$.tmp"
388         test_external "$@" 4> "$stderr"
389         [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
390         descr="no stderr: $1"
391         shift
392         say >&3 "expecting no stderr from previous command"
393         if [ ! -s "$stderr" ]; then
394                 rm "$stderr"
395                 test_ok_ "$descr"
396         else
397                 if [ "$verbose" = t ]; then
398                         output=`echo; echo Stderr is:; cat "$stderr"`
399                 else
400                         output=
401                 fi
402                 # rm first in case test_failure exits.
403                 rm "$stderr"
404                 test_failure_ "$descr" "$@" "$output"
405         fi
406 }
407
408 # This is not among top-level (test_expect_success | test_expect_failure)
409 # but is a prefix that can be used in the test script, like:
410 #
411 #       test_expect_success 'complain and die' '
412 #           do something &&
413 #           do something else &&
414 #           test_must_fail git checkout ../outerspace
415 #       '
416 #
417 # Writing this as "! git checkout ../outerspace" is wrong, because
418 # the failure could be due to a segv.  We want a controlled failure.
419
420 test_must_fail () {
421         "$@"
422         test $? -gt 0 -a $? -le 129 -o $? -gt 192
423 }
424
425 # test_cmp is a helper function to compare actual and expected output.
426 # You can use it like:
427 #
428 #       test_expect_success 'foo works' '
429 #               echo expected >expected &&
430 #               foo >actual &&
431 #               test_cmp expected actual
432 #       '
433 #
434 # This could be written as either "cmp" or "diff -u", but:
435 # - cmp's output is not nearly as easy to read as diff -u
436 # - not all diff versions understand "-u"
437
438 test_cmp() {
439         $GIT_TEST_CMP "$@"
440 }
441
442 # Most tests can use the created repository, but some may need to create more.
443 # Usage: test_create_repo <directory>
444 test_create_repo () {
445         test "$#" = 1 ||
446         error "bug in the test script: not 1 parameter to test-create-repo"
447         owd=`pwd`
448         repo="$1"
449         mkdir -p "$repo"
450         cd "$repo" || error "Cannot setup test environment"
451         "$GIT_EXEC_PATH/git" init "--template=$owd/../templates/blt/" >&3 2>&4 ||
452         error "cannot run git init -- have you built things yet?"
453         mv .git/hooks .git/hooks-disabled
454         cd "$owd"
455 }
456
457 test_done () {
458         trap - EXIT
459         test_results_dir="$TEST_DIRECTORY/test-results"
460         mkdir -p "$test_results_dir"
461         test_results_path="$test_results_dir/${0%.sh}-$$"
462
463         echo "total $test_count" >> $test_results_path
464         echo "success $test_success" >> $test_results_path
465         echo "fixed $test_fixed" >> $test_results_path
466         echo "broken $test_broken" >> $test_results_path
467         echo "failed $test_failure" >> $test_results_path
468         echo "" >> $test_results_path
469
470         if test "$test_fixed" != 0
471         then
472                 say_color pass "fixed $test_fixed known breakage(s)"
473         fi
474         if test "$test_broken" != 0
475         then
476                 say_color error "still have $test_broken known breakage(s)"
477                 msg="remaining $(($test_count-$test_broken)) test(s)"
478         else
479                 msg="$test_count test(s)"
480         fi
481         case "$test_failure" in
482         0)
483                 # We could:
484                 # cd .. && rm -fr 'trash directory'
485                 # but that means we forbid any tests that use their own
486                 # subdirectory from calling test_done without coming back
487                 # to where they started from.
488                 # The Makefile provided will clean this test area so
489                 # we will leave things as they are.
490
491                 say_color pass "passed all $msg"
492
493                 test -d "$remove_trash" &&
494                 cd "$(dirname "$remove_trash")" &&
495                 rm -rf "$(basename "$remove_trash")"
496
497                 exit 0 ;;
498
499         *)
500                 say_color error "failed $test_failure among $msg"
501                 exit 1 ;;
502
503         esac
504 }
505
506 # Test the binaries we have just built.  The tests are kept in
507 # t/ subdirectory and are run in 'trash directory' subdirectory.
508 TEST_DIRECTORY=$(pwd)
509 if test -z "$valgrind"
510 then
511         PATH=$TEST_DIRECTORY/..:$PATH
512         GIT_EXEC_PATH=$TEST_DIRECTORY/..
513 else
514         make_symlink () {
515                 test -h "$2" &&
516                 test "$1" = "$(readlink "$2")" || {
517                         # be super paranoid
518                         if mkdir "$2".lock
519                         then
520                                 rm -f "$2" &&
521                                 ln -s "$1" "$2" &&
522                                 rm -r "$2".lock
523                         else
524                                 while test -d "$2".lock
525                                 do
526                                         say "Waiting for lock on $2."
527                                         sleep 1
528                                 done
529                         fi
530                 }
531         }
532
533         make_valgrind_symlink () {
534                 # handle only executables
535                 test -x "$1" || return
536
537                 base=$(basename "$1")
538                 symlink_target=$TEST_DIRECTORY/../$base
539                 # do not override scripts
540                 if test -x "$symlink_target" &&
541                     test ! -d "$symlink_target" &&
542                     test "#!" != "$(head -c 2 < "$symlink_target")"
543                 then
544                         symlink_target=../valgrind.sh
545                 fi
546                 case "$base" in
547                 *.sh|*.perl)
548                         symlink_target=../unprocessed-script
549                 esac
550                 # create the link, or replace it if it is out of date
551                 make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
552         }
553
554         # override all git executables in TEST_DIRECTORY/..
555         GIT_VALGRIND=$TEST_DIRECTORY/valgrind
556         mkdir -p "$GIT_VALGRIND"/bin
557         for file in $TEST_DIRECTORY/../git* $TEST_DIRECTORY/../test-*
558         do
559                 make_valgrind_symlink $file
560         done
561         OLDIFS=$IFS
562         IFS=:
563         for path in $PATH
564         do
565                 ls "$path"/git-* 2> /dev/null |
566                 while read file
567                 do
568                         make_valgrind_symlink "$file"
569                 done
570         done
571         IFS=$OLDIFS
572         PATH=$GIT_VALGRIND/bin:$PATH
573         GIT_EXEC_PATH=$GIT_VALGRIND/bin
574         export GIT_VALGRIND
575 fi
576 GIT_TEMPLATE_DIR=$(pwd)/../templates/blt
577 unset GIT_CONFIG
578 GIT_CONFIG_NOSYSTEM=1
579 GIT_CONFIG_NOGLOBAL=1
580 export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_CONFIG_NOGLOBAL
581
582 GITPERLLIB=$(pwd)/../perl/blib/lib:$(pwd)/../perl/blib/arch/auto/Git
583 export GITPERLLIB
584 test -d ../templates/blt || {
585         error "You haven't built things yet, have you?"
586 }
587
588 if ! test -x ../test-chmtime; then
589         echo >&2 'You need to build test-chmtime:'
590         echo >&2 'Run "make test-chmtime" in the source (toplevel) directory'
591         exit 1
592 fi
593
594 . ../GIT-BUILD-OPTIONS
595
596 # Test repository
597 test="trash directory.$(basename "$0" .sh)"
598 test ! -z "$debug" || remove_trash="$TEST_DIRECTORY/$test"
599 rm -fr "$test" || {
600         trap - EXIT
601         echo >&5 "FATAL: Cannot prepare test area"
602         exit 1
603 }
604
605 test_create_repo "$test"
606 # Use -P to resolve symlinks in our working directory so that the cwd
607 # in subprocesses like git equals our $PWD (for pathname comparisons).
608 cd -P "$test" || exit 1
609
610 this_test=${0##*/}
611 this_test=${this_test%%-*}
612 for skp in $GIT_SKIP_TESTS
613 do
614         to_skip=
615         for skp in $GIT_SKIP_TESTS
616         do
617                 case "$this_test" in
618                 $skp)
619                         to_skip=t
620                 esac
621         done
622         case "$to_skip" in
623         t)
624                 say_color skip >&3 "skipping test $this_test altogether"
625                 say_color skip "skip all tests in $this_test"
626                 test_done
627         esac
628 done