Home | History | Annotate | Line # | Download | only in unit-tests
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.
     36 TESTS+=		# archive	# broken on FreeBSD
     37 TESTS+=		cmdline
     38 TESTS+=		comment
     39 TESTS+=		cond-late
     40 TESTS+=		cond-short
     41 TESTS+=		cond1
     42 TESTS+=		cond2
     43 TESTS+=		counter
     44 TESTS+=		dir
     45 TESTS+=		directives
     46 TESTS+=		dollar
     47 TESTS+=		doterror
     48 TESTS+=		dotwait
     49 TESTS+=		envfirst
     50 TESTS+=		error
     51 TESTS+=		# escape	# broken by reverting POSIX changes
     52 TESTS+=		export
     53 TESTS+=		export-all
     54 TESTS+=		export-env
     55 TESTS+=		forloop
     56 TESTS+=		forsubst
     57 TESTS+=		hash
     58 TESTS+=		impsrc
     59 TESTS+=		include-main
     60 TESTS+=		lint
     61 TESTS+=		misc
     62 TESTS+=		moderrs
     63 TESTS+=		modmatch
     64 TESTS+=		modmisc
     65 TESTS+=		modorder
     66 TESTS+=		modts
     67 TESTS+=		modword
     68 TESTS+=		order
     69 TESTS+=		phony-end
     70 TESTS+=		posix
     71 TESTS+=		# posix1	# broken by reverting POSIX changes
     72 TESTS+=		qequals
     73 TESTS+=		recursive
     74 TESTS+=		# suffixes	# runs into an endless loop (try -dA)
     75 TESTS+=		sunshcmd
     76 TESTS+=		sysv
     77 TESTS+=		ternary
     78 TESTS+=		unexport
     79 TESTS+=		unexport-env
     80 TESTS+=		varcmd
     81 TESTS+=		vardebug
     82 TESTS+=		varfind
     83 TESTS+=		varmisc
     84 TESTS+=		varmod-edge
     85 TESTS+=		varparse-dynamic
     86 TESTS+=		varquote
     87 TESTS+=		varshell
     88 
     89 # Override environment variables for some of the tests.
     90 ENV.counter=		-i
     91 ENV.envfirst=		FROM_ENV=value-from-env
     92 ENV.export=		-i PATH=${PATH:Q}
     93 ENV.lint=		-i
     94 ENV.recursive=		-i
     95 ENV.varmisc=		FROM_ENV=env
     96 ENV.varmisc+=		FROM_ENV_BEFORE=env
     97 ENV.varmisc+=		FROM_ENV_AFTER=env
     98 
     99 # Override make flags for some of the tests; default is -k.
    100 FLAGS.counter=		-r -dv
    101 FLAGS.doterror=		# none
    102 FLAGS.envfirst=		-e
    103 FLAGS.export=		-r
    104 FLAGS.lint=		-dL -k
    105 FLAGS.order=		-j1
    106 FLAGS.recursive=	-dL -r
    107 FLAGS.vardebug=		-k -dv FROM_CMDLINE=
    108 
    109 # Some tests need extra post-processing.
    110 SED_CMDS.moderrs+=	-e 's,\(Regex compilation error:\).*,\1 (details omitted),'
    111 SED_CMDS.modmisc+=	-e 's,\(Regex compilation error:\).*,\1 (details omitted),'
    112 SED_CMDS.varmod-edge+=	-e 's, line [0-9]*:, line omitted:,'
    113 SED_CMDS.varshell+=	-e 's,^[a-z]*sh: ,,'
    114 SED_CMDS.varshell+=	-e '/command/s,No such.*,not found,'
    115 
    116 # Some tests need an additional round of postprocessing.
    117 POSTPROC.counter=	${TOOL_SED} -n -e '/:RELEVANT = yes/,/:RELEVANT = no/p'
    118 POSTPROC.vardebug=	${TOOL_SED} -n -e '/:RELEVANT = yes/,/:RELEVANT = no/p'
    119 
    120 # End of the configuration section.
    121 
    122 .MAIN: all
    123 
    124 UNIT_TESTS:=	${.PARSEDIR}
    125 .PATH: ${UNIT_TESTS}
    126 
    127 OUTFILES=	${TESTS:=.out}
    128 
    129 all: ${OUTFILES}
    130 
    131 CLEANFILES+=		*.rawout *.out *.status *.tmp *.core *.tmp
    132 CLEANFILES+=		obj*.[och] lib*.a	# posix1.mk
    133 CLEANFILES+=		issue* .[ab]*		# suffixes.mk
    134 CLEANRECURSIVE+=	dir dummy		# posix1.mk
    135 
    136 clean:
    137 	rm -f ${CLEANFILES}
    138 .if !empty(CLEANRECURSIVE)
    139 	rm -rf ${CLEANRECURSIVE}
    140 .endif
    141 
    142 TEST_MAKE?=	${.MAKE}
    143 TOOL_SED?=	sed
    144 
    145 # ensure consistent results from sort(1)
    146 LC_ALL=		C
    147 LANG=		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
    184 test:	${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 
    196 accept:
    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 
    203 SYNC_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 
    217 sync-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