]> git.ipfire.org Git - thirdparty/git.git/blame - t/test-lib.sh
Merge branch 'db/fetch-pack'
[thirdparty/git.git] / t / test-lib.sh
CommitLineData
e1970ce4
JH
1#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano
4#
5
6# For repeatability, reset the environment to known value.
7LANG=C
899460f3 8LC_ALL=C
d9bdd39e 9PAGER=cat
e1970ce4 10TZ=UTC
899460f3 11export LANG LC_ALL PAGER TZ
8ff99e74
EW
12EDITOR=:
13VISUAL=:
18508c39 14unset GIT_EDITOR
e1970ce4
JH
15unset AUTHOR_DATE
16unset AUTHOR_EMAIL
17unset AUTHOR_NAME
18unset COMMIT_AUTHOR_EMAIL
19unset COMMIT_AUTHOR_NAME
28a94f88 20unset EMAIL
e1970ce4
JH
21unset GIT_ALTERNATE_OBJECT_DIRECTORIES
22unset GIT_AUTHOR_DATE
29e55cd5
JH
23GIT_AUTHOR_EMAIL=author@example.com
24GIT_AUTHOR_NAME='A U Thor'
6677c466 25unset GIT_COMMITTER_DATE
29e55cd5
JH
26GIT_COMMITTER_EMAIL=committer@example.com
27GIT_COMMITTER_NAME='C O Mitter'
e1970ce4
JH
28unset GIT_DIFF_OPTS
29unset GIT_DIR
892c41b9 30unset GIT_WORK_TREE
e1970ce4
JH
31unset GIT_EXTERNAL_DIFF
32unset GIT_INDEX_FILE
33unset GIT_OBJECT_DIRECTORY
34unset SHA1_FILE_DIRECTORIES
35unset SHA1_FILE_DIRECTORY
8d0fc48f
SP
36GIT_MERGE_VERBOSITY=5
37export GIT_MERGE_VERBOSITY
29e55cd5
JH
38export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
39export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
8ff99e74 40export EDITOR VISUAL
e1970ce4 41
886a3907
JH
42# Protect ourselves from common misconfiguration to export
43# CDPATH into the environment
44unset CDPATH
45
3d5c0cc9 46case $(echo $GIT_TRACE |tr "[A-Z]" "[a-z]") in
6ce4e61f
CC
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 ! *"
52 ;;
53esac
54
e1970ce4
JH
55# Each test should start with something like this, after copyright notices:
56#
57# test_description='Description of this test...
58# This test checks if command xyzzy does the right thing...
59# '
60# . ./test-lib.sh
61
62error () {
63 echo "* error: $*"
41184273 64 trap - exit
e1970ce4
JH
65 exit 1
66}
67
68say () {
69 echo "* $*"
70}
71
72test "${test_description}" != "" ||
73error "Test script did not set test_description."
74
75while test "$#" -ne 0
76do
77 case "$1" in
78 -d|--d|--de|--deb|--debu|--debug)
79 debug=t; shift ;;
886856ab
JH
80 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
81 immediate=t; shift ;;
e1970ce4
JH
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 ;;
abb7c7b3 87 --no-python)
7cdbff14
JH
88 # noop now...
89 shift ;;
e1970ce4
JH
90 *)
91 break ;;
92 esac
93done
94
4d9d62fa 95exec 5>&1
e1970ce4
JH
96if test "$verbose" = "t"
97then
98 exec 4>&2 3>&1
99else
100 exec 4>/dev/null 3>/dev/null
101fi
102
103test_failure=0
104test_count=0
105
41184273
PR
106trap 'echo >&5 "FATAL: Unexpected exit with code $?"; exit 1' exit
107
04509738
JH
108test_tick () {
109 if test -z "${test_tick+set}"
110 then
21afc41c 111 test_tick=1112911993
04509738
JH
112 else
113 test_tick=$(($test_tick + 60))
114 fi
21afc41c
JH
115 GIT_COMMITTER_DATE="$test_tick -0700"
116 GIT_AUTHOR_DATE="$test_tick -0700"
04509738
JH
117 export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
118}
e1970ce4 119
886856ab
JH
120# You are not expected to call test_ok_ and test_failure_ directly, use
121# the text_expect_* functions instead.
122
123test_ok_ () {
e1970ce4 124 test_count=$(expr "$test_count" + 1)
94e8afa2 125 say " ok $test_count: $@"
e1970ce4
JH
126}
127
886856ab 128test_failure_ () {
e1970ce4
JH
129 test_count=$(expr "$test_count" + 1)
130 test_failure=$(expr "$test_failure" + 1);
bf0dd8a8
JH
131 say "FAIL $test_count: $1"
132 shift
133 echo "$@" | sed -e 's/^/ /'
41184273 134 test "$immediate" = "" || { trap - exit; exit 1; }
886856ab
JH
135}
136
137
138test_debug () {
8e832ebc 139 test "$debug" = "" || eval "$1"
e1970ce4
JH
140}
141
4d9d62fa 142test_run_ () {
4d9d62fa
PR
143 eval >&3 2>&4 "$1"
144 eval_ret="$?"
4d9d62fa
PR
145 return 0
146}
147
04ece593
JH
148test_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
153 do
154 case "$this_test" in
155 $skp)
156 to_skip=t
157 esac
158 done
159 case "$to_skip" in
160 t)
161 say >&3 "skipping test: $@"
162 test_count=$(expr "$test_count" + 1)
163 say "skip $test_count: $1"
164 : true
165 ;;
166 *)
167 false
168 ;;
169 esac
170}
171
e1970ce4 172test_expect_failure () {
8e832ebc 173 test "$#" = 2 ||
e1970ce4 174 error "bug in the test script: not 2 parameters to test-expect-failure"
04ece593 175 if ! test_skip "$@"
e1970ce4 176 then
04ece593
JH
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_ "$@"
184 fi
e1970ce4 185 fi
7a0cf2d0 186 echo >&3 ""
e1970ce4
JH
187}
188
189test_expect_success () {
8e832ebc 190 test "$#" = 2 ||
e1970ce4 191 error "bug in the test script: not 2 parameters to test-expect-success"
04ece593 192 if ! test_skip "$@"
e1970ce4 193 then
04ece593
JH
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_ "$@"
201 fi
e1970ce4 202 fi
7a0cf2d0 203 echo >&3 ""
e1970ce4
JH
204}
205
d3bfdb75
FK
206test_expect_code () {
207 test "$#" = 3 ||
208 error "bug in the test script: not 3 parameters to test-expect-code"
04ece593 209 if ! test_skip "$@"
d3bfdb75 210 then
04ece593
JH
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_ "$@"
218 fi
d3bfdb75 219 fi
7a0cf2d0 220 echo >&3 ""
d3bfdb75
FK
221}
222
eedf8f97
CW
223# Most tests can use the created repository, but some amy need to create more.
224# Usage: test_create_repo <directory>
225test_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"
5c94f87e
NP
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?"
eedf8f97
CW
234 mv .git/hooks .git/hooks-disabled
235 cd "$owd"
236}
a6080a0a 237
e1970ce4 238test_done () {
41184273 239 trap - exit
e1970ce4 240 case "$test_failure" in
10b94e28 241 0)
e1970ce4
JH
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.
249
250 say "passed all $test_count test(s)"
251 exit 0 ;;
252
253 *)
254 say "failed $test_failure among $test_count test(s)"
255 exit 1 ;;
256
257 esac
258}
259
260# Test the binaries we have just built. The tests are kept in
261# t/ subdirectory and are run in trash subdirectory.
262PATH=$(pwd)/..:$PATH
a0fa2a10 263GIT_EXEC_PATH=$(pwd)/..
8683a45d 264GIT_TEMPLATE_DIR=$(pwd)/../templates/blt
8565d2d8
JS
265GIT_CONFIG=.git/config
266export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG
49ccb087 267
6fcca938
PB
268GITPERLLIB=$(pwd)/../perl/blib/lib:$(pwd)/../perl/blib/arch/auto/Git
269export GITPERLLIB
eea42069
JH
270test -d ../templates/blt || {
271 error "You haven't built things yet, have you?"
272}
e1970ce4 273
56cf9806
EW
274if ! 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
278fi
279
e1970ce4
JH
280# Test repository
281test=trash
282rm -fr "$test"
eedf8f97
CW
283test_create_repo $test
284cd "$test"
04ece593
JH
285
286this_test=$(expr "./$0" : '.*/\(t[0-9]*\)-[^/]*$')
287for skp in $GIT_SKIP_TESTS
288do
289 to_skip=
290 for skp in $GIT_SKIP_TESTS
291 do
292 case "$this_test" in
293 $skp)
294 to_skip=t
295 esac
296 done
297 case "$to_skip" in
298 t)
299 say >&3 "skipping test $this_test altogether"
300 say "skip all tests in $this_test"
301 test_done
302 esac
303done