Makefile revision 1.66
11.66Srillig# $NetBSD: Makefile,v 1.66 2020/07/26 22:15:36 rillig Exp $
21.1Ssjg#
31.1Ssjg# Unit tests for make(1)
41.60Srillig#
51.1Ssjg# The main targets are:
61.1Ssjg#
71.60Srillig# all:
81.60Srillig#	run all the tests
91.60Srillig# test:
101.60Srillig#	run 'all', and compare to expected results
111.60Srillig# accept:
121.60Srillig#	move generated output to expected results
131.60Srillig#
141.61Srillig# Settable variables
151.61Srillig#
161.61Srillig# TEST_MAKE
171.61Srillig#	The make program to be tested.
181.61Srillig#
191.60Srillig#
201.60Srillig# Adding a test case
211.60Srillig#
221.11Ssjg# Each feature should get its own set of tests in its own suitably
231.42Sapb# named makefile (*.mk), with its own set of expected results (*.exp),
241.61Srillig# and it should be added to the TESTS list.
251.60Srillig#
261.60Srillig# Any added files must also be added to src/distrib/sets/lists/tests/mi.
271.61Srillig# Makefiles that are not added to TESTS must be ignored in
281.60Srillig# src/tests/usr.bin/make/t_make.sh (example: include-sub).
291.60Srillig#
301.1Ssjg
311.61Srillig# Each test is in a sub-makefile.
321.61Srillig# Keep the list sorted.
331.61SrilligTESTS+=		comment
341.61SrilligTESTS+=		cond-late
351.61SrilligTESTS+=		cond-short
361.61SrilligTESTS+=		cond1
371.61SrilligTESTS+=		cond2
381.61SrilligTESTS+=		dollar
391.61SrilligTESTS+=		doterror
401.61SrilligTESTS+=		dotwait
411.64SrilligTESTS+=		envfirst
421.61SrilligTESTS+=		error
431.63SsjgTESTS+=		# escape	# broken by reverting POSIX changes
441.61SrilligTESTS+=		export
451.61SrilligTESTS+=		export-all
461.61SrilligTESTS+=		export-env
471.61SrilligTESTS+=		forloop
481.61SrilligTESTS+=		forsubst
491.61SrilligTESTS+=		hash
501.63SsjgTESTS+=		# impsrc	# broken by reverting POSIX changes
511.61SrilligTESTS+=		include-main
521.61SrilligTESTS+=		misc
531.61SrilligTESTS+=		moderrs
541.61SrilligTESTS+=		modmatch
551.61SrilligTESTS+=		modmisc
561.61SrilligTESTS+=		modorder
571.61SrilligTESTS+=		modts
581.61SrilligTESTS+=		modword
591.61SrilligTESTS+=		order
601.63SsjgTESTS+=		# phony-end	# broken by reverting POSIX changes
611.61SrilligTESTS+=		posix
621.63SsjgTESTS+=		# posix1	# broken by reverting POSIX changes
631.61SrilligTESTS+=		qequals
641.63SsjgTESTS+=		# suffixes	# broken by reverting POSIX changes
651.61SrilligTESTS+=		sunshcmd
661.61SrilligTESTS+=		sysv
671.61SrilligTESTS+=		ternary
681.61SrilligTESTS+=		unexport
691.61SrilligTESTS+=		unexport-env
701.61SrilligTESTS+=		varcmd
711.64SrilligTESTS+=		vardebug
721.64SrilligTESTS+=		varfind
731.61SrilligTESTS+=		varmisc
741.61SrilligTESTS+=		varmod-edge
751.66SrilligTESTS+=		varparse-dynamic
761.61SrilligTESTS+=		varquote
771.61SrilligTESTS+=		varshell
781.61Srillig
791.64Srillig# Override environment variables for some of the tests.
801.64SrilligENV.envfirst=		FROM_ENV=value-from-env
811.65SrilligENV.varmisc=		FROM_ENV=env
821.65SrilligENV.varmisc+=		FROM_ENV_BEFORE=env
831.65SrilligENV.varmisc+=		FROM_ENV_AFTER=env
841.64Srillig
851.64Srillig# Override make flags for some of the tests; default is -k.
861.61SrilligFLAGS.doterror=		# none
871.61SrilligFLAGS.order=		-j1
881.64SrilligFLAGS.envfirst=		-e
891.64SrilligFLAGS.vardebug=		-k -dv FROM_CMDLINE=
901.61Srillig
911.61Srillig# Some tests need extra post-processing.
921.62SrilligSED_CMDS.modmisc+=	-e 's,\(substitution error:\).*,\1 (details omitted),'
931.61SrilligSED_CMDS.varshell+=	-e 's,^[a-z]*sh: ,,'
941.61SrilligSED_CMDS.varshell+=	-e '/command/s,No such.*,not found,'
951.61Srillig
961.64Srillig# Some tests need an additional round of postprocessing.
971.64SrilligPOSTPROC.vardebug=	${TOOL_SED} -n -e '/:RELEVANT = yes/,/:RELEVANT = no/p'
981.64Srillig
991.61Srillig# End of the configuration section.
1001.61Srillig
1011.1Ssjg.MAIN: all
1021.1Ssjg
1031.61SrilligUNIT_TESTS:=	${.PARSEDIR}
1041.44Sapb.PATH: ${UNIT_TESTS}
1051.7Ssjg
1061.61SrilligOUTFILES=	${TESTS:=.out}
1071.42Sapb
1081.42Sapball: ${OUTFILES}
1091.9Ssjg
1101.61SrilligCLEANFILES+=		*.rawout *.out *.status *.tmp *.core *.tmp
1111.61SrilligCLEANFILES+=		obj*.[och] lib*.a	# posix1.mk
1121.61SrilligCLEANFILES+=		issue* .[ab]*		# suffixes.mk
1131.61SrilligCLEANRECURSIVE+=	dir dummy		# posix1.mk
1141.48Sapb
1151.1Ssjgclean:
1161.48Sapb	rm -f ${CLEANFILES}
1171.48Sapb.if !empty(CLEANRECURSIVE)
1181.48Sapb	rm -rf ${CLEANRECURSIVE}
1191.48Sapb.endif
1201.1Ssjg
1211.61SrilligTEST_MAKE?=	${.MAKE}
1221.61SrilligTOOL_SED?=	sed
1231.1Ssjg
1241.28Ssjg# ensure consistent results from sort(1)
1251.61SrilligLC_ALL=		C
1261.61SrilligLANG=		C
1271.29Ssjg.export LANG LC_ALL
1281.28Ssjg
1291.42Sapb# the tests are actually done with sub-makes.
1301.42Sapb.SUFFIXES: .mk .rawout .out
1311.42Sapb.mk.rawout:
1321.64Srillig	@echo testing ${.IMPSRC}
1331.64Srillig	@set -eu; \
1341.64Srillig	cd ${.OBJDIR}; \
1351.64Srillig	${ENV.${.TARGET:R}} ${TEST_MAKE} \
1361.64Srillig	  ${FLAGS.${.TARGET:R}:U-k} -f ${.IMPSRC} \
1371.64Srillig	  > ${.TARGET}.tmp 2>&1 \
1381.64Srillig	&& status=$$? || status=$$?; \
1391.64Srillig	echo $$status > ${.TARGET:R}.status
1401.42Sapb	@mv ${.TARGET}.tmp ${.TARGET}
1411.42Sapb
1421.62Srillig# Post-process the test output so that the results can be compared.
1431.62Srillig#
1441.62Srillig# always pretend .MAKE was called 'make'
1451.61Srillig_SED_CMDS+=	-e 's,^${TEST_MAKE:T:S,.,\\.,g}[][0-9]*:,make:,'
1461.61Srillig_SED_CMDS+=	-e 's,${TEST_MAKE:S,.,\\.,g},make,'
1471.62Srillig# replace anything after 'stopped in' with unit-tests
1481.61Srillig_SED_CMDS+=	-e '/stopped/s, /.*, unit-tests,'
1491.62Srillig# strip ${.CURDIR}/ from the output
1501.61Srillig_SED_CMDS+=	-e 's,${.CURDIR:S,.,\\.,g}/,,g'
1511.61Srillig_SED_CMDS+=	-e 's,${UNIT_TESTS:S,.,\\.,g}/,,g'
1521.61Srillig
1531.42Sapb.rawout.out:
1541.42Sapb	@echo postprocess ${.TARGET}
1551.61Srillig	@${TOOL_SED} ${_SED_CMDS} ${SED_CMDS.${.TARGET:R}} \
1561.64Srillig	  < ${.IMPSRC} > ${.TARGET}.tmp1
1571.64Srillig	@${POSTPROC.${.TARGET:R}:U${TOOL_SED}} < ${.TARGET}.tmp1 > ${.TARGET}.tmp2
1581.64Srillig	@rm ${.TARGET}.tmp1
1591.64Srillig	@echo "exit status `cat ${.TARGET:R}.status`" >> ${.TARGET}.tmp2
1601.64Srillig	@mv ${.TARGET}.tmp2 ${.TARGET}
1611.42Sapb
1621.42Sapb# Compare all output files
1631.42Sapbtest:	${OUTFILES} .PHONY
1641.42Sapb	@failed= ; \
1651.61Srillig	for test in ${TESTS}; do \
1661.42Sapb	  diff -u ${UNIT_TESTS}/$${test}.exp $${test}.out \
1671.42Sapb	  || failed="$${failed}$${failed:+ }$${test}" ; \
1681.42Sapb	done ; \
1691.42Sapb	if [ -n "$${failed}" ]; then \
1701.42Sapb	  echo "Failed tests: $${failed}" ; false ; \
1711.42Sapb	else \
1721.42Sapb	  echo "All tests passed" ; \
1731.42Sapb	fi
1741.1Ssjg
1751.1Ssjgaccept:
1761.61Srillig	@for test in ${TESTS}; do \
1771.42Sapb	  cmp -s ${UNIT_TESTS}/$${test}.exp $${test}.out \
1781.42Sapb	  || { echo "Replacing $${test}.exp" ; \
1791.42Sapb	       cp $${test}.out ${UNIT_TESTS}/$${test}.exp ; } \
1801.42Sapb	done
1811.1Ssjg
1821.50Ssjg.if exists(${TEST_MAKE})
1831.61Srillig${TESTS:=.rawout}: ${TEST_MAKE}
1841.50Ssjg.endif
1851.50Ssjg
1861.42Sapb.-include <bsd.obj.mk>
187