[PATCH] Trapping exit in tests, using return for errors
[git/git.git] / t / test-lib.sh
CommitLineData
e1970ce4
JH
1#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano
4#
5
6# For repeatability, reset the environment to known value.
7LANG=C
d9bdd39e 8PAGER=cat
e1970ce4 9TZ=UTC
d9bdd39e 10export LANG PAGER TZ
e1970ce4
JH
11unset AUTHOR_DATE
12unset AUTHOR_EMAIL
13unset AUTHOR_NAME
14unset COMMIT_AUTHOR_EMAIL
15unset COMMIT_AUTHOR_NAME
16unset GIT_ALTERNATE_OBJECT_DIRECTORIES
17unset GIT_AUTHOR_DATE
18unset GIT_AUTHOR_EMAIL
19unset GIT_AUTHOR_NAME
20unset GIT_COMMITTER_EMAIL
21unset GIT_COMMITTER_NAME
22unset GIT_DIFF_OPTS
23unset GIT_DIR
24unset GIT_EXTERNAL_DIFF
25unset GIT_INDEX_FILE
26unset GIT_OBJECT_DIRECTORY
27unset SHA1_FILE_DIRECTORIES
28unset SHA1_FILE_DIRECTORY
29
30# Each test should start with something like this, after copyright notices:
31#
32# test_description='Description of this test...
33# This test checks if command xyzzy does the right thing...
34# '
35# . ./test-lib.sh
36
37error () {
38 echo "* error: $*"
39 exit 1
40}
41
42say () {
43 echo "* $*"
44}
45
46test "${test_description}" != "" ||
47error "Test script did not set test_description."
48
49while test "$#" -ne 0
50do
51 case "$1" in
52 -d|--d|--de|--deb|--debu|--debug)
53 debug=t; shift ;;
886856ab
JH
54 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
55 immediate=t; shift ;;
e1970ce4
JH
56 -h|--h|--he|--hel|--help)
57 echo "$test_description"
58 exit 0 ;;
59 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
60 verbose=t; shift ;;
61 *)
62 break ;;
63 esac
64done
65
4d9d62fa 66exec 5>&1
e1970ce4
JH
67if test "$verbose" = "t"
68then
69 exec 4>&2 3>&1
70else
71 exec 4>/dev/null 3>/dev/null
72fi
73
74test_failure=0
75test_count=0
76
e1970ce4 77
886856ab
JH
78# You are not expected to call test_ok_ and test_failure_ directly, use
79# the text_expect_* functions instead.
80
81test_ok_ () {
e1970ce4 82 test_count=$(expr "$test_count" + 1)
94e8afa2 83 say " ok $test_count: $@"
e1970ce4
JH
84}
85
886856ab 86test_failure_ () {
e1970ce4
JH
87 test_count=$(expr "$test_count" + 1)
88 test_failure=$(expr "$test_failure" + 1);
bf0dd8a8
JH
89 say "FAIL $test_count: $1"
90 shift
91 echo "$@" | sed -e 's/^/ /'
8e832ebc 92 test "$immediate" = "" || exit 1
886856ab
JH
93}
94
95
96test_debug () {
8e832ebc 97 test "$debug" = "" || eval "$1"
e1970ce4
JH
98}
99
4d9d62fa
PR
100test_run_ () {
101 trap 'echo >&5 "FATAL: Unexpected exit with code $?"; exit 1' exit
102 eval >&3 2>&4 "$1"
103 eval_ret="$?"
104 trap - exit
105 return 0
106}
107
e1970ce4 108test_expect_failure () {
8e832ebc 109 test "$#" = 2 ||
e1970ce4
JH
110 error "bug in the test script: not 2 parameters to test-expect-failure"
111 say >&3 "expecting failure: $2"
4d9d62fa
PR
112 test_run_ "$2"
113 if [ "$?" = 0 -a "$eval_ret" != 0 ]
e1970ce4 114 then
886856ab 115 test_ok_ "$1"
4d9d62fa
PR
116 else
117 test_failure_ "$@"
e1970ce4
JH
118 fi
119}
120
121test_expect_success () {
8e832ebc 122 test "$#" = 2 ||
e1970ce4
JH
123 error "bug in the test script: not 2 parameters to test-expect-success"
124 say >&3 "expecting success: $2"
4d9d62fa
PR
125 test_run_ "$2"
126 if [ "$?" = 0 -a "$eval_ret" = 0 ]
e1970ce4 127 then
886856ab 128 test_ok_ "$1"
e1970ce4 129 else
886856ab 130 test_failure_ "$@"
e1970ce4
JH
131 fi
132}
133
134test_done () {
135 case "$test_failure" in
136 0)
137 # We could:
138 # cd .. && rm -fr trash
139 # but that means we forbid any tests that use their own
140 # subdirectory from calling test_done without coming back
141 # to where they started from.
142 # The Makefile provided will clean this test area so
143 # we will leave things as they are.
144
145 say "passed all $test_count test(s)"
146 exit 0 ;;
147
148 *)
149 say "failed $test_failure among $test_count test(s)"
150 exit 1 ;;
151
152 esac
153}
154
155# Test the binaries we have just built. The tests are kept in
156# t/ subdirectory and are run in trash subdirectory.
157PATH=$(pwd)/..:$PATH
158
159# Test repository
160test=trash
161rm -fr "$test"
162mkdir "$test"
163cd "$test"
164git-init-db 2>/dev/null || error "cannot run git-init-db"