- support make check V=1 to run the checks in verbose mode
[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 verbose=0
23 debug=0
24 force=0
25 for x in "$@" ; do
26         case "$x" in
27         -v|--verbose)  verbose=1; shift;;
28         -d|--debug)    debug=1; shift;;
29         -f|--force)    force=1; shift;;
30         --)            break;;
31         -*)            echo "Unknown option '$x'"; exit 1;;
32         *)             break;;
33         esac
34 done
35
36 if [ -n "$VERBOSE" ] ; then
37         verbose=1
38 fi
39 if [ -n "$DEBUG" ] ; then
40         debug=1
41 fi
42
43 export FAILCOUNT=0
44
45 # Helper functions
46
47 config_is_set ()
48 {
49   local uc_what=$(echo ${1?} | tr a-z A-Z)
50   grep -q "^[   ]*CONFIG_${uc_what}" ${bindir:-..}/.config || \
51     grep -q "^[         ]*BB_CONFIG_${uc_what}" ${bindir:-..}/.config
52   return $?
53 }
54
55 # The testing function
56
57 testing ()
58 {
59   if [ $# -ne 5 ]
60   then
61     echo "Test $1 has the wrong number of arguments" >&2
62     exit
63   fi
64
65   if [ $debug -eq 1 ] ; then
66     set -x
67   fi
68
69   if [ -n "$_BB_CONFIG_DEP" ] && [ ${force} -eq 0 ]
70   then
71     if ! config_is_set "$_BB_CONFIG_DEP"
72     then
73       echo "SKIPPED: $1"
74       return 0
75     fi
76   fi
77
78   echo -ne "$3" > expected
79   echo -ne "$4" > input
80   echo -n -e "$5" | eval "$COMMAND $2" > actual
81   RETVAL=$?
82
83   cmp expected actual > /dev/null
84   if [ $? -ne 0 ]
85   then
86         ((FAILCOUNT++))
87         echo "FAIL: $1"
88         if [ $verbose -eq 1 ]
89         then
90                 diff -u expected actual
91         fi
92   else
93         echo "PASS: $1"
94   fi
95   rm -f input expected actual
96
97   if [ $debug -eq 1 ] ; then
98     set +x
99   fi
100
101   return $RETVAL
102 }