blob: 9a4fccbbf89cb38714129cf346893ab0754bed1f [file] [log] [blame]
Matt Kraai38890782001-10-30 23:11:20 +00001#!/bin/sh
Denis Vlasenko3b92eaa2008-02-13 14:30:33 +00002# Usage:
3# runtest [applet1] [applet2...]
4
Denys Vlasenko875f7822009-07-18 16:34:01 +02005. ./testing.sh
Denis Vlasenkoabe49fa2008-07-11 21:41:14 +00006
Dan Fandrich140ac912010-08-29 04:47:03 +02007total_failed=0
8
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +00009# Run one old-style test.
10# Tests are stored in applet/testcase shell scripts.
11# They are run using "sh -x -e applet/testcase".
12# Option -e will make testcase stop on the first failed command.
Denis Vlasenko018e0852007-02-25 00:40:37 +000013run_applet_testcase()
Matt Kraai38890782001-10-30 23:11:20 +000014{
Denis Vlasenko95842fb2008-04-25 08:43:01 +000015 local applet="$1"
Denis Vlasenko58dc2742008-04-23 06:45:11 +000016 local testcase="$2"
Matt Kraai38890782001-10-30 23:11:20 +000017
Denis Vlasenko81e97a12008-05-15 22:43:48 +000018 local status=0
Denis Vlasenko92a6f872008-04-27 03:33:29 +000019 local uc_applet=$(echo "$applet" | tr a-z A-Z)
20 local testname="$testcase"
Matt Kraai38890782001-10-30 23:11:20 +000021
Denis Vlasenko5d0f4aa2008-04-27 13:34:43 +000022 testname="${testname##*/}" # take basename
23 if grep "^# CONFIG_$uc_applet is not set$" "$bindir/.config" >/dev/null; then
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000024 echo "UNTESTED: $testname"
Matt Kraai38890782001-10-30 23:11:20 +000025 return 0
26 fi
27
Denis Vlasenko5d0f4aa2008-04-27 13:34:43 +000028 if grep "^# FEATURE: " "$testcase" >/dev/null; then
Denis Vlasenko95842fb2008-04-25 08:43:01 +000029 local feature=$(sed -ne 's/^# FEATURE: //p' "$testcase")
Matt Kraai38890782001-10-30 23:11:20 +000030
Chris Metcalf208d35d2010-04-02 09:57:27 +020031 for f in $feature; do
32 if grep "^# $f is not set$" "$bindir/.config" >/dev/null; then
33 echo "UNTESTED: $testname"
34 return 0
35 fi
36 done
Matt Kraai38890782001-10-30 23:11:20 +000037 fi
38
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000039 rm -rf ".tmpdir.$applet"
40 mkdir -p ".tmpdir.$applet"
41 cd ".tmpdir.$applet" || return 1
Matt Kraai38890782001-10-30 23:11:20 +000042
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000043# echo "Running testcase $testcase"
Denis Vlasenko81e97a12008-05-15 22:43:48 +000044 d="$tsdir" \
45 sh -x -e "$testcase" >"$testname.stdout.txt" 2>&1 || status=$?
Denis Vlasenko64d5bfd2008-04-26 10:14:25 +000046 if [ $status -ne 0 ]; then
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000047 echo "FAIL: $testname"
48 if [ x"$VERBOSE" != x ]; then
49 cat "$testname.stdout.txt"
Bernhard Reutner-Fischer89a22ea2006-05-25 13:24:02 +000050 fi
Matt Kraai38890782001-10-30 23:11:20 +000051 else
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000052 echo "PASS: $testname"
Matt Kraai38890782001-10-30 23:11:20 +000053 fi
54
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000055 cd ..
56 rm -rf ".tmpdir.$applet"
Matt Kraai38890782001-10-30 23:11:20 +000057
58 return $status
59}
60
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000061# Run all old-style tests for given applet
Denis Vlasenko3c07e4b2008-04-29 14:11:57 +000062run_oldstyle_applet_tests()
Matt Kraai38890782001-10-30 23:11:20 +000063{
Denis Vlasenko5d0f4aa2008-04-27 13:34:43 +000064 local applet="$1"
Matt Kraai38890782001-10-30 23:11:20 +000065 local status=0
Denis Vlasenko92a6f872008-04-27 03:33:29 +000066
67 for testcase in "$tsdir/$applet"/*; do
68 # switch on basename of $testcase
69 case "${testcase##*/}" in
Denis Vlasenko57be4ab2008-05-02 09:19:29 +000070 .*) continue ;; # .svn, .git etc
71 *~) continue ;; # backup files
72 "CVS") continue ;;
73 \#*) continue ;; # CVS merge residues
74 *.mine) continue ;; # svn-produced junk
75 *.r[0-9]*) continue ;; # svn-produced junk
Denis Vlasenko58dc2742008-04-23 06:45:11 +000076 esac
Denis Vlasenko81e97a12008-05-15 22:43:48 +000077 run_applet_testcase "$applet" "$testcase" || status=1
Dan Fandrich140ac912010-08-29 04:47:03 +020078 total_failed=$((total_failed + status))
Matt Kraai38890782001-10-30 23:11:20 +000079 done
Matt Kraai38890782001-10-30 23:11:20 +000080 return $status
81}
82
83
Matt Kraai38890782001-10-30 23:11:20 +000084
Denis Vlasenko92a6f872008-04-27 03:33:29 +000085lcwd=$(pwd)
Denys Vlasenkoed910c72010-01-31 00:10:18 +010086[ x"$tsdir" != x"" ] || tsdir="$lcwd"
87[ x"$bindir" != x"" ] || bindir="${lcwd%/*}" # one directory up from $lcwd
Denis Vlasenko8d0a7342007-11-13 22:23:57 +000088PATH="$bindir:$PATH"
Denys Vlasenkoed910c72010-01-31 00:10:18 +010089export bindir # some tests need to look at $bindir/.config
Denis Vlasenko8d0a7342007-11-13 22:23:57 +000090
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000091if [ x"$VERBOSE" = x ]; then
92 export VERBOSE=
93fi
94
Matt Kraai01d2ea92002-01-02 20:37:59 +000095if [ x"$1" = x"-v" ]; then
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000096 export VERBOSE=1
Matt Kraai01d2ea92002-01-02 20:37:59 +000097 shift
98fi
99
Denis Vlasenko8d0a7342007-11-13 22:23:57 +0000100implemented=$(
Denys Vlasenkoe2082792010-01-16 02:49:40 +0100101 printf "busybox " # always implemented
Denis Vlasenko92a6f872008-04-27 03:33:29 +0000102 "$bindir/busybox" 2>&1 |
Denis Vlasenko8d0a7342007-11-13 22:23:57 +0000103 while read line; do
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000104 if [ x"$line" = x"Currently defined functions:" ]; then
Denis Vlasenko018e0852007-02-25 00:40:37 +0000105 xargs | sed 's/,//g'
106 break
107 fi
108 done
109 )
Denis Vlasenko8d0a7342007-11-13 22:23:57 +0000110
111applets="$implemented"
112if [ $# -ne 0 ]; then
113 applets="$@"
114fi
115
116# Populate a directory with links to all busybox applets
117
118LINKSDIR="$bindir/runtest-tempdir-links"
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200119
120# Comment this line out if you have put a different binary in $LINKSDIR
121# (say, a "standard" tool's binary) in order to run tests against it:
122rm -rf "$LINKSDIR" 2>/dev/null
123
Denys Vlasenkocd3dd422009-06-15 09:16:27 +0200124mkdir "$LINKSDIR" 2>/dev/null
Denis Vlasenko8d0a7342007-11-13 22:23:57 +0000125for i in $implemented; do
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200126 # Note: if $LINKSDIR/applet exists, we do not overwrite it.
127 # Useful if one wants to run tests against a standard utility,
128 # not an applet.
Denys Vlasenkocd3dd422009-06-15 09:16:27 +0200129 ln -s "$bindir/busybox" "$LINKSDIR/$i" 2>/dev/null
Rob Landley2824ded2006-03-16 16:02:06 +0000130done
131
Rob Landley48c61572005-11-07 08:50:53 +0000132# Set up option flags so tests can be selective.
Denis Vlasenko57be4ab2008-05-02 09:19:29 +0000133export OPTIONFLAGS=:$(
134 sed -nr 's/^CONFIG_//p' "$bindir/.config" |
135 sed 's/=.*//' | xargs | sed 's/ /:/g'
Denys Vlasenko29f354e2010-03-14 00:51:56 +0100136 ):
Rob Landley48c61572005-11-07 08:50:53 +0000137
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000138status=0
Matt Kraai38890782001-10-30 23:11:20 +0000139for applet in $applets; do
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000140 # Any old-style tests for this applet?
Denis Vlasenko3c07e4b2008-04-29 14:11:57 +0000141 if [ -d "$tsdir/$applet" ]; then
Denis Vlasenko81e97a12008-05-15 22:43:48 +0000142 run_oldstyle_applet_tests "$applet" || status=1
Matt Kraai38890782001-10-30 23:11:20 +0000143 fi
Rob Landley48c61572005-11-07 08:50:53 +0000144
145 # Is this a new-style test?
Denis Vlasenko5d0f4aa2008-04-27 13:34:43 +0000146 if [ -f "$applet.tests" ]; then
Denys Vlasenkocd3dd422009-06-15 09:16:27 +0200147 if [ ! -e "$LINKSDIR/$applet" ]; then
Denis Vlasenko95842fb2008-04-25 08:43:01 +0000148 # (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
Rob Landley48c61572005-11-07 08:50:53 +0000153 fi
Denis Vlasenko3c07e4b2008-04-29 14:11:57 +0000154# echo "Running test $tsdir/$applet.tests"
Denis Vlasenko81e97a12008-05-15 22:43:48 +0000155 PATH="$LINKSDIR:$tsdir:$bindir:$PATH" \
Dan Fandrich140ac912010-08-29 04:47:03 +0200156 "$tsdir/$applet.tests"
157 rc=$?
158 total_failed=$((total_failed + rc))
159 test $rc -ne 0 && status=1
Rob Landley16890752005-09-02 00:41:53 +0000160 fi
Matt Kraai38890782001-10-30 23:11:20 +0000161done
Denis Vlasenko4e1e7202007-11-26 05:38:20 +0000162
163# Leaving the dir makes it somewhat easier to run failed test by hand
164#rm -rf "$LINKSDIR"
165
Denis Vlasenko57be4ab2008-05-02 09:19:29 +0000166if [ $status -ne 0 ] && [ x"$VERBOSE" = x ]; then
Dan Fandrich140ac912010-08-29 04:47:03 +0200167 echo "$total_failed failure(s) detected; running with -v (verbose) will give more info"
Denis Vlasenko4e1e7202007-11-26 05:38:20 +0000168fi
Matt Kraai38890782001-10-30 23:11:20 +0000169exit $status