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