3 # Common code used run regression tests for usr.bin/make.
8 # Output a message and exit with an error.
12 echo "fatal: $*" >/dev/stderr
17 # Check whether the working directory exists - it must.
21 if [ ! -d ${WORK_DIR} ] ; then
22 fatal "working directory ${WORK_DIR} does not exist."
27 # Make sure all tests have been run
31 if [ -z "${TEST_N}" ] ; then
37 while [ ${N} -le ${TEST_N} ] ; do
38 if ! skip_test ${N} ; then
39 if [ ! -f ${OUTPUT_DIR}/status.${N} -o \
40 ! -f ${OUTPUT_DIR}/stdout.${N} -o \
41 ! -f ${OUTPUT_DIR}/stderr.${N} ] ; then
42 echo "Test ${SUBDIR}/${N} no yet run"
49 if [ ! -z "${FAIL}" ] ; then
55 # Output usage messsage.
59 echo "Usage: sh -v -m <path> -w <dir> $0 command(s)"
60 echo " setup - setup working directory"
61 echo " run - run the tests"
62 echo " show - show test results"
63 echo " compare - compare actual and expected results"
64 echo " diff - diff actual and expected results"
65 echo " reset - reset the test to its initial state"
66 echo " clean - delete working and output directory"
67 echo " test - setup + run + compare"
68 echo " prove - setup + run + compare + clean"
69 echo " desc - print short description"
70 echo " update - update the expected results with the current results"
71 echo " help - show this information"
75 # Return 0 if we should skip the test. 1 otherwise
79 eval skip=\${TEST_${1}_SKIP}
80 if [ -z "${skip}" ] ; then
88 # Common function for setup and reset.
93 # If a Makefile exists in the source directory - copy it over
95 if [ -e Makefile -a ! -e ${WORK_DIR}/Makefile ] ; then
96 cp Makefile ${WORK_DIR}/Makefile
100 # If the TEST_MAKE_DIRS variable is set, create those directories
102 set -- ${TEST_MAKE_DIRS}
103 while [ $# -ne 0 ] ; do
104 if [ ! -d ${WORK_DIR}/${1} ] ; then
105 mkdir -p -m ${2} ${WORK_DIR}/${1}
107 chmod ${2} ${WORK_DIR}/${1}
113 # If the TEST_COPY_FILES variable is set, copy those files over to
114 # the working directory. The value is assumed to be pairs of
115 # filenames and modes.
117 set -- ${TEST_COPY_FILES}
118 while [ $# -ne 0 ] ; do
119 if [ ! -e ${WORK_DIR}/${1} ] ; then
120 cp ${1} ${WORK_DIR}/${1}
122 chmod ${2} ${WORK_DIR}/${1}
127 # If the TEST_TOUCH variable is set, it is taken to be a list
128 # of pairs of filenames and arguments to touch(1). The arguments
129 # to touch must be surrounded by single quotes if there are more
132 eval set -- ${TEST_TOUCH}
133 while [ $# -ne 0 ] ; do
134 eval touch ${2} ${WORK_DIR}/${1}
141 eval set -- ${TEST_LINKS}
142 while [ $# -ne 0 ] ; do
143 eval ln ${WORK_DIR}/${1} ${WORK_DIR}/${2}
149 # Setup the test. This creates the working and output directories and
150 # populates it with files. If there is a setup_test() function - call it.
155 # Check whether the working directory exists. If it does exit
156 # fatally so that we don't clobber a test the user is working on.
158 if [ -d ${WORK_DIR} ] ; then
159 fatal "working directory ${WORK_DIR} already exists."
163 # Now create it and the output directory
167 mkdir -p ${OUTPUT_DIR}
175 # Now after all execute the user's setup function if it exists.
181 # Default setup_test function does nothing. This may be overriden by
189 # Reset the test. Here we need to rely on information from the test.
190 # We executed the same steps as in the setup, by try not to clobber existing
192 # All files and directories that are listed on the TEST_CLEAN_FILES
193 # variable are removed. Then the TEST_TOUCH list is executed and finally
194 # the reset_test() function called if it exists.
201 # Clean the output directory
203 rm -rf ${OUTPUT_DIR}/*
213 for f in ${TEST_CLEAN_FILES} ; do
214 rm -rf ${WORK_DIR}/${f}
218 # Execute test's function
224 # Default reset_test function does nothing. This may be overriden by
232 # Clean the test. This simply removes the working and output directories.
247 if [ -z "${TEST_N}" ] ; then
252 while [ ${N} -le ${TEST_N} ] ; do
253 if ! skip_test ${N} ; then
255 exec 1>${OUTPUT_DIR}/stdout.${N} 2>${OUTPUT_DIR}/stderr.${N}
257 echo $? >${OUTPUT_DIR}/status.${N}
265 # Default run_test() function. It can be replaced by the
266 # user specified regression test. The argument to this function is
271 eval args=\${TEST_${1}-test${1}}
282 if [ -z "${TEST_N}" ] ; then
287 while [ ${N} -le ${TEST_N} ] ; do
288 if ! skip_test ${N} ; then
289 echo "=== Test ${N} Status =================="
290 cat ${OUTPUT_DIR}/status.${N}
291 echo ".......... Stdout .................."
292 cat ${OUTPUT_DIR}/stdout.${N}
293 echo ".......... Stderr .................."
294 cat ${OUTPUT_DIR}/stderr.${N}
301 # Compare results with expected results
308 if [ -z "${TEST_N}" ] ; then
314 while [ ${N} -le ${TEST_N} ] ; do
317 if ! skip_test ${N} ; then
318 do_compare stdout ${N} || fail="${fail}stdout "
319 do_compare stderr ${N} || fail="${fail}stderr "
320 do_compare status ${N} || fail="${fail}status "
321 eval todo=\${TEST_${N}_TODO}
323 if [ ! -z "$fail" ]; then
326 echo -n "ok ${N} ${SUBDIR}/${N}"
327 if [ ! -z "$fail" -o ! -z "$todo" ]; then
330 if [ ! -z "$todo" ] ; then
331 echo -n "TODO $todo; "
333 if [ ! -z "$fail" ] ; then
334 echo "reason: ${fail}"
342 # Check if the test result is the same as the expected result.
349 local EXPECTED RESULT
350 EXPECTED="expected.$1.$2"
351 RESULT="${OUTPUT_DIR}/$1.$2"
353 if [ -f $EXPECTED ]; then
354 diff -q $EXPECTED $RESULT 1>/dev/null 2>/dev/null
362 # Diff current and expected results
369 if [ -z "${TEST_N}" ] ; then
374 while [ ${N} -le ${TEST_N} ] ; do
375 if ! skip_test ${N} ; then
386 # Check if the test result is the same as the expected result.
393 local EXPECTED RESULT
394 EXPECTED="expected.$1.$2"
395 RESULT="${OUTPUT_DIR}/$1.$2"
397 echo diff -u $EXPECTED $RESULT
398 if [ -f $EXPECTED ]; then
399 diff -u $EXPECTED $RESULT
401 echo "${EXPECTED} does not exist"
406 # Update expected results
413 if [ -z "${TEST_N}" ] ; then
419 while [ ${N} -le ${TEST_N} ] ; do
420 if ! skip_test ${N} ; then
421 cp ${OUTPUT_DIR}/stdout.${N} expected.stdout.${N}
422 cp ${OUTPUT_DIR}/stderr.${N} expected.stderr.${N}
423 cp ${OUTPUT_DIR}/status.${N} expected.status.${N}
434 echo "${SUBDIR}: ${DESC}"
448 # Run the test for prove(1)
459 # Main function. Execute the command(s) on the command line.
463 if [ $# -eq 0 ] ; then
464 # if no arguments given default to 'prove'
472 setup | run | compare | diff | clean | reset | show | \
473 test | prove | desc | update)
483 ##############################################################################
489 # Parse command line arguments.
491 args=`getopt m:w:v $*`
521 # Determine our sub-directory. Argh.
524 SRC_BASE=`while [ ! -f common.sh ] ; do cd .. ; done ; pwd`
525 SUBDIR=`echo ${SRC_DIR} | sed "s@${SRC_BASE}/@@"`
528 # Construct working directory
530 WORK_BASE=${WORK_BASE:-"/tmp/$USER.make.test"}
531 WORK_DIR=${WORK_BASE}/${SUBDIR}
532 OUTPUT_DIR=${WORK_DIR}.OUTPUT
537 MAKE_PROG=${MAKE_PROG:-/usr/bin/make}