git-svn: fix log with single revision against a non-HEAD branch
[git/dscho.git] / t / test-lib.sh
blobcc1253ccabf6afe0a3903f9f15177f73e8e33183
1 #!/bin/sh
3 # Copyright (c) 2005 Junio C Hamano
6 # For repeatability, reset the environment to known value.
7 LANG=C
8 LC_ALL=C
9 PAGER=cat
10 TZ=UTC
11 export LANG LC_ALL PAGER TZ
12 EDITOR=:
13 VISUAL=:
14 unset GIT_EDITOR
15 unset AUTHOR_DATE
16 unset AUTHOR_EMAIL
17 unset AUTHOR_NAME
18 unset COMMIT_AUTHOR_EMAIL
19 unset COMMIT_AUTHOR_NAME
20 unset EMAIL
21 unset GIT_ALTERNATE_OBJECT_DIRECTORIES
22 unset GIT_AUTHOR_DATE
23 GIT_AUTHOR_EMAIL=author@example.com
24 GIT_AUTHOR_NAME='A U Thor'
25 unset GIT_COMMITTER_DATE
26 GIT_COMMITTER_EMAIL=committer@example.com
27 GIT_COMMITTER_NAME='C O Mitter'
28 unset GIT_DIFF_OPTS
29 unset GIT_DIR
30 unset GIT_WORK_TREE
31 unset GIT_EXTERNAL_DIFF
32 unset GIT_INDEX_FILE
33 unset GIT_OBJECT_DIRECTORY
34 unset SHA1_FILE_DIRECTORIES
35 unset SHA1_FILE_DIRECTORY
36 GIT_MERGE_VERBOSITY=5
37 export GIT_MERGE_VERBOSITY
38 export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
39 export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
40 export EDITOR VISUAL
42 # Protect ourselves from common misconfiguration to export
43 # CDPATH into the environment
44 unset CDPATH
46 case $(echo $GIT_TRACE |tr "[A-Z]" "[a-z]") in
47 1|2|true)
48 echo "* warning: Some tests will not work if GIT_TRACE" \
49 "is set as to trace on STDERR ! *"
50 echo "* warning: Please set GIT_TRACE to something" \
51 "other than 1, 2 or true ! *"
53 esac
55 # Each test should start with something like this, after copyright notices:
57 # test_description='Description of this test...
58 # This test checks if command xyzzy does the right thing...
59 # '
60 # . ./test-lib.sh
62 error () {
63 echo "* error: $*"
64 trap - exit
65 exit 1
68 say () {
69 echo "* $*"
72 test "${test_description}" != "" ||
73 error "Test script did not set test_description."
75 while test "$#" -ne 0
77 case "$1" in
78 -d|--d|--de|--deb|--debu|--debug)
79 debug=t; shift ;;
80 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
81 immediate=t; shift ;;
82 -h|--h|--he|--hel|--help)
83 echo "$test_description"
84 exit 0 ;;
85 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
86 verbose=t; shift ;;
87 --no-python)
88 # noop now...
89 shift ;;
91 break ;;
92 esac
93 done
95 exec 5>&1
96 if test "$verbose" = "t"
97 then
98 exec 4>&2 3>&1
99 else
100 exec 4>/dev/null 3>/dev/null
103 test_failure=0
104 test_count=0
106 trap 'echo >&5 "FATAL: Unexpected exit with code $?"; exit 1' exit
108 test_tick () {
109 if test -z "${test_tick+set}"
110 then
111 test_tick=1112911993
112 else
113 test_tick=$(($test_tick + 60))
115 GIT_COMMITTER_DATE="$test_tick -0700"
116 GIT_AUTHOR_DATE="$test_tick -0700"
117 export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
120 # You are not expected to call test_ok_ and test_failure_ directly, use
121 # the text_expect_* functions instead.
123 test_ok_ () {
124 test_count=$(expr "$test_count" + 1)
125 say " ok $test_count: $@"
128 test_failure_ () {
129 test_count=$(expr "$test_count" + 1)
130 test_failure=$(expr "$test_failure" + 1);
131 say "FAIL $test_count: $1"
132 shift
133 echo "$@" | sed -e 's/^/ /'
134 test "$immediate" = "" || { trap - exit; exit 1; }
138 test_debug () {
139 test "$debug" = "" || eval "$1"
142 test_run_ () {
143 eval >&3 2>&4 "$1"
144 eval_ret="$?"
145 return 0
148 test_skip () {
149 this_test=$(expr "./$0" : '.*/\(t[0-9]*\)-[^/]*$')
150 this_test="$this_test.$(expr "$test_count" + 1)"
151 to_skip=
152 for skp in $GIT_SKIP_TESTS
154 case "$this_test" in
155 $skp)
156 to_skip=t
157 esac
158 done
159 case "$to_skip" in
161 say >&3 "skipping test: $@"
162 test_count=$(expr "$test_count" + 1)
163 say "skip $test_count: $1"
164 : true
167 false
169 esac
172 test_expect_failure () {
173 test "$#" = 2 ||
174 error "bug in the test script: not 2 parameters to test-expect-failure"
175 if ! test_skip "$@"
176 then
177 say >&3 "expecting failure: $2"
178 test_run_ "$2"
179 if [ "$?" = 0 -a "$eval_ret" != 0 -a "$eval_ret" -lt 129 ]
180 then
181 test_ok_ "$1"
182 else
183 test_failure_ "$@"
186 echo >&3 ""
189 test_expect_success () {
190 test "$#" = 2 ||
191 error "bug in the test script: not 2 parameters to test-expect-success"
192 if ! test_skip "$@"
193 then
194 say >&3 "expecting success: $2"
195 test_run_ "$2"
196 if [ "$?" = 0 -a "$eval_ret" = 0 ]
197 then
198 test_ok_ "$1"
199 else
200 test_failure_ "$@"
203 echo >&3 ""
206 test_expect_code () {
207 test "$#" = 3 ||
208 error "bug in the test script: not 3 parameters to test-expect-code"
209 if ! test_skip "$@"
210 then
211 say >&3 "expecting exit code $1: $3"
212 test_run_ "$3"
213 if [ "$?" = 0 -a "$eval_ret" = "$1" ]
214 then
215 test_ok_ "$2"
216 else
217 test_failure_ "$@"
220 echo >&3 ""
223 # Most tests can use the created repository, but some amy need to create more.
224 # Usage: test_create_repo <directory>
225 test_create_repo () {
226 test "$#" = 1 ||
227 error "bug in the test script: not 1 parameter to test-create-repo"
228 owd=`pwd`
229 repo="$1"
230 mkdir "$repo"
231 cd "$repo" || error "Cannot setup test environment"
232 "$GIT_EXEC_PATH/git" init --template=$GIT_EXEC_PATH/templates/blt/ >/dev/null 2>&1 ||
233 error "cannot run git init -- have you built things yet?"
234 mv .git/hooks .git/hooks-disabled
235 cd "$owd"
238 test_done () {
239 trap - exit
240 case "$test_failure" in
242 # We could:
243 # cd .. && rm -fr trash
244 # but that means we forbid any tests that use their own
245 # subdirectory from calling test_done without coming back
246 # to where they started from.
247 # The Makefile provided will clean this test area so
248 # we will leave things as they are.
250 say "passed all $test_count test(s)"
251 exit 0 ;;
254 say "failed $test_failure among $test_count test(s)"
255 exit 1 ;;
257 esac
260 # Test the binaries we have just built. The tests are kept in
261 # t/ subdirectory and are run in trash subdirectory.
262 PATH=$(pwd)/..:$PATH
263 GIT_EXEC_PATH=$(pwd)/..
264 GIT_TEMPLATE_DIR=$(pwd)/../templates/blt
265 GIT_CONFIG=.git/config
266 export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG
268 GITPERLLIB=$(pwd)/../perl/blib/lib:$(pwd)/../perl/blib/arch/auto/Git
269 export GITPERLLIB
270 test -d ../templates/blt || {
271 error "You haven't built things yet, have you?"
274 if ! test -x ../test-chmtime; then
275 echo >&2 'You need to build test-chmtime:'
276 echo >&2 'Run "make test-chmtime" in the source (toplevel) directory'
277 exit 1
280 # Test repository
281 test=trash
282 rm -fr "$test"
283 test_create_repo $test
284 cd "$test"
286 this_test=$(expr "./$0" : '.*/\(t[0-9]*\)-[^/]*$')
287 for skp in $GIT_SKIP_TESTS
289 to_skip=
290 for skp in $GIT_SKIP_TESTS
292 case "$this_test" in
293 $skp)
294 to_skip=t
295 esac
296 done
297 case "$to_skip" in
299 say >&3 "skipping test $this_test altogether"
300 say "skip all tests in $this_test"
301 test_done
302 esac
303 done