Makefile revision 1.44
11.44Sapb# $NetBSD: Makefile,v 1.44 2014/08/21 22:00:30 apb Exp $
21.1Ssjg#
31.1Ssjg# Unit tests for make(1)
41.1Ssjg# The main targets are:
51.1Ssjg# 
61.1Ssjg# all:	run all the tests
71.42Sapb# test:	run 'all', and compare to expected results
81.1Ssjg# accept: move generated output to expected results
91.1Ssjg#
101.1Ssjg# Adding a test case.  
111.11Ssjg# Each feature should get its own set of tests in its own suitably
121.42Sapb# named makefile (*.mk), with its own set of expected results (*.exp),
131.42Sapb# and it should be added to the TESTNAMES list.
141.1Ssjg# 
151.1Ssjg
161.1Ssjg.MAIN: all
171.1Ssjg
181.7SsjgUNIT_TESTS:= ${.PARSEDIR}
191.44Sapb.PATH: ${UNIT_TESTS}
201.7Ssjg
211.42Sapb# Each test is in a sub-makefile.
221.42Sapb# Keep the list sorted.
231.42SapbTESTNAMES= \
241.16Srpaulo	comment \
251.12Ssjg	cond1 \
261.26Ssjg	error \
271.21Ssjg	export \
281.21Ssjg	export-all \
291.36Ssjg	export-env \
301.26Ssjg	doterror \
311.19Sapb	dotwait \
321.34Ssjg	forloop \
331.24Ssjg	forsubst \
341.32Sjoerg	hash \
351.31Ssjg	misc \
361.20Ssjg	moderrs \
371.11Ssjg	modmatch \
381.18Ssjg	modmisc \
391.15Ssjg	modorder \
401.11Ssjg	modts \
411.11Ssjg	modword \
421.35Ssjg	order \
431.33Ssjg	phony-end \
441.13Ssjg	posix \
451.22Ssjg	qequals \
461.38Ssjg	sunshcmd \
471.31Ssjg	sysv \
481.11Ssjg	ternary \
491.25Ssjg	unexport \
501.25Ssjg	unexport-env \
511.39Sapb	varcmd \
521.43Sapb	varmisc \
531.39Sapb	varshell
541.11Ssjg
551.42Sapb# Override make flags for certain tests
561.26Ssjgflags.doterror=
571.35Ssjgflags.order=-j1
581.26Ssjg
591.42SapbOUTFILES= ${TESTNAMES:S/$/.out/}
601.42Sapb
611.42Sapball: ${OUTFILES}
621.9Ssjg
631.1Ssjgclean:
641.42Sapb	rm -f *.rawout *.out *.status *.tmp *.core *.tmp
651.1Ssjg
661.15SsjgTEST_MAKE?= ${.MAKE}
671.27SsjgTOOL_SED?= sed
681.1Ssjg
691.28Ssjg# ensure consistent results from sort(1)
701.30SsjgLC_ALL= C
711.28SsjgLANG= C
721.29Ssjg.export LANG LC_ALL
731.28Ssjg
741.42Sapb# the tests are actually done with sub-makes.
751.42Sapb.SUFFIXES: .mk .rawout .out
761.42Sapb.mk.rawout:
771.42Sapb	@echo ${TEST_MAKE} ${flags.${.TARGET:R}:U-k} -f ${.IMPSRC}
781.42Sapb	-@cd ${.OBJDIR} && \
791.42Sapb	{ ${TEST_MAKE} ${flags.${.TARGET:R}:U-k} -f ${.IMPSRC} \
801.42Sapb	  2>&1 ; echo $$? >${.TARGET:R}.status ; } > ${.TARGET}.tmp
811.42Sapb	@mv ${.TARGET}.tmp ${.TARGET}
821.42Sapb
831.12Ssjg# We always pretend .MAKE was called 'make' 
841.12Ssjg# and strip ${.CURDIR}/ from the output
851.13Ssjg# and replace anything after 'stopped in' with unit-tests
861.12Ssjg# so the results can be compared.
871.42Sapb.rawout.out:
881.42Sapb	@echo postprocess ${.TARGET}
891.42Sapb	@${TOOL_SED} -e 's,^${TEST_MAKE:T:C/\./\\\./g}[][0-9]*:,make:,' \
901.42Sapb	  -e 's,${TEST_MAKE:C/\./\\\./g},make,' \
911.42Sapb	  -e '/stopped/s, /.*, unit-tests,' \
921.42Sapb	  -e 's,${.CURDIR:C/\./\\\./g}/,,g' \
931.42Sapb	  -e 's,${UNIT_TESTS:C/\./\\\./g}/,,g' \
941.42Sapb	  < ${.IMPSRC} > ${.TARGET}.tmp
951.42Sapb	@echo "exit status `cat ${.TARGET:R}.status`" >> ${.TARGET}.tmp
961.42Sapb	@mv ${.TARGET}.tmp ${.TARGET}
971.42Sapb
981.42Sapb# Compare all output files
991.42Sapbtest:	${OUTFILES} .PHONY
1001.42Sapb	@failed= ; \
1011.42Sapb	for test in ${TESTNAMES}; do \
1021.42Sapb	  diff -u ${UNIT_TESTS}/$${test}.exp $${test}.out \
1031.42Sapb	  || failed="$${failed}$${failed:+ }$${test}" ; \
1041.42Sapb	done ; \
1051.42Sapb	if [ -n "$${failed}" ]; then \
1061.42Sapb	  echo "Failed tests: $${failed}" ; false ; \
1071.42Sapb	else \
1081.42Sapb	  echo "All tests passed" ; \
1091.42Sapb	fi
1101.1Ssjg
1111.1Ssjgaccept:
1121.42Sapb	@for test in ${TESTNAMES}; do \
1131.42Sapb	  cmp -s ${UNIT_TESTS}/$${test}.exp $${test}.out \
1141.42Sapb	  || { echo "Replacing $${test}.exp" ; \
1151.42Sapb	       cp $${test}.out ${UNIT_TESTS}/$${test}.exp ; } \
1161.42Sapb	done
1171.1Ssjg
1181.42Sapb.-include <bsd.obj.mk>
119