color-tests: coloring can be forced on non-ANSI terminals as well
[platform/upstream/automake.git] / lib / am / check.am
1 ## automake - create Makefile.in from Makefile.am
2 ## Copyright (C) 2001-2012 Free Software Foundation, Inc.
3
4 ## This program is free software; you can redistribute it and/or modify
5 ## it under the terms of the GNU General Public License as published by
6 ## the Free Software Foundation; either version 2, or (at your option)
7 ## any later version.
8
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ## GNU General Public License for more details.
13
14 ## You should have received a copy of the GNU General Public License
15 ## along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 am__tty_colors_dummy = \
18   mgn= red= grn= lgn= blu= brg= std=; \
19   am__color_tests=no
20 if %?COLOR%
21 am__tty_colors = { \
22   $(am__tty_colors_dummy); \
23   if test "X$(AM_COLOR_TESTS)" = Xno; then \
24     am__color_tests=no; \
25   elif test "X$(AM_COLOR_TESTS)" = Xalways; then \
26     am__color_tests=yes; \
27 ## If stdout is a non-dumb tty, use colors.  If test -t is not supported,
28 ## then this check fails; a conservative approach.  Of course do not
29 ## redirect stdout here, just stderr.
30   elif test "X$$TERM" != Xdumb && { test -t 1; } 2>/dev/null; then \
31     am__color_tests=yes; \
32   fi; \
33   if test $$am__color_tests = yes; then \
34     red='\e[0;31m'; \
35     grn='\e[0;32m'; \
36     lgn='\e[1;32m'; \
37     blu='\e[1;34m'; \
38     mgn='\e[0;35m'; \
39     brg='\e[1m'; \
40     std='\e[m'; \
41   fi; \
42 }
43 else !%?COLOR%
44 am__tty_colors = $(am__tty_colors_dummy)
45 endif !%?COLOR%
46
47 .PHONY: check-TESTS
48
49 if %?PARALLEL_TESTS%
50
51 include inst-vars.am
52
53 ## New parallel test driver.
54 ##
55 ## The first version of the code here was adapted from check.mk, which was
56 ## originally written at EPITA/LRDE, further developed at Gostai, then made
57 ## its way from GNU coreutils to end up, largely rewritten, in Automake.
58 ## The current version is an heavy rewrite of that, to allow for support
59 ## of more test metadata, and the use of custom test derivers and protocols
60 ## (among them, TAP).
61
62 # Restructured Text title and section.
63 am__rst_title = { sed 's/.*/   &   /;h;s/./=/g;p;x;s/ *$$//;p;g' && echo; }
64 am__rst_section = { sed 'p;s/./=/g;' && echo; }
65
66 # Solaris 10 'make', and several other traditional 'make' implementations,
67 # pass "-e" to $(SHELL), and POSIX 2008 even requires this.  Work around it
68 # by disabling -e (using the XSI extension "set +e") if it's set.
69 am__sh_e_setup = case $$- in *e*) set +e;; esac
70
71 # Default flags passed to test drivers.
72 am__common_driver_flags = \
73   --color-tests "$$am__color_tests" \
74   --enable-hard-errors "$$am__enable_hard_errors" \
75   --expect-failure "$$am__expect_failure"
76
77 # To be inserted before the command running the test.  Creates the
78 # directory for the log if needed.  Stores in $dir the directory
79 # containing $f, in $tst the test, in $log the log.  Executes the
80 # developer- defined test setup AM_TESTS_ENVIRONMENT (if any), and
81 # passes TESTS_ENVIRONMENT.  Set up options for the wrapper that
82 # will run the test scripts (or their associated LOG_COMPILER, if
83 # thy have one).
84 am__check_pre =                                         \
85 $(am__sh_e_setup);                                      \
86 $(am__vpath_adj_setup) $(am__vpath_adj)                 \
87 $(am__tty_colors);                                      \
88 srcdir=$(srcdir); export srcdir;                        \
89 am__odir=`echo "./$@" | sed 's|/[^/]*$$||'`;            \
90 test "x$$am__odir" = x. || $(MKDIR_P) "$$am__odir" || exit $$?; \
91 if test -f "./$$f"; then dir=./;                        \
92 elif test -f "$$f"; then dir=;                          \
93 else dir="$(srcdir)/"; fi;                              \
94 tst=$$dir$$f; log='$@';                                 \
95 if test -n '$(DISABLE_HARD_ERRORS)'; then               \
96   am__enable_hard_errors=no;                            \
97 else                                                    \
98   am__enable_hard_errors=yes;                           \
99 fi;                                                     \
100 ## The use of $dir below is required to account for VPATH
101 ## rewriting done by Sun make.
102 case " $(XFAIL_TESTS) " in                              \
103   *[\ \ ]$$f[\ \        ]* | *[\ \      ]$$dir$$f[\ \   ]*) \
104     am__expect_failure=yes;;                            \
105   *)                                                    \
106     am__expect_failure=no;;                             \
107 esac;                                                   \
108 $(AM_TESTS_ENVIRONMENT) $(TESTS_ENVIRONMENT)
109
110 # A shell command to get the names of the tests scripts with any registered
111 # extension removed (i.e., equivalently, the names of the test logs, with
112 # the '.log' extension removed).  The result is saved in the shell variable
113 # '$bases'.  This honors runtime overriding of TESTS and TEST_LOGS.  Sadly,
114 # we cannot use something simpler, involving e.g., "$(TEST_LOGS:.log=)",
115 # since that might cause problem with VPATH rewrites for suffix-less tests.
116 # See also 'test-harness-vpath-rewrite.test' and 'test-trs-basic.test'.
117 am__set_TESTS_bases = \
118   bases='$(TEST_LOGS)'; \
119   bases=`for i in $$bases; do echo $$i; done | sed 's/\.log$$//'`; \
120 ## Trim away any extra whitespace.  This has already proved useful
121 ## in avoiding weird bug on lesser make implementations.  It also
122 ## works around the GNU make 3.80 bug where trailing whitespace in
123 ## "TESTS = foo.test $(empty)" causes $(TESTS_LOGS)  to erroneously
124 ## expand to "foo.log .log".
125   bases=`echo $$bases`
126
127 # Recover from deleted '.trs' file; this should ensure that
128 # "rm -f foo.log; make foo.trs" re-run 'foo.test', and re-create
129 # both 'foo.log' and 'foo.trs'.  Break the recipe in two subshells
130 # to avoid problems with "make -n".
131 .log.trs:
132         rm -f $< $@
133         $(MAKE) $(AM_MAKEFLAGS) $<
134
135 $(TEST_SUITE_LOG): $(TEST_LOGS)
136         @$(am__set_TESTS_bases); \
137 ## Helper shell function, tells whether a path refers to an existing,
138 ## regular, readable file.
139         am__f_ok () { test -f "$$1" && test -r "$$1"; }; \
140 ## We need to ensures that all the required '.trs' and '.log' files will
141 ## be present and readable.  The direct dependencies of $(TEST_SUITE_LOG)
142 ## only ensure that all the '.log' files exists; they don't ensure that
143 ## the '.log' files are readable, and worse, they don't ensure that the
144 ## '.trs' files even exist.
145         redo_bases=`for i in $$bases; do \
146                       am__f_ok $$i.trs && am__f_ok $$i.log || echo $$i; \
147                     done`; \
148         if test -n "$$redo_bases"; then \
149 ## Uh-oh, either some '.log' files were unreadable, or some '.trs' files
150 ## were missing (or unreadable).  We need to re-run the corresponding
151 ## tests in order to re-create them.
152           redo_logs=`for i in $$redo_bases; do echo $$i.log; done`; \
153           redo_results=`for i in $$redo_bases; do echo $$i.trs; done`; \
154           if $(am__make_dryrun); then :; else \
155 ## Break "rm -f" into two calls to minimize the possibility of exceeding
156 ## command line length limits.
157             rm -f $$redo_logs && rm -f $$redo_results || exit 1; \
158           fi; \
159         fi; \
160 ## Use a trick to to ensure that we don't go into an infinite recursion
161 ## in case a test log in $(TEST_LOGS) is the same as $(TEST_SUITE_LOG).
162 ## Yes, this has already happened in practice.  Sigh!
163         if test -n "$$am__remaking_logs"; then \
164           echo "fatal: making $(TEST_SUITE_LOG): possible infinite" \
165                "recursion detected" >&2; \
166         else \
167           am__remaking_logs=yes $(MAKE) $(AM_MAKEFLAGS) $$redo_logs; \
168         fi; \
169         if $(am__make_dryrun); then :; else \
170 ## Sanity check: each unreadable or non-existent test result file should
171 ## has been properly remade at this point, as should the corresponding log
172 ## file.
173           st=0;  \
174           errmsg="fatal: making $(TEST_SUITE_LOG): failed to create"; \
175           for i in $$redo_bases; do \
176             test -f $$i.trs && test -r $$i.trs \
177               || { echo "$$errmsg $$i.trs" >&2; st=1; }; \
178             test -f $$i.log && test -r $$i.log \
179               || { echo "$$errmsg $$i.log" >&2; st=1; }; \
180           done; \
181           test $$st -eq 0 || exit 1; \
182         fi
183 ## We need a new subshell to work portably with "make -n", since the
184 ## previous part of the recipe contained a $(MAKE) invocation.
185         @$(am__sh_e_setup); $(am__tty_colors); $(am__set_TESTS_bases); \
186         ws='[   ]'; \
187 ## List of test result files.
188         results=`for b in $$bases; do echo $$b.trs; done`; \
189         test -n "$$results" || results=/dev/null; \
190 ## Prepare data for the test suite summary.  These do not take into account
191 ## unreadable test results, but they'll be appropriately updated later if
192 ## needed.
193         all=`  grep "^$$ws*:test-result:"           $$results | wc -l`; \
194         pass=` grep "^$$ws*:test-result:$$ws*PASS"  $$results | wc -l`; \
195         fail=` grep "^$$ws*:test-result:$$ws*FAIL"  $$results | wc -l`; \
196         skip=` grep "^$$ws*:test-result:$$ws*SKIP"  $$results | wc -l`; \
197         xfail=`grep "^$$ws*:test-result:$$ws*XFAIL" $$results | wc -l`; \
198         xpass=`grep "^$$ws*:test-result:$$ws*XPASS" $$results | wc -l`; \
199         error=`grep "^$$ws*:test-result:$$ws*ERROR" $$results | wc -l`; \
200 ## Whether the testsuite was successful or not.
201         if test `expr $$fail + $$xpass + $$error` -eq 0; then \
202           success=true; \
203         else \
204           success=false; \
205         fi; \
206 ## Make $br a line of exactly 76 '=' characters, that will be used to
207 ## enclose the testsuite summary report when displayed on the console.
208         br='==================='; br=$$br$$br$$br$$br; \
209 ## When writing the test summary to the console, we want to color a line
210 ## reporting the count of some result *only* if at least one test
211 ## experienced such a result.  This function is handy in this regard.
212         result_count () \
213         { \
214             if test x"$$1" = x"--maybe-color"; then \
215               maybe_colorize=yes; \
216             elif test x"$$1" = x"--no-color"; then \
217               maybe_colorize=no; \
218             else \
219               echo "$@: invalid 'result_count' usage" >&2; exit 4; \
220             fi; \
221             shift; \
222             desc=$$1 count=$$2; \
223             if test $$maybe_colorize = yes && test $$count -gt 0; then \
224               color_start=$$3 color_end=$$std; \
225             else \
226               color_start= color_end=; \
227             fi; \
228             echo "$${color_start}# $$desc $$count$${color_end}"; \
229         }; \
230 ## A shell function that creates the testsuite summary.  We need it
231 ## because we have to create *two* summaries, one for test-suite.log,
232 ## and a possibly-colorized one for console output.
233         create_testsuite_report () \
234         { \
235           result_count $$1 "TOTAL:" $$all   "$$brg"; \
236           result_count $$1 "PASS: " $$pass  "$$grn"; \
237           result_count $$1 "SKIP: " $$skip  "$$blu"; \
238           result_count $$1 "XFAIL:" $$xfail "$$lgn"; \
239           result_count $$1 "FAIL: " $$fail  "$$red"; \
240           result_count $$1 "XPASS:" $$xpass "$$red"; \
241           result_count $$1 "ERROR:" $$error "$$mgn"; \
242         }; \
243 ## Write "global" testsuite log.
244         {                                                               \
245           echo "$(PACKAGE_STRING): $(subdir)/$(TEST_SUITE_LOG)" |       \
246             $(am__rst_title);                                           \
247           create_testsuite_report --no-color;                           \
248           echo;                                                         \
249           echo ".. contents:: :depth: 2";                               \
250           echo;                                                         \
251           for i in $$bases; do                                          \
252 ## FIXME: one fork per test -- this is horrendously inefficient!
253             if grep "^$$ws*:copy-in-global-log:$$ws*no$$ws*$$" $$i.trs \
254                  >/dev/null; then continue; \
255             fi; \
256 ## Get the declared "global result" of the test.
257 ## FIXME: yet another one fork per test here!
258             glob_res=`sed -n -e "s/$$ws*$$//" \
259                              -e "s/^$$ws*:global-test-result:$$ws*//p" \
260                         $$i.trs`; \
261 ## If no global result is explicitly declared, we'll merely mark the
262 ## test as "RUN" in the global test log.
263             test -n "$$glob_res" || glob_res=RUN; \
264 ## Write the name and result of the test as an RST section title.
265             echo "$$glob_res: $$i" | $(am__rst_section); \
266 ## If we should have remade any unreadable '.log', above.
267             if test ! -r $$i.log; then \
268               echo "fatal: making $@: $$i.log is unreadable" >&2; \
269               exit 1; \
270             fi; \
271             cat $$i.log; echo; \
272           done; \
273         } >$(TEST_SUITE_LOG).tmp;                                       \
274         mv $(TEST_SUITE_LOG).tmp $(TEST_SUITE_LOG);                     \
275 ## Emit the test summary on the console.
276         if $$success; then                                              \
277           col="$$grn";                                                  \
278          else                                                           \
279           col="$$red";                                                  \
280           test x"$$VERBOSE" = x || cat $(TEST_SUITE_LOG);               \
281         fi;                                                             \
282 ## Multi line coloring is problematic with "less -R", so we really need
283 ## to color each line individually.
284         echo "$${col}$$br$${std}";                                      \
285         echo "$${col}Testsuite summary for $(PACKAGE_STRING)$${std}";   \
286         echo "$${col}$$br$${std}";                                      \
287 ## This is expected to go to the console, so it might have to be colorized.
288         create_testsuite_report --maybe-color;                          \
289         echo "$$col$$br$$std";                                          \
290         if $$success; then :; else                                      \
291           echo "$${col}See $(subdir)/$(TEST_SUITE_LOG)$${std}";         \
292           if test -n "$(PACKAGE_BUGREPORT)"; then                       \
293             echo "$${col}Please report to $(PACKAGE_BUGREPORT)$${std}"; \
294           fi;                                                           \
295           echo "$$col$$br$$std";                                        \
296         fi;                                                             \
297 ## Be sure to exit with the proper exit status.  The use of "exit 1" below
298 ## is required to work around a FreeBSD make bug (present only when running
299 ## in concurrent mode).  See automake bug#9245:
300 ##  <http://debbugs.gnu.org/cgi/bugreport.cgi?bug=9245>
301 ## and FreeBSD PR bin/159730:
302 ##  <http://www.freebsd.org/cgi/query-pr.cgi?pr=159730>.
303         $$success || exit 1
304
305 RECHECK_LOGS = $(TEST_LOGS)
306
307 ## ------------------------------------------ ##
308 ## Running all tests, or rechecking failures. ##
309 ## ------------------------------------------ ##
310
311 check-TESTS recheck:
312 ## If we are running "make recheck", it's not the user which can decide
313 ## which tests to consider for re-execution, so we must ignore the value
314 ## of $(RECHECK_LOGS).
315 ## Here and below, we expand $(RECHECK_LOGS) only once, to avoid exceeding
316 ## line length limits.
317         @if test $@ != recheck; then \
318            list='$(RECHECK_LOGS)'; \
319            test -z "$$list" || rm -f $$list; \
320          fi
321         @if test $@ != recheck; then \
322            list='$(RECHECK_LOGS:.log=.trs)'; \
323            test -z "$$list" || rm -f $$list; \
324          fi
325 ## We always have to remove TEST_SUITE_LOG, to ensure its rule is run
326 ## in any case even in lazy mode: otherwise, if no test needs rerunning,
327 ## or a prior run plus reruns all happen within the same timestamp (can
328 ## happen with a prior "make TESTS=<subset>"), then we get no log output.
329 ## OTOH, this means that, in the rule for '$(TEST_SUITE_LOG)', we
330 ## cannot use '$?' to compute the set of lazily rerun tests, lest
331 ## we rely on .PHONY to work portably.
332         @test -z "$(TEST_SUITE_LOG)" || rm -f $(TEST_SUITE_LOG)
333         @ws='[  ]'; \
334         log_list='' trs_list=''; $(am__set_TESTS_bases); \
335         for i in $$bases; do \
336 ## If running a "make recheck", we must only consider tests that had an
337 ## unexpected outcome (FAIL or XPASS) in the earlier run.  In particular,
338 ## skip tests that haven't been run.  But recover gracefully from deleted
339 ## '.trs' files.
340           if test $@ = recheck; then \
341             test -f $$i.trs || test -f $$i.log || continue; \
342 ## FIXME: one fork per test -- this is horrendously inefficient!
343             grep "^$$ws*:recheck:$$ws*no$$ws*$$" $$i.trs \
344               >/dev/null 2>&1 && continue; \
345           else :; fi; \
346 ## Be careful to avoid extra whitespace in the definition of $list, since
347 ## its value will be passed to the recursive make invocation below through
348 ## the TEST_LOGS macro, and leading/trailing white space in a make macro
349 ## definition can be problematic.  In this particular case, trailing white
350 ## space was known to cause a segmentation fault on Solaris 10 XPG4 make:
351 ## <http://lists.gnu.org/archive/html/bug-automake/2010-08/msg00004.html>
352           if test -z "$$log_list"; then \
353             log_list="$$i.log"; \
354           else \
355             log_list="$$log_list $$i.log"; \
356           fi; \
357           if test -z "$$trs_list"; then \
358             trs_list="$$i.trs"; \
359           else \
360             trs_list="$$trs_list $$i.trs"; \
361           fi; \
362         done; \
363 ## Under "make recheck", remove the .log and .trs files associated
364 ## with the files to recheck, so that those will be rerun by the
365 ## "make test-suite.log" recursive invocation below.  But use a proper
366 ## hack to avoid extra files removal when running under "make -n".
367         if test $@ != recheck || $(am__make_dryrun); then :; else \
368           test -z "$$log_list" || rm -f $$log_list; \
369           test -z "$$trs_list" || rm -f $$trs_list; \
370         fi; \
371         $(MAKE) $(AM_MAKEFLAGS) $(TEST_SUITE_LOG) TEST_LOGS="$$log_list"
372
373 ## Recheck must depend on $(check_SCRIPTS), $(check_PROGRAMS), etc.
374 ## It must also depend on the 'all' target.  See automake bug#11252.
375 recheck: all %CHECK_DEPS%
376
377 AM_RECURSIVE_TARGETS += check recheck
378
379 .PHONY: recheck
380
381 else !%?PARALLEL_TESTS%
382
383 check-TESTS: $(TESTS)
384         @failed=0; all=0; xfail=0; xpass=0; skip=0; \
385         srcdir=$(srcdir); export srcdir; \
386 ## Make sure Solaris VPATH-expands all members of this list, even
387 ## the first and the last one; thus the spaces around $(TESTS)
388         list=' $(TESTS) '; \
389         $(am__tty_colors); \
390         if test -n "$$list"; then \
391           for tst in $$list; do \
392             if test -f ./$$tst; then dir=./; \
393 ## Note: Solaris 2.7 seems to expand TESTS using VPATH.  That's
394 ## why we also try 'dir='.
395             elif test -f $$tst; then dir=; \
396             else dir="$(srcdir)/"; fi; \
397             if $(TESTS_ENVIRONMENT) $${dir}$$tst $(AM_TESTS_FD_REDIRECT); then \
398 ## Success
399               all=`expr $$all + 1`; \
400               case " $(XFAIL_TESTS) " in \
401               *[\ \     ]$$tst[\ \      ]*) \
402                 xpass=`expr $$xpass + 1`; \
403                 failed=`expr $$failed + 1`; \
404                 col=$$red; res=XPASS; \
405               ;; \
406               *) \
407                 col=$$grn; res=PASS; \
408               ;; \
409               esac; \
410             elif test $$? -ne 77; then \
411 ## Failure
412               all=`expr $$all + 1`; \
413               case " $(XFAIL_TESTS) " in \
414               *[\ \     ]$$tst[\ \      ]*) \
415                 xfail=`expr $$xfail + 1`; \
416                 col=$$lgn; res=XFAIL; \
417               ;; \
418               *) \
419                 failed=`expr $$failed + 1`; \
420                 col=$$red; res=FAIL; \
421               ;; \
422               esac; \
423             else \
424 ## Skipped
425               skip=`expr $$skip + 1`; \
426               col=$$blu; res=SKIP; \
427             fi; \
428             echo "$${col}$$res$${std}: $$tst"; \
429           done; \
430 ## Prepare the banner
431           if test "$$all" -eq 1; then \
432             tests="test"; \
433             All=""; \
434           else \
435             tests="tests"; \
436             All="All "; \
437           fi; \
438           if test "$$failed" -eq 0; then \
439             if test "$$xfail" -eq 0; then \
440               banner="$$All$$all $$tests passed"; \
441             else \
442               if test "$$xfail" -eq 1; then failures=failure; else failures=failures; fi; \
443               banner="$$All$$all $$tests behaved as expected ($$xfail expected $$failures)"; \
444             fi; \
445           else \
446             if test "$$xpass" -eq 0; then \
447               banner="$$failed of $$all $$tests failed"; \
448             else \
449               if test "$$xpass" -eq 1; then passes=pass; else passes=passes; fi; \
450               banner="$$failed of $$all $$tests did not behave as expected ($$xpass unexpected $$passes)"; \
451             fi; \
452           fi; \
453 ## DASHES should contain the largest line of the banner.
454           dashes="$$banner"; \
455           skipped=""; \
456           if test "$$skip" -ne 0; then \
457             if test "$$skip" -eq 1; then \
458               skipped="($$skip test was not run)"; \
459             else \
460               skipped="($$skip tests were not run)"; \
461             fi; \
462             test `echo "$$skipped" | wc -c` -le `echo "$$banner" | wc -c` || \
463               dashes="$$skipped"; \
464           fi; \
465           report=""; \
466           if test "$$failed" -ne 0 && test -n "$(PACKAGE_BUGREPORT)"; then \
467             report="Please report to $(PACKAGE_BUGREPORT)"; \
468             test `echo "$$report" | wc -c` -le `echo "$$banner" | wc -c` || \
469               dashes="$$report"; \
470           fi; \
471           dashes=`echo "$$dashes" | sed s/./=/g`; \
472           if test "$$failed" -eq 0; then \
473             col="$$grn"; \
474           else \
475             col="$$red"; \
476           fi; \
477 ## Multi line coloring is problematic with "less -R", so we really need
478 ## to color each line individually.
479           echo "$${col}$$dashes$${std}"; \
480           echo "$${col}$$banner$${std}"; \
481           test -z "$$skipped" || echo "$${col}$$skipped$${std}"; \
482           test -z "$$report" || echo "$${col}$$report$${std}"; \
483           echo "$${col}$$dashes$${std}"; \
484           test "$$failed" -eq 0; \
485         else :; fi
486
487 endif !%?PARALLEL_TESTS%