summaryrefslogtreecommitdiff
path: root/tests/tester.sh
blob: bb49609c355b880ba2973cf45fc54837d54e34aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
#
# tester.sh - reads testcases from file and tests busybox applets vs GNU
# counterparts
#

# set up defaults (can be changed with cmd-line options)
BUSYBOX=../busybox
TESTCASES=testcases
LOGFILE=tester.log
BB_OUT=bb.out
GNU_OUT=gnu.out
SETUP=""
CLEANUP=""

# internal-use vars
fail_only=0


while getopts 'p:t:l:b:g:s:c:f' opt
do
	case $opt in
		p) BUSYBOX=$OPTARG; ;;
		t) TESTCASES=$OPTARG; ;;
		l) LOGFILE=$OPTARG; ;;
		b) BB_OUT=$OPTARG; ;;
		g) GNU_OUT=$OPTARG; ;;
		s) SETUP=$OPTARG; ;;
		c) CLEANUP=$OPTARG; ;;
		f) fail_only=1; ;;
		*)
			echo "usage: $0 [-ptlbgsc]"
			echo "  -p PATH  path to busybox executable"
			echo "  -t FILE  run testcases in FILE"
			echo "  -l FILE  log test results in FILE"
			echo "  -b FILE  store temporary busybox output in FILE"
			echo "  -g FILE  store temporary GNU output in FILE"
			echo "  -s FILE  (setup) run commands in FILE before testcases"
			echo "  -c FILE  (cleanup) run commands in FILE after testcases"
			echo "  -f       display only testcases that fail"
			exit 1
			;;
	esac
done
#shift `expr $OPTIND - 1`


# do normal setup
[ -e $LOGFILE ] && rm $LOGFILE
unalias -a	# gets rid of aliases that might create different output

# do extra setup (if any)
if [ ! -z $SETUP ] 
then
	echo "running setup commands in $SETUP"
	sh $SETUP
	# XXX: Would 'eval' or 'source' work better instead of 'sh'?
fi


# go through each line in the testcase file
cat $TESTCASES | while read line
do
	#echo $line
	# only process non-blank lines and non-comment lines
	if [ "$line" ]
	then
		if [ `echo "$line" | cut -c1` != "#" ]
		then
			[ $fail_only -eq 0 ] && echo "testing: $line" | tee -a $LOGFILE

			# test if the applet was compiled into busybox
			applet=`echo $line | cut -d' ' -f1`
			$BUSYBOX 2>&1 | grep -qw $applet
			if [ $? -eq 1 ]
			then
				echo "WHOOPS: $applet not compiled into busybox" | tee -a $LOGFILE
			else
				$BUSYBOX $line > $BB_OUT
				$line > $GNU_OUT
				diff -q $BB_OUT $GNU_OUT > /dev/null
				if [ $? -eq 1 ]
				then
					echo "FAILED: $line" | tee -a $LOGFILE
					diff -u $BB_OUT $GNU_OUT >> $LOGFILE 
				fi
			fi
		fi
	fi
done

echo "Finished. Results are in $LOGFILE"


# do normal cleanup
rm -f $BB_OUT $GNU_OUT

# do extra cleanup (if any)
if [ ! -z $CLEANUP ] 
then
	echo "running cleanup commands in $CLEANUP"
	sh $CLEANUP
	# XXX: Would 'eval' or 'source' work better instead of 'sh'?
fi