2 # Derived from git's t/test-lib.sh, which is Copyright (c) 2005 Junio C Hamano
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.
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
12 # For repeatability, reset the environment to known value.
18 . ./init.sh || { echo >&2 you must run make first; exit 1; }
20 # Protect ourselves from common misconfiguration to export
21 # CDPATH into the environment
24 # Each test should start with something like this, after copyright notices:
26 # test_description='Description of this test...
27 # This test checks if command xyzzy does the right thing...
40 this_test_() { expr "./$0" : '.*/t-\([^/]*\)\.sh$'; }
42 test "${test_description}" != "" ||
43 error "Test script did not set test_description."
49 -d|--d|--de|--deb|--debu|--debug)
51 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
53 -h|--h|--he|--hel|--help)
54 echo "$test_description"
56 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
60 verboselevel=$(($verboselevel - 1))
63 echo "$0: unsupported option $1"
70 if test "$verbose" = "t"
74 exec 4>/dev/null 3>/dev/null
80 trap 'echo >&5 "FATAL: Unexpected exit with code $?"; exit 1' exit
82 # You are not expected to call test_ok_ and test_failure_ directly, use
83 # the text_expect_* functions instead.
86 test_count=$(expr "$test_count" + 1)
87 say " ok $test_count: $@"
91 test_count=$(expr "$test_count" + 1)
92 test_failure=$(expr "$test_failure" + 1);
93 say "FAIL $test_count: $1"
95 echo "$@" | sed -e 's/^/ /'
96 test "$immediate" = "" || exit 1
100 test "$debug" = "" || eval "$1"
110 this_test=$(this_test_)
111 this_test="$this_test.$(expr "$test_count" + 1)"
113 for skp in $SKIP_TESTS
122 say >&3 "skipping test: $@"
123 test_count=$(expr "$test_count" + 1)
124 say "skip $test_count: $1"
133 test_expect_failure () {
135 error "bug in the test script: not 2 parameters to test-expect-failure"
138 say >&3 "expecting failure: $2"
140 if [ "$?" = 0 -a "$eval_ret" != 0 -a "$eval_ret" -lt 129 ]
150 test_expect_success () {
152 error "bug in the test script: not 2 parameters to test-expect-success"
155 say >&3 "expecting success: $2"
157 if [ "$?" = 0 -a "$eval_ret" = 0 ]
167 test_expect_code () {
169 error "bug in the test script: not 3 parameters to test-expect-code"
172 say >&3 "expecting exit code $1: $3"
174 if [ "$?" = 0 -a "$eval_ret" = "$1" ]
185 case "$test_failure" in
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.
195 say "passed all $test_count test(s)"
199 say "failed $test_failure among $test_count test(s)"
205 this_test=$(this_test_)
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"
220 test_dir_=${LVM_TEST_DIR-.}
221 test "$test_dir_" = . && test_dir_=$pwd_
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.
228 for skp in $SKIP_TESTS
231 for skp in $SKIP_TESTS
240 say >&3 "skipping test $this_test altogether"
241 say "skip all tests in $this_test"
247 test_dir_rand_=$($abs_srcdir/mkdtemp $test_dir_ lvm-$this_test.XXXXXXXXXX) \
248 || error "failed to create temporary directory in $test_dir_"
252 cd "$test_dir_" && chmod -R u+rwx "$d" && rm -rf "$d"
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
260 cd $test_dir_rand_ || error "failed to cd to $test_dir_rand_"
262 if test $skip_ = 0; then
263 . $abs_srcdir/lvm-utils.sh || exit 1
266 if ( diff --version < /dev/null 2>&1 | grep GNU ) 2>&1 > /dev/null; then
268 elif ( cmp --version < /dev/null 2>&1 | grep GNU ) 2>&1 > /dev/null; then