Test commit
[cogito/jonas.git] / t / test-lib.sh
blobfb7c46f37d7e3d29f5252c902da8d1632ed5febc
1 #!/bin/sh
3 # Copyright (c) 2005 Junio C Hamano
6 # For repeatability, reset the environment to known value.
7 LANG=C
8 TZ=UTC
9 export LANG TZ
10 unset AUTHOR_DATE
11 unset AUTHOR_EMAIL
12 unset AUTHOR_NAME
13 unset COMMIT_AUTHOR_EMAIL
14 unset COMMIT_AUTHOR_NAME
15 unset GIT_ALTERNATE_OBJECT_DIRECTORIES
16 unset GIT_AUTHOR_DATE
17 unset GIT_AUTHOR_EMAIL
18 unset GIT_AUTHOR_NAME
19 unset GIT_COMMITTER_EMAIL
20 unset GIT_COMMITTER_NAME
21 unset GIT_DIFF_OPTS
22 unset GIT_DIR
23 unset GIT_EXTERNAL_DIFF
24 unset GIT_INDEX_FILE
25 unset GIT_OBJECT_DIRECTORY
26 unset SHA1_FILE_DIRECTORIES
27 unset SHA1_FILE_DIRECTORY
29 # Each test should start with something like this, after copyright notices:
31 # test_description='Description of this test...
32 # This test checks if command xyzzy does the right thing...
33 # '
34 # . ./test-lib.sh
36 error () {
37 echo "* error: $*"
38 exit 1
41 say () {
42 echo "* $*"
45 test "${test_description}" != "" ||
46 error "Test script did not set test_description."
48 while test "$#" -ne 0
50 case "$1" in
51 -d|--d|--de|--deb|--debu|--debug)
52 debug=t; shift ;;
53 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
54 immediate=t; shift ;;
55 -h|--h|--he|--hel|--help)
56 echo "$test_description"
57 exit 0 ;;
58 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
59 verbose=t; shift ;;
61 break ;;
62 esac
63 done
65 if test "$verbose" = "t"
66 then
67 exec 4>&2 3>&1 </dev/null
68 else
69 exec 4>/dev/null 3>/dev/null </dev/null
70 # At least in SLES10, cmp is broken and will always return true
71 # if stdout is redirected to /dev/null and -s is not passed.
72 cmp () { `which cmp` -s "$@"; }
75 test_failure=0
76 test_count=0
79 # You are not expected to call test_ok_ and test_failure_ directly, use
80 # the text_expect_* functions instead.
82 test_ok_ () {
83 test_count=$(expr "$test_count" + 1)
84 say " ok $test_count: $@"
87 test_failure_ () {
88 test_count=$(expr "$test_count" + 1)
89 test_failure=$(expr "$test_failure" + 1);
90 say "FAIL $test_count: $1"
91 shift
92 echo "$@" | sed -e 's/^/ /'
93 test "$immediate" = "" || exit 1
97 test_debug () {
98 test "$debug" = "" || eval "$1"
101 test_expect_failure () {
102 test "$#" = 2 ||
103 error "bug in the test script: not 2 parameters to test-expect-failure"
104 say >&3 "expecting failure: $2"
105 if eval </dev/null >&3 2>&4 "$2"
106 then
107 test_failure_ "$@"
108 else
109 test_ok_ "$1"
113 test_expect_success () {
114 test "$#" = 2 ||
115 error "bug in the test script: not 2 parameters to test-expect-success"
116 say >&3 "expecting success: $2"
117 if eval </dev/null >&3 2>&4 "$2"
118 then
119 test_ok_ "$1"
120 else
121 test_failure_ "$@"
125 test_done () {
126 case "$test_failure" in
128 # We could:
129 # cd .. && rm -fr trash
130 # but that means we forbid any tests that use their own
131 # subdirectory from calling test_done without coming back
132 # to where they started from.
133 # The Makefile provided will clean this test area so
134 # we will leave things as they are.
136 say "passed all $test_count test(s)"
137 exit 0 ;;
140 say "failed $test_failure among $test_count test(s)"
141 exit 1 ;;
143 esac
146 # Test the binaries we have just built. The tests are kept in
147 # t/ subdirectory and are run in trash subdirectory.
148 export PATH=$(pwd)/..:$PATH
150 # Test repository
151 test=trash
152 rm -fr "$test"
153 mkdir "$test"
154 cd "$test"
155 git-init-db 2>/dev/null || error "cannot run git-init-db"