Merge branch 'master' of .
[git/gitweb.git] / t / test-lib.sh
blob1523d2ebbf1f355fe60ad0d5ef948d24cebc85ad
1 #!/bin/sh
3 # Copyright (c) 2005 Junio C Hamano
6 # For repeatability, reset the environment to known value.
7 LANG=C
8 PAGER=cat
9 TZ=UTC
10 export LANG PAGER TZ
11 unset AUTHOR_DATE
12 unset AUTHOR_EMAIL
13 unset AUTHOR_NAME
14 unset COMMIT_AUTHOR_EMAIL
15 unset COMMIT_AUTHOR_NAME
16 unset GIT_ALTERNATE_OBJECT_DIRECTORIES
17 unset GIT_AUTHOR_DATE
18 unset GIT_AUTHOR_EMAIL
19 unset GIT_AUTHOR_NAME
20 unset GIT_COMMITTER_EMAIL
21 unset GIT_COMMITTER_NAME
22 unset GIT_DIFF_OPTS
23 unset GIT_DIR
24 unset GIT_EXTERNAL_DIFF
25 unset GIT_INDEX_FILE
26 unset GIT_OBJECT_DIRECTORY
27 unset SHA1_FILE_DIRECTORIES
28 unset SHA1_FILE_DIRECTORY
30 # Each test should start with something like this, after copyright notices:
32 # test_description='Description of this test...
33 # This test checks if command xyzzy does the right thing...
34 # '
35 # . ./test-lib.sh
37 error () {
38 echo "* error: $*"
39 trap - exit
40 exit 1
43 say () {
44 echo "* $*"
47 test "${test_description}" != "" ||
48 error "Test script did not set test_description."
50 while test "$#" -ne 0
52 case "$1" in
53 -d|--d|--de|--deb|--debu|--debug)
54 debug=t; shift ;;
55 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
56 immediate=t; shift ;;
57 -h|--h|--he|--hel|--help)
58 echo "$test_description"
59 exit 0 ;;
60 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
61 verbose=t; shift ;;
63 break ;;
64 esac
65 done
67 exec 5>&1
68 if test "$verbose" = "t"
69 then
70 exec 4>&2 3>&1
71 else
72 exec 4>/dev/null 3>/dev/null
75 test_failure=0
76 test_count=0
78 trap 'echo >&5 "FATAL: Unexpected exit with code $?"; exit 1' exit
81 # You are not expected to call test_ok_ and test_failure_ directly, use
82 # the text_expect_* functions instead.
84 test_ok_ () {
85 test_count=$(expr "$test_count" + 1)
86 say " ok $test_count: $@"
89 test_failure_ () {
90 test_count=$(expr "$test_count" + 1)
91 test_failure=$(expr "$test_failure" + 1);
92 say "FAIL $test_count: $1"
93 shift
94 echo "$@" | sed -e 's/^/ /'
95 test "$immediate" = "" || { trap - exit; exit 1; }
99 test_debug () {
100 test "$debug" = "" || eval "$1"
103 test_run_ () {
104 eval >&3 2>&4 "$1"
105 eval_ret="$?"
106 return 0
109 test_expect_failure () {
110 test "$#" = 2 ||
111 error "bug in the test script: not 2 parameters to test-expect-failure"
112 say >&3 "expecting failure: $2"
113 test_run_ "$2"
114 if [ "$?" = 0 -a "$eval_ret" != 0 ]
115 then
116 test_ok_ "$1"
117 else
118 test_failure_ "$@"
122 test_expect_success () {
123 test "$#" = 2 ||
124 error "bug in the test script: not 2 parameters to test-expect-success"
125 say >&3 "expecting success: $2"
126 test_run_ "$2"
127 if [ "$?" = 0 -a "$eval_ret" = 0 ]
128 then
129 test_ok_ "$1"
130 else
131 test_failure_ "$@"
135 test_done () {
136 trap - exit
137 case "$test_failure" in
139 # We could:
140 # cd .. && rm -fr trash
141 # but that means we forbid any tests that use their own
142 # subdirectory from calling test_done without coming back
143 # to where they started from.
144 # The Makefile provided will clean this test area so
145 # we will leave things as they are.
147 say "passed all $test_count test(s)"
148 exit 0 ;;
151 say "failed $test_failure among $test_count test(s)"
152 exit 1 ;;
154 esac
157 # Test the binaries we have just built. The tests are kept in
158 # t/ subdirectory and are run in trash subdirectory.
159 PATH=$(pwd)/..:$PATH
161 # Test repository
162 test=trash
163 rm -fr "$test"
164 mkdir "$test"
165 cd "$test"
166 git-init-db 2>/dev/null || error "cannot run git-init-db"