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