Hide the test_create_repo output
[stgit] / t / test-lib.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Junio C Hamano
4 # Copyright (c) 2006 Yann Dirson - tuning for stgit
5 #
6
7 # For repeatability, reset the environment to known value.
8 LANG=C
9 LC_ALL=C
10 PAGER=cat
11 TZ=UTC
12 export LANG LC_ALL PAGER TZ
13 unset AUTHOR_DATE
14 unset AUTHOR_EMAIL
15 unset AUTHOR_NAME
16 unset COMMIT_AUTHOR_EMAIL
17 unset COMMIT_AUTHOR_NAME
18 unset GIT_ALTERNATE_OBJECT_DIRECTORIES
19 unset GIT_AUTHOR_DATE
20 GIT_AUTHOR_EMAIL=author@example.com
21 GIT_AUTHOR_NAME='A U Thor'
22 unset GIT_COMMITTER_DATE
23 GIT_COMMITTER_EMAIL=committer@example.com
24 GIT_COMMITTER_NAME='C O Mitter'
25 unset GIT_DIFF_OPTS
26 unset GIT_DIR
27 unset GIT_EXTERNAL_DIFF
28 unset GIT_INDEX_FILE
29 unset GIT_OBJECT_DIRECTORY
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
34
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
41
42 error () {
43 echo "* error: $*"
44 trap - exit
45 exit 1
46 }
47
48 say () {
49 echo "* $*"
50 }
51
52 test "${test_description}" != "" ||
53 error "Test script did not set test_description."
54
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 *)
68 break ;;
69 esac
70 done
71
72 exec 5>&1
73 if test "$verbose" = "t"
74 then
75 exec 4>&2 3>&1
76 else
77 exec 4>/dev/null 3>/dev/null
78 fi
79
80 test_failure=0
81 test_count=0
82
83 trap 'echo >&5 "FATAL: Unexpected exit with code $?"; exit 1' exit
84
85
86 # You are not expected to call test_ok_ and test_failure_ directly, use
87 # the text_expect_* functions instead.
88
89 test_ok_ () {
90 test_count=$(expr "$test_count" + 1)
91 say " ok $test_count: $@"
92 }
93
94 test_failure_ () {
95 test_count=$(expr "$test_count" + 1)
96 test_failure=$(expr "$test_failure" + 1);
97 say "FAIL $test_count: $1"
98 shift
99 echo "$@" | sed -e 's/^/ /'
100 test "$immediate" = "" || { trap - exit; exit 1; }
101 }
102
103
104 test_debug () {
105 test "$debug" = "" || eval "$1"
106 }
107
108 test_run_ () {
109 eval >&3 2>&4 "$1"
110 eval_ret="$?"
111 return 0
112 }
113
114 test_expect_failure () {
115 test "$#" = 2 ||
116 error "bug in the test script: not 2 parameters to test-expect-failure"
117 say >&3 "expecting failure: $2"
118 test_run_ "$2"
119 if [ "$?" = 0 -a "$eval_ret" != 0 ]
120 then
121 test_ok_ "$1"
122 else
123 test_failure_ "$@"
124 fi
125 }
126
127 test_expect_success () {
128 test "$#" = 2 ||
129 error "bug in the test script: not 2 parameters to test-expect-success"
130 say >&3 "expecting success: $2"
131 test_run_ "$2"
132 if [ "$?" = 0 -a "$eval_ret" = 0 ]
133 then
134 test_ok_ "$1"
135 else
136 test_failure_ "$@"
137 fi
138 }
139
140 test_expect_code () {
141 test "$#" = 3 ||
142 error "bug in the test script: not 3 parameters to test-expect-code"
143 say >&3 "expecting exit code $1: $3"
144 test_run_ "$3"
145 if [ "$?" = 0 -a "$eval_ret" = "$1" ]
146 then
147 test_ok_ "$2"
148 else
149 test_failure_ "$@"
150 fi
151 }
152
153 # Most tests can use the created repository, but some amy need to create more.
154 # Usage: test_create_repo <directory>
155 test_create_repo () {
156 test "$#" = 1 ||
157 error "bug in the test script: not 1 parameter to test-create-repo"
158 owd=`pwd`
159 repo="$1"
160 mkdir "$repo"
161 cd "$repo" || error "Cannot setup test environment"
162 git-init-db >&3 2>&4 ||
163 error "cannot run git-init-db -- have you installed git-core?"
164 mv .git/hooks .git/hooks-disabled
165 echo "empty start" |
166 git-commit-tree `git-write-tree` >.git/refs/heads/master 2>&4 ||
167 error "cannot run git-commit -- is your git-core funtionning?"
168 cd "$owd"
169 }
170
171 test_done () {
172 trap - exit
173 case "$test_failure" in
174 0)
175 # We could:
176 # cd .. && rm -fr trash
177 # but that means we forbid any tests that use their own
178 # subdirectory from calling test_done without coming back
179 # to where they started from.
180 # The Makefile provided will clean this test area so
181 # we will leave things as they are.
182
183 say "passed all $test_count test(s)"
184 exit 0 ;;
185
186 *)
187 say "failed $test_failure among $test_count test(s)"
188 exit 1 ;;
189
190 esac
191 }
192
193 # Test the binaries we have just built. The tests are kept in
194 # t/ subdirectory and are run in trash subdirectory.
195 PATH=$(pwd)/..:$PATH
196 HOME=$(pwd)/trash
197 export PATH HOME
198
199
200 # Test repository
201 test=trash
202 rm -fr "$test"
203 test_create_repo $test
204 cd "$test"