testsuite: add tests for cut and grep;
[oweals/busybox.git] / testsuite / runtest
1 #!/bin/sh
2
3 # Run old-style test.
4
5 run_applet_testcase()
6 {
7         local applet=$1
8         local testcase=$2
9
10         local status=0
11         local RES=
12
13         local uc_applet=$(echo $applet | tr a-z A-Z)
14         local testname=$(basename $testcase)
15
16         if grep -q "^# CONFIG_${uc_applet} is not set$" $bindir/.config; then
17                 echo UNTESTED: $testname
18                 return 0
19         fi
20
21         if grep -q "^# FEATURE: " $testcase; then
22                 local feature=`sed -ne 's/^# FEATURE: //p' $testcase`
23
24                 if grep -q "^# ${feature} is not set$" $bindir/.config; then
25                         echo UNTESTED: $testname
26                         return 0
27                 fi
28         fi
29
30         rm -rf tmp
31         mkdir -p tmp
32         pushd tmp > /dev/null
33
34 #       echo Running testcase $testcase
35         d=$tsdir sh -x -e $testcase >.logfile.txt 2>&1 || status=$?
36
37         if [ $status -ne 0 ]; then
38                 echo FAIL: $testname
39                 if [ $verbose -gt 0 ]; then
40                         cat .logfile.txt
41                 fi
42                 status=$?
43         else
44                 echo PASS: $testname
45                 rm -f .logfile.txt
46                 status=$?
47         fi
48
49         popd > /dev/null
50         rm -rf tmp
51
52         return $status
53 }
54
55 run_applet_tests()
56 {
57         local applet=$1
58
59         local status=0
60
61         for testcase in $tsdir/$applet/*; do
62                 if [ "$testcase" = "$tsdir/$applet/CVS" ]; then
63                         continue
64                 fi
65                 if ! run_applet_testcase $applet $testcase; then
66                         status=1
67                 fi
68         done
69
70         return $status
71 }
72
73
74 status=0
75 verbose=0
76
77 [ -n "$tsdir" ] || tsdir=$(pwd)
78 [ -n "$bindir" ] || bindir=$(dirname $(pwd))
79 PATH="$bindir:$PATH"
80
81 if [ x"$1" = x"-v" ]; then
82         verbose=1
83         export VERBOSE=$verbose
84         shift
85 fi
86
87 implemented=$(
88         $bindir/busybox 2>&1 |
89         while read line; do
90                 if test x"$line" = x"Currently defined functions:"; then
91                         xargs | sed 's/,//g'
92                         break
93                 fi
94         done
95         )
96
97 applets="$implemented"
98 if [ $# -ne 0 ]; then
99         applets="$@"
100 fi
101
102 # Populate a directory with links to all busybox applets
103
104 LINKSDIR="$bindir/runtest-tempdir-links"
105 rm -rf "$LINKSDIR" 2>/dev/null
106 mkdir "$LINKSDIR"
107 for i in $implemented; do
108         ln -s $bindir/busybox "$LINKSDIR"/$i
109 done
110
111 # Set up option flags so tests can be selective.
112
113 configfile=${bindir}/.config
114 export OPTIONFLAGS=:$(sed -nr 's/^CONFIG_(.*)=.*/\1/p' $configfile | xargs | sed 's/ /:/g')
115
116 for applet in $applets; do
117         if [ "$applet" = "links" ]; then continue; fi
118         if [ "$applet" != "CVS" -a -d "$tsdir/$applet" ]; then
119                 if ! run_applet_tests $applet; then
120                         status=1
121                 fi
122         fi
123
124         # Is this a new-style test?
125         if [ -f ${applet}.tests ]; then
126                 if [ ! -h "$LINKSDIR/$applet" ] && [ "${applet:0:4}" != "all_" ]; then
127                         echo "SKIPPED: $applet (not built)"
128                         continue
129                 fi
130                 if PATH="$LINKSDIR:$tsdir:$bindir:$PATH" \
131                                 "${tsdir:-.}/$applet".tests
132                 then
133                         :
134                 else
135                         status=1
136                 fi
137         fi
138
139 done
140
141 # Leaving the dir makes it somewhat easier to run failed test by hand
142 #rm -rf "$LINKSDIR"
143
144 if [ $status != 0 -a x"$VERBOSE" = x ]; then
145         echo "Failures detected, running with VERBOSE=1 will give more info"
146 fi
147 exit $status