420cfa1efba4fcc729a73b6691031c86c4322fd0
[oweals/busybox.git] / testsuite / testing.sh
1 # Simple test harness infrastructurei for BusyBox
2 #
3 # Copyright 2005 by Rob Landley
4 #
5 # License is GPLv2, see LICENSE in the busybox tarball for full license text.
6
7 # The "testing" function uses one environment variable:
8 #       COMMAND = command to execute
9 #
10 # The function takes five arguments:
11 #       $1) Description to display when running command
12 #       $2) Command line arguments to command"
13 #       $3) Expected result (on stdout)"
14 #       $4) Data written to file "input"
15 #       $5) Data written to stdin
16 #
17 # The exit value of testing is the exit value of the command it ran.
18 #
19 # The environment variable "FAILCOUNT" contains a cumulative total of the
20
21
22 # The command line parsing is ugly and should be improved.
23
24 if [ "$1" == "-v" ]
25 then
26   verbose=1
27 fi
28
29 export FAILCOUNT=0
30
31 # Helper functions
32
33 config_is_set ()
34 {
35   local uc_what=$(echo ${1?} | tr a-z A-Z)
36   grep -q "^[   ]*CONFIG_${uc_what}" ${bindir:-..}/.config || \
37     grep -q "^[         ]*BB_CONFIG_${uc_what}" ${bindir:-..}/.config
38   return $?
39 }
40
41 # The testing function
42
43 testing()
44 {
45   if [ $# -ne 5 ]
46   then
47     echo "Test $1 has the wrong number of arguments" >&2
48     exit
49   fi
50
51   if [ ${force_tests:-0} -ne 1 -a -n "$_BB_CONFIG_DEP" ]
52   then
53     if ! config_is_set "$_BB_CONFIG_DEP"
54     then
55       echo "UNTESTED: $1"
56       return 0
57     fi
58   fi
59
60   f=$FAILCOUNT
61   echo -ne "$3" > expected
62   echo -ne "$4" > input
63   echo -n -e "$5" | eval "$COMMAND $2" > actual
64   RETVAL=$?
65
66   cmp expected actual > /dev/null
67   if [ $? -ne 0 ]
68   then
69         FAILCOUNT=$[$FAILCOUNT+1]
70         echo "FAIL: $1"
71         if [ $verbose ]
72         then
73                 diff -u expected actual
74         fi
75   else
76         echo "PASS: $1"
77   fi
78   rm -f input expected actual
79
80   return $RETVAL
81 }