Hide the test_create_repo output
[stgit] / t / test-lib.sh
CommitLineData
aa9b1b9d
YD
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.
8LANG=C
9LC_ALL=C
10PAGER=cat
11TZ=UTC
12export LANG LC_ALL PAGER TZ
13unset AUTHOR_DATE
14unset AUTHOR_EMAIL
15unset AUTHOR_NAME
16unset COMMIT_AUTHOR_EMAIL
17unset COMMIT_AUTHOR_NAME
18unset GIT_ALTERNATE_OBJECT_DIRECTORIES
19unset GIT_AUTHOR_DATE
20GIT_AUTHOR_EMAIL=author@example.com
21GIT_AUTHOR_NAME='A U Thor'
22unset GIT_COMMITTER_DATE
23GIT_COMMITTER_EMAIL=committer@example.com
24GIT_COMMITTER_NAME='C O Mitter'
25unset GIT_DIFF_OPTS
26unset GIT_DIR
27unset GIT_EXTERNAL_DIFF
28unset GIT_INDEX_FILE
29unset GIT_OBJECT_DIRECTORY
30unset SHA1_FILE_DIRECTORIES
31unset SHA1_FILE_DIRECTORY
32export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
33export 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
42error () {
43 echo "* error: $*"
44 trap - exit
45 exit 1
46}
47
48say () {
49 echo "* $*"
50}
51
52test "${test_description}" != "" ||
53error "Test script did not set test_description."
54
55while test "$#" -ne 0
56do
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
70done
71
72exec 5>&1
73if test "$verbose" = "t"
74then
75 exec 4>&2 3>&1
76else
77 exec 4>/dev/null 3>/dev/null
78fi
79
80test_failure=0
81test_count=0
82
83trap '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
89test_ok_ () {
90 test_count=$(expr "$test_count" + 1)
91 say " ok $test_count: $@"
92}
93
94test_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
104test_debug () {
105 test "$debug" = "" || eval "$1"
106}
107
108test_run_ () {
109 eval >&3 2>&4 "$1"
110 eval_ret="$?"
111 return 0
112}
113
114test_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
127test_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
140test_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>
155test_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"
56ba8918 162 git-init-db >&3 2>&4 ||
aa9b1b9d
YD
163 error "cannot run git-init-db -- have you installed git-core?"
164 mv .git/hooks .git/hooks-disabled
165 echo "empty start" |
56ba8918 166 git-commit-tree `git-write-tree` >.git/refs/heads/master 2>&4 ||
aa9b1b9d
YD
167 error "cannot run git-commit -- is your git-core funtionning?"
168 cd "$owd"
169}
170
171test_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.
195PATH=$(pwd)/..:$PATH
29d9e8ce
PR
196HOME=$(pwd)/trash
197export PATH HOME
aa9b1b9d
YD
198
199
200# Test repository
201test=trash
202rm -fr "$test"
203test_create_repo $test
204cd "$test"