tests: "am_using_tap=yes" -> "am_test_protocol=tap"
[platform/upstream/automake.git] / t / ax / test-lib.sh
1 # -*- shell-script -*-
2 #
3 # Copyright (C) 1996-2012 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   # Avoid command substitution failure, for it might cause problems with
55   # "set -e" on some shells.
56   case `(set -o) 2>/dev/null || :` in *posix*) set -o posix;; esac
57 fi
58
59 # A single whitespace character.
60 sp=' '
61 # A tabulation character.
62 tab='   '
63 # A newline character.
64 nl='
65 '
66
67 # As autoconf-generated configure scripts do, ensure that IFS
68 # is defined initially, so that saving and restoring $IFS works.
69 IFS=$sp$tab$nl
70
71 # The name of the current test (without the '.sh' or '.tap' suffix).
72 me=${argv0##*/} # Strip all directory components.
73 case $me in     # Strip test suffix.
74    *.tap) me=${me%.tap};;
75     *.sh) me=${me%.sh} ;;
76  esac
77
78 # Source extra package-specific configuration.
79 . test-defs.sh
80 # And fail hard if something went wrong.
81 test $? -eq 0 || exit 99
82
83 # We use a trap below for cleanup.  This requires us to go through
84 # hoops to get the right exit status transported through the signal.
85 # Turn off errexit here so that we don't trip the bug with OSF1/Tru64
86 # sh inside this function (FIXME: is this still relevant now that we
87 # require a POSIX shell?).
88 _am_exit ()
89 {
90   set +e
91   # See comments in the exit trap for the reason we do this.
92   test 77 = $1 && am__test_skipped=yes
93   # Spurious escaping to ensure we do not call our 'exit' alias.
94   (\exit $1); \exit $1
95 }
96 # Avoid interferences from the environment
97 am__test_skipped=no
98 # This alias must actually be placed before any use if 'exit' -- even
99 # just inside a function definition.  Weird, but real.
100 alias exit=_am_exit
101
102 ## ------------------------------------ ##
103 ##  General testsuite shell functions.  ##
104 ## ------------------------------------ ##
105
106 # Print warnings (e.g., about skipped and failed tests) to this file
107 # number.  Override by putting, say:
108 #   AM_TESTS_ENVIRONMENT = stderr_fileno_=9; export stderr_fileno_;
109 #   AM_TESTS_FD_REDIRECT = 9>&2
110 # in your Makefile.am.
111 # This is useful when using automake's parallel tests mode, to print the
112 # reason for skip/failure to console, rather than to the *.log files.
113 : ${stderr_fileno_=2}
114
115 # Helper functions used by "plain" tests of the Automake testsuite
116 # (i.e., tests that don't use any test protocol).
117 # TAP tests will override these functions with their TAP-enhanced
118 # equivalents later  (see sourcing of 'tap-functions.sh' below).
119 # These are copied from Gnulib's 'tests/init.sh'.
120 warn_ () { echo "$@" 1>&$stderr_fileno_; }
121 fail_ () { warn_ "$me: failed test: $@"; exit 1; }
122 skip_ () { warn_ "$me: skipped test: $@"; exit 77; }
123 fatal_ () { warn_ "$me: hard error: $@"; exit 99; }
124 framework_failure_ () { warn_ "$me: set-up failure: $@"; exit 99; }
125 # For compatibility with TAP functions.
126 skip_all_ () { skip_ "$@"; }
127
128 if test $am_test_protocol = tap; then
129   . tap-functions.sh
130 fi
131
132 ## ---------------------------- ##
133 ##  Auxiliary shell functions.  ##
134 ## ---------------------------- ##
135
136 # Tell whether we should keep the test directories around, even in
137 # case of success.  By default, we don't.
138 am_keeping_testdirs ()
139 {
140   case $keep_testdirs in
141      ""|n|no|NO) return 1;;
142               *) return 0;;
143   esac
144 }
145
146 # seq_ - print a sequence of numbers
147 # ----------------------------------
148 # This function simulates GNU seq(1) portably.  Valid usages:
149 #  - seq LAST
150 #  - seq FIRST LAST
151 #  - seq FIRST INCREMENT LAST
152 seq_ ()
153 {
154   case $# in
155     0) fatal_ "seq_: missing argument";;
156     1) seq_first=1  seq_incr=1  seq_last=$1;;
157     2) seq_first=$1 seq_incr=1  seq_last=$2;;
158     3) seq_first=$1 seq_incr=$2 seq_last=$3;;
159     *) fatal_ "seq_: too many arguments";;
160   esac
161   i=$seq_first
162   while test $i -le $seq_last; do
163     echo $i
164     i=$(($i + $seq_incr))
165   done
166 }
167
168 # rm_rf_ [FILES OR DIRECTORIES ...]
169 # ---------------------------------
170 # Recursively remove the given files or directory, also handling the case
171 # of non-writable subdirectories.
172 rm_rf_ ()
173 {
174   test $# -gt 0 || return 0
175   # Ignore failures in find, we are only interested in failures of the
176   # final rm.
177   find "$@" -type d ! -perm -700 -exec chmod u+rwx {} \; || :
178   rm -rf "$@"
179 }
180
181 commented_sed_unindent_prog='
182   /^$/b                    # Nothing to do for empty lines.
183   x                        # Get x<indent> into pattern space.
184   /^$/{                    # No prior x<indent>, go prepare it.
185     g                      # Copy this 1st non-blank line into pattern space.
186     s/^\(['"$tab"' ]*\).*/x\1/   # Prepare x<indent> in pattern space.
187   }                        # Now: x<indent> in pattern and <line> in hold.
188   G                        # Build x<indent>\n<line> in pattern space, and
189   h                        # duplicate it into hold space.
190   s/\n.*$//                # Restore x<indent> in pattern space, and
191   x                        # exchange with the above duplicate in hold space.
192   s/^x\(.*\)\n\1//         # Remove leading <indent> from <line>.
193   s/^x.*\n//               # Restore <line> when there is no leading <indent>.
194 '
195
196 # unindent [input files...]
197 # -------------------------
198 # Remove the "proper" amount of leading whitespace from the given files,
199 # and output the result on stdout.  That amount is determined by looking
200 # at the leading whitespace of the first non-blank line in the input
201 # files.  If no input file is specified, standard input is implied.
202 unindent ()
203 {
204   if test x"$sed_unindent_prog" = x; then
205     sed_unindent_prog=$(printf '%s\n' "$commented_sed_unindent_prog" \
206                           | sed -e "s/  *# .*//")
207   fi
208   sed "$sed_unindent_prog" ${1+"$@"}
209 }
210 sed_unindent_prog="" # Avoid interferences from the environment.
211
212 ## ---------------------------------------------------------------- ##
213 ##  Create and set up of the temporary directory used by the test.  ##
214 ##  Set up of the exit trap for cleanup of said directory.          ##
215 ## ---------------------------------------------------------------- ##
216
217 # Set up the exit trap.
218 am_exit_trap ()
219 {
220   exit_status=$1
221   set +e
222   cd "$am_top_builddir"
223   if test $am_test_protocol = tap; then
224     if test "$planned_" = later && test $exit_status -eq 0; then
225       plan_ "now"
226     fi
227     test $exit_status -eq 0 && test $tap_pass_count_ -eq $tap_count_ \
228       || keep_testdirs=yes
229   else
230     # This is to ensure that a test script does give a SKIP outcome just
231     # because a command in it happens to exit with status 77.  This
232     # behaviour, while from time to time useful to developers, is not
233     # meant to be enabled by default, as it could cause spurious failures
234     # in the wild.  Thus it will be enabled only when the variable
235     # "am_explicit_skips" is set to a "true" value.
236     case $am_explicit_skips in
237       [yY]|[yY]es|1)
238         if test $exit_status -eq 77 && test $am__test_skipped != yes; then
239           echo "$me: implicit skip turned into failure"
240           exit_status=78
241         fi;;
242     esac
243     test $exit_status -eq 0 || keep_testdirs=yes
244   fi
245   am_keeping_testdirs || rm_rf_ $am_test_subdir
246   set +x
247   echo "$me: exit $exit_status"
248   # Spurious escaping to ensure we do not call our "exit" alias.
249   \exit $exit_status
250 }
251
252 am_set_exit_traps ()
253 {
254   trap 'am_exit_trap $?' 0
255   trap "fatal_ 'caught signal SIGHUP'" 1
256   trap "fatal_ 'caught signal SIGINT'" 2
257   trap "fatal_ 'caught signal SIGTERM'" 15
258   # Various shells seems to just ignore SIGQUIT under some circumstances,
259   # even if the signal is not blocked; however, if the signal it trapped,
260   # the trap gets correctly executed.  So we also trap SIGQUIT.
261   # Here is a list of some shells that have been verified to exhibit the
262   # problematic behavior with SIGQUIT:
263   #  - zsh 4.3.12 on Debian GNU/Linux
264   #  - /bin/ksh and /usr/xpg4/bin/sh on Solaris 10
265   #  - Bash 3.2.51 on Solaris 10 and bash 4.1.5 on Debian GNU/Linux
266   #  - AT&T ksh on Debian Gnu/Linux (deb package ksh, version 93u-1)
267   # OTOH, at least these shells that do *not* exhibit that behaviour:
268   #  - modern version of the Almquist Shell (at least 0.5.5.1), on
269   #    both Solaris and GNU/Linux
270   #  - public domain Korn Shell, version 5.2.14, on Debian GNU/Linux
271   trap "fatal_ 'caught signal SIGQUIT'" 3
272   # Ignore further SIGPIPE in the trap code.  This is required to avoid
273   # a very weird issue with some shells, at least when the execution of
274   # the automake testsuite is driven by the 'prove' utility: if prove
275   # (or the make process that has spawned it) gets interrupted with
276   # Ctrl-C, the shell might go in a loop, continually getting a SIGPIPE,
277   # sometimes finally dumping core, other times hanging indefinitely.
278   # See also Test::Harness bug [rt.cpan.org #70855], archived at
279   # <https://rt.cpan.org/Ticket/Display.html?id=70855>
280   trap "trap '' 13; fatal_ 'caught signal SIGPIPE'" 13
281 }
282
283 am_test_setup ()
284 {
285   process_requirements $required
286   am_set_exit_traps
287   # Create and populate the temporary directory, if required.
288   if test x"$am_create_testdir" = x"no"; then
289     am_test_subdir=
290   else
291     am_setup_testdir
292   fi
293   am_extra_info
294   set -x
295   pwd
296 }