]> Tony Duckles's Git Repositories (git.nynim.org) - svn2svn.git/blob - tests/test-lib.sh
Fix typo in comment
[svn2svn.git] / tests / test-lib.sh
1 #!/bin/sh
2
3 # if --tee was passed, write the output not only to the terminal, but
4 # additionally to the file test-results/$BASENAME.out, too.
5 case "$SVN2SVN_TEST_TEE_STARTED, $* " in
6 done,*)
7 # do not redirect again
8 ;;
9 *' --tee '*|*' --va'*)
10 mkdir -p test-results
11 BASE=test-results/$(basename "$0" .sh)
12 (SVN2SVN_TEST_TEE_STARTED=done ${SHELL-sh} "$0" "$@" 2>&1;
13 echo $? > $BASE.exit) | tee $BASE.out
14 test "$(cat $BASE.exit)" = 0
15 exit
16 ;;
17 esac
18
19 # Check if terminal supports color
20 [ "x$TERM" != "xdumb" ] && (
21 [ -t 1 ] &&
22 tput bold >/dev/null 2>&1 &&
23 tput setaf 1 >/dev/null 2>&1 &&
24 tput sgr0 >/dev/null 2>&1
25 ) &&
26 color=t
27
28 # Handle test options
29 while test "$#" -ne 0
30 do
31 case "$1" in
32 -d|--d|--de|--deb|--debu|--debug)
33 debug=t; shift ;;
34 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
35 immediate=t; shift ;;
36 -h|--h|--he|--hel|--help)
37 help=t; shift ;;
38 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
39 verbose=t; shift ;;
40 -q|--q|--qu|--qui|--quie|--quiet)
41 quiet=t; shift ;;
42 --no-color)
43 color=; shift ;;
44 --tee)
45 shift ;; # was handled already
46 *)
47 echo "error: unknown test option '$1'" exit 1 ;;
48 esac
49 done
50
51 # Define print-helper tags
52 if test -n "$color"; then
53 say_color () {
54 (
55 case "$1" in
56 error) tput bold; tput setaf 1;; # bold red
57 skip) tput bold; tput setaf 2;; # bold green
58 pass) tput setaf 2;; # green
59 info) tput setaf 3;; # brown
60 *) test -n "$quiet" && return;;
61 esac
62 shift
63 printf "%s" "$*"
64 tput sgr0
65 echo
66 )
67 }
68 else
69 say_color() {
70 test -z "$1" && test -n "$quiet" && return
71 shift
72 echo "$*"
73 }
74 fi
75
76 error () {
77 say_color error "error: $*"
78 SVN2SVN_EXIT_OK=t
79 exit 1
80 }
81
82 say () {
83 say_color info "$*"
84 }
85
86 # Make sure parent test was setup correctly
87 test "${test_description}" != "" ||
88 error "Test script did not set test_description."
89
90 # Handle --help
91 if test "$help" = "t"
92 then
93 echo "$test_description"
94 exit 0
95 fi
96
97 exec 5>&1
98 exec 6<&0
99 if test "$verbose" = "t"
100 then
101 exec 4>&2 3>&1
102 else
103 exec 4>/dev/null 3>/dev/null
104 fi
105
106 test_failure=0
107 test_count=0
108 test_fixed=0
109 test_broken=0
110 test_success=0
111
112 test_external_has_tap=0
113
114 die () {
115 code=$?
116 if test -n "$SVN2SVN_EXIT_OK"
117 then
118 exit $code
119 else
120 echo >&5 "FATAL: Unexpected exit with code $code"
121 exit 1
122 fi
123 }
124
125 SVN2SVN_EXIT_OK=
126 trap 'die' EXIT
127
128 . "${TEST_DIRECTORY:-.}"/test-lib-functions.sh
129
130 # You are not expected to call test_ok_ and test_failure_ directly, use
131 # the text_expect_* functions instead.
132
133 test_ok_ () {
134 test_success=$(($test_success + 1))
135 say_color "" "ok $test_count - $@"
136 }
137
138 test_failure_ () {
139 test_failure=$(($test_failure + 1))
140 say_color error "not ok - $test_count $1"
141 shift
142 echo "$@" | sed -e 's/^/# /'
143 test "$immediate" = "" || { SVN2SVN_EXIT_OK=t; exit 1; }
144 }
145
146 test_known_broken_ok_ () {
147 test_fixed=$(($test_fixed+1))
148 say_color "" "ok $test_count - $@ # TODO known breakage"
149 }
150
151 test_known_broken_failure_ () {
152 test_broken=$(($test_broken+1))
153 say_color skip "not ok $test_count - $@ # TODO known breakage"
154 }
155
156 test_debug () {
157 test "$debug" = "" || eval "$1"
158 }
159
160 test_eval_ () {
161 # This is a separate function because some tests use
162 # "return" to end a test_expect_success block early.
163 eval </dev/null >&3 2>&4 "$*"
164 }
165
166 test_run_ () {
167 test_cleanup=:
168 expecting_failure=$2
169 test_eval_ "$1"
170 eval_ret=$?
171
172 if test -z "$immediate" || test $eval_ret = 0 || test -n "$expecting_failure"
173 then
174 test_eval_ "$test_cleanup"
175 fi
176 if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"; then
177 echo ""
178 fi
179 return "$eval_ret"
180 }
181
182 test_skip () {
183 test_count=$(($test_count+1))
184 to_skip=
185 for skp in $SVN2SVN_SKIP_TESTS
186 do
187 case $this_test.$test_count in
188 $skp)
189 to_skip=t
190 break
191 esac
192 done
193 if test -z "$to_skip" && test -n "$test_prereq" &&
194 ! test_have_prereq "$test_prereq"
195 then
196 to_skip=t
197 fi
198 case "$to_skip" in
199 t)
200 of_prereq=
201 if test "$missing_prereq" != "$test_prereq"
202 then
203 of_prereq=" of $test_prereq"
204 fi
205
206 say_color skip >&3 "skipping test: $@"
207 say_color skip "ok $test_count # skip $1 (missing $missing_prereq${of_prereq})"
208 : true
209 ;;
210 *)
211 false
212 ;;
213 esac
214 }
215
216 # stub
217 test_at_end_hook_ () {
218 :
219 }
220
221 test_done () {
222 SVN2SVN_EXIT_OK=t
223
224 if test -z "$HARNESS_ACTIVE"; then
225 test_results_dir="$TEST_OUTPUT_DIRECTORY/test-results"
226 mkdir -p "$test_results_dir"
227 test_results_path="$test_results_dir/${0%.sh}-$$.counts"
228
229 cat >>"$test_results_path" <<-EOF
230 total $test_count
231 success $test_success
232 fixed $test_fixed
233 broken $test_broken
234 failed $test_failure
235
236 EOF
237 fi
238
239 if test "$test_fixed" != 0
240 then
241 say_color pass "# fixed $test_fixed known breakage(s)"
242 fi
243 if test "$test_broken" != 0
244 then
245 say_color error "# still have $test_broken known breakage(s)"
246 msg="remaining $(($test_count-$test_broken)) test(s)"
247 else
248 msg="$test_count test(s)"
249 fi
250 case "$test_failure" in
251 0)
252 # Maybe print SKIP message
253 [ -z "$skip_all" ] || skip_all=" # SKIP $skip_all"
254
255 if test $test_external_has_tap -eq 0; then
256 say_color pass "# passed all $msg"
257 say "1..$test_count$skip_all"
258 fi
259
260 test -d "$remove_trash" &&
261 cd "$(dirname "$remove_trash")" &&
262 rm -rf "$(basename "$remove_trash")"
263
264 test_at_end_hook_
265
266 exit 0 ;;
267
268 *)
269 if test $test_external_has_tap -eq 0; then
270 say_color error "# failed $test_failure among $msg"
271 say "1..$test_count"
272 fi
273
274 exit 1 ;;
275
276 esac
277 }
278
279 # Test the binaries we have just built. The tests are kept in
280 # t/ subdirectory and are run in 'trash directory' subdirectory.
281 if test -z "$TEST_DIRECTORY"
282 then
283 # We allow tests to override this, in case they want to run tests
284 # outside of t/, e.g. for running tests on the test library
285 # itself.
286 TEST_DIRECTORY=$(pwd)
287 fi
288 if test -z "$TEST_OUTPUT_DIRECTORY"
289 then
290 # Similarly, override this to store the test-results subdir
291 # elsewhere
292 TEST_OUTPUT_DIRECTORY=$TEST_DIRECTORY
293 fi
294