tests: make tests requiring a delay to pass, more robust
[coreutils/ericb.git] / tests / test-lib.sh
blob82d6535f683643ce3373d2afc5b127f43e083493
1 # source this file; set up for tests
3 # Copyright (C) 2009-2010 Free Software Foundation, Inc.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Skip this test if the shell lacks support for functions.
19 unset function_test
20 eval 'function_test() { return 11; }; function_test'
21 if test $? != 11; then
22 echo "$0: /bin/sh lacks support for functions; skipping this test." 1>&2
23 Exit 77
26 # We use a trap below for cleanup. This requires us to go through
27 # hoops to get the right exit status transported through the signal.
28 # So use `Exit STATUS' instead of `exit STATUS' inside of the tests.
29 # Turn off errexit here so that we don't trip the bug with OSF1/Tru64
30 # sh inside this function.
31 Exit ()
33 set +e
34 (exit $1)
35 exit $1
38 test -f "$srcdir/init.cfg" \
39 && . "$srcdir/init.cfg"
41 test_dir_=$(pwd)
43 this_test_() { echo "./$0" | sed 's,.*/,,'; }
44 this_test=$(this_test_)
46 # This is a stub function that is run upon trap (upon regular exit and
47 # interrupt). Override it with a per-test function, e.g., to unmount
48 # a partition, or to undo any other global state changes.
49 cleanup_() { :; }
51 t_=$("$abs_top_builddir/src/mktemp" -d --tmp="$test_dir_" cu-$this_test.XXXXXXXXXX)\
52 || error_ "failed to create temporary directory in $test_dir_"
54 remove_tmp_()
56 __st=$?
57 cleanup_
58 cd "$test_dir_" && chmod -R u+rwx "$t_" && rm -rf "$t_" && exit $__st
61 # Run each test from within a temporary sub-directory named after the
62 # test itself, and arrange to remove it upon exception or normal exit.
63 trap remove_tmp_ 0
64 trap 'Exit 129' 1
65 trap 'Exit 130' 2
66 trap 'Exit 131' 3
67 trap 'Exit 141' 13
68 trap 'Exit 143' 15
70 cd "$t_" || error_ "failed to cd to $t_"
72 if ( diff --version < /dev/null 2>&1 | grep GNU ) 2>&1 > /dev/null; then
73 compare() { diff -u "$@"; }
74 elif ( cmp --version < /dev/null 2>&1 | grep GNU ) 2>&1 > /dev/null; then
75 compare() { cmp -s "$@"; }
76 else
77 compare() { cmp "$@"; }
80 # Initialize; all bourne shell scripts end with "Exit $fail".
81 fail=0