1# source this file; set up for tests
2
3# Copyright (C) 2009-2019 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 3 of the License, or
8# (at your option) 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 <https://www.gnu.org/licenses/>.
17
18# Using this file in a test
19# =========================
20#
21# The typical skeleton of a test looks like this:
22#
23#   #!/bin/sh
24#   . "${srcdir=.}/init.sh"; path_prepend_ .
25#   Execute some commands.
26#   Note that these commands are executed in a subdirectory, therefore you
27#   need to prepend "../" to relative filenames in the build directory.
28#   Note that the "path_prepend_ ." is useful only if the body of your
29#   test invokes programs residing in the initial directory.
30#   For example, if the programs you want to test are in src/, and this test
31#   script is named tests/test-1, then you would use "path_prepend_ ../src",
32#   or perhaps export PATH='$(abs_top_builddir)/src$(PATH_SEPARATOR)'"$$PATH"
33#   to all tests via automake's TESTS_ENVIRONMENT.
34#   Set the exit code 0 for success, 77 for skipped, or 1 or other for failure.
35#   Use the skip_ and fail_ functions to print a diagnostic and then exit
36#   with the corresponding exit code.
37#   Exit $?
38
39# Executing a test that uses this file
40# ====================================
41#
42# Running a single test:
43#   $ make check TESTS=test-foo.sh
44#
45# Running a single test, with verbose output:
46#   $ make check TESTS=test-foo.sh VERBOSE=yes
47#
48# Running a single test, keeping the temporary directory:
49#   $ make check TESTS=test-foo.sh KEEP=yes
50#
51# Running a single test, with single-stepping:
52#   1. Go into a sub-shell:
53#   $ bash
54#   2. Set relevant environment variables from TESTS_ENVIRONMENT in the
55#      Makefile:
56#   $ export srcdir=../../tests # this is an example
57#   3. Execute the commands from the test, copy&pasting them one by one:
58#   $ . "$srcdir/init.sh"; path_prepend_ .
59#   ...
60#   4. Finally
61#   $ exit
62
63# =============================================================================
64# Elementary diagnostics
65
66ME_=`expr "./$0" : '.*/\(.*\)$'`
67
68# Prepare PATH_SEPARATOR.
69# The user is always right.
70if test "${PATH_SEPARATOR+set}" != set; then
71  # Determine PATH_SEPARATOR by trying to find /bin/sh in a PATH which
72  # contains only /bin. Note that ksh looks also at the FPATH variable,
73  # so we have to set that as well for the test.
74  PATH_SEPARATOR=:
75  (PATH='/bin;/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 \
76    && { (PATH='/bin:/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 \
77           || PATH_SEPARATOR=';'
78       }
79fi
80
81# We use a trap below for cleanup.  This requires us to go through
82# hoops to get the right exit status transported through the handler.
83# So use 'Exit STATUS' instead of 'exit STATUS' inside of the tests.
84# Turn off errexit here so that we don't trip the bug with OSF1/Tru64
85# sh inside this function.
86Exit () { set +e; (exit $1); exit $1; }
87
88# Print warnings (e.g., about skipped and failed tests) to this file number.
89# Override by defining to say, 9, in init.cfg, and putting say,
90#   export ...ENVVAR_SETTINGS...; $(SHELL) 9>&2
91# in the definition of TESTS_ENVIRONMENT in your tests/Makefile.am file.
92# This is useful when using automake's parallel tests mode, to print
93# the reason for skip/failure to console, rather than to the .log files.
94: ${stderr_fileno_=2}
95
96# Note that correct expansion of "$*" depends on IFS starting with ' '.
97# Always write the full diagnostic to stderr.
98# When stderr_fileno_ is not 2, also emit the first line of the
99# diagnostic to that file descriptor.
100warn_ ()
101{
102  # If IFS does not start with ' ', set it and emit the warning in a subshell.
103  case $IFS in
104    ' '*) printf '%s\n' "$*" >&2
105          test $stderr_fileno_ = 2 \
106            || { printf '%s\n' "$*" | sed 1q >&$stderr_fileno_ ; } ;;
107    *) (IFS=' '; warn_ "$@");;
108  esac
109}
110fail_ () { warn_ "$ME_: failed test: $@"; Exit 1; }
111skip_ () { warn_ "$ME_: skipped test: $@"; Exit 77; }
112fatal_ () { warn_ "$ME_: hard error: $@"; Exit 99; }
113framework_failure_ () { warn_ "$ME_: set-up failure: $@"; Exit 99; }
114
115# =============================================================================
116# Ensure the shell supports modern syntax.
117
118# Sanitize this shell to POSIX mode, if possible.
119DUALCASE=1; export DUALCASE
120if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then
121  emulate sh
122  NULLCMD=:
123  alias -g '${1+"$@"}'='"$@"'
124  setopt NO_GLOB_SUBST
125else
126  case `(set -o) 2>/dev/null` in
127    *posix*) set -o posix ;;
128  esac
129fi
130
131# We require $(...) support unconditionally.
132# We require non-surprising "local" semantics (this eliminates dash).
133# This takes the admittedly draconian step of eliminating dash, because the
134# assignment tab=$(printf '\t') works fine, yet preceding it with "local "
135# transforms it into an assignment that sets the variable to the empty string.
136# That is too counter-intuitive, and can lead to subtle run-time malfunction.
137# The example below is less subtle in that with dash, it evokes the run-time
138# exception "dash: 1: local: 1: bad variable name".
139# We require a few additional shell features only when $EXEEXT is nonempty,
140# in order to support automatic $EXEEXT emulation:
141# - hyphen-containing alias names
142# - we prefer to use ${var#...} substitution, rather than having
143#   to work around lack of support for that feature.
144# The following code attempts to find a shell with support for these features.
145# If the current shell passes the test, we're done.  Otherwise, test other
146# shells until we find one that passes.  If one is found, re-exec it.
147# If no acceptable shell is found, skip the current test.
148#
149# The "...set -x; P=1 true 2>err..." test is to disqualify any shell that
150# emits "P=1" into err, as /bin/sh from SunOS 5.11 and OpenBSD 4.7 do.
151#
152# Use "9" to indicate success (rather than 0), in case some shell acts
153# like Solaris 10's /bin/sh but exits successfully instead of with status 2.
154
155# Eval this code in a subshell to determine a shell's suitability.
156# 10 - passes all tests; ok to use
157#  9 - ok, but enabling "set -x" corrupts app stderr; prefer higher score
158#  ? - not ok
159gl_shell_test_script_='
160test $(echo y) = y || exit 1
161f_local_() { local v=1; }; f_local_ || exit 1
162f_dash_local_fail_() { local t=$(printf " 1"); }; f_dash_local_fail_
163score_=10
164if test "$VERBOSE" = yes; then
165  test -n "$( (exec 3>&1; set -x; P=1 true 2>&3) 2> /dev/null)" && score_=9
166fi
167test -z "$EXEEXT" && exit $score_
168shopt -s expand_aliases
169alias a-b="echo zoo"
170v=abx
171     test ${v%x} = ab \
172  && test ${v#a} = bx \
173  && test $(a-b) = zoo \
174  && exit $score_
175'
176
177if test "x$1" = "x--no-reexec"; then
178  shift
179else
180  # Assume a working shell.  Export to subshells (setup_ needs this).
181  gl_set_x_corrupts_stderr_=false
182  export gl_set_x_corrupts_stderr_
183
184  # Record the first marginally acceptable shell.
185  marginal_=
186
187  # Search for a shell that meets our requirements.
188  for re_shell_ in __current__ "${CONFIG_SHELL:-no_shell}" \
189      /bin/sh bash dash zsh pdksh fail
190  do
191    test "$re_shell_" = no_shell && continue
192
193    # If we've made it all the way to the sentinel, "fail" without
194    # finding even a marginal shell, skip this test.
195    if test "$re_shell_" = fail; then
196      test -z "$marginal_" && skip_ failed to find an adequate shell
197      re_shell_=$marginal_
198      break
199    fi
200
201    # When testing the current shell, simply "eval" the test code.
202    # Otherwise, run it via $re_shell_ -c ...
203    if test "$re_shell_" = __current__; then
204      # 'eval'ing this code makes Solaris 10's /bin/sh exit with
205      # $? set to 2.  It does not evaluate any of the code after the
206      # "unexpected" first '('.  Thus, we must run it in a subshell.
207      ( eval "$gl_shell_test_script_" ) > /dev/null 2>&1
208    else
209      "$re_shell_" -c "$gl_shell_test_script_" 2>/dev/null
210    fi
211
212    st_=$?
213
214    # $re_shell_ works just fine.  Use it.
215    if test $st_ = 10; then
216      gl_set_x_corrupts_stderr_=false
217      break
218    fi
219
220    # If this is our first marginally acceptable shell, remember it.
221    if test "$st_:$marginal_" = 9: ; then
222      marginal_="$re_shell_"
223      gl_set_x_corrupts_stderr_=true
224    fi
225  done
226
227  if test "$re_shell_" != __current__; then
228    # Found a usable shell.  Preserve -v and -x.
229    case $- in
230      *v*x* | *x*v*) opts_=-vx ;;
231      *v*) opts_=-v ;;
232      *x*) opts_=-x ;;
233      *) opts_= ;;
234    esac
235    re_shell=$re_shell_
236    export re_shell
237    exec "$re_shell_" $opts_ "$0" --no-reexec "$@"
238    echo "$ME_: exec failed" 1>&2
239    exit 127
240  fi
241fi
242
243# =============================================================================
244# Ensure the shell behaves reasonably.
245
246# If this is bash, turn off all aliases.
247test -n "$BASH_VERSION" && unalias -a
248
249# Note that when supporting $EXEEXT (transparently mapping from PROG_NAME to
250# PROG_NAME.exe), we want to support hyphen-containing names like test-acos.
251# That is part of the shell-selection test above.  Why use aliases rather
252# than functions?  Because support for hyphen-containing aliases is more
253# widespread than that for hyphen-containing function names.
254test -n "$EXEEXT" && test -n "$BASH_VERSION" && shopt -s expand_aliases
255
256# =============================================================================
257# Creating a temporary directory (needed by the core test framework)
258
259# Create a temporary directory, much like mktemp -d does.
260# Written by Jim Meyering.
261#
262# Usage: mktempd_ /tmp phoey.XXXXXXXXXX
263#
264# First, try to use the mktemp program.
265# Failing that, we'll roll our own mktemp-like function:
266#  - try to get random bytes from /dev/urandom
267#  - failing that, generate output from a combination of quickly-varying
268#      sources and gzip.  Ignore non-varying gzip header, and extract
269#      "random" bits from there.
270#  - given those bits, map to file-name bytes using tr, and try to create
271#      the desired directory.
272#  - make only $MAX_TRIES_ attempts
273
274# Helper function.  Print $N pseudo-random bytes from a-zA-Z0-9.
275rand_bytes_ ()
276{
277  n_=$1
278
279  # Maybe try openssl rand -base64 $n_prime_|tr '+/=\012' abcd first?
280  # But if they have openssl, they probably have mktemp, too.
281
282  chars_=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
283  dev_rand_=/dev/urandom
284  if test -r "$dev_rand_"; then
285    # Note: 256-length($chars_) == 194; 3 copies of $chars_ is 186 + 8 = 194.
286    dd ibs=$n_ count=1 if=$dev_rand_ 2>/dev/null \
287      | LC_ALL=C tr -c $chars_ 01234567$chars_$chars_$chars_
288    return
289  fi
290
291  n_plus_50_=`expr $n_ + 50`
292  cmds_='date; date +%N; free; who -a; w; ps auxww; ps -ef'
293  data_=` (eval "$cmds_") 2>&1 | gzip `
294
295  # Ensure that $data_ has length at least 50+$n_
296  while :; do
297    len_=`echo "$data_"|wc -c`
298    test $n_plus_50_ -le $len_ && break;
299    data_=` (echo "$data_"; eval "$cmds_") 2>&1 | gzip `
300  done
301
302  echo "$data_" \
303    | dd bs=1 skip=50 count=$n_ 2>/dev/null \
304    | LC_ALL=C tr -c $chars_ 01234567$chars_$chars_$chars_
305}
306
307mktempd_ ()
308{
309  case $# in
310  2);;
311  *) fail_ "Usage: mktempd_ DIR TEMPLATE";;
312  esac
313
314  destdir_=$1
315  template_=$2
316
317  MAX_TRIES_=4
318
319  # Disallow any trailing slash on specified destdir:
320  # it would subvert the post-mktemp "case"-based destdir test.
321  case $destdir_ in
322  / | //) destdir_slash_=$destdir;;
323  */) fail_ "invalid destination dir: remove trailing slash(es)";;
324  *) destdir_slash_=$destdir_/;;
325  esac
326
327  case $template_ in
328  *XXXX) ;;
329  *) fail_ \
330       "invalid template: $template_ (must have a suffix of at least 4 X's)";;
331  esac
332
333  # First, try to use mktemp.
334  d=`unset TMPDIR; { mktemp -d -t -p "$destdir_" "$template_"; } 2>/dev/null` &&
335
336  # The resulting name must be in the specified directory.
337  case $d in "$destdir_slash_"*) :;; *) false;; esac &&
338
339  # It must have created the directory.
340  test -d "$d" &&
341
342  # It must have 0700 permissions.  Handle sticky "S" bits.
343  perms=`ls -dgo "$d" 2>/dev/null` &&
344  case $perms in drwx--[-S]---*) :;; *) false;; esac && {
345    echo "$d"
346    return
347  }
348
349  # If we reach this point, we'll have to create a directory manually.
350
351  # Get a copy of the template without its suffix of X's.
352  base_template_=`echo "$template_"|sed 's/XX*$//'`
353
354  # Calculate how many X's we've just removed.
355  template_length_=`echo "$template_" | wc -c`
356  nx_=`echo "$base_template_" | wc -c`
357  nx_=`expr $template_length_ - $nx_`
358
359  err_=
360  i_=1
361  while :; do
362    X_=`rand_bytes_ $nx_`
363    candidate_dir_="$destdir_slash_$base_template_$X_"
364    err_=`mkdir -m 0700 "$candidate_dir_" 2>&1` \
365      && { echo "$candidate_dir_"; return; }
366    test $MAX_TRIES_ -le $i_ && break;
367    i_=`expr $i_ + 1`
368  done
369  fail_ "$err_"
370}
371
372# =============================================================================
373# Core test framework
374
375# An arbitrary prefix to help distinguish test directories.
376testdir_prefix_ () { printf gt; }
377
378# Set up the environment for the test to run in.
379setup_ ()
380{
381  if test "$VERBOSE" = yes; then
382    # Test whether set -x may cause the selected shell to corrupt an
383    # application's stderr.  Many do, including zsh-4.3.10 and the /bin/sh
384    # from SunOS 5.11, OpenBSD 4.7 and Irix 5.x and 6.5.
385    # If enabling verbose output this way would cause trouble, simply
386    # issue a warning and refrain.
387    if $gl_set_x_corrupts_stderr_; then
388      warn_ "using SHELL=$SHELL with 'set -x' corrupts stderr"
389    else
390      set -x
391    fi
392  fi
393
394  initial_cwd_=$PWD
395
396  # Create and enter the temporary directory.
397  pfx_=`testdir_prefix_`
398  test_dir_=`mktempd_ "$initial_cwd_" "$pfx_-$ME_.XXXX"` \
399    || fail_ "failed to create temporary directory in $initial_cwd_"
400  cd "$test_dir_" || fail_ "failed to cd to temporary directory"
401  # Set variables srcdir, builddir, for the convenience of the test.
402  case $srcdir in
403    /* | ?:*) ;;
404    *) srcdir="../$srcdir" ;;
405  esac
406  builddir=".."
407  export srcdir builddir
408
409  # As autoconf-generated configure scripts do, ensure that IFS
410  # is defined initially, so that saving and restoring $IFS works.
411  gl_init_sh_nl_='
412'
413  IFS=" ""	$gl_init_sh_nl_"
414
415  # This trap statement, along with a trap on 0 below, ensure that the
416  # temporary directory, $test_dir_, is removed upon exit as well as
417  # upon receipt of any of the listed signals.
418  for sig_ in 1 2 3 13 15; do
419    eval "trap 'Exit $(expr $sig_ + 128)' $sig_"
420  done
421}
422
423# This is a stub function that is run upon trap (upon regular exit and
424# interrupt).  Override it with a per-test function, e.g., to unmount
425# a partition, or to undo any other global state changes.
426cleanup_ () { :; }
427
428# Run the user-overridable cleanup_ function, remove the temporary
429# directory and exit with the incoming value of $?.
430remove_tmp_ ()
431{
432  __st=$?
433  cleanup_
434  if test "$KEEP" = yes; then
435    echo "Not removing temporary directory $test_dir_"
436  else
437    # cd out of the directory we're about to remove
438    cd "$initial_cwd_" || cd / || cd /tmp
439    chmod -R u+rwx "$test_dir_"
440    # If removal fails and exit status was to be 0, then change it to 1.
441    rm -rf "$test_dir_" || { test $__st = 0 && __st=1; }
442  fi
443  exit $__st
444}
445
446# =============================================================================
447# Prepending directories to PATH
448
449# Given a directory name, DIR, if every entry in it that matches *.exe
450# contains only the specified bytes (see the case stmt below), then print
451# a space-separated list of those names and return 0.  Otherwise, don't
452# print anything and return 1.  Naming constraints apply also to DIR.
453find_exe_basenames_ ()
454{
455  feb_dir_=$1
456  feb_fail_=0
457  feb_result_=
458  feb_sp_=
459  for feb_file_ in $feb_dir_/*.exe; do
460    # If there was no *.exe file, or there existed a file named "*.exe" that
461    # was deleted between the above glob expansion and the existence test
462    # below, just skip it.
463    test "x$feb_file_" = "x$feb_dir_/*.exe" && test ! -f "$feb_file_" \
464      && continue
465    # Exempt [.exe, since we can't create a function by that name, yet
466    # we can't invoke [ by PATH search anyways due to shell builtins.
467    test "x$feb_file_" = "x$feb_dir_/[.exe" && continue
468    case $feb_file_ in
469      *[!-a-zA-Z/0-9_.+]*) feb_fail_=1; break;;
470      *) # Remove leading file name components as well as the .exe suffix.
471         feb_file_=${feb_file_##*/}
472         feb_file_=${feb_file_%.exe}
473         feb_result_="$feb_result_$feb_sp_$feb_file_";;
474    esac
475    feb_sp_=' '
476  done
477  test $feb_fail_ = 0 && printf %s "$feb_result_"
478  return $feb_fail_
479}
480
481# Consider the files in directory, $1.
482# For each file name of the form PROG.exe, create an alias named
483# PROG that simply invokes PROG.exe, then return 0.  If any selected
484# file name or the directory name, $1, contains an unexpected character,
485# define no alias and return 1.
486create_exe_shims_ ()
487{
488  case $EXEEXT in
489    '') return 0 ;;
490    .exe) ;;
491    *) echo "$0: unexpected \$EXEEXT value: $EXEEXT" 1>&2; return 1 ;;
492  esac
493
494  base_names_=`find_exe_basenames_ $1` \
495    || { echo "$0 (exe_shim): skipping directory: $1" 1>&2; return 0; }
496
497  if test -n "$base_names_"; then
498    for base_ in $base_names_; do
499      alias "$base_"="$base_$EXEEXT"
500    done
501  fi
502
503  return 0
504}
505
506# Use this function to prepend to PATH an absolute name for each
507# specified, possibly-$initial_cwd_-relative, directory.
508path_prepend_ ()
509{
510  while test $# != 0; do
511    path_dir_=$1
512    case $path_dir_ in
513      '') fail_ "invalid path dir: '$1'";;
514      /* | ?:*) abs_path_dir_=$path_dir_;;
515      *) abs_path_dir_=$initial_cwd_/$path_dir_;;
516    esac
517    case $abs_path_dir_ in
518      *$PATH_SEPARATOR*) fail_ "invalid path dir: '$abs_path_dir_'";;
519    esac
520    PATH="$abs_path_dir_$PATH_SEPARATOR$PATH"
521
522    # Create an alias, FOO, for each FOO.exe in this directory.
523    create_exe_shims_ "$abs_path_dir_" \
524      || fail_ "something failed (above): $abs_path_dir_"
525    shift
526  done
527  export PATH
528}
529
530# =============================================================================
531# Convenience environment variables for the tests
532
533# -----------------------------------------------------------------------------
534
535# Enable glibc's malloc-perturbing option.
536# This is useful for exposing code that depends on the fact that
537# malloc-related functions often return memory that is mostly zeroed.
538# If you have the time and cycles, use valgrind to do an even better job.
539: ${MALLOC_PERTURB_=87}
540export MALLOC_PERTURB_
541
542# -----------------------------------------------------------------------------
543
544# The interpreter for Bourne-shell scripts.
545# No special standards compatibility requirements.
546# Some environments, such as Android, don't have /bin/sh.
547if test -f /bin/sh$EXEEXT; then
548  BOURNE_SHELL=/bin/sh
549else
550  BOURNE_SHELL=sh
551fi
552
553# =============================================================================
554# Convenience functions for the tests
555
556# -----------------------------------------------------------------------------
557# Return value checking
558
559# This is used to simplify checking of the return value
560# which is useful when ensuring a command fails as desired.
561# I.e., just doing `command ... &&fail=1` will not catch
562# a segfault in command for example.  With this helper you
563# instead check an explicit exit code like
564#   returns_ 1 command ... || fail
565returns_ () {
566  # Disable tracing so it doesn't interfere with stderr of the wrapped command
567  { set +x; } 2>/dev/null
568
569  local exp_exit="$1"
570  shift
571  "$@"
572  test $? -eq $exp_exit && ret_=0 || ret_=1
573
574  if test "$VERBOSE" = yes && test "$gl_set_x_corrupts_stderr_" = false; then
575    set -x
576  fi
577  { return $ret_; } 2>/dev/null
578}
579
580# -----------------------------------------------------------------------------
581# Text file comparison
582
583# Emit a header similar to that from diff -u;  Print the simulated "diff"
584# command so that the order of arguments is clear.  Don't bother with @@ lines.
585emit_diff_u_header_ ()
586{
587  printf '%s\n' "diff -u $*" \
588    "--- $1	1970-01-01" \
589    "+++ $2	1970-01-01"
590}
591
592# Arrange not to let diff or cmp operate on /dev/null,
593# since on some systems (at least OSF/1 5.1), that doesn't work.
594# When there are not two arguments, or no argument is /dev/null, return 2.
595# When one argument is /dev/null and the other is not empty,
596# cat the nonempty file to stderr and return 1.
597# Otherwise, return 0.
598compare_dev_null_ ()
599{
600  test $# = 2 || return 2
601
602  if test "x$1" = x/dev/null; then
603    test -s "$2" || return 0
604    emit_diff_u_header_ "$@"; sed 's/^/+/' "$2"
605    return 1
606  fi
607
608  if test "x$2" = x/dev/null; then
609    test -s "$1" || return 0
610    emit_diff_u_header_ "$@"; sed 's/^/-/' "$1"
611    return 1
612  fi
613
614  return 2
615}
616
617for diff_opt_ in -u -U3 -c '' no; do
618  test "$diff_opt_" != no &&
619    diff_out_=`exec 2>/dev/null; diff $diff_opt_ "$0" "$0" < /dev/null` &&
620    break
621done
622if test "$diff_opt_" != no; then
623  if test -z "$diff_out_"; then
624    compare_ () { diff $diff_opt_ "$@"; }
625  else
626    compare_ ()
627    {
628      # If no differences were found, AIX and HP-UX 'diff' produce output
629      # like "No differences encountered".  Hide this output.
630      diff $diff_opt_ "$@" > diff.out
631      diff_status_=$?
632      test $diff_status_ -eq 0 || cat diff.out || diff_status_=2
633      rm -f diff.out || diff_status_=2
634      return $diff_status_
635    }
636  fi
637elif cmp -s /dev/null /dev/null 2>/dev/null; then
638  compare_ () { cmp -s "$@"; }
639else
640  compare_ () { cmp "$@"; }
641fi
642
643# Usage: compare EXPECTED ACTUAL
644#
645# Given compare_dev_null_'s preprocessing, defer to compare_ if 2 or more.
646# Otherwise, propagate $? to caller: any diffs have already been printed.
647compare ()
648{
649  # This looks like it can be factored to use a simple "case $?"
650  # after unchecked compare_dev_null_ invocation, but that would
651  # fail in a "set -e" environment.
652  if compare_dev_null_ "$@"; then
653    return 0
654  else
655    case $? in
656      1) return 1;;
657      *) compare_ "$@";;
658    esac
659  fi
660}
661
662# -----------------------------------------------------------------------------
663
664# If you want to override the testdir_prefix_ function,
665# or to add more utility functions, use this file.
666test -f "$srcdir/init.cfg" \
667  && . "$srcdir/init.cfg"
668
669# =============================================================================
670# Set up the environment for the test to run in.
671
672setup_ "$@"
673# This trap is here, rather than in the setup_ function, because some
674# shells run the exit trap at shell function exit, rather than script exit.
675trap remove_tmp_ 0
676