summaryrefslogtreecommitdiffstats
path: root/tools/regression/usr.bin/make/common.sh
blob: 4bd9f7fe533cc1946b0646fef8fca28a0abc7b29 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/bin/sh
#
# Common code used run regression tests for usr.bin/make.
#
# $FreeBSD$

#
# Output usage messsage.
#
print_usage()
{
	echo "Usage: $0 command"
	echo "	clean	- remove temp files (get initial state)"
	echo "	compare	- compare result of test to expected"
	echo "	desc	- print description of test"
	echo "	diff	- print diffs between results and expected"
	echo "	harness	- produce output suiteable for Test::Harness"
	echo "	run	- run the {test, compare, clean}"
	echo "	test	- run test case"
	echo "	update	- update the expected with current results"
}

#
# Check if the test result is the same as the expected result.
#
# $1	Input file
#
hack_cmp()
{
	local EXPECTED RESULT
	EXPECTED="expected.$1"
	RESULT=${WORK_DIR}/$1

	if [ -f $EXPECTED ]; then
		diff -q $EXPECTED $RESULT 1> /dev/null 2> /dev/null
		return $?
	else
		return 1	# FAIL
	fi
}

#
# Check if the test result is the same as the expected result.
#
# $1	Input file
#
hack_diff()
{
	local EXPECTED RESULT
	EXPECTED="expected.$1"
	RESULT=${WORK_DIR}/$1

	echo diff -u $EXPECTED $RESULT
	if [ -f $EXPECTED ]; then
		diff -u $EXPECTED $RESULT
		return $?
	else
		return 1	# FAIL
	fi
}

#
# Default setup_test() function.
#
# The default function just does nothing.
#
# Both the variables SRC_BASE WORK_BASE are available.
#
setup_test()
{
}

#
# Default run_test() function.  It can be replace by the
# user specified regression test.
#
# Both the variables SRC_BASE WORK_BASE are available.
#
# Note: this function executes from a subshell.
#
run_test()
{
	cd ${WORK_DIR}
        $MAKE_PROG 1> stdout 2> stderr
        echo $? > status
}

#
# Default clean routine
#
clean_test()
{
}

#
# Clean working directory
#
eval_clean()
{
	rm -f ${WORK_DIR}/stdout
	rm -f ${WORK_DIR}/stderr
	rm -f ${WORK_DIR}/status
	clean_test
}

#
# Compare results with expected results
#
eval_compare()
{
	hack_cmp stdout || FAIL="stdout $FAIL"
	hack_cmp stderr || FAIL="stderr $FAIL"
	hack_cmp status || FAIL="status $FAIL"

	if [ ! -z "$FAIL" ]; then
		FAIL=`echo $FAIL`
		echo "$SUBDIR: Test failed {$FAIL}"
	fi
}

#
# Compare results with expected results for prove(1)
#
eval_hcompare()
{
	FAIL=
	hack_cmp stdout || FAIL="stdout $FAIL"
	hack_cmp stderr || FAIL="stderr $FAIL"
	hack_cmp status || FAIL="status $FAIL"

	if [ ! -z "$FAIL" ]; then
		FAIL=`echo $FAIL`
		echo "not ok 1 $SUBDIR # reason: {$FAIL}"
	else
		echo "ok 1 $SUBDIR"
	fi
}

#
# Print description
#
eval_desc()
{
	echo -n "$SUBDIR: "
	desc_test
}

#
# Prepare and run the test
#
eval_test()
{
	[ -d ${WORK_DIR} ] || mkdir -p ${WORK_DIR}
	if [ -f Makefile ] ; then
		cp Makefile ${WORK_DIR}
	fi
	setup_test
	( run_test )
}

#
# Diff current and expected results
#
eval_diff()
{
	eval_test
	echo "------------------------"
	echo "- $SUBDIR"
	echo "------------------------"
	hack_diff stdout
	hack_diff stderr
	hack_diff status
}

#
# Run the test for prove(1)
#
eval_harness()
{
	echo 1..1
	eval_test
	eval_hcompare
	eval_clean
}

#
# Run the test
#
eval_run()
{
	eval_test
	eval_compare
	eval_clean
}

#
# Update expected results
#
eval_update()
{
	eval_test
	cat ${WORK_DIR}/stdout > expected.stdout
	cat ${WORK_DIR}/stderr > expected.stderr
	cat ${WORK_DIR}/status > expected.status
}

#
# Note: Uses global variable $DIR which might be assigned by
#	the script which sourced this file.
#
eval_cmd()
{
	if [ $# -eq 0 ] ; then
		set -- harness
	fi

	case $1 in
	clean|compare|hcompare|desc|diff|harness|run|test|update)
		eval eval_$1
		;;
	*)
		print_usage
		;;
	esac
}

#
# Parse command line arguments.
#
args=`getopt m:w:v $*`
if [ $? != 0 ]; then
	echo 'Usage: ...'
	exit 2
fi
set -- $args
for i; do
	case "$i" in
	-m)
		MAKE_PROG="$2"
		shift
		shift
		;;
	-w)
		WORK_BASE="$2"
		shift
		shift
		;;
	-v)
		VERBOSE=1
		shift
		;;
	--)
		shift
		break
		;;
	esac
done

#
# Determine our sub-directory. Argh.
#
SRC_DIR=`pwd`
SRC_BASE=`while [ ! -f common.sh ] ; do cd .. ; done ; pwd`
SUBDIR=`echo ${SRC_DIR} | sed "s@${SRC_BASE}/@@"`

WORK_BASE=${WORK_BASE:-"/tmp/$USER.make.test"}
WORK_DIR=${WORK_BASE}/${SUBDIR}
MAKE_PROG=${MAKE_PROG:-/usr/bin/make}

export MAKE_PROG
export VERBOSE
export SRC_BASE
export WORK_BASE
export SUBDIR
export SRC_DIR
export WORK_DIR
OpenPOWER on IntegriCloud