OSDN Git Service

Add -h and -L to file.
[android-x86/external-toybox.git] / scripts / runtest.sh
1 # Simple test harness infrastructure
2 #
3 # Copyright 2005 by Rob Landley
4
5 # This file defines two functions, "testing" and "optionflag"
6
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")
12 #
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
19 #
20 # The exit value of testing is the exit value of the command it ran.
21 #
22 # The environment variable "FAILCOUNT" contains a cumulative total of the
23 # number of failed tests.
24
25 # The "optional" function is used to skip certain tests, ala:
26 #   optionflag CFG_THINGY
27 #
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).
32
33 export FAILCOUNT=0
34 export SKIP=
35
36 # Helper functions
37
38 # Check config to see if option is enabled, set SKIP if not.
39
40 SHOWPASS=PASS
41 SHOWFAIL=FAIL
42 SHOWSKIP=SKIP
43
44 if tty -s <&1
45 then
46   SHOWPASS="$(echo -e "\033[1;32m${SHOWPASS}\033[0m")"
47   SHOWFAIL="$(echo -e "\033[1;31m${SHOWFAIL}\033[0m")"
48   SHOWSKIP="$(echo -e "\033[1;33m${SHOWSKIP}\033[0m")"
49 fi
50
51 optional()
52 {
53   option=`echo "$OPTIONFLAGS" | egrep "(^|:)$1(:|\$)"`
54   # Not set?
55   if [ -z "$1" ] || [ -z "$OPTIONFLAGS" ] || [ ${#option} -ne 0 ]
56   then
57     SKIP=""
58     return
59   fi
60   SKIP=1
61 }
62
63 # The testing function
64
65 testing()
66 {
67   NAME="$CMDNAME $1"
68   [ -z "$1" ] && NAME=$2
69
70   if [ $# -ne 5 ]
71   then
72     echo "Test $NAME has the wrong number of arguments ($# $*)" >&2
73     exit
74   fi
75
76   [ -n "$DEBUG" ] && set -x
77
78   if [ -n "$SKIP" ] || ( [ -n "$SKIP_HOST" ] && [ -n "$TEST_HOST" ])
79   then
80     [ ! -z "$VERBOSE" ] && echo "$SHOWSKIP: $NAME"
81     return 0
82   fi
83
84   echo -ne "$3" > expected
85   echo -ne "$4" > input
86   echo -ne "$5" | ${EVAL:-eval} "$2" > actual
87   RETVAL=$?
88
89   # Catch segfaults
90   [ $RETVAL -gt 128 ] && [ $RETVAL -lt 255 ] &&
91     echo "exited with signal (or returned $RETVAL)" >> actual
92  
93   DIFF="$(diff -au${NOSPACE:+b} expected actual)"
94   if [ ! -z "$DIFF" ]
95   then
96     FAILCOUNT=$[$FAILCOUNT+1]
97     echo "$SHOWFAIL: $NAME"
98     if [ -n "$VERBOSE" ]
99     then
100       [ ! -z "$4" ] && echo "echo -ne \"$4\" > input"
101       echo "echo -ne '$5' |$EVAL $2"
102       echo "$DIFF"
103       [ "$VERBOSE" == fail ] && exit 1
104     fi
105   else
106     echo "$SHOWPASS: $NAME"
107   fi
108   rm -f input expected actual
109
110   [ -n "$DEBUG" ] && set +x
111
112   return 0
113 }
114
115 # Recursively grab an executable and all the libraries needed to run it.
116 # Source paths beginning with / will be copied into destpath, otherwise
117 # the file is assumed to already be there and only its library dependencies
118 # are copied.
119
120 mkchroot()
121 {
122   [ $# -lt 2 ] && return
123
124   echo -n .
125
126   dest=$1
127   shift
128   for i in "$@"
129   do
130     [ "${i:0:1}" == "/" ] || i=$(which $i)
131     [ -f "$dest/$i" ] && continue
132     if [ -e "$i" ]
133     then
134       d=`echo "$i" | grep -o '.*/'` &&
135       mkdir -p "$dest/$d" &&
136       cat "$i" > "$dest/$i" &&
137       chmod +x "$dest/$i"
138     else
139       echo "Not found: $i"
140     fi
141     mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ')
142   done
143 }
144
145 # Set up a chroot environment and run commands within it.
146 # Needed commands listed on command line
147 # Script fed to stdin.
148
149 dochroot()
150 {
151   mkdir tmpdir4chroot
152   mount -t ramfs tmpdir4chroot tmpdir4chroot
153   mkdir -p tmpdir4chroot/{etc,sys,proc,tmp,dev}
154   cp -L testing.sh tmpdir4chroot
155
156   # Copy utilities from command line arguments
157
158   echo -n "Setup chroot"
159   mkchroot tmpdir4chroot $*
160   echo
161
162   mknod tmpdir4chroot/dev/tty c 5 0
163   mknod tmpdir4chroot/dev/null c 1 3
164   mknod tmpdir4chroot/dev/zero c 1 5
165
166   # Copy script from stdin
167
168   cat > tmpdir4chroot/test.sh
169   chmod +x tmpdir4chroot/test.sh
170   chroot tmpdir4chroot /test.sh
171   umount -l tmpdir4chroot
172   rmdir tmpdir4chroot
173 }
174