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.
237 # If you have special cleaning needs, provide a 'cleanup' shell script.
239 if [ -n "${TEST_CLEANUP}" ] ; then
253 if [ -z "${TEST_N}" ] ; then
258 while [ ${N} -le ${TEST_N} ] ; do
259 if ! skip_test ${N} ; then
261 exec 1>${OUTPUT_DIR}/stdout.${N} 2>${OUTPUT_DIR}/stderr.${N}
263 echo $? >${OUTPUT_DIR}/status.${N}
271 # Default run_test() function. It can be replaced by the
272 # user specified regression test. The argument to this function is
277 eval args=\${TEST_${1}-test${1}}
288 if [ -z "${TEST_N}" ] ; then
293 while [ ${N} -le ${TEST_N} ] ; do
294 if ! skip_test ${N} ; then
295 echo "=== Test ${N} Status =================="
296 cat ${OUTPUT_DIR}/status.${N}
297 echo ".......... Stdout .................."
298 cat ${OUTPUT_DIR}/stdout.${N}
299 echo ".......... Stderr .................."
300 cat ${OUTPUT_DIR}/stderr.${N}
307 # Compare results with expected results
314 if [ -z "${TEST_N}" ] ; then
320 while [ ${N} -le ${TEST_N} ] ; do
323 if ! skip_test ${N} ; then
324 do_compare stdout ${N} || fail="${fail}stdout "
325 do_compare stderr ${N} || fail="${fail}stderr "
326 do_compare status ${N} || fail="${fail}status "
327 eval todo=\${TEST_${N}_TODO}
329 if [ ! -z "$fail" ]; then
332 echo -n "ok ${N} ${SUBDIR}/${N}"
333 if [ ! -z "$fail" -o ! -z "$todo" ]; then
336 if [ ! -z "$todo" ] ; then
337 echo -n "TODO $todo; "
339 if [ ! -z "$fail" ] ; then
340 echo "reason: ${fail}"
348 # Check if the test result is the same as the expected result.
355 local EXPECTED RESULT
356 EXPECTED="expected.$1.$2"
357 RESULT="${OUTPUT_DIR}/$1.$2"
359 if [ -f $EXPECTED ]; then
360 diff -q $EXPECTED $RESULT 1>/dev/null 2>/dev/null
368 # Diff current and expected results
375 if [ -z "${TEST_N}" ] ; then
380 while [ ${N} -le ${TEST_N} ] ; do
381 if ! skip_test ${N} ; then
392 # Check if the test result is the same as the expected result.
399 local EXPECTED RESULT
400 EXPECTED="expected.$1.$2"
401 RESULT="${OUTPUT_DIR}/$1.$2"
403 echo diff -u $EXPECTED $RESULT
404 if [ -f $EXPECTED ]; then
405 diff -u $EXPECTED $RESULT
407 echo "${EXPECTED} does not exist"
412 # Update expected results
419 if [ -z "${TEST_N}" ] ; then
425 while [ ${N} -le ${TEST_N} ] ; do
426 if ! skip_test ${N} ; then
427 cp ${OUTPUT_DIR}/stdout.${N} expected.stdout.${N}
428 cp ${OUTPUT_DIR}/stderr.${N} expected.stderr.${N}
429 cp ${OUTPUT_DIR}/status.${N} expected.status.${N}
440 echo "${SUBDIR}: ${DESC}"
454 # Run the test for prove(1)
465 # Main function. Execute the command(s) on the command line.
469 if [ $# -eq 0 ] ; then
470 # if no arguments given default to 'prove'
478 setup | run | compare | diff | clean | reset | show | \
479 test | prove | desc | update)
489 ##############################################################################
495 # Parse command line arguments.
497 args=`getopt m:w:v $*`
527 # Determine our sub-directory. Argh.
530 SRC_BASE=`while [ ! -f common.sh ] ; do cd .. ; done ; pwd`
531 SUBDIR=`echo ${SRC_DIR} | sed "s@${SRC_BASE}/@@"`
534 # Construct working directory
536 WORK_BASE=${WORK_BASE:-"/tmp/$USER.make.test"}
537 WORK_DIR=${WORK_BASE}/${SUBDIR}
538 OUTPUT_DIR=${WORK_DIR}.OUTPUT
543 MAKE_PROG=${MAKE_PROG:-/usr/bin/make}