Code

c3d3984b33f4fb31d228e262a54a4b121e90d4b2
[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 unset AUTHOR_DATE
13 unset AUTHOR_EMAIL
14 unset AUTHOR_NAME
15 unset COMMIT_AUTHOR_EMAIL
16 unset COMMIT_AUTHOR_NAME
17 unset GIT_ALTERNATE_OBJECT_DIRECTORIES
18 unset GIT_AUTHOR_DATE
19 GIT_AUTHOR_EMAIL=author@example.com
20 GIT_AUTHOR_NAME='A U Thor'
21 unset GIT_COMMITTER_DATE
22 GIT_COMMITTER_EMAIL=committer@example.com
23 GIT_COMMITTER_NAME='C O Mitter'
24 unset GIT_DIFF_OPTS
25 unset GIT_DIR
26 unset GIT_EXTERNAL_DIFF
27 unset GIT_INDEX_FILE
28 unset GIT_OBJECT_DIRECTORY
29 unset GIT_TRACE
30 unset SHA1_FILE_DIRECTORIES
31 unset SHA1_FILE_DIRECTORY
32 export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
33 export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
35 # Each test should start with something like this, after copyright notices:
36 #
37 # test_description='Description of this test...
38 # This test checks if command xyzzy does the right thing...
39 # '
40 # . ./test-lib.sh
42 error () {
43         echo "* error: $*"
44         trap - exit
45         exit 1
46 }
48 say () {
49         echo "* $*"
50 }
52 test "${test_description}" != "" ||
53 error "Test script did not set test_description."
55 while test "$#" -ne 0
56 do
57         case "$1" in
58         -d|--d|--de|--deb|--debu|--debug)
59                 debug=t; shift ;;
60         -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
61                 immediate=t; shift ;;
62         -h|--h|--he|--hel|--help)
63                 echo "$test_description"
64                 exit 0 ;;
65         -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
66                 verbose=t; shift ;;
67         --no-python)
68                 no_python=t; shift ;;
69         *)
70                 break ;;
71         esac
72 done
74 exec 5>&1
75 if test "$verbose" = "t"
76 then
77         exec 4>&2 3>&1
78 else
79         exec 4>/dev/null 3>/dev/null
80 fi
82 test_failure=0
83 test_count=0
85 trap 'echo >&5 "FATAL: Unexpected exit with code $?"; exit 1' exit
88 # You are not expected to call test_ok_ and test_failure_ directly, use
89 # the text_expect_* functions instead.
91 test_ok_ () {
92         test_count=$(expr "$test_count" + 1)
93         say "  ok $test_count: $@"
94 }
96 test_failure_ () {
97         test_count=$(expr "$test_count" + 1)
98         test_failure=$(expr "$test_failure" + 1);
99         say "FAIL $test_count: $1"
100         shift
101         echo "$@" | sed -e 's/^/        /'
102         test "$immediate" = "" || { trap - exit; exit 1; }
106 test_debug () {
107         test "$debug" = "" || eval "$1"
110 test_run_ () {
111         eval >&3 2>&4 "$1"
112         eval_ret="$?"
113         return 0
116 test_expect_failure () {
117         test "$#" = 2 ||
118         error "bug in the test script: not 2 parameters to test-expect-failure"
119         say >&3 "expecting failure: $2"
120         test_run_ "$2"
121         if [ "$?" = 0 -a "$eval_ret" != 0 ]
122         then
123                 test_ok_ "$1"
124         else
125                 test_failure_ "$@"
126         fi
129 test_expect_success () {
130         test "$#" = 2 ||
131         error "bug in the test script: not 2 parameters to test-expect-success"
132         say >&3 "expecting success: $2"
133         test_run_ "$2"
134         if [ "$?" = 0 -a "$eval_ret" = 0 ]
135         then
136                 test_ok_ "$1"
137         else
138                 test_failure_ "$@"
139         fi
142 test_expect_code () {
143         test "$#" = 3 ||
144         error "bug in the test script: not 3 parameters to test-expect-code"
145         say >&3 "expecting exit code $1: $3"
146         test_run_ "$3"
147         if [ "$?" = 0 -a "$eval_ret" = "$1" ]
148         then
149                 test_ok_ "$2"
150         else
151                 test_failure_ "$@"
152         fi
155 # Most tests can use the created repository, but some amy need to create more.
156 # Usage: test_create_repo <directory>
157 test_create_repo () {
158         test "$#" = 1 ||
159         error "bug in the test script: not 1 parameter to test-create-repo"
160         owd=`pwd`
161         repo="$1"
162         mkdir "$repo"
163         cd "$repo" || error "Cannot setup test environment"
164         "$GIT_EXEC_PATH/git" init-db --template=$GIT_EXEC_PATH/templates/blt/ 2>/dev/null ||
165         error "cannot run git init-db -- have you built things yet?"
166         mv .git/hooks .git/hooks-disabled
167         cd "$owd"
169         
170 test_done () {
171         trap - exit
172         case "$test_failure" in
173         0)
174                 # We could:
175                 # cd .. && rm -fr trash
176                 # but that means we forbid any tests that use their own
177                 # subdirectory from calling test_done without coming back
178                 # to where they started from.
179                 # The Makefile provided will clean this test area so
180                 # we will leave things as they are.
182                 say "passed all $test_count test(s)"
183                 exit 0 ;;
185         *)
186                 say "failed $test_failure among $test_count test(s)"
187                 exit 1 ;;
189         esac
192 # Test the binaries we have just built.  The tests are kept in
193 # t/ subdirectory and are run in trash subdirectory.
194 PATH=$(pwd)/..:$PATH
195 GIT_EXEC_PATH=$(pwd)/..
196 export PATH GIT_EXEC_PATH
198 # Similarly use ../compat/subprocess.py if our python does not
199 # have subprocess.py on its own.
200 PYTHON=`sed -e '1{
201         s/^#!//
202         q
203 }' ../git-merge-recursive` || {
204         error "You haven't built things yet, have you?"
206 "$PYTHON" -c 'import subprocess' 2>/dev/null || {
207         PYTHONPATH=$(pwd)/../compat
208         export PYTHONPATH
210 test -d ../templates/blt || {
211         error "You haven't built things yet, have you?"
214 # Test repository
215 test=trash
216 rm -fr "$test"
217 test_create_repo $test
218 cd "$test"