test-lib.sh 11 KB

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