]> asedeno.scripts.mit.edu Git - git.git/blob - t/test-lib.sh
t/test-lib.sh: add test_external and test_external_without_stderr
[git.git] / t / test-lib.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Junio C Hamano
4 #
5
6 # Keep the original TERM for say_color
7 ORIGINAL_TERM=$TERM
8
9 # For repeatability, reset the environment to known value.
10 LANG=C
11 LC_ALL=C
12 PAGER=cat
13 TZ=UTC
14 TERM=dumb
15 export LANG LC_ALL PAGER TERM TZ
16 EDITOR=:
17 VISUAL=:
18 unset GIT_EDITOR
19 unset AUTHOR_DATE
20 unset AUTHOR_EMAIL
21 unset AUTHOR_NAME
22 unset COMMIT_AUTHOR_EMAIL
23 unset COMMIT_AUTHOR_NAME
24 unset EMAIL
25 unset GIT_ALTERNATE_OBJECT_DIRECTORIES
26 unset GIT_AUTHOR_DATE
27 GIT_AUTHOR_EMAIL=author@example.com
28 GIT_AUTHOR_NAME='A U Thor'
29 unset GIT_COMMITTER_DATE
30 GIT_COMMITTER_EMAIL=committer@example.com
31 GIT_COMMITTER_NAME='C O Mitter'
32 unset GIT_DIFF_OPTS
33 unset GIT_DIR
34 unset GIT_WORK_TREE
35 unset GIT_EXTERNAL_DIFF
36 unset GIT_INDEX_FILE
37 unset GIT_OBJECT_DIRECTORY
38 unset SHA1_FILE_DIRECTORIES
39 unset SHA1_FILE_DIRECTORY
40 GIT_MERGE_VERBOSITY=5
41 export GIT_MERGE_VERBOSITY
42 export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
43 export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
44 export EDITOR VISUAL
45 GIT_TEST_CMP=${GIT_TEST_CMP:-diff -u}
46
47 # Protect ourselves from common misconfiguration to export
48 # CDPATH into the environment
49 unset CDPATH
50
51 case $(echo $GIT_TRACE |tr "[A-Z]" "[a-z]") in
52         1|2|true)
53                 echo "* warning: Some tests will not work if GIT_TRACE" \
54                         "is set as to trace on STDERR ! *"
55                 echo "* warning: Please set GIT_TRACE to something" \
56                         "other than 1, 2 or true ! *"
57                 ;;
58 esac
59
60 # Each test should start with something like this, after copyright notices:
61 #
62 # test_description='Description of this test...
63 # This test checks if command xyzzy does the right thing...
64 # '
65 # . ./test-lib.sh
66 [ "x$ORIGINAL_TERM" != "xdumb" ] && (
67                 TERM=$ORIGINAL_TERM &&
68                 export TERM &&
69                 [ -t 1 ] &&
70                 tput bold >/dev/null 2>&1 &&
71                 tput setaf 1 >/dev/null 2>&1 &&
72                 tput sgr0 >/dev/null 2>&1
73         ) &&
74         color=t
75
76 while test "$#" -ne 0
77 do
78         case "$1" in
79         -d|--d|--de|--deb|--debu|--debug)
80                 debug=t; shift ;;
81         -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
82                 immediate=t; shift ;;
83         -h|--h|--he|--hel|--help)
84                 help=t; shift ;;
85         -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
86                 verbose=t; shift ;;
87         -q|--q|--qu|--qui|--quie|--quiet)
88                 quiet=t; shift ;;
89         --no-color)
90                 color=; shift ;;
91         --no-python)
92                 # noop now...
93                 shift ;;
94         *)
95                 break ;;
96         esac
97 done
98
99 if test -n "$color"; then
100         say_color () {
101                 (
102                 TERM=$ORIGINAL_TERM
103                 export TERM
104                 case "$1" in
105                         error) tput bold; tput setaf 1;; # bold red
106                         skip)  tput bold; tput setaf 2;; # bold green
107                         pass)  tput setaf 2;;            # green
108                         info)  tput setaf 3;;            # brown
109                         *) test -n "$quiet" && return;;
110                 esac
111                 shift
112                 echo "* $*"
113                 tput sgr0
114                 )
115         }
116 else
117         say_color() {
118                 test -z "$1" && test -n "$quiet" && return
119                 shift
120                 echo "* $*"
121         }
122 fi
123
124 error () {
125         say_color error "error: $*"
126         trap - exit
127         exit 1
128 }
129
130 say () {
131         say_color info "$*"
132 }
133
134 test "${test_description}" != "" ||
135 error "Test script did not set test_description."
136
137 if test "$help" = "t"
138 then
139         echo "$test_description"
140         exit 0
141 fi
142
143 exec 5>&1
144 if test "$verbose" = "t"
145 then
146         exec 4>&2 3>&1
147 else
148         exec 4>/dev/null 3>/dev/null
149 fi
150
151 test_failure=0
152 test_count=0
153 test_fixed=0
154 test_broken=0
155
156 die () {
157         echo >&5 "FATAL: Unexpected exit with code $?"
158         exit 1
159 }
160
161 trap 'die' exit
162
163 # The semantics of the editor variables are that of invoking
164 # sh -c "$EDITOR \"$@\"" files ...
165 #
166 # If our trash directory contains shell metacharacters, they will be
167 # interpreted if we just set $EDITOR directly, so do a little dance with
168 # environment variables to work around this.
169 #
170 # In particular, quoting isn't enough, as the path may contain the same quote
171 # that we're using.
172 test_set_editor () {
173         FAKE_EDITOR="$1"
174         export FAKE_EDITOR
175         VISUAL='"$FAKE_EDITOR"'
176         export VISUAL
177 }
178
179 test_tick () {
180         if test -z "${test_tick+set}"
181         then
182                 test_tick=1112911993
183         else
184                 test_tick=$(($test_tick + 60))
185         fi
186         GIT_COMMITTER_DATE="$test_tick -0700"
187         GIT_AUTHOR_DATE="$test_tick -0700"
188         export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
189 }
190
191 # You are not expected to call test_ok_ and test_failure_ directly, use
192 # the text_expect_* functions instead.
193
194 test_ok_ () {
195         test_count=$(expr "$test_count" + 1)
196         say_color "" "  ok $test_count: $@"
197 }
198
199 test_failure_ () {
200         test_count=$(expr "$test_count" + 1)
201         test_failure=$(expr "$test_failure" + 1);
202         say_color error "FAIL $test_count: $1"
203         shift
204         echo "$@" | sed -e 's/^/        /'
205         test "$immediate" = "" || { trap - exit; exit 1; }
206 }
207
208 test_known_broken_ok_ () {
209         test_count=$(expr "$test_count" + 1)
210         test_fixed=$(($test_fixed+1))
211         say_color "" "  FIXED $test_count: $@"
212 }
213
214 test_known_broken_failure_ () {
215         test_count=$(expr "$test_count" + 1)
216         test_broken=$(($test_broken+1))
217         say_color skip "  still broken $test_count: $@"
218 }
219
220 test_debug () {
221         test "$debug" = "" || eval "$1"
222 }
223
224 test_run_ () {
225         eval >&3 2>&4 "$1"
226         eval_ret="$?"
227         return 0
228 }
229
230 test_skip () {
231         this_test=$(expr "./$0" : '.*/\(t[0-9]*\)-[^/]*$')
232         this_test="$this_test.$(expr "$test_count" + 1)"
233         to_skip=
234         for skp in $GIT_SKIP_TESTS
235         do
236                 case "$this_test" in
237                 $skp)
238                         to_skip=t
239                 esac
240         done
241         case "$to_skip" in
242         t)
243                 say_color skip >&3 "skipping test: $@"
244                 test_count=$(expr "$test_count" + 1)
245                 say_color skip "skip $test_count: $1"
246                 : true
247                 ;;
248         *)
249                 false
250                 ;;
251         esac
252 }
253
254 test_expect_failure () {
255         test "$#" = 2 ||
256         error "bug in the test script: not 2 parameters to test-expect-failure"
257         if ! test_skip "$@"
258         then
259                 say >&3 "checking known breakage: $2"
260                 test_run_ "$2"
261                 if [ "$?" = 0 -a "$eval_ret" = 0 ]
262                 then
263                         test_known_broken_ok_ "$1"
264                 else
265                     test_known_broken_failure_ "$1"
266                 fi
267         fi
268         echo >&3 ""
269 }
270
271 test_expect_success () {
272         test "$#" = 2 ||
273         error "bug in the test script: not 2 parameters to test-expect-success"
274         if ! test_skip "$@"
275         then
276                 say >&3 "expecting success: $2"
277                 test_run_ "$2"
278                 if [ "$?" = 0 -a "$eval_ret" = 0 ]
279                 then
280                         test_ok_ "$1"
281                 else
282                         test_failure_ "$@"
283                 fi
284         fi
285         echo >&3 ""
286 }
287
288 test_expect_code () {
289         test "$#" = 3 ||
290         error "bug in the test script: not 3 parameters to test-expect-code"
291         if ! test_skip "$@"
292         then
293                 say >&3 "expecting exit code $1: $3"
294                 test_run_ "$3"
295                 if [ "$?" = 0 -a "$eval_ret" = "$1" ]
296                 then
297                         test_ok_ "$2"
298                 else
299                         test_failure_ "$@"
300                 fi
301         fi
302         echo >&3 ""
303 }
304
305 # test_external runs external test scripts that provide continuous
306 # test output about their progress, and succeeds/fails on
307 # zero/non-zero exit code.  It outputs the test output on stdout even
308 # in non-verbose mode, and announces the external script with "* run
309 # <n>: ..." before running it.  When providing relative paths, keep in
310 # mind that all scripts run in "trash directory".
311 # Usage: test_external description command arguments...
312 # Example: test_external 'Perl API' perl ../path/to/test.pl
313 test_external () {
314         test "$#" -eq 3 ||
315         error >&5 "bug in the test script: not 3 parameters to test_external"
316         descr="$1"
317         shift
318         if ! test_skip "$descr" "$@"
319         then
320                 # Announce the script to reduce confusion about the
321                 # test output that follows.
322                 say_color "" " run $(expr "$test_count" + 1): $descr ($*)"
323                 # Run command; redirect its stderr to &4 as in
324                 # test_run_, but keep its stdout on our stdout even in
325                 # non-verbose mode.
326                 "$@" 2>&4
327                 if [ "$?" = 0 ]
328                 then
329                         test_ok_ "$descr"
330                 else
331                         test_failure_ "$descr" "$@"
332                 fi
333         fi
334 }
335
336 # Like test_external, but in addition tests that the command generated
337 # no output on stderr.
338 test_external_without_stderr () {
339         # The temporary file has no (and must have no) security
340         # implications.
341         tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
342         stderr="$tmp/git-external-stderr.$$.tmp"
343         test_external "$@" 4> "$stderr"
344         [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
345         descr="no stderr: $1"
346         shift
347         say >&3 "expecting no stderr from previous command"
348         if [ ! -s "$stderr" ]; then
349                 rm "$stderr"
350                 test_ok_ "$descr"
351         else
352                 if [ "$verbose" = t ]; then
353                         output=`echo; echo Stderr is:; cat "$stderr"`
354                 else
355                         output=
356                 fi
357                 # rm first in case test_failure exits.
358                 rm "$stderr"
359                 test_failure_ "$descr" "$@" "$output"
360         fi
361 }
362
363 # This is not among top-level (test_expect_success | test_expect_failure)
364 # but is a prefix that can be used in the test script, like:
365 #
366 #       test_expect_success 'complain and die' '
367 #           do something &&
368 #           do something else &&
369 #           test_must_fail git checkout ../outerspace
370 #       '
371 #
372 # Writing this as "! git checkout ../outerspace" is wrong, because
373 # the failure could be due to a segv.  We want a controlled failure.
374
375 test_must_fail () {
376         "$@"
377         test $? -gt 0 -a $? -le 129
378 }
379
380 # test_cmp is a helper function to compare actual and expected output.
381 # You can use it like:
382 #
383 #       test_expect_success 'foo works' '
384 #               echo expected >expected &&
385 #               foo >actual &&
386 #               test_cmp expected actual
387 #       '
388 #
389 # This could be written as either "cmp" or "diff -u", but:
390 # - cmp's output is not nearly as easy to read as diff -u
391 # - not all diff versions understand "-u"
392
393 test_cmp() {
394         $GIT_TEST_CMP "$@"
395 }
396
397 # Most tests can use the created repository, but some may need to create more.
398 # Usage: test_create_repo <directory>
399 test_create_repo () {
400         test "$#" = 1 ||
401         error "bug in the test script: not 1 parameter to test-create-repo"
402         owd=`pwd`
403         repo="$1"
404         mkdir "$repo"
405         cd "$repo" || error "Cannot setup test environment"
406         "$GIT_EXEC_PATH/git" init "--template=$GIT_EXEC_PATH/templates/blt/" >/dev/null 2>&1 ||
407         error "cannot run git init -- have you built things yet?"
408         mv .git/hooks .git/hooks-disabled
409         cd "$owd"
410 }
411
412 test_done () {
413         trap - exit
414
415         if test "$test_fixed" != 0
416         then
417                 say_color pass "fixed $test_fixed known breakage(s)"
418         fi
419         if test "$test_broken" != 0
420         then
421                 say_color error "still have $test_broken known breakage(s)"
422                 msg="remaining $(($test_count-$test_broken)) test(s)"
423         else
424                 msg="$test_count test(s)"
425         fi
426         case "$test_failure" in
427         0)
428                 # We could:
429                 # cd .. && rm -fr 'trash directory'
430                 # but that means we forbid any tests that use their own
431                 # subdirectory from calling test_done without coming back
432                 # to where they started from.
433                 # The Makefile provided will clean this test area so
434                 # we will leave things as they are.
435
436                 say_color pass "passed all $msg"
437                 exit 0 ;;
438
439         *)
440                 say_color error "failed $test_failure among $msg"
441                 exit 1 ;;
442
443         esac
444 }
445
446 # Test the binaries we have just built.  The tests are kept in
447 # t/ subdirectory and are run in 'trash directory' subdirectory.
448 PATH=$(pwd)/..:$PATH
449 GIT_EXEC_PATH=$(pwd)/..
450 GIT_TEMPLATE_DIR=$(pwd)/../templates/blt
451 unset GIT_CONFIG
452 unset GIT_CONFIG_LOCAL
453 GIT_CONFIG_NOSYSTEM=1
454 GIT_CONFIG_NOGLOBAL=1
455 export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_CONFIG_NOGLOBAL
456
457 GITPERLLIB=$(pwd)/../perl/blib/lib:$(pwd)/../perl/blib/arch/auto/Git
458 export GITPERLLIB
459 test -d ../templates/blt || {
460         error "You haven't built things yet, have you?"
461 }
462
463 if ! test -x ../test-chmtime; then
464         echo >&2 'You need to build test-chmtime:'
465         echo >&2 'Run "make test-chmtime" in the source (toplevel) directory'
466         exit 1
467 fi
468
469 . ../GIT-BUILD-OPTIONS
470
471 # Test repository
472 test="trash directory"
473 rm -fr "$test" || {
474         trap - exit
475         echo >&5 "FATAL: Cannot prepare test area"
476         exit 1
477 }
478
479 test_create_repo "$test"
480 # Use -P to resolve symlinks in our working directory so that the cwd
481 # in subprocesses like git equals our $PWD (for pathname comparisons).
482 cd -P "$test" || exit 1
483
484 this_test=$(expr "./$0" : '.*/\(t[0-9]*\)-[^/]*$')
485 for skp in $GIT_SKIP_TESTS
486 do
487         to_skip=
488         for skp in $GIT_SKIP_TESTS
489         do
490                 case "$this_test" in
491                 $skp)
492                         to_skip=t
493                 esac
494         done
495         case "$to_skip" in
496         t)
497                 say_color skip >&3 "skipping test $this_test altogether"
498                 say_color skip "skip all tests in $this_test"
499                 test_done
500         esac
501 done