yacc tests: fix spurious failure with parallel make
[platform/upstream/automake.git] / t / self-check-reexec.tap
1 #! /bin/sh
2 # Copyright (C) 2011-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 # Sanity check for the automake testsuite.
18 # Check that automatic re-execution of test script with the
19 # configure-time $AM_TEST_RUNNER_SHELL.
20
21 am_create_testdir=empty
22 . ./defs || exit 1
23
24 plan_ 32
25
26 unset AM_TESTS_REEXEC BASH_VERSION || :
27
28 cwd=$(pwd) || fatal_ "getting current working directory"
29 cp "$am_top_builddir"/defs . || fatal_ "fetching 'defs' from top_builddir"
30
31 #
32 # Search for required bash and non-bash shells.
33 #
34
35 for bash_shell in \
36   "$SHELL" "$AM_TEST_RUNNER_SHELL" bash bash3 bash4 :
37 do
38   test "$bash_shell" = : && break
39   $bash_shell --version || continue
40   $bash_shell -c 'test -n "$BASH_VERSION"' || continue
41   break
42 done
43
44 # This might not be optimal, but it's much better than writing wrapper
45 # scripts acting as "fake" shells.
46 for non_bash_shell in \
47   /bin/sh /bin/ksh sh ksh ash dash pdksh "$SHELL" "$AM_TEST_RUNNER_SHELL" :
48 do
49   test "$non_bash_shell" = : && break
50   $non_bash_shell -c 'exit 0' || continue
51   # Be sure to reject also any shell that is Zsh "in disguise" (as can
52   # be found on some Debian systems, where /bin/ksh can be symlinked to
53   # /bin/zsh4).  This is required because our testsuite does not support
54   # older versions of Zsh, and that has caused spurious failures in the
55   # past.
56   $non_bash_shell -c 'test -n "$ZSH_VERSION$BASH_VERSION"' && continue
57   break
58 done
59
60 echo "bash_shell='$bash_shell'"
61 echo "non_bash_shell='$non_bash_shell'"
62
63 # This would denote an internal error.
64 if test "$bash_shell" = : && test "$non_bash_shell" = :; then
65   fatal_ "we couldn't find a bash shell nor a non-bash one"
66 fi
67
68 #
69 # Functions used throughout the test.
70 #
71
72 get_ddata ()
73 {
74   case $1 in
75     ""|*/) dsep=;;
76         *) dsep=/;;
77   esac
78   case $1 in
79     "") dname="no dir";;
80     /*) dname="absolute dir";;
81      *) dname="dir '$1'";;
82   esac
83 }
84
85 get_sh ()
86 {
87   case $1 in
88     bash) sh=$bash_shell;;
89     non-bash) sh=$non_bash_shell;;
90     *) fatal_ "get_sh: invalid shell type '$1'";;
91   esac
92 }
93
94 #
95 # Check how to default, force or prevent a re-execution.
96 #
97
98 cat > need-bash.sh <<'END'
99 #!/bin/false
100 . ./defs
101 # Ensure that the script gets re-executed with bash.  Also ensure that
102 # non-standard syntax used after the inclusion of './defs' doesn't cause
103 # non-bash shells to fail.
104 # Subshell required to prevent some shells (e.g., Solaris 10 /bin/sh)
105 # from only complaining on stderr but then exiting with exit status 0.
106 (foo=abac && test xbxc = ${foo//a/x} && test -n "$BASH_VERSION")
107 END
108
109 sh_var=AM_TEST_RUNNER_SHELL
110 sed -e "s|^am_top_builddir=.*|am_top_builddir='$cwd'|" \
111     -e "s|^$sh_var=.*$|$sh_var=bash; export $sh_var|" \
112     < "$am_top_builddir"/defs-static >defs-static
113
114 do_reexec ()
115 {
116   command_ok_ "re-exec if AM_TESTS_REEXEC=$1" \
117               env AM_TESTS_REEXEC="$1" $non_bash_shell need-bash.sh
118 }
119
120 dont_reexec ()
121 {
122   command_ok_ "don't re-exec if AM_TESTS_REEXEC=$1" \
123               not env AM_TESTS_REEXEC="$1" $non_bash_shell need-bash.sh
124 }
125
126 if test "$bash_shell" = :; then
127   skip_row_ 10 -r "no bash shell found" AM_TESTS_REEXEC
128 elif test "$non_bash_shell" = :; then
129   skip_row_ 10 -r "no non-bash shell found" AM_TESTS_REEXEC
130 else
131   command_ok_ "re-exec if AM_TESTS_REEXEC unset" \
132               $non_bash_shell need-bash.sh
133   do_reexec ''
134   do_reexec yes
135   do_reexec y
136   do_reexec true
137   do_reexec 1
138   dont_reexec no
139   dont_reexec n
140   dont_reexec false
141   dont_reexec 0
142 fi
143
144 #
145 # Check message about the re-execution.  Also check that arguments passed
146 # to a test script are preserved by a re-exec, even in "corner" cases.
147 #
148
149 cat > dummy.sh <<'END'
150 #!/bin/sh
151 . ./defs
152 :
153 END
154
155 cat > checkargs.sh <<'END'
156 . ./defs
157 test $# -eq 3 && test x"$1" = x'a' && test x"$2" = x && test x"$3" = x"-e"
158 END
159
160 chmod a+x dummy.sh checkargs.sh
161
162 mkdir sub
163 cp dummy.sh checkargs.sh defs sub
164 sed -e "s|^am_top_builddir=.*|am_top_builddir='$cwd'|" \
165     <  "$am_top_builddir"/defs-static > defs-static
166 sed -e "s|^am_top_builddir=.*|am_top_builddir='$cwd/sub'|" \
167     <  "$am_top_builddir"/defs-static > sub/defs-static
168
169 check_preserve_args ()
170 {
171   dir=$1; shift
172   get_ddata "$dir"
173   $sh "${dir}${dsep}checkargs.sh" a '' -e && r='ok' || r='not ok'
174   result_ "$r" "$sh re-exec preserving args [$dname]"
175 }
176
177 check_reexec_message ()
178 {
179   dir=$1; shift
180   get_ddata "$dir"
181   $sh "${dir}${dsep}dummy.sh" "$@" \
182     | grep "^exec $AM_TEST_RUNNER_SHELL ${dir}${dsep}dummy\\.sh $*\$" \
183     && r='ok' || r='not ok'
184   result_ "$r" "$sh display re-exec message [$dname] [args: $*]"
185 }
186
187 ./dummy.sh a b \
188   | grep "^exec $AM_TEST_RUNNER_SHELL \\./dummy\\.sh a b$" \
189   && r='ok' || r='not ok'
190 result_ "$r" "direct run display re-exec message [args: a b]"
191
192 ./checkargs.sh a '' -e && r='ok' || r='not ok'
193 result_ "$r" "direct re-exec preserving args"
194
195 for sh_type in non-bash bash; do
196   get_sh $sh_type
197   if test "$sh" = :; then
198     skip_row_ 5 -r "no $sh_type shell available" "re-exec message"
199     skip_row_ 5 -r "no $sh_type shell available" "re-exec preserving args"
200     continue
201   fi
202   check_preserve_args ''
203   check_reexec_message '' a b c
204   check_preserve_args .
205   check_reexec_message .  a b c
206   cd sub
207   check_preserve_args ..
208   check_reexec_message .. a b c
209   cd ..
210   check_preserve_args "$cwd"
211   check_reexec_message "$cwd" a -b c-
212   check_preserve_args sub
213   check_reexec_message sub 1 2 3 4
214 done
215
216 :