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