git-tag: update usage string and documentation.
[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: $*"
41184273 39 trap - exit
e1970ce4
JH
40 exit 1
41}
42
43say () {
44 echo "* $*"
45}
46
47test "${test_description}" != "" ||
48error "Test script did not set test_description."
49
50while test "$#" -ne 0
51do
52 case "$1" in
53 -d|--d|--de|--deb|--debu|--debug)
54 debug=t; shift ;;
886856ab
JH
55 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
56 immediate=t; shift ;;
e1970ce4
JH
57 -h|--h|--he|--hel|--help)
58 echo "$test_description"
59 exit 0 ;;
60 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
61 verbose=t; shift ;;
62 *)
63 break ;;
64 esac
65done
66
4d9d62fa 67exec 5>&1
e1970ce4
JH
68if test "$verbose" = "t"
69then
70 exec 4>&2 3>&1
71else
72 exec 4>/dev/null 3>/dev/null
73fi
74
75test_failure=0
76test_count=0
77
41184273
PR
78trap 'echo >&5 "FATAL: Unexpected exit with code $?"; exit 1' exit
79
e1970ce4 80
886856ab
JH
81# You are not expected to call test_ok_ and test_failure_ directly, use
82# the text_expect_* functions instead.
83
84test_ok_ () {
e1970ce4 85 test_count=$(expr "$test_count" + 1)
94e8afa2 86 say " ok $test_count: $@"
e1970ce4
JH
87}
88
886856ab 89test_failure_ () {
e1970ce4
JH
90 test_count=$(expr "$test_count" + 1)
91 test_failure=$(expr "$test_failure" + 1);
bf0dd8a8
JH
92 say "FAIL $test_count: $1"
93 shift
94 echo "$@" | sed -e 's/^/ /'
41184273 95 test "$immediate" = "" || { trap - exit; exit 1; }
886856ab
JH
96}
97
98
99test_debug () {
8e832ebc 100 test "$debug" = "" || eval "$1"
e1970ce4
JH
101}
102
4d9d62fa 103test_run_ () {
4d9d62fa
PR
104 eval >&3 2>&4 "$1"
105 eval_ret="$?"
4d9d62fa
PR
106 return 0
107}
108
e1970ce4 109test_expect_failure () {
8e832ebc 110 test "$#" = 2 ||
e1970ce4
JH
111 error "bug in the test script: not 2 parameters to test-expect-failure"
112 say >&3 "expecting failure: $2"
4d9d62fa
PR
113 test_run_ "$2"
114 if [ "$?" = 0 -a "$eval_ret" != 0 ]
e1970ce4 115 then
886856ab 116 test_ok_ "$1"
4d9d62fa
PR
117 else
118 test_failure_ "$@"
e1970ce4
JH
119 fi
120}
121
122test_expect_success () {
8e832ebc 123 test "$#" = 2 ||
e1970ce4
JH
124 error "bug in the test script: not 2 parameters to test-expect-success"
125 say >&3 "expecting success: $2"
4d9d62fa
PR
126 test_run_ "$2"
127 if [ "$?" = 0 -a "$eval_ret" = 0 ]
e1970ce4 128 then
886856ab 129 test_ok_ "$1"
e1970ce4 130 else
886856ab 131 test_failure_ "$@"
e1970ce4
JH
132 fi
133}
134
135test_done () {
41184273 136 trap - exit
e1970ce4
JH
137 case "$test_failure" in
138 0)
139 # We could:
140 # cd .. && rm -fr trash
141 # but that means we forbid any tests that use their own
142 # subdirectory from calling test_done without coming back
143 # to where they started from.
144 # The Makefile provided will clean this test area so
145 # we will leave things as they are.
146
147 say "passed all $test_count test(s)"
148 exit 0 ;;
149
150 *)
151 say "failed $test_failure among $test_count test(s)"
152 exit 1 ;;
153
154 esac
155}
156
157# Test the binaries we have just built. The tests are kept in
158# t/ subdirectory and are run in trash subdirectory.
159PATH=$(pwd)/..:$PATH
160
161# Test repository
162test=trash
163rm -fr "$test"
164mkdir "$test"
165cd "$test"
166git-init-db 2>/dev/null || error "cannot run git-init-db"