Fixed t0000-basic.sh and test-lib.sh permissions
[git/gitweb.git] / t / test-lib.sh
blob079e5bd7aed73dce1ad2f99ecdae1389e46235bd
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 -h|--h|--he|--hel|--help)
54 echo "$test_description"
55 exit 0 ;;
56 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
57 verbose=t; shift ;;
59 break ;;
60 esac
61 done
63 if test "$verbose" = "t"
64 then
65 exec 4>&2 3>&1
66 else
67 exec 4>/dev/null 3>/dev/null
70 test_failure=0
71 test_count=0
73 test_debug () {
74 test "$debug" == "" || eval "$1"
77 test_ok () {
78 test_count=$(expr "$test_count" + 1)
79 say "ok #$test_count: $@"
82 test_failure () {
83 test_count=$(expr "$test_count" + 1)
84 test_failure=$(expr "$test_failure" + 1);
85 say "NO #$test_count: $@"
88 test_expect_failure () {
89 test "$#" == 2 ||
90 error "bug in the test script: not 2 parameters to test-expect-failure"
91 say >&3 "expecting failure: $2"
92 if eval >&3 2>&4 "$2"
93 then
94 test_failure "$@"
95 else
96 test_ok "$1"
100 test_expect_success () {
101 test "$#" == 2 ||
102 error "bug in the test script: not 2 parameters to test-expect-success"
103 say >&3 "expecting success: $2"
104 if eval >&3 2>&4 "$2"
105 then
106 test_ok "$1"
107 else
108 test_failure "$@"
112 test_done () {
113 case "$test_failure" in
115 # We could:
116 # cd .. && rm -fr trash
117 # but that means we forbid any tests that use their own
118 # subdirectory from calling test_done without coming back
119 # to where they started from.
120 # The Makefile provided will clean this test area so
121 # we will leave things as they are.
123 say "passed all $test_count test(s)"
124 exit 0 ;;
127 say "failed $test_failure among $test_count test(s)"
128 exit 1 ;;
130 esac
133 # Test the binaries we have just built. The tests are kept in
134 # t/ subdirectory and are run in trash subdirectory.
135 PATH=$(pwd)/..:$PATH
137 # Test repository
138 test=trash
139 rm -fr "$test"
140 mkdir "$test"
141 cd "$test"
142 git-init-db 2>/dev/null || error "cannot run git-init-db"