Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 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 | |
Mike Frysinger | 3f91d7a | 2005-09-24 00:52:58 +0000 | [diff] [blame] | 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 |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 35 | |
Mike Frysinger | 3f91d7a | 2005-09-24 00:52:58 +0000 | [diff] [blame] | 36 | if [ -n "$VERBOSE" ] ; then |
| 37 | verbose=1 |
| 38 | fi |
| 39 | if [ -n "$DEBUG" ] ; then |
| 40 | debug=1 |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 41 | fi |
| 42 | |
| 43 | export FAILCOUNT=0 |
| 44 | |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 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 | |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 55 | # The testing function |
| 56 | |
Bernhard Reutner-Fischer | e34e878 | 2005-10-06 12:48:03 +0000 | [diff] [blame] | 57 | testing () |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 58 | { |
| 59 | if [ $# -ne 5 ] |
| 60 | then |
| 61 | echo "Test $1 has the wrong number of arguments" >&2 |
| 62 | exit |
| 63 | fi |
| 64 | |
Bernhard Reutner-Fischer | e34e878 | 2005-10-06 12:48:03 +0000 | [diff] [blame] | 65 | if [ $debug -eq 1 ] ; then |
Mike Frysinger | 3f91d7a | 2005-09-24 00:52:58 +0000 | [diff] [blame] | 66 | set -x |
| 67 | fi |
| 68 | |
Bernhard Reutner-Fischer | e34e878 | 2005-10-06 12:48:03 +0000 | [diff] [blame] | 69 | if [ -n "$_BB_CONFIG_DEP" ] && [ ${force} -eq 0 ] |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 70 | then |
| 71 | if ! config_is_set "$_BB_CONFIG_DEP" |
| 72 | then |
Mike Frysinger | 3f91d7a | 2005-09-24 00:52:58 +0000 | [diff] [blame] | 73 | echo "SKIPPED: $1" |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 74 | return 0 |
| 75 | fi |
| 76 | fi |
| 77 | |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 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 |
Mike Frysinger | 3f91d7a | 2005-09-24 00:52:58 +0000 | [diff] [blame] | 86 | ((FAILCOUNT++)) |
Rob Landley | b766c39 | 2005-09-04 11:10:37 +0000 | [diff] [blame] | 87 | echo "FAIL: $1" |
Bernhard Reutner-Fischer | e34e878 | 2005-10-06 12:48:03 +0000 | [diff] [blame] | 88 | if [ $verbose -eq 1 ] |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 89 | then |
| 90 | diff -u expected actual |
| 91 | fi |
| 92 | else |
Rob Landley | b766c39 | 2005-09-04 11:10:37 +0000 | [diff] [blame] | 93 | echo "PASS: $1" |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 94 | fi |
| 95 | rm -f input expected actual |
| 96 | |
Bernhard Reutner-Fischer | e34e878 | 2005-10-06 12:48:03 +0000 | [diff] [blame] | 97 | if [ $debug -eq 1 ] ; then |
Mike Frysinger | 3f91d7a | 2005-09-24 00:52:58 +0000 | [diff] [blame] | 98 | set +x |
| 99 | fi |
| 100 | |
Rob Landley | 1689075 | 2005-09-02 00:41:53 +0000 | [diff] [blame] | 101 | return $RETVAL |
| 102 | } |