Makefile revision 1.91
1# $NetBSD: Makefile,v 1.91 2020/08/09 16:32:28 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# To do that, just run "make sync-mi" in this directory.
28#
29# A few *.mk files are helper files for other tests (such as include-sub.mk)
30# and are thus not added to TESTS.  Such files must be ignored in
31# src/tests/usr.bin/make/t_make.sh.
32#
33
34# Each test is in a sub-makefile.
35# Keep the list sorted.
36TESTS+=		# archive	# broken on FreeBSD
37TESTS+=		cmdline
38TESTS+=		comment
39TESTS+=		cond-late
40TESTS+=		cond-short
41TESTS+=		cond1
42TESTS+=		cond2
43TESTS+=		counter
44TESTS+=		dir
45TESTS+=		directives
46TESTS+=		dollar
47TESTS+=		doterror
48TESTS+=		dotwait
49TESTS+=		envfirst
50TESTS+=		error
51TESTS+=		# escape	# broken by reverting POSIX changes
52TESTS+=		export
53TESTS+=		export-all
54TESTS+=		export-env
55TESTS+=		export-variants
56TESTS+=		forloop
57TESTS+=		forsubst
58TESTS+=		hash
59TESTS+=		impsrc
60TESTS+=		include-main
61TESTS+=		lint
62TESTS+=		make-exported
63TESTS+=		misc
64TESTS+=		moderrs
65TESTS+=		modmatch
66TESTS+=		modmisc
67TESTS+=		modorder
68TESTS+=		modts
69TESTS+=		modword
70TESTS+=		order
71TESTS+=		phony-end
72TESTS+=		posix
73TESTS+=		# posix1	# broken by reverting POSIX changes
74TESTS+=		qequals
75TESTS+=		recursive
76TESTS+=		# suffixes	# runs into an endless loop (try -dA)
77TESTS+=		sunshcmd
78TESTS+=		sysv
79TESTS+=		ternary
80TESTS+=		unexport
81TESTS+=		unexport-env
82TESTS+=		use-inference
83TESTS+=		varcmd
84TESTS+=		vardebug
85TESTS+=		varfind
86TESTS+=		varmisc
87TESTS+=		varmod-edge
88TESTS+=		varparse-dynamic
89TESTS+=		varquote
90TESTS+=		varshell
91
92# Override environment variables for some of the tests.
93ENV.counter=		-i
94ENV.envfirst=		FROM_ENV=value-from-env
95ENV.export=		-i PATH=${PATH:Q}
96ENV.export-variants=	-i PATH=${PATH:Q}
97ENV.lint=		-i
98ENV.make-exported=	-i PATH=${PATH:Q}
99ENV.recursive=		-i
100ENV.varmisc=		FROM_ENV=env
101ENV.varmisc+=		FROM_ENV_BEFORE=env
102ENV.varmisc+=		FROM_ENV_AFTER=env
103
104# Override make flags for some of the tests; default is -k.
105FLAGS.counter=		-r -dv
106FLAGS.doterror=		# none
107FLAGS.envfirst=		-e
108FLAGS.export=		-r
109FLAGS.lint=		-dL -k
110FLAGS.order=		-j1
111FLAGS.recursive=	-dL -r
112FLAGS.vardebug=		-k -dv FROM_CMDLINE=
113
114# Some tests need extra post-processing.
115SED_CMDS.moderrs+=	-e 's,\(Regex compilation error:\).*,\1 (details omitted),'
116SED_CMDS.modmisc+=	-e 's,\(Regex compilation error:\).*,\1 (details omitted),'
117SED_CMDS.varmod-edge+=	-e 's, line [0-9]*:, line omitted:,'
118SED_CMDS.varshell+=	-e 's,^[a-z]*sh: ,,'
119SED_CMDS.varshell+=	-e '/command/s,No such.*,not found,'
120
121# Some tests need an additional round of postprocessing.
122POSTPROC.counter=	${TOOL_SED} -n -e '/:RELEVANT = yes/,/:RELEVANT = no/p'
123POSTPROC.vardebug=	${TOOL_SED} -n -e '/:RELEVANT = yes/,/:RELEVANT = no/p'
124
125export-all.rawout: export.mk
126unexport.rawout: export.mk
127unexport-env.rawout: export.mk
128
129# End of the configuration section.
130
131.MAIN: all
132
133UNIT_TESTS:=	${.PARSEDIR}
134.PATH: ${UNIT_TESTS}
135
136OUTFILES=	${TESTS:=.out}
137
138all: ${OUTFILES}
139
140CLEANFILES+=		*.rawout *.out *.status *.tmp *.core *.tmp
141CLEANFILES+=		obj*.[och] lib*.a	# posix1.mk
142CLEANFILES+=		issue* .[ab]*		# suffixes.mk
143CLEANRECURSIVE+=	dir dummy		# posix1.mk
144
145clean:
146	rm -f ${CLEANFILES}
147.if !empty(CLEANRECURSIVE)
148	rm -rf ${CLEANRECURSIVE}
149.endif
150
151TEST_MAKE?=	${.MAKE}
152TOOL_SED?=	sed
153
154# ensure consistent results from sort(1)
155LC_ALL=		C
156LANG=		C
157.export LANG LC_ALL
158
159# the tests are actually done with sub-makes.
160.SUFFIXES: .mk .rawout .out
161.mk.rawout:
162	@echo testing ${.IMPSRC}
163	@set -eu; \
164	cd ${.OBJDIR}; \
165	env ${ENV.${.TARGET:R}} ${TEST_MAKE} -C ${.CURDIR} \
166	  ${FLAGS.${.TARGET:R}:U-k} -f ${.IMPSRC} \
167	  > ${.TARGET}.tmp 2>&1 \
168	&& status=$$? || status=$$?; \
169	echo $$status > ${.TARGET:R}.status
170	@mv ${.TARGET}.tmp ${.TARGET}
171
172# Post-process the test output so that the results can be compared.
173#
174# always pretend .MAKE was called 'make'
175_SED_CMDS+=	-e 's,^${TEST_MAKE:T:S,.,\\.,g}[][0-9]*:,make:,'
176_SED_CMDS+=	-e 's,${TEST_MAKE:S,.,\\.,g},make,'
177# replace anything after 'stopped in' with unit-tests
178_SED_CMDS+=	-e '/stopped/s, /.*, unit-tests,'
179# strip ${.CURDIR}/ from the output
180_SED_CMDS+=	-e 's,${.CURDIR:S,.,\\.,g}/,,g'
181_SED_CMDS+=	-e 's,${UNIT_TESTS:S,.,\\.,g}/,,g'
182
183.rawout.out:
184	@echo postprocess ${.TARGET}
185	@${TOOL_SED} ${_SED_CMDS} ${SED_CMDS.${.TARGET:R}} \
186	  < ${.IMPSRC} > ${.TARGET}.tmp1
187	@${POSTPROC.${.TARGET:R}:Ucat} < ${.TARGET}.tmp1 > ${.TARGET}.tmp2
188	@rm ${.TARGET}.tmp1
189	@echo "exit status `cat ${.TARGET:R}.status`" >> ${.TARGET}.tmp2
190	@mv ${.TARGET}.tmp2 ${.TARGET}
191
192# Compare all output files
193test:	${OUTFILES} .PHONY
194	@failed= ; \
195	for test in ${TESTS}; do \
196	  diff -u ${UNIT_TESTS}/$${test}.exp $${test}.out \
197	  || failed="$${failed}$${failed:+ }$${test}" ; \
198	done ; \
199	if [ -n "$${failed}" ]; then \
200	  echo "Failed tests: $${failed}" ; false ; \
201	else \
202	  echo "All tests passed" ; \
203	fi
204
205accept:
206	@for test in ${TESTS}; do \
207	  cmp -s ${UNIT_TESTS}/$${test}.exp $${test}.out \
208	  || { echo "Replacing $${test}.exp" ; \
209	       cp $${test}.out ${UNIT_TESTS}/$${test}.exp ; } \
210	done
211
212SYNC_MI_AWK= \
213	BEGIN {								\
214	  testsdir = "usr.bin/make/unit-tests";				\
215	  linestart = "./usr/tests/" testsdir;				\
216	  fmt = linestart "/%s\ttests-usr.bin-tests\tcompattestfile,atf\\n"; \
217	  cmd = "cd " testsdir " && ls *.exp *.mk | xargs printf '" fmt "'" \
218	}								\
219	function startswith(s, prefix) {				\
220	  return substr(s, 1, length(prefix)) == prefix;		\
221	}								\
222	startswith($$1, linestart) && $$1 ~ /\.(exp|mk)$$/ { next }	\
223	{ print $$0 }							\
224	$$1 == linestart "/Makefile" { system(cmd) }
225
226sync-mi:
227	@set -eu;							\
228	cd "${MAKEFILE:tA:H}/../../..";					\
229	mi="distrib/sets/lists/tests/mi";				\
230	awk ${SYNC_MI_AWK:Q} < "$$mi" > "$$mi.$@";			\
231	mv -f "$$mi.$@" "$$mi";						\
232	cvs diff "$$mi" || true
233
234.if exists(${TEST_MAKE})
235${TESTS:=.rawout}: ${TEST_MAKE}
236.endif
237
238.-include <bsd.obj.mk>
239