Makefile revision 1.141
1# $NetBSD: Makefile,v 1.141 2020/09/14 06:22: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.
36# Any test that is commented out must be ignored in
37# src/tests/usr.bin/make/t_make.sh as well.
38TESTS+=		archive
39TESTS+=		archive-suffix
40TESTS+=		cmd-interrupt
41TESTS+=		cmdline
42TESTS+=		comment
43TESTS+=		cond-cmp-numeric
44TESTS+=		cond-cmp-numeric-eq
45TESTS+=		cond-cmp-numeric-ge
46TESTS+=		cond-cmp-numeric-gt
47TESTS+=		cond-cmp-numeric-le
48TESTS+=		cond-cmp-numeric-lt
49TESTS+=		cond-cmp-numeric-ne
50TESTS+=		cond-cmp-string
51TESTS+=		cond-cmp-unary
52TESTS+=		cond-func
53TESTS+=		cond-func-commands
54TESTS+=		cond-func-defined
55TESTS+=		cond-func-empty
56TESTS+=		cond-func-exists
57TESTS+=		cond-func-make
58TESTS+=		cond-func-target
59TESTS+=		cond-late
60TESTS+=		cond-op
61TESTS+=		cond-op-and
62TESTS+=		cond-op-not
63TESTS+=		cond-op-or
64TESTS+=		cond-op-parentheses
65TESTS+=		cond-short
66TESTS+=		cond-token-number
67TESTS+=		cond-token-plain
68TESTS+=		cond-token-string
69TESTS+=		cond-token-var
70TESTS+=		cond1
71TESTS+=		cond2
72TESTS+=		counter
73TESTS+=		dep
74TESTS+=		dep-colon
75TESTS+=		dep-double-colon
76TESTS+=		dep-exclam
77TESTS+=		dep-none
78TESTS+=		dep-var
79TESTS+=		dep-wildcards
80TESTS+=		depsrc
81TESTS+=		depsrc-exec
82TESTS+=		depsrc-ignore
83TESTS+=		depsrc-made
84TESTS+=		depsrc-make
85TESTS+=		depsrc-meta
86TESTS+=		depsrc-nometa
87TESTS+=		depsrc-nometa_cmp
88TESTS+=		depsrc-nopath
89TESTS+=		depsrc-notmain
90TESTS+=		depsrc-optional
91TESTS+=		depsrc-phony
92TESTS+=		depsrc-precious
93TESTS+=		depsrc-recursive
94TESTS+=		depsrc-silent
95TESTS+=		depsrc-use
96TESTS+=		depsrc-usebefore
97TESTS+=		depsrc-usebefore-double-colon
98TESTS+=		depsrc-wait
99TESTS+=		deptgt
100TESTS+=		deptgt-begin
101TESTS+=		deptgt-default
102TESTS+=		deptgt-delete_on_error
103TESTS+=		deptgt-end
104TESTS+=		deptgt-error
105TESTS+=		deptgt-ignore
106TESTS+=		deptgt-interrupt
107TESTS+=		deptgt-main
108TESTS+=		deptgt-makeflags
109TESTS+=		deptgt-no_parallel
110TESTS+=		deptgt-nopath
111TESTS+=		deptgt-notparallel
112TESTS+=		deptgt-objdir
113TESTS+=		deptgt-order
114TESTS+=		deptgt-path
115TESTS+=		deptgt-path-suffix
116TESTS+=		deptgt-phony
117TESTS+=		deptgt-precious
118TESTS+=		deptgt-shell
119TESTS+=		deptgt-silent
120TESTS+=		deptgt-stale
121TESTS+=		deptgt-suffixes
122TESTS+=		dir
123TESTS+=		dir-expand-path
124TESTS+=		directive
125TESTS+=		directive-dinclude
126TESTS+=		directive-elif
127TESTS+=		directive-elifdef
128TESTS+=		directive-elifmake
129TESTS+=		directive-elifndef
130TESTS+=		directive-elifnmake
131TESTS+=		directive-else
132TESTS+=		directive-endif
133TESTS+=		directive-error
134TESTS+=		directive-export
135TESTS+=		directive-export-env
136TESTS+=		directive-export-literal
137TESTS+=		directive-for
138TESTS+=		directive-for-generating-endif
139TESTS+=		directive-hyphen-include
140TESTS+=		directive-if
141TESTS+=		directive-ifdef
142TESTS+=		directive-ifmake
143TESTS+=		directive-ifndef
144TESTS+=		directive-ifnmake
145TESTS+=		directive-include
146TESTS+=		directive-include-fatal
147TESTS+=		directive-info
148TESTS+=		directive-sinclude
149TESTS+=		directive-undef
150TESTS+=		directive-unexport
151TESTS+=		directive-unexport-env
152TESTS+=		directive-warning
153TESTS+=		directives
154TESTS+=		dollar
155TESTS+=		doterror
156TESTS+=		dotwait
157TESTS+=		envfirst
158TESTS+=		error
159TESTS+=		# escape	# broken by reverting POSIX changes
160TESTS+=		export
161TESTS+=		export-all
162TESTS+=		export-env
163TESTS+=		export-variants
164TESTS+=		forloop
165TESTS+=		forsubst
166TESTS+=		impsrc
167TESTS+=		include-main
168TESTS+=		lint
169TESTS+=		make-exported
170TESTS+=		misc
171TESTS+=		moderrs
172TESTS+=		modmatch
173TESTS+=		modmisc
174TESTS+=		modts
175TESTS+=		modword
176TESTS+=		opt
177TESTS+=		opt-backwards
178TESTS+=		opt-chdir
179TESTS+=		opt-debug
180TESTS+=		opt-debug-all
181TESTS+=		opt-debug-archive
182TESTS+=		opt-debug-curdir
183TESTS+=		opt-debug-cond
184TESTS+=		opt-debug-dir
185TESTS+=		opt-debug-errors
186TESTS+=		opt-debug-file
187TESTS+=		opt-debug-for
188TESTS+=		opt-debug-graph1
189TESTS+=		opt-debug-graph2
190TESTS+=		opt-debug-graph3
191TESTS+=		opt-debug-hash
192TESTS+=		opt-debug-jobs
193TESTS+=		opt-debug-lint
194TESTS+=		opt-debug-loud
195TESTS+=		opt-debug-meta
196TESTS+=		opt-debug-making
197TESTS+=		opt-debug-no-rm
198TESTS+=		opt-debug-parse
199TESTS+=		opt-debug-suff
200TESTS+=		opt-debug-targets
201TESTS+=		opt-debug-varraw
202TESTS+=		opt-debug-var
203TESTS+=		opt-debug-x-trace
204TESTS+=		opt-define
205TESTS+=		opt-env
206TESTS+=		opt-file
207TESTS+=		opt-ignore
208TESTS+=		opt-include-dir
209TESTS+=		opt-jobs
210TESTS+=		opt-jobs-internal
211TESTS+=		opt-keep-going
212TESTS+=		opt-m-include-dir
213TESTS+=		opt-no-action
214TESTS+=		opt-no-action-at-all
215TESTS+=		opt-query
216TESTS+=		opt-raw
217TESTS+=		opt-silent
218TESTS+=		opt-touch
219TESTS+=		opt-tracefile
220TESTS+=		opt-var-expanded
221TESTS+=		opt-var-literal
222TESTS+=		opt-warnings-as-errors
223TESTS+=		opt-where-am-i
224TESTS+=		opt-x-reduce-exported
225TESTS+=		order
226TESTS+=		phony-end
227TESTS+=		posix
228TESTS+=		# posix1	# broken by reverting POSIX changes
229TESTS+=		qequals
230TESTS+=		recursive
231TESTS+=		sh
232TESTS+=		sh-dots
233TESTS+=		sh-jobs
234TESTS+=		sh-jobs-error
235TESTS+=		sh-leading-at
236TESTS+=		sh-leading-hyphen
237TESTS+=		sh-leading-plus
238TESTS+=		sh-meta-chars
239TESTS+=		sh-multi-line
240TESTS+=		sh-single-line
241TESTS+=		# suffixes	# runs into an endless loop (try -dA)
242TESTS+=		sunshcmd
243TESTS+=		sysv
244TESTS+=		ternary
245TESTS+=		unexport
246TESTS+=		unexport-env
247TESTS+=		use-inference
248TESTS+=		var-class
249TESTS+=		var-class-cmdline
250TESTS+=		var-class-env
251TESTS+=		var-class-global
252TESTS+=		var-class-local
253TESTS+=		var-class-local-legacy
254TESTS+=		var-op
255TESTS+=		var-op-append
256TESTS+=		var-op-assign
257TESTS+=		var-op-default
258TESTS+=		var-op-expand
259TESTS+=		var-op-shell
260TESTS+=		varcmd
261TESTS+=		vardebug
262TESTS+=		varfind
263TESTS+=		varmisc
264TESTS+=		varmod
265TESTS+=		varmod-assign
266TESTS+=		varmod-defined
267TESTS+=		varmod-edge
268TESTS+=		varmod-exclam-shell
269TESTS+=		varmod-extension
270TESTS+=		varmod-gmtime
271TESTS+=		varmod-hash
272TESTS+=		varmod-head
273TESTS+=		varmod-ifelse
274TESTS+=		varmod-l-name-to-value
275TESTS+=		varmod-localtime
276TESTS+=		varmod-loop
277TESTS+=		varmod-match
278TESTS+=		varmod-match-escape
279TESTS+=		varmod-no-match
280TESTS+=		varmod-order
281TESTS+=		varmod-order-reverse
282TESTS+=		varmod-order-shuffle
283TESTS+=		varmod-path
284TESTS+=		varmod-quote
285TESTS+=		varmod-quote-dollar
286TESTS+=		varmod-range
287TESTS+=		varmod-remember
288TESTS+=		varmod-root
289TESTS+=		varmod-select-words
290TESTS+=		varmod-shell
291TESTS+=		varmod-subst
292TESTS+=		varmod-subst-regex
293TESTS+=		varmod-sysv
294TESTS+=		varmod-tail
295TESTS+=		varmod-to-abs
296TESTS+=		varmod-to-lower
297TESTS+=		varmod-to-many-words
298TESTS+=		varmod-to-one-word
299TESTS+=		varmod-to-separator
300TESTS+=		varmod-to-upper
301TESTS+=		varmod-undefined
302TESTS+=		varmod-unique
303TESTS+=		varname
304TESTS+=		varname-dollar
305TESTS+=		varname-dot-alltargets
306TESTS+=		varname-dot-curdir
307TESTS+=		varname-dot-includes
308TESTS+=		varname-dot-includedfromdir
309TESTS+=		varname-dot-includedfromfile
310TESTS+=		varname-dot-libs
311TESTS+=		varname-dot-make-dependfile
312TESTS+=		varname-dot-make-expand_variables
313TESTS+=		varname-dot-make-exported
314TESTS+=		varname-dot-make-jobs
315TESTS+=		varname-dot-make-jobs-prefix
316TESTS+=		varname-dot-make-level
317TESTS+=		varname-dot-make-makefile_preference
318TESTS+=		varname-dot-make-makefiles
319TESTS+=		varname-dot-make-meta-bailiwick
320TESTS+=		varname-dot-make-meta-created
321TESTS+=		varname-dot-make-meta-files
322TESTS+=		varname-dot-make-meta-ignore_filter
323TESTS+=		varname-dot-make-meta-ignore_paths
324TESTS+=		varname-dot-make-meta-ignore_patterns
325TESTS+=		varname-dot-make-meta-prefix
326TESTS+=		varname-dot-make-mode
327TESTS+=		varname-dot-make-path_filemon
328TESTS+=		varname-dot-make-pid
329TESTS+=		varname-dot-make-ppid
330TESTS+=		varname-dot-make-save_dollars
331TESTS+=		varname-dot-makeoverrides
332TESTS+=		varname-dot-newline
333TESTS+=		varname-dot-objdir
334TESTS+=		varname-dot-parsedir
335TESTS+=		varname-dot-parsefile
336TESTS+=		varname-dot-path
337TESTS+=		varname-dot-shell
338TESTS+=		varname-dot-targets
339TESTS+=		varname-empty
340TESTS+=		varname-make
341TESTS+=		varname-make_print_var_on_error
342TESTS+=		varname-makefile
343TESTS+=		varname-makeflags
344TESTS+=		varname-pwd
345TESTS+=		varname-vpath
346TESTS+=		varparse-dynamic
347TESTS+=		varquote
348TESTS+=		varshell
349
350# Additional environment variables for some of the tests.
351# The base environment is -i PATH="$PATH".
352ENV.envfirst=		FROM_ENV=value-from-env
353ENV.varmisc=		FROM_ENV=env
354ENV.varmisc+=		FROM_ENV_BEFORE=env
355ENV.varmisc+=		FROM_ENV_AFTER=env
356
357# Override make flags for some of the tests; default is -k.
358# If possible, write ".MAKEFLAGS: -dv" in the test .mk file instead of
359# settings FLAGS.test=-dv here, since that is closer to the test code.
360FLAGS.counter=		-dv
361FLAGS.directive-ifmake=	first second
362FLAGS.doterror=		# none
363FLAGS.envfirst=		-e
364FLAGS.export=		# none
365FLAGS.lint=		-dL -k
366FLAGS.opt-ignore=	-i
367FLAGS.opt-keep-going=	-k
368FLAGS.opt-no-action=	-n
369FLAGS.opt-query=	-q
370FLAGS.opt-var-expanded=	-v VAR -v VALUE
371FLAGS.opt-var-literal=	-V VAR -V VALUE
372FLAGS.opt-warnings-as-errors= -W
373FLAGS.order=		-j1
374FLAGS.recursive=	-dL
375FLAGS.sh-leading-plus=	-n
376FLAGS.vardebug=		-k -dv FROM_CMDLINE=
377FLAGS.varmod-match-escape= -dv
378FLAGS.varname-dot-shell= -dpv
379FLAGS.varname-empty=	-dv '$${:U}=cmdline-u' '=cmline-plain'
380
381# Some tests need extra postprocessing.
382SED_CMDS.opt-debug-graph1= \
383			-e 's,${.CURDIR},CURDIR,'
384SED_CMDS.opt-debug-graph1+= \
385			-e '/Global Variables:/,/Suffixes:/d'
386SED_CMDS.sh-dots=	-e 's,^.*\.\.\.:.*,<normalized: ...: not found>,'
387SED_CMDS.varmod-subst-regex+= \
388			-e 's,\(Regex compilation error:\).*,\1 (details omitted),'
389SED_CMDS.varmod-edge+=	-e 's, line [0-9]*:, line omitted:,'
390SED_CMDS.varshell+=	-e 's,^${.SHELL:T}: ,,'
391SED_CMDS.varshell+=	-e '/command/s,No such.*,not found,'
392SED_CMDS.varname-dot-shell=	-e 's, = /.*, = (details omitted),'
393SED_CMDS.varname-dot-shell+=	-e 's,"/[^"]*","(details omitted)",'
394SED_CMDS.varname-dot-shell+=	-e 's,\[/[^]]*\],[(details omitted)],'
395
396# Some tests need an additional round of postprocessing.
397POSTPROC.counter=	${TOOL_SED} -n -e '/:RELEVANT = yes/,/:RELEVANT = no/p'
398POSTPROC.deptgt-suffixes= \
399			${TOOL_SED} -n -e '/^\#\*\*\* Suffixes/,/^\#\*/p'
400POSTPROC.vardebug=	${TOOL_SED} -n -e '/:RELEVANT = yes/,/:RELEVANT = no/p'
401POSTPROC.varmod-match-escape= ${TOOL_SED} -n -e '/^Pattern/p'
402POSTPROC.varname=	${TOOL_SED} -n -e '/^MAGIC/p' -e '/^ORDER_/p'
403POSTPROC.varname-dot-shell= \
404			awk '/\.SHELL/ || /^ParseReadLine/'
405POSTPROC.varname-empty=	${TOOL_SED} -n -e '/^Var_Set/p' -e '/^out:/p'
406
407# Some tests reuse other tests, which makes them unnecessarily fragile.
408export-all.rawout: export.mk
409unexport.rawout: export.mk
410unexport-env.rawout: export.mk
411
412# End of the configuration section.
413
414.MAIN: all
415
416UNIT_TESTS:=	${.PARSEDIR}
417.PATH: ${UNIT_TESTS}
418
419OUTFILES=	${TESTS:=.out}
420
421all: ${OUTFILES}
422
423CLEANFILES=		*.rawout *.out *.status *.tmp *.core *.tmp
424CLEANFILES+=		obj*.[och] lib*.a	# posix1.mk
425CLEANFILES+=		issue* .[ab]*		# suffixes.mk
426CLEANDIRS=		dir dummy		# posix1.mk
427
428clean:
429	rm -f ${CLEANFILES}
430	rm -rf ${CLEANDIRS}
431
432TEST_MAKE?=	${.MAKE}
433TOOL_SED?=	sed
434
435# ensure consistent results from sort(1)
436LC_ALL=		C
437LANG=		C
438.export LANG LC_ALL
439
440# Each test is run in a sub-make, to keep the tests for interfering with
441# each other, and because they use different environment variables and
442# command line options.
443.SUFFIXES: .mk .rawout .out
444.mk.rawout:
445	@${_MKMSG_TEST:Uecho '#      test '} ${.PREFIX}
446	@set -eu; \
447	cd ${.OBJDIR}; \
448	env -i PATH="$$PATH" ${ENV.${.TARGET:R}} \
449	  ${TEST_MAKE} \
450	    -r -C ${.CURDIR} -f ${.IMPSRC} \
451	    ${FLAGS.${.TARGET:R}:U-k} \
452	    > ${.TARGET}.tmp 2>&1 \
453	&& status=$$? || status=$$?; \
454	echo $$status > ${.TARGET:R}.status
455	@mv ${.TARGET}.tmp ${.TARGET}
456
457# Postprocess the test output so that the results can be compared.
458#
459# always pretend .MAKE was called 'make'
460_SED_CMDS+=	-e 's,^${TEST_MAKE:T:S,.,\\.,g}[][0-9]*:,make:,'
461_SED_CMDS+=	-e 's,${TEST_MAKE:S,.,\\.,g},make,'
462# replace anything after 'stopped in' with unit-tests
463_SED_CMDS+=	-e '/stopped/s, /.*, unit-tests,'
464# strip ${.CURDIR}/ from the output
465_SED_CMDS+=	-e 's,${.CURDIR:S,.,\\.,g}/,,g'
466_SED_CMDS+=	-e 's,${UNIT_TESTS:S,.,\\.,g}/,,g'
467
468.rawout.out:
469	@${TOOL_SED} ${_SED_CMDS} ${SED_CMDS.${.TARGET:R}} \
470	  < ${.IMPSRC} > ${.TARGET}.tmp1
471	@${POSTPROC.${.TARGET:R}:Ucat} < ${.TARGET}.tmp1 > ${.TARGET}.tmp2
472	@rm ${.TARGET}.tmp1
473	@echo "exit status `cat ${.TARGET:R}.status`" >> ${.TARGET}.tmp2
474	@mv ${.TARGET}.tmp2 ${.TARGET}
475
476# Compare all output files
477test:	${OUTFILES} .PHONY
478	@failed= ; \
479	for test in ${TESTS}; do \
480	  diff -u ${UNIT_TESTS}/$${test}.exp $${test}.out \
481	  || failed="$${failed}$${failed:+ }$${test}" ; \
482	done ; \
483	if [ -n "$${failed}" ]; then \
484	  echo "Failed tests: $${failed}" ; false ; \
485	else \
486	  echo "All tests passed" ; \
487	fi
488
489accept:
490	@for test in ${TESTS}; do \
491	  cmp -s ${UNIT_TESTS}/$${test}.exp $${test}.out \
492	  || { echo "Replacing $${test}.exp" ; \
493	       cp $${test}.out ${UNIT_TESTS}/$${test}.exp ; } \
494	done
495
496# Note: only works for adding tests.
497# To remove a test, the $$mi file must be edited manually.
498sync-mi:
499	@set -eu;							\
500	cd "${MAKEFILE:tA:H}/../../..";					\
501	mi="distrib/sets/lists/tests/mi";				\
502	cvs update "$$mi";						\
503	testsdir="usr.bin/make/unit-tests";				\
504	fmt="./usr/tests/$$testsdir/%s\ttests-usr.bin-tests\tcompattestfile,atf\\n"; \
505	(cd "$$testsdir" && ls *.exp *.mk) | xargs printf "$$fmt" >> "$$mi"; \
506	distrib/sets/fmt-list "$$mi";					\
507	cvs diff "$$mi" || true
508
509.if exists(${TEST_MAKE})
510${TESTS:=.rawout}: ${TEST_MAKE} ${.PARSEDIR}/Makefile
511.endif
512
513.-include <bsd.obj.mk>
514