blob: c1002a6aa8c6c1f0ea245171ca82c4ae88542570 [file] [log] [blame]
Rob Landley16890752005-09-02 00:41:53 +00001# 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
Rob Landley48c61572005-11-07 08:50:53 +00007# This file defines two functions, "testing" and "optionflag"
8
9# The "testing" function must have the following environment variable set:
10# COMMAND = command to execute
Rob Landley16890752005-09-02 00:41:53 +000011#
Rob Landley48c61572005-11-07 08:50:53 +000012# The following environment variables may be set to enable optional behavior
13# in "testing":
14# VERBOSE - Print the diff -u of each failed test case.
15# DEBUG - Enable command tracing.
16# SKIP - do not perform this test (this is set by "optionflag")
17#
18# The "testing" function takes five arguments:
Rob Landley16890752005-09-02 00:41:53 +000019# $1) Description to display when running command
20# $2) Command line arguments to command"
21# $3) Expected result (on stdout)"
22# $4) Data written to file "input"
23# $5) Data written to stdin
24#
25# The exit value of testing is the exit value of the command it ran.
26#
27# The environment variable "FAILCOUNT" contains a cumulative total of the
Rob Landley48c61572005-11-07 08:50:53 +000028# number of failed tests.
Rob Landley16890752005-09-02 00:41:53 +000029
Rob Landley48c61572005-11-07 08:50:53 +000030# The "optional" function is used to skip certain tests, ala:
31# optionflag CONFIG_FEATURE_THINGY
32#
33# The "optional" function checks the environment variable "OPTIONFLAGS",
34# which is either empty (in which case it always clears SKIP) or
35# else contains a colon-separated list of features (in which case the function
36# clears SKIP if the flag was found, or sets it to 1 if the flag was not found).
Rob Landley16890752005-09-02 00:41:53 +000037
38export FAILCOUNT=0
Rob Landley48c61572005-11-07 08:50:53 +000039export SKIP=
Rob Landley16890752005-09-02 00:41:53 +000040
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000041# Helper functions
42
Rob Landley48c61572005-11-07 08:50:53 +000043optional()
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000044{
Rob Landley48c61572005-11-07 08:50:53 +000045 option="$OPTIONFLAGS" | egrep "(^|:)$1(:|\$)"
46 # Not set?
47 if [[ -z "$1" || -z "$OPTIONFLAGS" || ${#option} -ne 0 ]]
48 then
49 SKIP=""
50 return
51 fi
52 SKIP=1
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000053}
54
Rob Landley16890752005-09-02 00:41:53 +000055# The testing function
56
Bernhard Reutner-Fischere34e8782005-10-06 12:48:03 +000057testing ()
Rob Landley16890752005-09-02 00:41:53 +000058{
59 if [ $# -ne 5 ]
60 then
61 echo "Test $1 has the wrong number of arguments" >&2
62 exit
63 fi
64
Rob Landley48c61572005-11-07 08:50:53 +000065 if [ -n "$DEBUG" ] ; then
Mike Frysinger3f91d7a2005-09-24 00:52:58 +000066 set -x
67 fi
68
Rob Landley48c61572005-11-07 08:50:53 +000069 if [ -n "$SKIP" ]
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000070 then
Rob Landley48c61572005-11-07 08:50:53 +000071 echo "SKIPPED: $1"
72 return 0
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000073 fi
74
Rob Landley16890752005-09-02 00:41:53 +000075 echo -ne "$3" > expected
76 echo -ne "$4" > input
77 echo -n -e "$5" | eval "$COMMAND $2" > actual
78 RETVAL=$?
79
80 cmp expected actual > /dev/null
81 if [ $? -ne 0 ]
82 then
Rob Landley48c61572005-11-07 08:50:53 +000083 FAILCOUNT=$[$FAILCOUNT+1]
84 echo "FAIL: $1"
85 if [ -n "$VERBOSE" ]
86 then
87 diff -u expected actual
88 fi
Rob Landley16890752005-09-02 00:41:53 +000089 else
Rob Landley48c61572005-11-07 08:50:53 +000090 echo "PASS: $1"
Rob Landley16890752005-09-02 00:41:53 +000091 fi
92 rm -f input expected actual
93
Rob Landley48c61572005-11-07 08:50:53 +000094 if [ -n "$DEBUG" ]
95 then
Mike Frysinger3f91d7a2005-09-24 00:52:58 +000096 set +x
97 fi
98
Rob Landley16890752005-09-02 00:41:53 +000099 return $RETVAL
100}