Makefile revision 1.79
11.79Srillig# $NetBSD: Makefile,v 1.79 2020/08/01 15:28:28 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.78Srillig# To do that, just run "make sync-mi" in this directory.
281.78Srillig#
291.78Srillig# A few *.mk files are helper files for other tests (such as include-sub.mk)
301.78Srillig# and are thus not added to TESTS.  Such files must be ignored in
311.78Srillig# src/tests/usr.bin/make/t_make.sh.
321.60Srillig#
331.1Ssjg
341.61Srillig# Each test is in a sub-makefile.
351.61Srillig# Keep the list sorted.
361.72SrilligTESTS+=		# archive	# broken on FreeBSD
371.75SrilligTESTS+=		cmdline
381.61SrilligTESTS+=		comment
391.61SrilligTESTS+=		cond-late
401.61SrilligTESTS+=		cond-short
411.61SrilligTESTS+=		cond1
421.61SrilligTESTS+=		cond2
431.77SrilligTESTS+=		dir
441.70SrilligTESTS+=		directives
451.61SrilligTESTS+=		dollar
461.61SrilligTESTS+=		doterror
471.61SrilligTESTS+=		dotwait
481.64SrilligTESTS+=		envfirst
491.61SrilligTESTS+=		error
501.63SsjgTESTS+=		# escape	# broken by reverting POSIX changes
511.61SrilligTESTS+=		export
521.61SrilligTESTS+=		export-all
531.61SrilligTESTS+=		export-env
541.61SrilligTESTS+=		forloop
551.61SrilligTESTS+=		forsubst
561.61SrilligTESTS+=		hash
571.63SsjgTESTS+=		# impsrc	# broken by reverting POSIX changes
581.61SrilligTESTS+=		include-main
591.61SrilligTESTS+=		misc
601.61SrilligTESTS+=		moderrs
611.61SrilligTESTS+=		modmatch
621.61SrilligTESTS+=		modmisc
631.61SrilligTESTS+=		modorder
641.61SrilligTESTS+=		modts
651.61SrilligTESTS+=		modword
661.61SrilligTESTS+=		order
671.63SsjgTESTS+=		# phony-end	# broken by reverting POSIX changes
681.61SrilligTESTS+=		posix
691.63SsjgTESTS+=		# posix1	# broken by reverting POSIX changes
701.61SrilligTESTS+=		qequals
711.63SsjgTESTS+=		# suffixes	# broken by reverting POSIX changes
721.61SrilligTESTS+=		sunshcmd
731.61SrilligTESTS+=		sysv
741.61SrilligTESTS+=		ternary
751.61SrilligTESTS+=		unexport
761.61SrilligTESTS+=		unexport-env
771.61SrilligTESTS+=		varcmd
781.64SrilligTESTS+=		vardebug
791.64SrilligTESTS+=		varfind
801.61SrilligTESTS+=		varmisc
811.61SrilligTESTS+=		varmod-edge
821.66SrilligTESTS+=		varparse-dynamic
831.61SrilligTESTS+=		varquote
841.61SrilligTESTS+=		varshell
851.61Srillig
861.64Srillig# Override environment variables for some of the tests.
871.64SrilligENV.envfirst=		FROM_ENV=value-from-env
881.69SrilligENV.export=		-i PATH=${PATH:Q}
891.65SrilligENV.varmisc=		FROM_ENV=env
901.65SrilligENV.varmisc+=		FROM_ENV_BEFORE=env
911.65SrilligENV.varmisc+=		FROM_ENV_AFTER=env
921.64Srillig
931.64Srillig# Override make flags for some of the tests; default is -k.
941.61SrilligFLAGS.doterror=		# none
951.68SrilligFLAGS.envfirst=		-e
961.71SsjgFLAGS.export=		-r
971.61SrilligFLAGS.order=		-j1
981.64SrilligFLAGS.vardebug=		-k -dv FROM_CMDLINE=
991.61Srillig
1001.61Srillig# Some tests need extra post-processing.
1011.76SrilligSED_CMDS.moderrs+=	-e 's,\(substitution error:\).*,\1 (details omitted),'
1021.62SrilligSED_CMDS.modmisc+=	-e 's,\(substitution error:\).*,\1 (details omitted),'
1031.79SrilligSED_CMDS.varmod-edge+=	-e 's, line [0-9]*:, line omitted:,'
1041.61SrilligSED_CMDS.varshell+=	-e 's,^[a-z]*sh: ,,'
1051.61SrilligSED_CMDS.varshell+=	-e '/command/s,No such.*,not found,'
1061.61Srillig
1071.64Srillig# Some tests need an additional round of postprocessing.
1081.64SrilligPOSTPROC.vardebug=	${TOOL_SED} -n -e '/:RELEVANT = yes/,/:RELEVANT = no/p'
1091.64Srillig
1101.61Srillig# End of the configuration section.
1111.61Srillig
1121.1Ssjg.MAIN: all
1131.1Ssjg
1141.61SrilligUNIT_TESTS:=	${.PARSEDIR}
1151.44Sapb.PATH: ${UNIT_TESTS}
1161.7Ssjg
1171.61SrilligOUTFILES=	${TESTS:=.out}
1181.42Sapb
1191.42Sapball: ${OUTFILES}
1201.9Ssjg
1211.61SrilligCLEANFILES+=		*.rawout *.out *.status *.tmp *.core *.tmp
1221.61SrilligCLEANFILES+=		obj*.[och] lib*.a	# posix1.mk
1231.61SrilligCLEANFILES+=		issue* .[ab]*		# suffixes.mk
1241.61SrilligCLEANRECURSIVE+=	dir dummy		# posix1.mk
1251.48Sapb
1261.1Ssjgclean:
1271.48Sapb	rm -f ${CLEANFILES}
1281.48Sapb.if !empty(CLEANRECURSIVE)
1291.48Sapb	rm -rf ${CLEANRECURSIVE}
1301.48Sapb.endif
1311.1Ssjg
1321.61SrilligTEST_MAKE?=	${.MAKE}
1331.61SrilligTOOL_SED?=	sed
1341.1Ssjg
1351.28Ssjg# ensure consistent results from sort(1)
1361.61SrilligLC_ALL=		C
1371.61SrilligLANG=		C
1381.29Ssjg.export LANG LC_ALL
1391.28Ssjg
1401.42Sapb# the tests are actually done with sub-makes.
1411.42Sapb.SUFFIXES: .mk .rawout .out
1421.42Sapb.mk.rawout:
1431.64Srillig	@echo testing ${.IMPSRC}
1441.64Srillig	@set -eu; \
1451.64Srillig	cd ${.OBJDIR}; \
1461.73Ssjg	env ${ENV.${.TARGET:R}} ${TEST_MAKE} -C ${.CURDIR} \
1471.64Srillig	  ${FLAGS.${.TARGET:R}:U-k} -f ${.IMPSRC} \
1481.64Srillig	  > ${.TARGET}.tmp 2>&1 \
1491.64Srillig	&& status=$$? || status=$$?; \
1501.64Srillig	echo $$status > ${.TARGET:R}.status
1511.42Sapb	@mv ${.TARGET}.tmp ${.TARGET}
1521.42Sapb
1531.62Srillig# Post-process the test output so that the results can be compared.
1541.62Srillig#
1551.62Srillig# always pretend .MAKE was called 'make'
1561.61Srillig_SED_CMDS+=	-e 's,^${TEST_MAKE:T:S,.,\\.,g}[][0-9]*:,make:,'
1571.61Srillig_SED_CMDS+=	-e 's,${TEST_MAKE:S,.,\\.,g},make,'
1581.62Srillig# replace anything after 'stopped in' with unit-tests
1591.61Srillig_SED_CMDS+=	-e '/stopped/s, /.*, unit-tests,'
1601.62Srillig# strip ${.CURDIR}/ from the output
1611.61Srillig_SED_CMDS+=	-e 's,${.CURDIR:S,.,\\.,g}/,,g'
1621.61Srillig_SED_CMDS+=	-e 's,${UNIT_TESTS:S,.,\\.,g}/,,g'
1631.61Srillig
1641.42Sapb.rawout.out:
1651.42Sapb	@echo postprocess ${.TARGET}
1661.61Srillig	@${TOOL_SED} ${_SED_CMDS} ${SED_CMDS.${.TARGET:R}} \
1671.64Srillig	  < ${.IMPSRC} > ${.TARGET}.tmp1
1681.74Ssjg	@${POSTPROC.${.TARGET:R}:Ucat} < ${.TARGET}.tmp1 > ${.TARGET}.tmp2
1691.64Srillig	@rm ${.TARGET}.tmp1
1701.64Srillig	@echo "exit status `cat ${.TARGET:R}.status`" >> ${.TARGET}.tmp2
1711.64Srillig	@mv ${.TARGET}.tmp2 ${.TARGET}
1721.42Sapb
1731.42Sapb# Compare all output files
1741.42Sapbtest:	${OUTFILES} .PHONY
1751.42Sapb	@failed= ; \
1761.61Srillig	for test in ${TESTS}; do \
1771.42Sapb	  diff -u ${UNIT_TESTS}/$${test}.exp $${test}.out \
1781.42Sapb	  || failed="$${failed}$${failed:+ }$${test}" ; \
1791.42Sapb	done ; \
1801.42Sapb	if [ -n "$${failed}" ]; then \
1811.42Sapb	  echo "Failed tests: $${failed}" ; false ; \
1821.42Sapb	else \
1831.42Sapb	  echo "All tests passed" ; \
1841.42Sapb	fi
1851.1Ssjg
1861.1Ssjgaccept:
1871.61Srillig	@for test in ${TESTS}; do \
1881.42Sapb	  cmp -s ${UNIT_TESTS}/$${test}.exp $${test}.out \
1891.42Sapb	  || { echo "Replacing $${test}.exp" ; \
1901.42Sapb	       cp $${test}.out ${UNIT_TESTS}/$${test}.exp ; } \
1911.42Sapb	done
1921.1Ssjg
1931.78SrilligSYNC_MI_AWK= \
1941.78Srillig	BEGIN {								\
1951.78Srillig	  testsdir = "usr.bin/make/unit-tests";				\
1961.78Srillig	  linestart = "./usr/tests/" testsdir;				\
1971.78Srillig	  fmt = linestart "/%s\ttests-usr.bin-tests\tcompattestfile,atf\\n"; \
1981.78Srillig	  cmd = "cd " testsdir " && ls *.exp *.mk | xargs printf '" fmt "'" \
1991.78Srillig	}								\
2001.78Srillig	function startswith(s, prefix) {				\
2011.78Srillig	  return substr(s, 1, length(prefix)) == prefix;		\
2021.78Srillig	}								\
2031.78Srillig	startswith($$1, linestart) && $$1 ~ /\.(exp|mk)$$/ { next }	\
2041.78Srillig	{ print $$0 }							\
2051.78Srillig	$$1 == linestart "/Makefile" { system(cmd) }
2061.78Srillig
2071.78Srilligsync-mi:
2081.78Srillig	@set -eu;							\
2091.78Srillig	cd "${MAKEFILE:tA:H}/../../..";					\
2101.78Srillig	mi="distrib/sets/lists/tests/mi";				\
2111.78Srillig	awk ${SYNC_MI_AWK:Q} < "$$mi" > "$$mi.$@";			\
2121.78Srillig	mv -f "$$mi.$@" "$$mi";						\
2131.78Srillig	cvs diff "$$mi" || true
2141.78Srillig
2151.50Ssjg.if exists(${TEST_MAKE})
2161.61Srillig${TESTS:=.rawout}: ${TEST_MAKE}
2171.50Ssjg.endif
2181.50Ssjg
2191.42Sapb.-include <bsd.obj.mk>
220