common.sh revision 145620
1#!/bin/sh
2#
3# Common code used run regression tests for usr.bin/make.
4#
5# $FreeBSD: head/tools/regression/usr.bin/make/common.sh 145620 2005-04-28 13:20:48Z harti $
6
7#
8# Output usage messsage.
9#
10print_usage()
11{
12	echo "Usage: $0 command"
13	echo "	clean	- remove temp files (get initial state)"
14	echo "	compare	- compare result of test to expected"
15	echo "	desc	- print description of test"
16	echo "	diff	- print diffs between results and expected"
17	echo "	harness	- produce output suiteable for Test::Harness"
18	echo "	run	- run the {test, compare, clean}"
19	echo "	test	- run test case"
20	echo "	update	- update the expected with current results"
21}
22
23#
24# Check if the test result is the same as the expected result.
25#
26# $1	Input file
27#
28hack_cmp()
29{
30	local EXPECTED RESULT
31	EXPECTED="expected.$1"
32	RESULT=${WORK_DIR}/$1
33
34	if [ -f $EXPECTED ]; then
35		diff -q $EXPECTED $RESULT 1> /dev/null 2> /dev/null
36		return $?
37	else
38		return 1	# FAIL
39	fi
40}
41
42#
43# Check if the test result is the same as the expected result.
44#
45# $1	Input file
46#
47hack_diff()
48{
49	local EXPECTED RESULT
50	EXPECTED="expected.$1"
51	RESULT=${WORK_DIR}/$1
52
53	echo diff -u $EXPECTED $RESULT
54	if [ -f $EXPECTED ]; then
55		diff -u $EXPECTED $RESULT
56		return $?
57	else
58		return 1	# FAIL
59	fi
60}
61
62#
63# Default setup_test() function.
64#
65# The default function just does nothing.
66#
67# Both the variables SRC_BASE WORK_BASE are available.
68#
69setup_test()
70{
71}
72
73#
74# Default run_test() function.  It can be replace by the
75# user specified regression test.
76#
77# Both the variables SRC_BASE WORK_BASE are available.
78#
79# Note: this function executes from a subshell.
80#
81run_test()
82{
83	cd ${WORK_DIR}
84        $MAKE_PROG 1> stdout 2> stderr
85        echo $? > status
86}
87
88#
89# Default clean routine
90#
91clean_test()
92{
93}
94
95#
96# Clean working directory
97#
98eval_clean()
99{
100	rm -f ${WORK_DIR}/stdout
101	rm -f ${WORK_DIR}/stderr
102	rm -f ${WORK_DIR}/status
103	clean_test
104}
105
106#
107# Compare results with expected results
108#
109eval_compare()
110{
111	hack_cmp stdout || FAIL="stdout $FAIL"
112	hack_cmp stderr || FAIL="stderr $FAIL"
113	hack_cmp status || FAIL="status $FAIL"
114
115	if [ ! -z "$FAIL" ]; then
116		FAIL=`echo $FAIL`
117		echo "$SUBDIR: Test failed {$FAIL}"
118	fi
119}
120
121#
122# Compare results with expected results for prove(1)
123#
124eval_hcompare()
125{
126	FAIL=
127	hack_cmp stdout || FAIL="stdout $FAIL"
128	hack_cmp stderr || FAIL="stderr $FAIL"
129	hack_cmp status || FAIL="status $FAIL"
130
131	if [ ! -z "$FAIL" ]; then
132		FAIL=`echo $FAIL`
133		echo "not ok 1 $SUBDIR # reason: {$FAIL}"
134	else
135		echo "ok 1 $SUBDIR"
136	fi
137}
138
139#
140# Print description
141#
142eval_desc()
143{
144	echo -n "$SUBDIR: "
145	desc_test
146}
147
148#
149# Prepare and run the test
150#
151eval_test()
152{
153	[ -d ${WORK_DIR} ] || mkdir -p ${WORK_DIR}
154	if [ -f Makefile ] ; then
155		cp Makefile ${WORK_DIR}
156	fi
157	setup_test
158	( run_test )
159}
160
161#
162# Diff current and expected results
163#
164eval_diff()
165{
166	eval_test
167	echo "------------------------"
168	echo "- $SUBDIR"
169	echo "------------------------"
170	hack_diff stdout
171	hack_diff stderr
172	hack_diff status
173}
174
175#
176# Run the test for prove(1)
177#
178eval_harness()
179{
180	echo 1..1
181	eval_test
182	eval_hcompare
183	eval_clean
184}
185
186#
187# Run the test
188#
189eval_run()
190{
191	eval_test
192	eval_compare
193	eval_clean
194}
195
196#
197# Update expected results
198#
199eval_update()
200{
201	eval_test
202	cat ${WORK_DIR}/stdout > expected.stdout
203	cat ${WORK_DIR}/stderr > expected.stderr
204	cat ${WORK_DIR}/status > expected.status
205}
206
207#
208# Note: Uses global variable $DIR which might be assigned by
209#	the script which sourced this file.
210#
211eval_cmd()
212{
213	if [ $# -eq 0 ] ; then
214		set -- harness
215	fi
216
217	case $1 in
218	clean|compare|hcompare|desc|diff|harness|run|test|update)
219		eval eval_$1
220		;;
221	*)
222		print_usage
223		;;
224	esac
225}
226
227#
228# Parse command line arguments.
229#
230args=`getopt m:w:v $*`
231if [ $? != 0 ]; then
232	echo 'Usage: ...'
233	exit 2
234fi
235set -- $args
236for i; do
237	case "$i" in
238	-m)
239		MAKE_PROG="$2"
240		shift
241		shift
242		;;
243	-w)
244		WORK_BASE="$2"
245		shift
246		shift
247		;;
248	-v)
249		VERBOSE=1
250		shift
251		;;
252	--)
253		shift
254		break
255		;;
256	esac
257done
258
259#
260# Determine our sub-directory. Argh.
261#
262SRC_DIR=`pwd`
263SRC_BASE=`while [ ! -f common.sh ] ; do cd .. ; done ; pwd`
264SUBDIR=`echo ${SRC_DIR} | sed "s@${SRC_BASE}/@@"`
265
266WORK_BASE=${WORK_BASE:-"/tmp/$USER.make.test"}
267WORK_DIR=${WORK_BASE}/${SUBDIR}
268MAKE_PROG=${MAKE_PROG:-/usr/bin/make}
269
270export MAKE_PROG
271export VERBOSE
272export SRC_BASE
273export WORK_BASE
274export SUBDIR
275export SRC_DIR
276export WORK_DIR
277