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