Port tests/rmdir/ignore away from GNU/Linux.
[coreutils/ericb.git] / tests / test-lib.sh
blobb90f98ff7610e3e7635b2ea7cf8be40f642552e6
1 # source this file; set up for tests
3 # Skip this test if the shell lacks support for functions.
4 unset function_test
5 eval 'function_test() { return 11; }; function_test'
6 if test $? != 11; then
7 echo "$0: /bin/sh lacks support for functions; skipping this test." 1>&2
8 (exit 77); exit 77
9 fi
11 skip_test_()
13 echo "$0: skipping test: $@" 1>&2
14 (exit 77); exit 77
17 require_ulimit_()
19 ulimit_works=yes
20 # Expect to be able to exec a program in 10MB of virtual memory,
21 # but not in 20KB. I chose "date". It must not be a shell built-in
22 # function, so you can't use echo, printf, true, etc.
23 # Of course, in coreutils, I could use $top_builddir/src/true,
24 # but this should be able to work for other projects, too.
25 ( ulimit -v 10000; date ) > /dev/null 2>&1 || ulimit_works=no
26 ( ulimit -v 20; date ) > /dev/null 2>&1 && ulimit_works=no
28 test $ulimit_works = no \
29 && skip_test_ "this shell lacks ulimit support"
32 uid_is_privileged_()
34 # Make sure id -u succeeds.
35 my_uid=$(id -u) \
36 || { echo "$0: cannot run \`id -u'" 1>&2; return 1; }
38 # Make sure it gives valid output.
39 case $my_uid in
40 0) ;;
41 *[!0-9]*)
42 echo "$0: invalid output (\`$my_uid') from \`id -u'" 1>&2
43 return 1 ;;
44 *) return 1 ;;
45 esac
48 skip_if_()
50 case $1 in
51 root) skip_test_ must be run as root ;;
52 non-root) skip_test_ must be run as non-root ;;
53 *) ;; # FIXME?
54 esac
57 require_selinux_()
59 case `ls -Zd .` in
60 '? .'|'unlabeled .')
61 skip_test_ "this system (or maybe just" \
62 "the current file system) lacks SELinux support"
64 esac
67 skip_if_root_() { uid_is_privileged_ && skip_test_ "must be run as non-root"; }
68 error_() { echo "$0: $@" 1>&2; (exit 1); exit 1; }
69 framework_failure() { error_ 'failure in testing framework'; }
71 test_dir_=$(pwd)
73 this_test_() { echo "./$0" | sed 's,.*/,,'; }
74 this_test=$(this_test_)
76 . $srcdir/../envvar-check
78 # This is a stub function that is run upon trap (upon regular exit and
79 # interrupt). Override it with a per-test function, e.g., to unmount
80 # a partition, or to undo any other global state changes.
81 cleanup_() { :; }
83 t_=$($abs_top_builddir/src/mktemp -d --tmp="$test_dir_" cu-$this_test.XXXXXXXXXX)\
84 || error_ "failed to create temporary directory in $test_dir_"
86 # Run each test from within a temporary sub-directory named after the
87 # test itself, and arrange to remove it upon exception or normal exit.
88 trap 'st=$?; cleanup_; d='"$t_"';
89 cd '"$test_dir_"' && chmod -R u+rwx "$d" && rm -rf "$d" && exit $st' 0
90 trap '(exit $?); exit $?' 1 2 13 15
92 cd $t_ || error_ "failed to cd to $t_"
94 if ( diff --version < /dev/null 2>&1 | grep GNU ) 2>&1 > /dev/null; then
95 compare() { diff -u "$@"; }
96 elif ( cmp --version < /dev/null 2>&1 | grep GNU ) 2>&1 > /dev/null; then
97 compare() { cmp -s "$@"; }
98 else
99 compare() { cmp "$@"; }
102 # Local Variables:
103 # indent-tabs-mode: nil
104 # End: