1 # Simple test harness infrastructure
3 # Copyright 2005 by Rob Landley
5 # This file defines two functions, "testing" and "optionflag"
7 # The following environment variables enable optional behavior in "testing":
8 # DEBUG - Show every command run by test script.
9 # VERBOSE - Print the diff -u of each failed test case.
10 # If equal to "fail", stop after first failed test.
11 # SKIP - do not perform this test (this is set by "optionflag")
13 # The "testing" function takes five arguments:
14 # $1) Description to display when running command
15 # $2) Command line arguments to command
16 # $3) Expected result (on stdout)
17 # $4) Data written to file "input"
18 # $5) Data written to stdin
20 # The exit value of testing is the exit value of the command it ran.
22 # The environment variable "FAILCOUNT" contains a cumulative total of the
23 # number of failed tests.
25 # The "optional" function is used to skip certain tests, ala:
26 # optionflag CFG_THINGY
28 # The "optional" function checks the environment variable "OPTIONFLAGS",
29 # which is either empty (in which case it always clears SKIP) or
30 # else contains a colon-separated list of features (in which case the function
31 # clears SKIP if the flag was found, or sets it to 1 if the flag was not found).
38 # Check config to see if option is enabled, set SKIP if not.
46 SHOWPASS="$(echo -e "\033[1m\033[32m${SHOWPASS}\033[0m")"
47 SHOWFAIL="$(echo -e "\033[1m\033[31m${SHOWFAIL}\033[0m")"
48 SHOWSKIP="$(echo -e "\033[1m\033[33m${SHOWSKIP}\033[0m")"
53 option=`echo "$OPTIONFLAGS" | egrep "(^|:)$1(:|\$)"`
55 if [ -z "$1" ] || [ -z "$OPTIONFLAGS" ] || [ ${#option} -ne 0 ]
63 # The testing function
68 [ -z "$1" ] && NAME=$2
72 echo "Test $NAME has the wrong number of arguments ($# $*)" >&2
76 [ -n "$DEBUG" ] && set -x
78 if [ -n "$SKIP" ] || ( [ -n "$SKIP_HOST" ] && [ -n "$TEST_HOST" ])
80 [ ! -z "$VERBOSE" ] && echo "$SHOWSKIP: $NAME"
84 echo -ne "$3" > expected
86 echo -ne "$5" | eval "$2" > actual
89 cmp expected actual > /dev/null 2>&1
92 FAILCOUNT=$[$FAILCOUNT+1]
93 echo "$SHOWFAIL: $NAME"
96 [ ! -z "$4" ] && echo "echo -ne \"$4\" > input"
97 echo "echo -ne '$5' | $2"
98 diff -au expected actual
99 [ "$VERBOSE" == fail ] && exit 1
102 echo "$SHOWPASS: $NAME"
104 rm -f input expected actual
106 [ -n "$DEBUG" ] && set +x
111 # Recursively grab an executable and all the libraries needed to run it.
112 # Source paths beginning with / will be copied into destpath, otherwise
113 # the file is assumed to already be there and only its library dependencies
118 [ $# -lt 2 ] && return
126 [ "${i:0:1}" == "/" ] || i=$(which $i)
127 [ -f "$dest/$i" ] && continue
130 d=`echo "$i" | grep -o '.*/'` &&
131 mkdir -p "$dest/$d" &&
132 cat "$i" > "$dest/$i" &&
137 mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ')
141 # Set up a chroot environment and run commands within it.
142 # Needed commands listed on command line
143 # Script fed to stdin.
148 mount -t ramfs tmpdir4chroot tmpdir4chroot
149 mkdir -p tmpdir4chroot/{etc,sys,proc,tmp,dev}
150 cp -L testing.sh tmpdir4chroot
152 # Copy utilities from command line arguments
154 echo -n "Setup chroot"
155 mkchroot tmpdir4chroot $*
158 mknod tmpdir4chroot/dev/tty c 5 0
159 mknod tmpdir4chroot/dev/null c 1 3
160 mknod tmpdir4chroot/dev/zero c 1 5
162 # Copy script from stdin
164 cat > tmpdir4chroot/test.sh
165 chmod +x tmpdir4chroot/test.sh
166 chroot tmpdir4chroot /test.sh
167 umount -l tmpdir4chroot