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