Code

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