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