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