ifplugd: code shrink
[oweals/busybox.git] / testsuite / runtest
1 #!/bin/sh
2 # Usage:
3 # runtest [applet1] [applet2...]
4
5 . ./testing.sh
6
7 # Run one old-style test.
8 # Tests are stored in applet/testcase shell scripts.
9 # They are run using "sh -x -e applet/testcase".
10 # Option -e will make testcase stop on the first failed command.
11 run_applet_testcase()
12 {
13         local applet="$1"
14         local testcase="$2"
15
16         local status=0
17         local uc_applet=$(echo "$applet" | tr a-z A-Z)
18         local testname="$testcase"
19
20         testname="${testname##*/}" # take basename
21         if grep "^# CONFIG_$uc_applet is not set$" "$bindir/.config" >/dev/null; then
22                 echo "UNTESTED: $testname"
23                 return 0
24         fi
25
26         if grep "^# FEATURE: " "$testcase" >/dev/null; then
27                 local feature=$(sed -ne 's/^# FEATURE: //p' "$testcase")
28
29                 for f in $feature; do
30                         if grep "^# $f is not set$" "$bindir/.config" >/dev/null; then
31                                 echo "UNTESTED: $testname"
32                                 return 0
33                         fi
34                 done
35         fi
36
37         rm -rf ".tmpdir.$applet"
38         mkdir -p ".tmpdir.$applet"
39         cd ".tmpdir.$applet" || return 1
40
41 #       echo "Running testcase $testcase"
42         d="$tsdir" \
43                 sh -x -e "$testcase" >"$testname.stdout.txt" 2>&1 || status=$?
44         if [ $status -ne 0 ]; then
45                 echo "FAIL: $testname"
46                 if [ x"$VERBOSE" != x ]; then
47                         cat "$testname.stdout.txt"
48                 fi
49         else
50                 echo "PASS: $testname"
51         fi
52
53         cd ..
54         rm -rf ".tmpdir.$applet"
55
56         return $status
57 }
58
59 # Run all old-style tests for given applet
60 run_oldstyle_applet_tests()
61 {
62         local applet="$1"
63         local status=0
64
65         for testcase in "$tsdir/$applet"/*; do
66                 # switch on basename of $testcase
67                 case "${testcase##*/}" in
68                         .*)     continue ;;    # .svn, .git etc
69                         *~)     continue ;;    # backup files
70                         "CVS")  continue ;;
71                         \#*)    continue ;;    # CVS merge residues
72                         *.mine) continue ;;    # svn-produced junk
73                         *.r[0-9]*) continue ;; # svn-produced junk
74                 esac
75                 run_applet_testcase "$applet" "$testcase" || status=1
76         done
77         return $status
78 }
79
80
81
82 lcwd=$(pwd)
83 [ x"$tsdir" != x"" ] || tsdir="$lcwd"
84 [ x"$bindir" != x"" ] || bindir="${lcwd%/*}" # one directory up from $lcwd
85 PATH="$bindir:$PATH"
86 export bindir   # some tests need to look at $bindir/.config
87
88 if [ x"$VERBOSE" = x ]; then
89         export VERBOSE=
90 fi
91
92 if [ x"$1" = x"-v" ]; then
93         export VERBOSE=1
94         shift
95 fi
96
97 implemented=$(
98         printf "busybox " # always 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
117 # Comment this line out if you have put a different binary in $LINKSDIR
118 # (say, a "standard" tool's binary) in order to run tests against it:
119 rm -rf "$LINKSDIR" 2>/dev/null
120
121 mkdir "$LINKSDIR" 2>/dev/null
122 for i in $implemented; do
123         # Note: if $LINKSDIR/applet exists, we do not overwrite it.
124         # Useful if one wants to run tests against a standard utility,
125         # not an applet.
126         ln -s "$bindir/busybox" "$LINKSDIR/$i" 2>/dev/null
127 done
128
129 # Set up option flags so tests can be selective.
130 export OPTIONFLAGS=:$(
131         sed -nr 's/^CONFIG_//p' "$bindir/.config" |
132         sed 's/=.*//' | xargs | sed 's/ /:/g'
133         ):
134
135 status=0
136 for applet in $applets; do
137         # Any old-style tests for this applet?
138         if [ -d "$tsdir/$applet" ]; then
139                 run_oldstyle_applet_tests "$applet" || status=1
140         fi
141
142         # Is this a new-style test?
143         if [ -f "$applet.tests" ]; then
144                 if [ ! -e "$LINKSDIR/$applet" ]; then
145                         # (avoiding bash'ism "${applet:0:4}")
146                         if ! echo "$applet" | grep "^all_" >/dev/null; then
147                                 echo "SKIPPED: $applet (not built)"
148                                 continue
149                         fi
150                 fi
151 #               echo "Running test $tsdir/$applet.tests"
152                 PATH="$LINKSDIR:$tsdir:$bindir:$PATH" \
153                         "$tsdir/$applet.tests" || status=1
154         fi
155 done
156
157 # Leaving the dir makes it somewhat easier to run failed test by hand
158 #rm -rf "$LINKSDIR"
159
160 if [ $status -ne 0 ] && [ x"$VERBOSE" = x ]; then
161         echo "Failures detected, running with -v (verbose) will give more info"
162 fi
163 exit $status