blob: 3ff4b96dab64a3e936aa543b238d2e6cefaca1ad [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
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +00007# 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.
Denis Vlasenko018e0852007-02-25 00:40:37 +000011run_applet_testcase()
Matt Kraai38890782001-10-30 23:11:20 +000012{
Denis Vlasenko95842fb2008-04-25 08:43:01 +000013 local applet="$1"
Denis Vlasenko58dc2742008-04-23 06:45:11 +000014 local testcase="$2"
Matt Kraai38890782001-10-30 23:11:20 +000015
Denis Vlasenko81e97a12008-05-15 22:43:48 +000016 local status=0
Denis Vlasenko92a6f872008-04-27 03:33:29 +000017 local uc_applet=$(echo "$applet" | tr a-z A-Z)
18 local testname="$testcase"
Matt Kraai38890782001-10-30 23:11:20 +000019
Denis Vlasenko5d0f4aa2008-04-27 13:34:43 +000020 testname="${testname##*/}" # take basename
21 if grep "^# CONFIG_$uc_applet is not set$" "$bindir/.config" >/dev/null; then
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000022 echo "UNTESTED: $testname"
Matt Kraai38890782001-10-30 23:11:20 +000023 return 0
24 fi
25
Denis Vlasenko5d0f4aa2008-04-27 13:34:43 +000026 if grep "^# FEATURE: " "$testcase" >/dev/null; then
Denis Vlasenko95842fb2008-04-25 08:43:01 +000027 local feature=$(sed -ne 's/^# FEATURE: //p' "$testcase")
Matt Kraai38890782001-10-30 23:11:20 +000028
Denis Vlasenko5d0f4aa2008-04-27 13:34:43 +000029 if grep "^# $feature is not set$" "$bindir/.config" >/dev/null; then
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000030 echo "UNTESTED: $testname"
Matt Kraai38890782001-10-30 23:11:20 +000031 return 0
32 fi
33 fi
34
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000035 rm -rf ".tmpdir.$applet"
36 mkdir -p ".tmpdir.$applet"
37 cd ".tmpdir.$applet" || return 1
Matt Kraai38890782001-10-30 23:11:20 +000038
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000039# echo "Running testcase $testcase"
Denis Vlasenko81e97a12008-05-15 22:43:48 +000040 d="$tsdir" \
41 sh -x -e "$testcase" >"$testname.stdout.txt" 2>&1 || status=$?
Denis Vlasenko64d5bfd2008-04-26 10:14:25 +000042 if [ $status -ne 0 ]; then
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000043 echo "FAIL: $testname"
44 if [ x"$VERBOSE" != x ]; then
45 cat "$testname.stdout.txt"
Bernhard Reutner-Fischer89a22ea2006-05-25 13:24:02 +000046 fi
Matt Kraai38890782001-10-30 23:11:20 +000047 else
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000048 echo "PASS: $testname"
Matt Kraai38890782001-10-30 23:11:20 +000049 fi
50
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000051 cd ..
52 rm -rf ".tmpdir.$applet"
Matt Kraai38890782001-10-30 23:11:20 +000053
54 return $status
55}
56
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000057# Run all old-style tests for given applet
Denis Vlasenko3c07e4b2008-04-29 14:11:57 +000058run_oldstyle_applet_tests()
Matt Kraai38890782001-10-30 23:11:20 +000059{
Denis Vlasenko5d0f4aa2008-04-27 13:34:43 +000060 local applet="$1"
Matt Kraai38890782001-10-30 23:11:20 +000061 local status=0
Denis Vlasenko92a6f872008-04-27 03:33:29 +000062
63 for testcase in "$tsdir/$applet"/*; do
64 # switch on basename of $testcase
65 case "${testcase##*/}" in
Denis Vlasenko57be4ab2008-05-02 09:19:29 +000066 .*) 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
Denis Vlasenko58dc2742008-04-23 06:45:11 +000072 esac
Denis Vlasenko81e97a12008-05-15 22:43:48 +000073 run_applet_testcase "$applet" "$testcase" || status=1
Matt Kraai38890782001-10-30 23:11:20 +000074 done
Matt Kraai38890782001-10-30 23:11:20 +000075 return $status
76}
77
78
Matt Kraai38890782001-10-30 23:11:20 +000079
Denis Vlasenko92a6f872008-04-27 03:33:29 +000080lcwd=$(pwd)
Denis Vlasenko7dff4332008-04-27 20:48:49 +000081[ x"$tsdir" != x ] || tsdir="$lcwd"
82[ x"$bindir" != x ] || bindir="${lcwd%/*}" # one directory up from $lcwd
Denis Vlasenko8d0a7342007-11-13 22:23:57 +000083PATH="$bindir:$PATH"
84
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000085if [ x"$VERBOSE" = x ]; then
86 export VERBOSE=
87fi
88
Matt Kraai01d2ea92002-01-02 20:37:59 +000089if [ x"$1" = x"-v" ]; then
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000090 export VERBOSE=1
Matt Kraai01d2ea92002-01-02 20:37:59 +000091 shift
92fi
93
Denis Vlasenko8d0a7342007-11-13 22:23:57 +000094implemented=$(
Denis Vlasenko92a6f872008-04-27 03:33:29 +000095 "$bindir/busybox" 2>&1 |
Denis Vlasenko8d0a7342007-11-13 22:23:57 +000096 while read line; do
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000097 if [ x"$line" = x"Currently defined functions:" ]; then
Denis Vlasenko018e0852007-02-25 00:40:37 +000098 xargs | sed 's/,//g'
99 break
100 fi
101 done
102 )
Denis Vlasenko8d0a7342007-11-13 22:23:57 +0000103
104applets="$implemented"
105if [ $# -ne 0 ]; then
106 applets="$@"
107fi
108
109# Populate a directory with links to all busybox applets
110
111LINKSDIR="$bindir/runtest-tempdir-links"
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200112
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:
115rm -rf "$LINKSDIR" 2>/dev/null
116
Denys Vlasenkocd3dd422009-06-15 09:16:27 +0200117mkdir "$LINKSDIR" 2>/dev/null
Denis Vlasenko8d0a7342007-11-13 22:23:57 +0000118for i in $implemented; do
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200119 # 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.
Denys Vlasenkocd3dd422009-06-15 09:16:27 +0200122 ln -s "$bindir/busybox" "$LINKSDIR/$i" 2>/dev/null
Rob Landley2824ded2006-03-16 16:02:06 +0000123done
124
Rob Landley48c61572005-11-07 08:50:53 +0000125# Set up option flags so tests can be selective.
Denis Vlasenko57be4ab2008-05-02 09:19:29 +0000126export OPTIONFLAGS=:$(
127 sed -nr 's/^CONFIG_//p' "$bindir/.config" |
128 sed 's/=.*//' | xargs | sed 's/ /:/g'
129 )
Rob Landley48c61572005-11-07 08:50:53 +0000130
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000131status=0
Matt Kraai38890782001-10-30 23:11:20 +0000132for applet in $applets; do
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000133 # Any old-style tests for this applet?
Denis Vlasenko3c07e4b2008-04-29 14:11:57 +0000134 if [ -d "$tsdir/$applet" ]; then
Denis Vlasenko81e97a12008-05-15 22:43:48 +0000135 run_oldstyle_applet_tests "$applet" || status=1
Matt Kraai38890782001-10-30 23:11:20 +0000136 fi
Rob Landley48c61572005-11-07 08:50:53 +0000137
138 # Is this a new-style test?
Denis Vlasenko5d0f4aa2008-04-27 13:34:43 +0000139 if [ -f "$applet.tests" ]; then
Denys Vlasenkocd3dd422009-06-15 09:16:27 +0200140 if [ ! -e "$LINKSDIR/$applet" ]; then
Denis Vlasenko95842fb2008-04-25 08:43:01 +0000141 # (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
Rob Landley48c61572005-11-07 08:50:53 +0000146 fi
Denis Vlasenko3c07e4b2008-04-29 14:11:57 +0000147# echo "Running test $tsdir/$applet.tests"
Denis Vlasenko81e97a12008-05-15 22:43:48 +0000148 PATH="$LINKSDIR:$tsdir:$bindir:$PATH" \
149 "$tsdir/$applet.tests" || status=1
Rob Landley16890752005-09-02 00:41:53 +0000150 fi
Matt Kraai38890782001-10-30 23:11:20 +0000151done
Denis Vlasenko4e1e7202007-11-26 05:38:20 +0000152
153# Leaving the dir makes it somewhat easier to run failed test by hand
154#rm -rf "$LINKSDIR"
155
Denis Vlasenko57be4ab2008-05-02 09:19:29 +0000156if [ $status -ne 0 ] && [ x"$VERBOSE" = x ]; then
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000157 echo "Failures detected, running with -v (verbose) will give more info"
Denis Vlasenko4e1e7202007-11-26 05:38:20 +0000158fi
Matt Kraai38890782001-10-30 23:11:20 +0000159exit $status