test harness: improve catching of usage errors in script 'test-driver'
[platform/upstream/automake.git] / t / color-tests2.sh
1 #! /bin/sh
2 # Copyright (C) 2007-2013 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 # Test Automake TESTS color output, using the expect(1) program.
18 # Keep this in sync with the sister test 'color-tests.sh'.
19
20 required='grep-nonprint'
21 # For gen-testsuite-part: ==> try-with-serial-tests <==
22 . test-init.sh
23
24 # Escape '[' for grep, below.
25 red="$esc\[0;31m"
26 grn="$esc\[0;32m"
27 lgn="$esc\[1;32m"
28 blu="$esc\[1;34m"
29 mgn="$esc\[0;35m"
30 std="$esc\[m"
31
32 # This test requires a working a working 'expect' program.
33 (set +e; expect -c 'exit 77'; test $? -eq 77) \
34   || skip_ "requires a working expect program"
35
36 # Also, if the $MAKE program fails to consider the standard output as a
37 # tty (this happens with e.g., BSD make and Solaris dmake when they're
38 # run in parallel mode; see the autoconf manual), there is little point
39 # in proceeding.
40 cat > Makefile <<'END'
41 all:
42 ## Creative quoting in the 'echo' below to avoid risk of spurious output
43 ## matches by 'expect', below.
44         @test -t 1 && echo "stdout" "is" "a" "tty"
45 END
46
47 cat > expect-check <<'END'
48 eval spawn $env(MAKE)
49 expect {
50   "stdout is a tty" { exit 0 }
51   default { exit 1 }
52 }
53 exit 1
54 END
55 MAKE=$MAKE expect -f expect-check \
56   || skip_ "make spawned by expect should have a tty stdout"
57 rm -f expect-check Makefile
58
59 # Do the tests.
60
61 cat >>configure.ac << 'END'
62 if $testsuite_colorized; then :; else
63   AC_SUBST([AM_COLOR_TESTS], [no])
64 fi
65 AC_OUTPUT
66 END
67
68 cat >Makefile.am <<'END'
69 TESTS = $(check_SCRIPTS)
70 check_SCRIPTS = pass fail skip xpass xfail error
71 XFAIL_TESTS = xpass xfail
72 END
73
74 cat >pass <<END
75 #! /bin/sh
76 exit 0
77 END
78
79 cat >fail <<END
80 #! /bin/sh
81 exit 1
82 END
83
84 cat >skip <<END
85 #! /bin/sh
86 exit 77
87 END
88
89 cat >error <<END
90 #! /bin/sh
91 exit 99
92 END
93
94 cp fail xfail
95 cp pass xpass
96 chmod +x pass fail skip xpass xfail error
97
98 $ACLOCAL
99 $AUTOCONF
100 $AUTOMAKE --add-missing
101
102 test_color ()
103 {
104   # Not a useless use of cat; see above comments "grep-nonprinting"
105   # requirement in 'test-init.sh'.
106   cat stdout | grep "^${grn}PASS${std}: .*pass"
107   cat stdout | grep "^${red}FAIL${std}: .*fail"
108   cat stdout | grep "^${blu}SKIP${std}: .*skip"
109   cat stdout | grep "^${lgn}XFAIL${std}: .*xfail"
110   cat stdout | grep "^${red}XPASS${std}: .*xpass"
111   # The old serial testsuite driver doesn't distinguish between failures
112   # and hard errors.
113   if test x"$am_serial_tests" = x"yes"; then
114     cat stdout | grep "^${red}FAIL${std}: .*error"
115   else
116     cat stdout | grep "^${mgn}ERROR${std}: .*error"
117   fi
118   :
119 }
120
121 test_no_color ()
122 {
123   # With make implementations that, like Solaris make, in case of errors
124   # print the whole failing recipe on standard output, we should content
125   # ourselves with a laxer check, to avoid false positives.
126   # Keep this in sync with lib/am/check.am:$(am__color_tests).
127   if $FGREP '= Xalways; then' stdout; then
128     # Extra verbose make, resort to laxer checks.
129     # Note that we also want to check that the testsuite summary is
130     # not unduly colorized.
131     (
132       set +e # In case some grepped regex below isn't matched.
133       # Not a useless use of cat; see above comments "grep-nonprinting"
134       # requirement in 'test-init.sh'.
135       cat stdout | grep "TOTAL.*:"
136       cat stdout | grep "PASS.*:"
137       cat stdout | grep "FAIL.*:"
138       cat stdout | grep "SKIP.*:"
139       cat stdout | grep "XFAIL.*:"
140       cat stdout | grep "XPASS.*:"
141       cat stdout | grep "ERROR.*:"
142       cat stdout | grep 'test.*expected'
143       cat stdout | grep 'test.*not run'
144       cat stdout | grep '===='
145       cat stdout | grep '[Ss]ee .*test-suite\.log'
146       cat stdout | grep '[Tt]estsuite summary'
147     ) | grep "$esc" && exit 1
148     : For shells with broken 'set -e'
149   else
150     cat stdout | grep "$esc" && exit 1
151     : For shells with broken 'set -e'
152   fi
153 }
154
155 our_make ()
156 {
157   set "MAKE=$MAKE" ${1+"$@"}
158   env "$@" expect -f $srcdir/expect-make >stdout || { cat stdout; exit 1; }
159   cat stdout
160 }
161
162 cat >expect-make <<'END'
163 eval spawn $env(MAKE) -e check
164 expect eof
165 END
166
167 for vpath in false :; do
168
169   if $vpath; then
170     mkdir build
171     cd build
172     srcdir=..
173   else
174     srcdir=.
175   fi
176
177   $srcdir/configure
178
179   our_make TERM=ansi
180   test_color
181
182   our_make TERM=dumb
183   test_no_color
184
185   our_make AM_COLOR_TESTS=no
186   test_no_color
187
188   $srcdir/configure testsuite_colorized=false
189
190   our_make TERM=ansi
191   test_no_color
192
193   our_make TERM=ansi MAKE="env AM_COLOR_TESTS=always $MAKE"
194   test_color
195
196   $MAKE distclean
197   cd $srcdir
198
199 done
200
201 :