Makefile revision 1.65
11.65Srillig# $NetBSD: Makefile,v 1.65 2020/07/26 11:10:29 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.61SrilligTESTS+=		varquote
761.61SrilligTESTS+=		varshell
771.61Srillig
781.64Srillig# Override environment variables for some of the tests.
791.64SrilligENV.envfirst=		FROM_ENV=value-from-env
801.65SrilligENV.varmisc=		FROM_ENV=env
811.65SrilligENV.varmisc+=		FROM_ENV_BEFORE=env
821.65SrilligENV.varmisc+=		FROM_ENV_AFTER=env
831.64Srillig
841.64Srillig# Override make flags for some of the tests; default is -k.
851.61SrilligFLAGS.doterror=		# none
861.61SrilligFLAGS.order=		-j1
871.64SrilligFLAGS.envfirst=		-e
881.64SrilligFLAGS.vardebug=		-k -dv FROM_CMDLINE=
891.61Srillig
901.61Srillig# Some tests need extra post-processing.
911.62SrilligSED_CMDS.modmisc+=	-e 's,\(substitution error:\).*,\1 (details omitted),'
921.61SrilligSED_CMDS.varshell+=	-e 's,^[a-z]*sh: ,,'
931.61SrilligSED_CMDS.varshell+=	-e '/command/s,No such.*,not found,'
941.61Srillig
951.64Srillig# Some tests need an additional round of postprocessing.
961.64SrilligPOSTPROC.vardebug=	${TOOL_SED} -n -e '/:RELEVANT = yes/,/:RELEVANT = no/p'
971.64Srillig
981.61Srillig# End of the configuration section.
991.61Srillig
1001.1Ssjg.MAIN: all
1011.1Ssjg
1021.61SrilligUNIT_TESTS:=	${.PARSEDIR}
1031.44Sapb.PATH: ${UNIT_TESTS}
1041.7Ssjg
1051.61SrilligOUTFILES=	${TESTS:=.out}
1061.42Sapb
1071.42Sapball: ${OUTFILES}
1081.9Ssjg
1091.61SrilligCLEANFILES+=		*.rawout *.out *.status *.tmp *.core *.tmp
1101.61SrilligCLEANFILES+=		obj*.[och] lib*.a	# posix1.mk
1111.61SrilligCLEANFILES+=		issue* .[ab]*		# suffixes.mk
1121.61SrilligCLEANRECURSIVE+=	dir dummy		# posix1.mk
1131.48Sapb
1141.1Ssjgclean:
1151.48Sapb	rm -f ${CLEANFILES}
1161.48Sapb.if !empty(CLEANRECURSIVE)
1171.48Sapb	rm -rf ${CLEANRECURSIVE}
1181.48Sapb.endif
1191.1Ssjg
1201.61SrilligTEST_MAKE?=	${.MAKE}
1211.61SrilligTOOL_SED?=	sed
1221.1Ssjg
1231.28Ssjg# ensure consistent results from sort(1)
1241.61SrilligLC_ALL=		C
1251.61SrilligLANG=		C
1261.29Ssjg.export LANG LC_ALL
1271.28Ssjg
1281.42Sapb# the tests are actually done with sub-makes.
1291.42Sapb.SUFFIXES: .mk .rawout .out
1301.42Sapb.mk.rawout:
1311.64Srillig	@echo testing ${.IMPSRC}
1321.64Srillig	@set -eu; \
1331.64Srillig	cd ${.OBJDIR}; \
1341.64Srillig	${ENV.${.TARGET:R}} ${TEST_MAKE} \
1351.64Srillig	  ${FLAGS.${.TARGET:R}:U-k} -f ${.IMPSRC} \
1361.64Srillig	  > ${.TARGET}.tmp 2>&1 \
1371.64Srillig	&& status=$$? || status=$$?; \
1381.64Srillig	echo $$status > ${.TARGET:R}.status
1391.42Sapb	@mv ${.TARGET}.tmp ${.TARGET}
1401.42Sapb
1411.62Srillig# Post-process the test output so that the results can be compared.
1421.62Srillig#
1431.62Srillig# always pretend .MAKE was called 'make'
1441.61Srillig_SED_CMDS+=	-e 's,^${TEST_MAKE:T:S,.,\\.,g}[][0-9]*:,make:,'
1451.61Srillig_SED_CMDS+=	-e 's,${TEST_MAKE:S,.,\\.,g},make,'
1461.62Srillig# replace anything after 'stopped in' with unit-tests
1471.61Srillig_SED_CMDS+=	-e '/stopped/s, /.*, unit-tests,'
1481.62Srillig# strip ${.CURDIR}/ from the output
1491.61Srillig_SED_CMDS+=	-e 's,${.CURDIR:S,.,\\.,g}/,,g'
1501.61Srillig_SED_CMDS+=	-e 's,${UNIT_TESTS:S,.,\\.,g}/,,g'
1511.61Srillig
1521.42Sapb.rawout.out:
1531.42Sapb	@echo postprocess ${.TARGET}
1541.61Srillig	@${TOOL_SED} ${_SED_CMDS} ${SED_CMDS.${.TARGET:R}} \
1551.64Srillig	  < ${.IMPSRC} > ${.TARGET}.tmp1
1561.64Srillig	@${POSTPROC.${.TARGET:R}:U${TOOL_SED}} < ${.TARGET}.tmp1 > ${.TARGET}.tmp2
1571.64Srillig	@rm ${.TARGET}.tmp1
1581.64Srillig	@echo "exit status `cat ${.TARGET:R}.status`" >> ${.TARGET}.tmp2
1591.64Srillig	@mv ${.TARGET}.tmp2 ${.TARGET}
1601.42Sapb
1611.42Sapb# Compare all output files
1621.42Sapbtest:	${OUTFILES} .PHONY
1631.42Sapb	@failed= ; \
1641.61Srillig	for test in ${TESTS}; do \
1651.42Sapb	  diff -u ${UNIT_TESTS}/$${test}.exp $${test}.out \
1661.42Sapb	  || failed="$${failed}$${failed:+ }$${test}" ; \
1671.42Sapb	done ; \
1681.42Sapb	if [ -n "$${failed}" ]; then \
1691.42Sapb	  echo "Failed tests: $${failed}" ; false ; \
1701.42Sapb	else \
1711.42Sapb	  echo "All tests passed" ; \
1721.42Sapb	fi
1731.1Ssjg
1741.1Ssjgaccept:
1751.61Srillig	@for test in ${TESTS}; do \
1761.42Sapb	  cmp -s ${UNIT_TESTS}/$${test}.exp $${test}.out \
1771.42Sapb	  || { echo "Replacing $${test}.exp" ; \
1781.42Sapb	       cp $${test}.out ${UNIT_TESTS}/$${test}.exp ; } \
1791.42Sapb	done
1801.1Ssjg
1811.50Ssjg.if exists(${TEST_MAKE})
1821.61Srillig${TESTS:=.rawout}: ${TEST_MAKE}
1831.50Ssjg.endif
1841.50Ssjg
1851.42Sapb.-include <bsd.obj.mk>
186