Makefile revision 1.69
1# $NetBSD: Makefile,v 1.69 2020/07/27 19:45:56 rillig Exp $
2#
3# Unit tests for make(1)
4#
5# The main targets are:
6#
7# all:
8#	run all the tests
9# test:
10#	run 'all', and compare to expected results
11# accept:
12#	move generated output to expected results
13#
14# Settable variables
15#
16# TEST_MAKE
17#	The make program to be tested.
18#
19#
20# Adding a test case
21#
22# Each feature should get its own set of tests in its own suitably
23# named makefile (*.mk), with its own set of expected results (*.exp),
24# and it should be added to the TESTS list.
25#
26# Any added files must also be added to src/distrib/sets/lists/tests/mi.
27# Makefiles that are not added to TESTS must be ignored in
28# src/tests/usr.bin/make/t_make.sh (example: include-sub).
29#
30
31# Each test is in a sub-makefile.
32# Keep the list sorted.
33TESTS+=		archive
34TESTS+=		comment
35TESTS+=		cond-late
36TESTS+=		cond-short
37TESTS+=		cond1
38TESTS+=		cond2
39TESTS+=		dollar
40TESTS+=		doterror
41TESTS+=		dotwait
42TESTS+=		envfirst
43TESTS+=		error
44TESTS+=		# escape	# broken by reverting POSIX changes
45TESTS+=		export
46TESTS+=		export-all
47TESTS+=		export-env
48TESTS+=		forloop
49TESTS+=		forsubst
50TESTS+=		hash
51TESTS+=		# impsrc	# broken by reverting POSIX changes
52TESTS+=		include-main
53TESTS+=		misc
54TESTS+=		moderrs
55TESTS+=		modmatch
56TESTS+=		modmisc
57TESTS+=		modorder
58TESTS+=		modts
59TESTS+=		modword
60TESTS+=		order
61TESTS+=		# phony-end	# broken by reverting POSIX changes
62TESTS+=		posix
63TESTS+=		# posix1	# broken by reverting POSIX changes
64TESTS+=		qequals
65TESTS+=		# suffixes	# broken by reverting POSIX changes
66TESTS+=		sunshcmd
67TESTS+=		sysv
68TESTS+=		ternary
69TESTS+=		unexport
70TESTS+=		unexport-env
71TESTS+=		varcmd
72TESTS+=		vardebug
73TESTS+=		varfind
74TESTS+=		varmisc
75TESTS+=		varmod-edge
76TESTS+=		varparse-dynamic
77TESTS+=		varquote
78TESTS+=		varshell
79
80# Override environment variables for some of the tests.
81ENV.envfirst=		FROM_ENV=value-from-env
82ENV.export=		-i PATH=${PATH:Q}
83ENV.varmisc=		FROM_ENV=env
84ENV.varmisc+=		FROM_ENV_BEFORE=env
85ENV.varmisc+=		FROM_ENV_AFTER=env
86
87# Override make flags for some of the tests; default is -k.
88FLAGS.doterror=		# none
89FLAGS.envfirst=		-e
90FLAGS.order=		-j1
91FLAGS.vardebug=		-k -dv FROM_CMDLINE=
92
93# Some tests need extra post-processing.
94SED_CMDS.modmisc+=	-e 's,\(substitution error:\).*,\1 (details omitted),'
95SED_CMDS.varshell+=	-e 's,^[a-z]*sh: ,,'
96SED_CMDS.varshell+=	-e '/command/s,No such.*,not found,'
97
98# Some tests need an additional round of postprocessing.
99POSTPROC.vardebug=	${TOOL_SED} -n -e '/:RELEVANT = yes/,/:RELEVANT = no/p'
100
101# End of the configuration section.
102
103.MAIN: all
104
105UNIT_TESTS:=	${.PARSEDIR}
106.PATH: ${UNIT_TESTS}
107
108OUTFILES=	${TESTS:=.out}
109
110all: ${OUTFILES}
111
112CLEANFILES+=		*.rawout *.out *.status *.tmp *.core *.tmp
113CLEANFILES+=		obj*.[och] lib*.a	# posix1.mk
114CLEANFILES+=		issue* .[ab]*		# suffixes.mk
115CLEANRECURSIVE+=	dir dummy		# posix1.mk
116
117clean:
118	rm -f ${CLEANFILES}
119.if !empty(CLEANRECURSIVE)
120	rm -rf ${CLEANRECURSIVE}
121.endif
122
123TEST_MAKE?=	${.MAKE}
124TOOL_SED?=	sed
125
126# ensure consistent results from sort(1)
127LC_ALL=		C
128LANG=		C
129.export LANG LC_ALL
130
131# the tests are actually done with sub-makes.
132.SUFFIXES: .mk .rawout .out
133.mk.rawout:
134	@echo testing ${.IMPSRC}
135	@set -eu; \
136	cd ${.OBJDIR}; \
137	env ${ENV.${.TARGET:R}} ${TEST_MAKE} \
138	  ${FLAGS.${.TARGET:R}:U-k} -f ${.IMPSRC} \
139	  > ${.TARGET}.tmp 2>&1 \
140	&& status=$$? || status=$$?; \
141	echo $$status > ${.TARGET:R}.status
142	@mv ${.TARGET}.tmp ${.TARGET}
143
144# Post-process the test output so that the results can be compared.
145#
146# always pretend .MAKE was called 'make'
147_SED_CMDS+=	-e 's,^${TEST_MAKE:T:S,.,\\.,g}[][0-9]*:,make:,'
148_SED_CMDS+=	-e 's,${TEST_MAKE:S,.,\\.,g},make,'
149# replace anything after 'stopped in' with unit-tests
150_SED_CMDS+=	-e '/stopped/s, /.*, unit-tests,'
151# strip ${.CURDIR}/ from the output
152_SED_CMDS+=	-e 's,${.CURDIR:S,.,\\.,g}/,,g'
153_SED_CMDS+=	-e 's,${UNIT_TESTS:S,.,\\.,g}/,,g'
154
155.rawout.out:
156	@echo postprocess ${.TARGET}
157	@${TOOL_SED} ${_SED_CMDS} ${SED_CMDS.${.TARGET:R}} \
158	  < ${.IMPSRC} > ${.TARGET}.tmp1
159	@${POSTPROC.${.TARGET:R}:U${TOOL_SED}} < ${.TARGET}.tmp1 > ${.TARGET}.tmp2
160	@rm ${.TARGET}.tmp1
161	@echo "exit status `cat ${.TARGET:R}.status`" >> ${.TARGET}.tmp2
162	@mv ${.TARGET}.tmp2 ${.TARGET}
163
164# Compare all output files
165test:	${OUTFILES} .PHONY
166	@failed= ; \
167	for test in ${TESTS}; do \
168	  diff -u ${UNIT_TESTS}/$${test}.exp $${test}.out \
169	  || failed="$${failed}$${failed:+ }$${test}" ; \
170	done ; \
171	if [ -n "$${failed}" ]; then \
172	  echo "Failed tests: $${failed}" ; false ; \
173	else \
174	  echo "All tests passed" ; \
175	fi
176
177accept:
178	@for test in ${TESTS}; do \
179	  cmp -s ${UNIT_TESTS}/$${test}.exp $${test}.out \
180	  || { echo "Replacing $${test}.exp" ; \
181	       cp $${test}.out ${UNIT_TESTS}/$${test}.exp ; } \
182	done
183
184.if exists(${TEST_MAKE})
185${TESTS:=.rawout}: ${TEST_MAKE}
186.endif
187
188.-include <bsd.obj.mk>
189