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