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