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