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