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