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