fix mountpoint test to not prevemt mkfs_xxx from making image in any file
[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                 if grep "^# $feature is not set$" "$bindir/.config" >/dev/null; then
30                         echo "UNTESTED: $testname"
31                         return 0
32                 fi
33         fi
34
35         rm -rf ".tmpdir.$applet"
36         mkdir -p ".tmpdir.$applet"
37         cd ".tmpdir.$applet" || return 1
38
39 #       echo "Running testcase $testcase"
40         d="$tsdir" \
41                 sh -x -e "$testcase" >"$testname.stdout.txt" 2>&1 || status=$?
42         if [ $status -ne 0 ]; then
43                 echo "FAIL: $testname"
44                 if [ x"$VERBOSE" != x ]; then
45                         cat "$testname.stdout.txt"
46                 fi
47         else
48                 echo "PASS: $testname"
49         fi
50
51         cd ..
52         rm -rf ".tmpdir.$applet"
53
54         return $status
55 }
56
57 # Run all old-style tests for given applet
58 run_oldstyle_applet_tests()
59 {
60         local applet="$1"
61         local status=0
62
63         for testcase in "$tsdir/$applet"/*; do
64                 # switch on basename of $testcase
65                 case "${testcase##*/}" in
66                         .*)     continue ;;    # .svn, .git etc
67                         *~)     continue ;;    # backup files
68                         "CVS")  continue ;;
69                         \#*)    continue ;;    # CVS merge residues
70                         *.mine) continue ;;    # svn-produced junk
71                         *.r[0-9]*) continue ;; # svn-produced junk
72                 esac
73                 run_applet_testcase "$applet" "$testcase" || status=1
74         done
75         return $status
76 }
77
78
79
80 lcwd=$(pwd)
81 [ x"$tsdir" != x ] || tsdir="$lcwd"
82 [ x"$bindir" != x ] || bindir="${lcwd%/*}" # one directory up from $lcwd
83 PATH="$bindir:$PATH"
84
85 if [ x"$VERBOSE" = x ]; then
86         export VERBOSE=
87 fi
88
89 if [ x"$1" = x"-v" ]; then
90         export VERBOSE=1
91         shift
92 fi
93
94 implemented=$(
95         "$bindir/busybox" 2>&1 |
96         while read line; do
97                 if [ x"$line" = x"Currently defined functions:" ]; then
98                         xargs | sed 's/,//g'
99                         break
100                 fi
101         done
102         )
103
104 applets="$implemented"
105 if [ $# -ne 0 ]; then
106         applets="$@"
107 fi
108
109 # Populate a directory with links to all busybox applets
110
111 LINKSDIR="$bindir/runtest-tempdir-links"
112
113 # Comment this line out if you have put a different binary in $LINKSDIR
114 # (say, a "standard" tool's binary) in order to run tests against it:
115 rm -rf "$LINKSDIR" 2>/dev/null
116
117 mkdir "$LINKSDIR" 2>/dev/null
118 for i in $implemented; do
119         # Note: if $LINKSDIR/applet exists, we do not overwrite it.
120         # Useful if one wants to run tests against a standard utility,
121         # not an applet.
122         ln -s "$bindir/busybox" "$LINKSDIR/$i" 2>/dev/null
123 done
124
125 # Set up option flags so tests can be selective.
126 export OPTIONFLAGS=:$(
127         sed -nr 's/^CONFIG_//p' "$bindir/.config" |
128         sed 's/=.*//' | xargs | sed 's/ /:/g'
129         )
130
131 status=0
132 for applet in $applets; do
133         # Any old-style tests for this applet?
134         if [ -d "$tsdir/$applet" ]; then
135                 run_oldstyle_applet_tests "$applet" || status=1
136         fi
137
138         # Is this a new-style test?
139         if [ -f "$applet.tests" ]; then
140                 if [ ! -e "$LINKSDIR/$applet" ]; then
141                         # (avoiding bash'ism "${applet:0:4}")
142                         if ! echo "$applet" | grep "^all_" >/dev/null; then
143                                 echo "SKIPPED: $applet (not built)"
144                                 continue
145                         fi
146                 fi
147 #               echo "Running test $tsdir/$applet.tests"
148                 PATH="$LINKSDIR:$tsdir:$bindir:$PATH" \
149                         "$tsdir/$applet.tests" || status=1
150         fi
151 done
152
153 # Leaving the dir makes it somewhat easier to run failed test by hand
154 #rm -rf "$LINKSDIR"
155
156 if [ $status -ne 0 ] && [ x"$VERBOSE" = x ]; then
157         echo "Failures detected, running with -v (verbose) will give more info"
158 fi
159 exit $status