blob: 3f7f1e1cb4aecf41c7f56a8663bd46b6ee5d977a (
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
|
#!/bin/sh
#
# Common code used run regression tests for usr.bin/make.
#
# $FreeBSD$
#
# Output a message and exit with an error.
#
fatal()
{
echo "fatal: $*" >/dev/stderr
exit 1
}
#
# Check whether the working directory exists - it must.
#
ensure_workdir()
{
if [ ! -d ${WORK_DIR} ] ; then
fatal "working directory ${WORK_DIR} does not exist."
fi
}
#
# Make sure all tests have been run
#
ensure_run()
{
if [ -z "${TEST_N}" ] ; then
TEST_N=1
fi
FAIL=
N=1
while [ ${N} -le ${TEST_N} ] ; do
eval skip=\${TEST_${N}_SKIP}
if [ -z "${skip}" ] ; then
if [ ! -f ${OUTPUT_DIR}/status.${N} -o \
! -f ${OUTPUT_DIR}/stdout.${N} -o \
! -f ${OUTPUT_DIR}/stderr.${N} ] ; then
echo "Test ${SUBDIR}/${N} no yet run"
FAIL=yes
fi
fi
N=$((N + 1))
done
if [ ! -z "${FAIL}" ] ; then
exit 1
fi
}
#
# Output usage messsage.
#
print_usage()
{
echo "Usage: sh -v -m <path> -w <dir> $0 command(s)"
echo " setup - setup working directory"
echo " run - run the tests"
echo " show - show test results"
echo " compare - compare actual and expected results"
echo " diff - diff actual and expected results"
echo " reset - reset the test to its initial state"
echo " clean - delete working and output directory"
echo " test - setup + run + compare"
echo " prove - setup + run + compare + clean"
echo " desc - print short description"
echo " update - update the expected results with the current results"
echo " help - show this information"
}
#
# Common function for setup and reset.
#
common_setup()
{
#
# If a Makefile exists in the source directory - copy it over
#
if [ -e Makefile -a ! -e ${WORK_DIR}/Makefile ] ; then
cp Makefile ${WORK_DIR}/Makefile
fi
#
# If the TEST_MAKE_DIRS variable is set, create those directories
#
set -- ${TEST_MAKE_DIRS}
while [ $# -ne 0 ] ; do
if [ ! -d ${WORK_DIR}/${1} ] ; then
mkdir -p -m ${2} ${WORK_DIR}/${1}
else
chmod ${2} ${WORK_DIR}/${1}
fi
shift ; shift
done
#
# If the TEST_COPY_FILES variable is set, copy those files over to
# the working directory. The value is assumed to be pairs of
# filenames and modes.
#
set -- ${TEST_COPY_FILES}
while [ $# -ne 0 ] ; do
if [ ! -e ${WORK_DIR}/${1} ] ; then
cp ${1} ${WORK_DIR}/${1}
fi
chmod ${2} ${WORK_DIR}/${1}
shift ; shift
done
#
# If the TEST_TOUCH variable is set, it is taken to be a list
# of pairs of filenames and arguments to touch(1). The arguments
# to touch must be surrounded by single quotes if there are more
# than one argument.
#
eval set -- ${TEST_TOUCH}
while [ $# -ne 0 ] ; do
eval touch ${2} ${WORK_DIR}/${1}
shift ; shift
done
#
# Now create links
#
eval set -- ${TEST_LINKS}
while [ $# -ne 0 ] ; do
eval ln ${WORK_DIR}/${1} ${WORK_DIR}/${2}
shift ; shift
done
}
#
# Setup the test. This creates the working and output directories and
# populates it with files. If there is a setup_test() function - call it.
#
eval_setup()
{
#
# Check whether the working directory exists. If it does exit
# fatally so that we don't clobber a test the user is working on.
#
if [ -d ${WORK_DIR} ] ; then
fatal "working directory ${WORK_DIR} already exists."
fi
#
# Now create it and the output directory
#
mkdir -p ${WORK_DIR}
rm -rf ${OUTPUT_DIR}
mkdir -p ${OUTPUT_DIR}
#
# Common stuff
#
common_setup
#
# Now after all execute the user's setup function if it exists.
#
setup_test
}
#
# Default setup_test function does nothing. This may be overriden by
# the test.
#
setup_test()
{
}
#
# Reset the test. Here we need to rely on information from the test.
# We executed the same steps as in the setup, by try not to clobber existing
# files.
# All files and directories that are listed on the TEST_CLEAN_FILES
# variable are removed. Then the TEST_TOUCH list is executed and finally
# the reset_test() function called if it exists.
#
eval_reset()
{
ensure_workdir
#
# Clean the output directory
#
rm -rf ${OUTPUT_DIR}/*
#
# Common stuff
#
common_setup
#
# Remove files.
#
for f in ${TEST_CLEAN_FILES} ; do
rm -rf ${WORK_DIR}/${f}
done
#
# Execute test's function
#
reset_test
}
#
# Default reset_test function does nothing. This may be overriden by
# the test.
#
reset_test()
{
}
#
# Clean the test. This simply removes the working and output directories.
#
eval_clean()
{
rm -rf ${WORK_DIR}
rm -rf ${OUTPUT_DIR}
}
#
# Run the test.
#
eval_run()
{
ensure_workdir
if [ -z "${TEST_N}" ] ; then
TEST_N=1
fi
N=1
while [ ${N} -le ${TEST_N} ] ; do
eval skip=\${TEST_${N}_SKIP}
if [ -z "${skip}" ] ; then
( cd ${WORK_DIR} ;
exec 1>${OUTPUT_DIR}/stdout.${N} 2>${OUTPUT_DIR}/stderr.${N}
run_test ${N}
echo $? >${OUTPUT_DIR}/status.${N}
)
fi
N=$((N + 1))
done
}
#
# Default run_test() function. It can be replaced by the
# user specified regression test. The argument to this function is
# the test number.
#
run_test()
{
eval args=\${TEST_${1}-test${1}}
${MAKE_PROG} $args
}
#
# Show test results.
#
eval_show()
{
ensure_workdir
if [ -z "${TEST_N}" ] ; then
TEST_N=1
fi
N=1
while [ ${N} -le ${TEST_N} ] ; do
eval skip=\${TEST_${N}_SKIP}
if [ -z "${skip}" ] ; then
echo "=== Test ${N} Status =================="
cat ${OUTPUT_DIR}/status.${N}
echo ".......... Stdout .................."
cat ${OUTPUT_DIR}/stdout.${N}
echo ".......... Stderr .................."
cat ${OUTPUT_DIR}/stderr.${N}
fi
N=$((N + 1))
done
}
#
# Compare results with expected results
#
eval_compare()
{
ensure_workdir
ensure_run
if [ -z "${TEST_N}" ] ; then
TEST_N=1
fi
echo "1..${TEST_N}"
N=1
while [ ${N} -le ${TEST_N} ] ; do
eval skip=\${TEST_${N}_SKIP}
if [ -z "${skip}" ] ; then
FAIL=
do_compare stdout ${N} || FAIL="${FAIL}stdout "
do_compare stderr ${N} || FAIL="${FAIL}stderr "
do_compare status ${N} || FAIL="${FAIL}status "
if [ ! -z "$FAIL" ]; then
echo "not ok ${N} ${SUBDIR}/${N} # reason: ${FAIL}"
else
echo "ok ${N} ${SUBDIR}/${N}"
fi
else
echo "ok ${N} ${SUBDIR}/${N} # skip: ${skip}"
fi
N=$((N + 1))
done
}
#
# Check if the test result is the same as the expected result.
#
# $1 Input file
# $2 Test number
#
do_compare()
{
local EXPECTED RESULT
EXPECTED="expected.$1.$2"
RESULT="${OUTPUT_DIR}/$1.$2"
if [ -f $EXPECTED ]; then
diff -q $EXPECTED $RESULT 1>/dev/null 2>/dev/null
return $?
else
return 1 # FAIL
fi
}
#
# Diff current and expected results
#
eval_diff()
{
ensure_workdir
ensure_run
if [ -z "${TEST_N}" ] ; then
TEST_N=1
fi
N=1
while [ ${N} -le ${TEST_N} ] ; do
eval skip=\${TEST_${N}_SKIP}
if [ -z "${skip}" ] ; then
FAIL=
do_diff stdout ${N}
do_diff stderr ${N}
do_diff status ${N}
fi
N=$((N + 1))
done
}
#
# Check if the test result is the same as the expected result.
#
# $1 Input file
# $2 Test number
#
do_diff()
{
local EXPECTED RESULT
EXPECTED="expected.$1.$2"
RESULT="${OUTPUT_DIR}/$1.$2"
echo diff -u $EXPECTED $RESULT
if [ -f $EXPECTED ]; then
diff -u $EXPECTED $RESULT
else
echo "${EXPECTED} does not exist"
fi
}
#
# Update expected results
#
eval_update()
{
ensure_workdir
ensure_run
if [ -z "${TEST_N}" ] ; then
TEST_N=1
fi
FAIL=
N=1
while [ ${N} -le ${TEST_N} ] ; do
eval skip=\${TEST_${N}_SKIP}
if [ -z "${skip}" ] ; then
cp ${OUTPUT_DIR}/stdout.${N} expected.stdout.${N}
cp ${OUTPUT_DIR}/stderr.${N} expected.stderr.${N}
cp ${OUTPUT_DIR}/status.${N} expected.status.${N}
fi
N=$((N + 1))
done
}
#
# Print description
#
eval_desc()
{
echo "${SUBDIR}: ${DESC}"
}
#
# Run the test
#
eval_test()
{
eval_setup
eval_run
eval_compare
}
#
# Run the test for prove(1)
#
eval_prove()
{
eval_setup
eval_run
eval_compare
eval_clean
}
#
# Main function. Execute the command(s) on the command line.
#
eval_cmd()
{
if [ $# -eq 0 ] ; then
# if no arguments given default to 'prove'
set -- prove
fi
for i
do
case $i in
setup | run | compare | diff | clean | reset | show | \
test | prove | desc | update)
eval eval_$i
;;
* | help)
print_usage
;;
esac
done
}
##############################################################################
#
# Main code
#
#
# 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}/@@"`
#
# Construct working directory
#
WORK_BASE=${WORK_BASE:-"/tmp/$USER.make.test"}
WORK_DIR=${WORK_BASE}/${SUBDIR}
OUTPUT_DIR=${WORK_DIR}.OUTPUT
#
# Make to use
#
MAKE_PROG=${MAKE_PROG:-/usr/bin/make}
|