strrchr: actually, last one was finding "" in "any" at pos 0,
[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=0
16         local uc_applet=$(echo "$applet" | tr a-z A-Z)
17         local testname="$testcase"
18
19         testname="${testname##*/}" # take basename
20         if grep "^# CONFIG_$uc_applet is not set$" "$bindir/.config" >/dev/null; then
21                 echo "UNTESTED: $testname"
22                 return 0
23         fi
24
25         if grep "^# FEATURE: " "$testcase" >/dev/null; then
26                 local feature=$(sed -ne 's/^# FEATURE: //p' "$testcase")
27
28                 if grep "^# $feature is not set$" "$bindir/.config" >/dev/null; 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" \
40                 sh -x -e "$testcase" >"$testname.stdout.txt" 2>&1 || 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_oldstyle_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                         .*)     continue ;;    # .svn, .git etc
66                         *~)     continue ;;    # backup files
67                         "CVS")  continue ;;
68                         \#*)    continue ;;    # CVS merge residues
69                         *.mine) continue ;;    # svn-produced junk
70                         *.r[0-9]*) continue ;; # svn-produced junk
71                 esac
72                 run_applet_testcase "$applet" "$testcase" || status=1
73         done
74         return $status
75 }
76
77
78
79 lcwd=$(pwd)
80 [ x"$tsdir" != x ] || tsdir="$lcwd"
81 [ x"$bindir" != x ] || bindir="${lcwd%/*}" # one directory up from $lcwd
82 PATH="$bindir:$PATH"
83
84 if [ x"$VERBOSE" = x ]; then
85         export VERBOSE=
86 fi
87
88 if [ x"$1" = x"-v" ]; then
89         export VERBOSE=1
90         shift
91 fi
92
93 implemented=$(
94         "$bindir/busybox" 2>&1 |
95         while read line; do
96                 if [ x"$line" = x"Currently defined functions:" ]; then
97                         xargs | sed 's/,//g'
98                         break
99                 fi
100         done
101         )
102
103 applets="$implemented"
104 if [ $# -ne 0 ]; then
105         applets="$@"
106 fi
107
108 # Populate a directory with links to all busybox applets
109
110 LINKSDIR="$bindir/runtest-tempdir-links"
111 rm -rf "$LINKSDIR" 2>/dev/null
112 mkdir "$LINKSDIR"
113 for i in $implemented; do
114         ln -s "$bindir/busybox" "$LINKSDIR/$i"
115 done
116
117 # Set up option flags so tests can be selective.
118 export OPTIONFLAGS=:$(
119         sed -nr 's/^CONFIG_//p' "$bindir/.config" |
120         sed 's/=.*//' | xargs | sed 's/ /:/g'
121         )
122
123 status=0
124 for applet in $applets; do
125         # Any old-style tests for this applet?
126         if [ -d "$tsdir/$applet" ]; then
127                 run_oldstyle_applet_tests "$applet" || status=1
128         fi
129
130         # Is this a new-style test?
131         if [ -f "$applet.tests" ]; then
132                 if [ ! -h "$LINKSDIR/$applet" ]; then
133                         # (avoiding bash'ism "${applet:0:4}")
134                         if ! echo "$applet" | grep "^all_" >/dev/null; then
135                                 echo "SKIPPED: $applet (not built)"
136                                 continue
137                         fi
138                 fi
139 #               echo "Running test $tsdir/$applet.tests"
140                 PATH="$LINKSDIR:$tsdir:$bindir:$PATH" \
141                         "$tsdir/$applet.tests" || status=1
142         fi
143 done
144
145 # Leaving the dir makes it somewhat easier to run failed test by hand
146 #rm -rf "$LINKSDIR"
147
148 if [ $status -ne 0 ] && [ x"$VERBOSE" = x ]; then
149         echo "Failures detected, running with -v (verbose) will give more info"
150 fi
151 exit $status