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