maint: no more make recursion in Automake's build system
[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 $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_ "cannot get 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 "$SHELL" bash bash3 bash4 :; do
36   test "$bash_shell" = : && break
37   $bash_shell --version || continue
38   $bash_shell -c 'test -n "$BASH_VERSION"' || continue
39   break
40 done
41
42 # This might not be optimal, but it's much better than writing wrapper
43 # scripts acting as "fake" shells.
44 for non_bash_shell in /bin/sh /bin/ksh "$SHELL" sh ksh ash dash pdksh :; do
45   test "$non_bash_shell" = : && break
46   $non_bash_shell -c 'exit 0' || continue
47   $non_bash_shell -c 'test -n "$BASH_VERSION"' && continue
48   break
49 done
50
51 echo "bash_shell='$bash_shell'"
52 echo "non_bash_shell='$non_bash_shell'"
53
54 # This would denote an internal error.
55 if test "$bash_shell" = : && test "$non_bash_shell" = :; then
56   fatal_ "we couldn't find a bash shell nor a non-bash one"
57 fi
58
59 #
60 # Functions used throughout the test.
61 #
62
63 get_ddata ()
64 {
65   case $1 in
66     ""|*/) dsep=;;
67         *) dsep=/;;
68   esac
69   case $1 in
70     "") dname="no dir";;
71     /*) dname="absolute dir";;
72      *) dname="dir '$1'";;
73   esac
74 }
75
76 get_sh ()
77 {
78   case $1 in
79     bash) sh=$bash_shell;;
80     non-bash) sh=$non_bash_shell;;
81     *) fatal_ "get_sh: invalid shell type '$1'";;
82   esac
83 }
84
85 #
86 # Check how to default, force or prevent a re-execution.
87 #
88
89 cat > need-bash.sh <<'END'
90 #!/bin/false
91 . ./defs
92 # Ensure that the script gets re-executed with bash.  Also ensure that
93 # non-standard syntax used after the inclusion of './defs' doesn't cause
94 # non-bash shells to fail.
95 # Subshell required to prevent some shells (e.g., Solaris 10 /bin/sh)
96 # from only complaining on stderr but then exiting with exit status 0.
97 (foo=abac && test xbxc = ${foo//a/x} && test -n "$BASH_VERSION")
98 END
99
100 sed -e "s|^am_top_builddir=.*|am_top_builddir='$cwd'|" \
101     -e 's|^SHELL=.*$|SHELL=bash; export SHELL|' \
102     < "$am_top_builddir"/defs-static >defs-static
103
104 do_reexec ()
105 {
106   command_ok_ "re-exec if AM_TESTS_REEXEC=$1" \
107               env AM_TESTS_REEXEC="$1" $non_bash_shell need-bash.sh
108 }
109
110 dont_reexec ()
111 {
112   command_ok_ "don't re-exec if AM_TESTS_REEXEC=$1" \
113               not env AM_TESTS_REEXEC="$1" $non_bash_shell need-bash.sh
114 }
115
116 if test "$bash_shell" = :; then
117   skip_row_ 10 -r "no bash shell found" AM_TESTS_REEXEC
118 elif test "$non_bash_shell" = :; then
119   skip_row_ 10 -r "no non-bash shell found" AM_TESTS_REEXEC
120 else
121   command_ok_ "re-exec if AM_TESTS_REEXEC unset" \
122               $non_bash_shell need-bash.sh
123   do_reexec ''
124   do_reexec yes
125   do_reexec y
126   do_reexec true
127   do_reexec 1
128   dont_reexec no
129   dont_reexec n
130   dont_reexec false
131   dont_reexec 0
132 fi
133
134 #
135 # Check message about the re-execution.  Also check that arguments passed
136 # to a test script are preserved by a re-exec, even in "corner" cases.
137 #
138
139 cat > dummy.sh <<'END'
140 #!/bin/sh
141 . ./defs
142 :
143 END
144
145 cat > checkargs.sh <<'END'
146 . ./defs
147 test $# -eq 3 && test x"$1" = x'a' && test x"$2" = x && test x"$3" = x"-e"
148 END
149
150 chmod a+x dummy.sh checkargs.sh
151
152 mkdir sub
153 cp dummy.sh checkargs.sh defs sub
154 sed -e "s|^am_top_builddir=.*|am_top_builddir='$cwd'|" \
155     <  "$am_top_builddir"/defs-static > defs-static
156 sed -e "s|^am_top_builddir=.*|am_top_builddir='$cwd/sub'|" \
157     <  "$am_top_builddir"/defs-static > sub/defs-static
158
159 check_preserve_args ()
160 {
161   dir=$1; shift
162   get_ddata "$dir"
163   $sh "${dir}${dsep}checkargs.sh" a '' -e && r='ok' || r='not ok'
164   result_ "$r" "$sh re-exec preserving args [$dname]"
165 }
166
167 check_reexec_message ()
168 {
169   dir=$1; shift
170   get_ddata "$dir"
171   $sh "${dir}${dsep}dummy.sh" "$@" \
172     | grep "^dummy: exec $SHELL ${dir}${dsep}dummy\\.sh $*\$" \
173     && r='ok' || r='not ok'
174   result_ "$r" "$sh display re-exec message [$dname] [args: $*]"
175 }
176
177 ./dummy.sh a b | grep "^dummy: exec $SHELL \\./dummy\\.sh a b$" \
178   && r='ok' || r='not ok'
179 result_ "$r" "direct run display re-exec message [args: a b]"
180
181 ./checkargs.sh a '' -e && r='ok' || r='not ok'
182 result_ "$r" "direct re-exec preserving args"
183
184 for sh_type in non-bash bash; do
185   get_sh $sh_type
186   if test "$sh" = :; then
187     skip_row_ 5 -r "no $sh_type shell available" "re-exec message"
188     skip_row_ 5 -r "no $sh_type shell available" "re-exec preserving args"
189     continue
190   fi
191   check_preserve_args ''
192   check_reexec_message '' a b c
193   check_preserve_args .
194   check_reexec_message .  a b c
195   cd sub
196   check_preserve_args ..
197   check_reexec_message .. a b c
198   cd ..
199   check_preserve_args "$cwd"
200   check_reexec_message "$cwd" a -b c-
201   check_preserve_args sub
202   check_reexec_message sub 1 2 3 4
203 done
204
205 :