gbs.conf: correct upstream branch name
[platform/upstream/automake.git] / t / ax / test-lib.sh
1 # -*- shell-script -*-
2 #
3 # Copyright (C) 1996-2013 Free Software Foundation, Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2, or (at your option)
8 # any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 ########################################################
19 ###  IMPORTANT NOTE: keep this file 'set -e' clean.  ###
20 ########################################################
21
22 # Do not source several times.
23 test ${test_lib_sourced-no} = yes && return 0
24 test_lib_sourced=yes
25
26 # CDPATH is evil if used in non-interactive scripts (and even more
27 # evil if exported in the environment).
28 CDPATH=; unset CDPATH
29
30 # Be more Bourne compatible.
31 # (Snippet inspired to configure's initialization in Autoconf 2.64)
32 DUALCASE=1; export DUALCASE # for MKS sh
33 if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then
34   emulate sh
35   NULLCMD=:
36   setopt NO_GLOB_SUBST
37   # If Zsh is not started directly in POSIX-compatibility mode, it has some
38   # incompatibilities in the handling of $0 that conflict with our usage;
39   # i.e., $0 inside a file sourced with the '.' builtin is temporarily set
40   # to the name of the sourced file.  Work around that.
41   # Note that a bug in some versions of Zsh prevents us from resetting $0
42   # in a sourced script, so the use of $argv0.  For more info see:
43   #   <http://www.zsh.org/mla/workers/2009/msg01140.html>
44   # The apparently useless 'eval' here is needed by at least dash 0.5.2,
45   # to prevent it from bailing out with an error like:
46   #   "Syntax error: Bad substitution".
47   eval 'argv0=${functrace[-1]%:*}' && test -f "$argv0" || {
48     echo "Cannot determine the path of running test script." >&2
49     echo "Your Zsh (version $ZSH_VERSION) is probably too old." >&2
50     exit 99
51   }
52 else
53   argv0=$0
54   # Ignore command substitution failure, for it might cause problems
55   # with "set -e" on some shells.
56   am_shell_opts=$(set -o) || :
57   case $am_shell_opts in *posix*) set -o posix;; esac
58   unset am_shell_opts
59 fi
60
61 # A single whitespace character.
62 sp=' '
63 # A tabulation character.
64 tab='   '
65 # A newline character.
66 nl='
67 '
68
69 # As autoconf-generated configure scripts do, ensure that IFS
70 # is defined initially, so that saving and restoring $IFS works.
71 IFS=$sp$tab$nl
72
73 # The name of the current test (without the '.sh' or '.tap' suffix).
74 me=${argv0##*/} # Strip all directory components.
75 case $me in     # Strip test suffix.
76    *.tap) me=${me%.tap};;
77     *.sh) me=${me%.sh} ;;
78  esac
79
80 # Source extra package-specific configuration.
81 . test-defs.sh
82 # And fail hard if something went wrong.
83 test $? -eq 0 || exit 99
84
85 # We use a trap below for cleanup.  This requires us to go through
86 # hoops to get the right exit status transported through the signal.
87 # Turn off errexit here so that we don't trip the bug with OSF1/Tru64
88 # sh inside this function (FIXME: is this still relevant now that we
89 # require a POSIX shell?).
90 _am_exit ()
91 {
92   set +e
93   # See comments in the exit trap for the reason we do this.
94   test 77 = $1 && am__test_skipped=yes
95   # Extra escaping to ensure we do not call our 'exit' alias.
96   (\exit $1); \exit $1
97 }
98 # Avoid interferences from the environment
99 am__test_skipped=no
100 # This alias must actually be placed before any use if 'exit' -- even
101 # just inside a function definition.  Weird, but real.
102 alias exit=_am_exit
103
104 # In some shells (e.g., Solaris 10 /bin/ksh, or NetBSD 5.1 /bin/sh),
105 # "unset VAR" returns a non-zero exit status in case the VAR variable
106 # is already unset.  This doesn't interact well with our usage of
107 # "set -e" in the testsuite.  This function and the alias below help
108 # to work around the issue.  But be sure to use them only if actually
109 # needed.  The repeated unset in the check below cater to the very
110 # unlikely case where the '_am_v' variable is set in the environment.
111 if unset _am_v && unset _am_v; then
112   : Nothing needs to be done.
113 else
114   _am_unset ()
115   {
116     for _am_v
117     do
118       # Extra escaping (here and below) to ensure we do not call our
119       # 'unset' alias.
120       eval ${_am_v}=dummy && \unset ${_am_v} || return 1
121     done
122     \unset _am_v
123   }
124   alias unset=_am_unset
125 fi
126
127 ## ------------------------------------ ##
128 ##  General testsuite shell functions.  ##
129 ## ------------------------------------ ##
130
131 # Print warnings (e.g., about skipped and failed tests) to this file
132 # number.  Override by putting, say:
133 #   AM_TESTS_ENVIRONMENT = stderr_fileno_=9; export stderr_fileno_;
134 #   AM_TESTS_FD_REDIRECT = 9>&2
135 # in your Makefile.am.
136 # This is useful when using automake's parallel tests mode, to print the
137 # reason for skip/failure to console, rather than to the *.log files.
138 : ${stderr_fileno_=2}
139
140 # Helper functions used by "plain" tests of the Automake testsuite
141 # (i.e., tests that don't use any test protocol).
142 # TAP tests will override these functions with their TAP-enhanced
143 # equivalents later  (see sourcing of 'tap-functions.sh' below).
144 # These are copied from Gnulib's 'tests/init.sh'.
145 warn_ () { echo "$@" 1>&$stderr_fileno_; }
146 fail_ () { warn_ "$me: failed test: $@"; exit 1; }
147 skip_ () { warn_ "$me: skipped test: $@"; exit 77; }
148 fatal_ () { warn_ "$me: hard error: $@"; exit 99; }
149 framework_failure_ () { warn_ "$me: set-up failure: $@"; exit 99; }
150 # For compatibility with TAP functions.
151 skip_all_ () { skip_ "$@"; }
152
153 if test $am_test_protocol = tap; then
154   . tap-functions.sh
155 fi
156
157 ## ---------------------------- ##
158 ##  Auxiliary shell functions.  ##
159 ## ---------------------------- ##
160
161 # Tell whether we should keep the test directories around, even in
162 # case of success.  By default, we don't.
163 am_keeping_testdirs ()
164 {
165   case $keep_testdirs in
166      ""|n|no|NO) return 1;;
167               *) return 0;;
168   esac
169 }
170
171 # seq_ - print a sequence of numbers
172 # ----------------------------------
173 # This function simulates GNU seq(1) portably.  Valid usages:
174 #  - seq LAST
175 #  - seq FIRST LAST
176 #  - seq FIRST INCREMENT LAST
177 seq_ ()
178 {
179   case $# in
180     0) fatal_ "seq_: missing argument";;
181     1) seq_first=1  seq_incr=1  seq_last=$1;;
182     2) seq_first=$1 seq_incr=1  seq_last=$2;;
183     3) seq_first=$1 seq_incr=$2 seq_last=$3;;
184     *) fatal_ "seq_: too many arguments";;
185   esac
186   i=$seq_first
187   while test $i -le $seq_last; do
188     echo $i
189     i=$(($i + $seq_incr))
190   done
191 }
192
193 # rm_rf_ [FILES OR DIRECTORIES ...]
194 # ---------------------------------
195 # Recursively remove the given files or directory, also handling the case
196 # of non-writable subdirectories.
197 rm_rf_ ()
198 {
199   test $# -gt 0 || return 0
200   $PERL "$am_testaux_srcdir"/deltree.pl "$@"
201 }
202
203 commented_sed_unindent_prog='
204   /^$/b                    # Nothing to do for empty lines.
205   x                        # Get x<indent> into pattern space.
206   /^$/{                    # No prior x<indent>, go prepare it.
207     g                      # Copy this 1st non-blank line into pattern space.
208     s/^\(['"$tab"' ]*\).*/x\1/   # Prepare x<indent> in pattern space.
209   }                        # Now: x<indent> in pattern and <line> in hold.
210   G                        # Build x<indent>\n<line> in pattern space, and
211   h                        # duplicate it into hold space.
212   s/\n.*$//                # Restore x<indent> in pattern space, and
213   x                        # exchange with the above duplicate in hold space.
214   s/^x\(.*\)\n\1//         # Remove leading <indent> from <line>.
215   s/^x.*\n//               # Restore <line> when there is no leading <indent>.
216 '
217
218 # unindent [input files...]
219 # -------------------------
220 # Remove the "proper" amount of leading whitespace from the given files,
221 # and output the result on stdout.  That amount is determined by looking
222 # at the leading whitespace of the first non-blank line in the input
223 # files.  If no input file is specified, standard input is implied.
224 unindent ()
225 {
226   if test x"$sed_unindent_prog" = x; then
227     sed_unindent_prog=$(printf '%s\n' "$commented_sed_unindent_prog" \
228                           | sed -e "s/  *# .*//")
229   fi
230   sed "$sed_unindent_prog" ${1+"$@"}
231 }
232 sed_unindent_prog="" # Avoid interferences from the environment.
233
234 ## ---------------------------------------------------------------- ##
235 ##  Create and set up of the temporary directory used by the test.  ##
236 ##  Set up of the exit trap for cleanup of said directory.          ##
237 ## ---------------------------------------------------------------- ##
238
239 # Set up the exit trap.
240 am_exit_trap ()
241 {
242   exit_status=$1
243   set +e
244   cd "$am_top_builddir"
245   if test $am_test_protocol = tap; then
246     if test "$planned_" = later && test $exit_status -eq 0; then
247       plan_ "now"
248     fi
249     test $exit_status -eq 0 && test $tap_pass_count_ -eq $tap_count_ \
250       || keep_testdirs=yes
251   else
252     # This is to ensure that a test script does give a SKIP outcome just
253     # because a command in it happens to exit with status 77.  This
254     # behaviour, while from time to time useful to developers, is not
255     # meant to be enabled by default, as it could cause spurious failures
256     # in the wild.  Thus it will be enabled only when the variable
257     # "am_explicit_skips" is set to a "true" value.
258     case $am_explicit_skips in
259       [yY]|[yY]es|1)
260         if test $exit_status -eq 77 && test $am__test_skipped != yes; then
261           echo "$me: implicit skip turned into failure"
262           exit_status=78
263         fi;;
264     esac
265     test $exit_status -eq 0 || keep_testdirs=yes
266   fi
267   am_keeping_testdirs || rm_rf_ $am_test_subdir
268   set +x
269   echo "$me: exit $exit_status"
270   # Spurious escaping to ensure we do not call our "exit" alias.
271   \exit $exit_status
272 }
273
274 am_set_exit_traps ()
275 {
276   trap 'am_exit_trap $?' 0
277   trap "fatal_ 'caught signal SIGHUP'" 1
278   trap "fatal_ 'caught signal SIGINT'" 2
279   trap "fatal_ 'caught signal SIGTERM'" 15
280   # Various shells seems to just ignore SIGQUIT under some circumstances,
281   # even if the signal is not blocked; however, if the signal is trapped,
282   # the trap gets correctly executed.  So we also trap SIGQUIT.
283   # Here is a list of some shells that have been verified to exhibit the
284   # problematic behavior with SIGQUIT:
285   #  - zsh 4.3.12 on Debian GNU/Linux
286   #  - /bin/ksh and /usr/xpg4/bin/sh on Solaris 10
287   #  - Bash 3.2.51 on Solaris 10 and bash 4.1.5 on Debian GNU/Linux
288   #  - AT&T ksh on Debian Gnu/Linux (deb package ksh, version 93u-1)
289   # OTOH, at least these shells that do *not* exhibit that behaviour:
290   #  - modern version of the Almquist Shell (at least 0.5.5.1), on
291   #    both Solaris and GNU/Linux
292   #  - public domain Korn Shell, version 5.2.14, on Debian GNU/Linux
293   trap "fatal_ 'caught signal SIGQUIT'" 3
294   # Ignore further SIGPIPE in the trap code.  This is required to avoid
295   # a very weird issue with some shells, at least when the execution of
296   # the automake testsuite is driven by the 'prove' utility: if prove
297   # (or the make process that has spawned it) gets interrupted with
298   # Ctrl-C, the shell might go in a loop, continually getting a SIGPIPE,
299   # sometimes finally dumping core, other times hanging indefinitely.
300   # See also Test::Harness bug [rt.cpan.org #70855], archived at
301   # <https://rt.cpan.org/Ticket/Display.html?id=70855>
302   trap "trap '' 13; fatal_ 'caught signal SIGPIPE'" 13
303 }
304
305 am_test_setup ()
306 {
307   process_requirements $required
308   am_set_exit_traps
309   # Create and populate the temporary directory, if required.
310   if test x"$am_create_testdir" = x"no"; then
311     am_test_subdir=
312   else
313     am_setup_testdir
314   fi
315   am_extra_info
316   set -x
317   pwd
318 }