Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / contrib / lvm2 / dist / test / test-lib.sh
1 #!/bin/sh
2 # Derived from git's t/test-lib.sh, which is Copyright (c) 2005 Junio C Hamano
3 #
4 # This copyrighted material is made available to anyone wishing to use,
5 # modify, copy, or redistribute it subject to the terms and conditions
6 # of the GNU General Public License v.2.
7 #
8 # You should have received a copy of the GNU General Public License
9 # along with this program; if not, write to the Free Software Foundation,
10 # Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
11
12 # For repeatability, reset the environment to known value.
13 LANG=C
14 LC_ALL=C
15 TZ=UTC
16 export LANG LC_ALL TZ
17
18 . ./init.sh || { echo >&2 you must run make first; exit 1; }
19
20 # Protect ourselves from common misconfiguration to export
21 # CDPATH into the environment
22 unset CDPATH
23
24 # Each test should start with something like this, after copyright notices:
25 #
26 # test_description='Description of this test...
27 # This test checks if command xyzzy does the right thing...
28 # '
29 # . ./test-lib.sh
30
31 error () {
32         echo "* error: $*"
33         exit 1
34 }
35
36 say () {
37         echo "* $*"
38 }
39
40 this_test_() { expr "./$0" : '.*/t-\([^/]*\)\.sh$'; }
41
42 test "${test_description}" != "" ||
43 error "Test script did not set test_description."
44
45 verboselevel=0
46 while test "$#" -ne 0
47 do
48         case "$1" in
49         -d|--d|--de|--deb|--debu|--debug)
50                 debug=t ;;
51         -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
52                 immediate=t ;;
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 ;;
58         -vv|-vvv|-vvvv)
59                 verboselevel=${#1}
60                 verboselevel=$(($verboselevel - 1))
61                 verbose=t ;;
62         *)
63                 echo "$0: unsupported option $1"
64                 exit 0 ;;
65         esac
66         shift
67 done
68
69 exec 5>&1
70 if test "$verbose" = "t"
71 then
72         exec 4>&2 3>&1
73 else
74         exec 4>/dev/null 3>/dev/null
75 fi
76
77 test_failure=0
78 test_count=0
79
80 trap 'echo >&5 "FATAL: Unexpected exit with code $?"; exit 1' exit
81
82 # You are not expected to call test_ok_ and test_failure_ directly, use
83 # the text_expect_* functions instead.
84
85 test_ok_ () {
86         test_count=$(expr "$test_count" + 1)
87         say "  ok $test_count: $@"
88 }
89
90 test_failure_ () {
91         test_count=$(expr "$test_count" + 1)
92         test_failure=$(expr "$test_failure" + 1);
93         say "FAIL $test_count: $1"
94         shift
95         echo "$@" | sed -e 's/^/        /'
96         test "$immediate" = "" || exit 1
97 }
98
99 test_debug () {
100         test "$debug" = "" || eval "$1"
101 }
102
103 test_run_ () {
104         eval >&3 2>&4 "$1"
105         eval_ret="$?"
106         return 0
107 }
108
109 test_skip () {
110         this_test=$(this_test_)
111         this_test="$this_test.$(expr "$test_count" + 1)"
112         to_skip=
113         for skp in $SKIP_TESTS
114         do
115                 case "$this_test" in
116                 $skp)
117                         to_skip=t
118                 esac
119         done
120         case "$to_skip" in
121         t)
122                 say >&3 "skipping test: $@"
123                 test_count=$(expr "$test_count" + 1)
124                 say "skip $test_count: $1"
125                 : true
126                 ;;
127         *)
128                 false
129                 ;;
130         esac
131 }
132
133 test_expect_failure () {
134         test "$#" = 2 ||
135         error "bug in the test script: not 2 parameters to test-expect-failure"
136         if ! test_skip "$@"
137         then
138                 say >&3 "expecting failure: $2"
139                 test_run_ "$2"
140                 if [ "$?" = 0 -a "$eval_ret" != 0 -a "$eval_ret" -lt 129 ]
141                 then
142                         test_ok_ "$1"
143                 else
144                         test_failure_ "$@"
145                 fi
146         fi
147         echo >&3 ""
148 }
149
150 test_expect_success () {
151         test "$#" = 2 ||
152         error "bug in the test script: not 2 parameters to test-expect-success"
153         if ! test_skip "$@"
154         then
155                 say >&3 "expecting success: $2"
156                 test_run_ "$2"
157                 if [ "$?" = 0 -a "$eval_ret" = 0 ]
158                 then
159                         test_ok_ "$1"
160                 else
161                         test_failure_ "$@"
162                 fi
163         fi
164         echo >&3 ""
165 }
166
167 test_expect_code () {
168         test "$#" = 3 ||
169         error "bug in the test script: not 3 parameters to test-expect-code"
170         if ! test_skip "$@"
171         then
172                 say >&3 "expecting exit code $1: $3"
173                 test_run_ "$3"
174                 if [ "$?" = 0 -a "$eval_ret" = "$1" ]
175                 then
176                         test_ok_ "$2"
177                 else
178                         test_failure_ "$@"
179                 fi
180         fi
181         echo >&3 ""
182 }
183
184 test_done () {
185         case "$test_failure" in
186         0)
187                 # We could:
188                 # cd .. && rm -fr trash
189                 # but that means we forbid any tests that use their own
190                 # subdirectory from calling test_done without coming back
191                 # to where they started from.
192                 # The Makefile provided will clean this test area so
193                 # we will leave things as they are.
194
195                 say "passed all $test_count test(s)"
196                 exit 0 ;;
197
198         *)
199                 say "failed $test_failure among $test_count test(s)"
200                 exit 1 ;;
201
202         esac
203 }
204
205 this_test=$(this_test_)
206
207 skip_=0
208 # If $privileges_required_ is nonempty, non-root skips this test.
209 if test "$privileges_required_" != ''; then
210     uid=`id -u` || error 'failed to run "id -u"'
211     if test "$uid" != 0; then
212         SKIP_TESTS="$SKIP_TESTS $this_test"
213         say "you have insufficient privileges for test $this_test"
214         skip_=1
215     fi
216 fi
217
218 pwd_=`pwd`
219
220 test_dir_=${LVM_TEST_DIR-.}
221 test "$test_dir_" = . && test_dir_=$pwd_
222
223 # This is a stub function that is run upon trap (upon regular exit and
224 # interrupt).  Override it with a per-test function, e.g., to unmount
225 # a partition, or to undo any other global state changes.
226 cleanup_() { :; }
227
228 for skp in $SKIP_TESTS
229 do
230         to_skip=
231         for skp in $SKIP_TESTS
232         do
233                 case "$this_test" in
234                 $skp)
235                         to_skip=t
236                 esac
237         done
238         case "$to_skip" in
239         t)
240                 say >&3 "skipping test $this_test altogether"
241                 say "skip all tests in $this_test"
242                 trap - exit
243                 test_done
244         esac
245 done
246
247 test_dir_rand_=$($abs_srcdir/mkdtemp $test_dir_ lvm-$this_test.XXXXXXXXXX) \
248     || error "failed to create temporary directory in $test_dir_"
249
250 testlib_cleanup_() {
251     d="$test_dir_rand_";
252     cd "$test_dir_" && chmod -R u+rwx "$d" && rm -rf "$d"
253 }
254
255 # Run each test from within a temporary sub-directory named after the
256 # test itself, and arrange to remove it upon exception or normal exit.
257 trap 'st=$?; cleanup_; testlib_cleanup_; exit $st' 0
258 trap '(exit $?); exit $?' 1 2 13 15
259
260 cd $test_dir_rand_ || error "failed to cd to $test_dir_rand_"
261
262 if test $skip_ = 0; then
263   . $abs_srcdir/lvm-utils.sh || exit 1
264 fi
265
266 if ( diff --version < /dev/null 2>&1 | grep GNU ) 2>&1 > /dev/null; then
267   compare='diff -u'
268 elif ( cmp --version < /dev/null 2>&1 | grep GNU ) 2>&1 > /dev/null; then
269   compare='cmp -s'
270 else
271   compare=cmp
272 fi