source: diffutils/vendor/current/gnulib-tests/init.sh@ 530

Last change on this file since 530 was 530, checked in by Yuri Dario, 12 years ago

diffutils: initial vendor import of diffutils 3.2.0.

File size: 15.4 KB
Line 
1# source this file; set up for tests
2
3# Copyright (C) 2009-2011 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 <http://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, with single-stepping:
49# 1. Go into a sub-shell:
50# $ bash
51# 2. Set relevant environment variables from TESTS_ENVIRONMENT in the
52# Makefile:
53# $ export srcdir=../../tests # this is an example
54# 3. Execute the commands from the test, copy&pasting them one by one:
55# $ . "$srcdir/init.sh"; path_prepend_ .
56# ...
57# 4. Finally
58# $ exit
59
60ME_=`expr "./$0" : '.*/\(.*\)$'`
61
62# We use a trap below for cleanup. This requires us to go through
63# hoops to get the right exit status transported through the handler.
64# So use `Exit STATUS' instead of `exit STATUS' inside of the tests.
65# Turn off errexit here so that we don't trip the bug with OSF1/Tru64
66# sh inside this function.
67Exit () { set +e; (exit $1); exit $1; }
68
69# Print warnings (e.g., about skipped and failed tests) to this file number.
70# Override by defining to say, 9, in init.cfg, and putting say,
71# export ...ENVVAR_SETTINGS...; $(SHELL) 9>&2
72# in the definition of TESTS_ENVIRONMENT in your tests/Makefile.am file.
73# This is useful when using automake's parallel tests mode, to print
74# the reason for skip/failure to console, rather than to the .log files.
75: ${stderr_fileno_=2}
76
77warn_ () { echo "$@" 1>&$stderr_fileno_; }
78fail_ () { warn_ "$ME_: failed test: $@"; Exit 1; }
79skip_ () { warn_ "$ME_: skipped test: $@"; Exit 77; }
80fatal_ () { warn_ "$ME_: hard error: $@"; Exit 99; }
81framework_failure_ () { warn_ "$ME_: set-up failure: $@"; Exit 99; }
82
83# Sanitize this shell to POSIX mode, if possible.
84DUALCASE=1; export DUALCASE
85if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then
86 emulate sh
87 NULLCMD=:
88 alias -g '${1+"$@"}'='"$@"'
89 setopt NO_GLOB_SUBST
90else
91 case `(set -o) 2>/dev/null` in
92 *posix*) set -o posix ;;
93 esac
94fi
95
96# We require $(...) support unconditionally.
97# We require a few additional shell features only when $EXEEXT is nonempty,
98# in order to support automatic $EXEEXT emulation:
99# - hyphen-containing alias names
100# - we prefer to use ${var#...} substitution, rather than having
101# to work around lack of support for that feature.
102# The following code attempts to find a shell with support for these features.
103# If the current shell passes the test, we're done. Otherwise, test other
104# shells until we find one that passes. If one is found, re-exec it.
105# If no acceptable shell is found, skip the current test.
106#
107# The "...set -x; P=1 true 2>err..." test is to disqualify any shell that
108# emits "P=1" into err, as /bin/sh from SunOS 5.11 and OpenBSD 4.7 do.
109#
110# Use "9" to indicate success (rather than 0), in case some shell acts
111# like Solaris 10's /bin/sh but exits successfully instead of with status 2.
112
113# Eval this code in a subshell to determine a shell's suitability.
114# 10 - passes all tests; ok to use
115# 9 - ok, but enabling "set -x" corrupts app stderr; prefer higher score
116# ? - not ok
117gl_shell_test_script_='
118test $(echo y) = y || exit 1
119score_=10
120if test "$VERBOSE" = yes; then
121 test -n "$( (exec 3>&1; set -x; P=1 true 2>&3) 2> /dev/null)" && score_=9
122fi
123test -z "$EXEEXT" && exit $score_
124shopt -s expand_aliases
125alias a-b="echo zoo"
126v=abx
127 test ${v%x} = ab \
128 && test ${v#a} = bx \
129 && test $(a-b) = zoo \
130 && exit $score_
131'
132
133if test "x$1" = "x--no-reexec"; then
134 shift
135else
136 # Assume a working shell. Export to subshells (setup_ needs this).
137 gl_set_x_corrupts_stderr_=false
138 export gl_set_x_corrupts_stderr_
139
140 # Record the first marginally acceptable shell.
141 marginal_=
142
143 # Search for a shell that meets our requirements.
144 for re_shell_ in __current__ "${CONFIG_SHELL:-no_shell}" \
145 /bin/sh bash dash zsh pdksh fail
146 do
147 test "$re_shell_" = no_shell && continue
148
149 # If we've made it all the way to the sentinel, "fail" without
150 # finding even a marginal shell, skip this test.
151 if test "$re_shell_" = fail; then
152 test -z "$marginal_" && skip_ failed to find an adequate shell
153 re_shell_=$marginal_
154 break
155 fi
156
157 # When testing the current shell, simply "eval" the test code.
158 # Otherwise, run it via $re_shell_ -c ...
159 if test "$re_shell_" = __current__; then
160 # 'eval'ing this code makes Solaris 10's /bin/sh exit with
161 # $? set to 2. It does not evaluate any of the code after the
162 # "unexpected" first `('. Thus, we must run it in a subshell.
163 ( eval "$gl_shell_test_script_" ) > /dev/null 2>&1
164 else
165 "$re_shell_" -c "$gl_shell_test_script_" 2>/dev/null
166 fi
167
168 st_=$?
169
170 # $re_shell_ works just fine. Use it.
171 if test $st_ = 10; then
172 gl_set_x_corrupts_stderr_=false
173 break
174 fi
175
176 # If this is our first marginally acceptable shell, remember it.
177 if test "$st_:$marginal_" = 9: ; then
178 marginal_="$re_shell_"
179 gl_set_x_corrupts_stderr_=true
180 fi
181 done
182
183 if test "$re_shell_" != __current__; then
184 # Found a usable shell. Preserve -v and -x.
185 case $- in
186 *v*x* | *x*v*) opts_=-vx ;;
187 *v*) opts_=-v ;;
188 *x*) opts_=-x ;;
189 *) opts_= ;;
190 esac
191 exec "$re_shell_" $opts_ "$0" --no-reexec "$@"
192 echo "$ME_: exec failed" 1>&2
193 exit 127
194 fi
195fi
196
197test -n "$EXEEXT" && shopt -s expand_aliases
198
199# Enable glibc's malloc-perturbing option.
200# This is useful for exposing code that depends on the fact that
201# malloc-related functions often return memory that is mostly zeroed.
202# If you have the time and cycles, use valgrind to do an even better job.
203: ${MALLOC_PERTURB_=87}
204export MALLOC_PERTURB_
205
206# This is a stub function that is run upon trap (upon regular exit and
207# interrupt). Override it with a per-test function, e.g., to unmount
208# a partition, or to undo any other global state changes.
209cleanup_ () { :; }
210
211if ( diff --version < /dev/null 2>&1 | grep GNU ) > /dev/null 2>&1; then
212 compare () { diff -u "$@"; }
213elif ( cmp --version < /dev/null 2>&1 | grep GNU ) > /dev/null 2>&1; then
214 compare () { cmp -s "$@"; }
215else
216 compare () { cmp "$@"; }
217fi
218
219# An arbitrary prefix to help distinguish test directories.
220testdir_prefix_ () { printf gt; }
221
222# Run the user-overridable cleanup_ function, remove the temporary
223# directory and exit with the incoming value of $?.
224remove_tmp_ ()
225{
226 __st=$?
227 cleanup_
228 # cd out of the directory we're about to remove
229 cd "$initial_cwd_" || cd / || cd /tmp
230 chmod -R u+rwx "$test_dir_"
231 # If removal fails and exit status was to be 0, then change it to 1.
232 rm -rf "$test_dir_" || { test $__st = 0 && __st=1; }
233 exit $__st
234}
235
236# Given a directory name, DIR, if every entry in it that matches *.exe
237# contains only the specified bytes (see the case stmt below), then print
238# a space-separated list of those names and return 0. Otherwise, don't
239# print anything and return 1. Naming constraints apply also to DIR.
240find_exe_basenames_ ()
241{
242 feb_dir_=$1
243 feb_fail_=0
244 feb_result_=
245 feb_sp_=
246 for feb_file_ in $feb_dir_/*.exe; do
247 # If there was no *.exe file, or there existed a file named "*.exe" that
248 # was deleted between the above glob expansion and the existence test
249 # below, just skip it.
250 test "x$feb_file_" = "x$feb_dir_/*.exe" && test ! -f "$feb_file_" \
251 && continue
252 # Exempt [.exe, since we can't create a function by that name, yet
253 # we can't invoke [ by PATH search anyways due to shell builtins.
254 test "x$feb_file_" = "x$feb_dir_/[.exe" && continue
255 case $feb_file_ in
256 *[!-a-zA-Z/0-9_.+]*) feb_fail_=1; break;;
257 *) # Remove leading file name components as well as the .exe suffix.
258 feb_file_=${feb_file_##*/}
259 feb_file_=${feb_file_%.exe}
260 feb_result_="$feb_result_$feb_sp_$feb_file_";;
261 esac
262 feb_sp_=' '
263 done
264 test $feb_fail_ = 0 && printf %s "$feb_result_"
265 return $feb_fail_
266}
267
268# Consider the files in directory, $1.
269# For each file name of the form PROG.exe, create an alias named
270# PROG that simply invokes PROG.exe, then return 0. If any selected
271# file name or the directory name, $1, contains an unexpected character,
272# define no alias and return 1.
273create_exe_shims_ ()
274{
275 case $EXEEXT in
276 '') return 0 ;;
277 .exe) ;;
278 *) echo "$0: unexpected \$EXEEXT value: $EXEEXT" 1>&2; return 1 ;;
279 esac
280
281 base_names_=`find_exe_basenames_ $1` \
282 || { echo "$0 (exe_shim): skipping directory: $1" 1>&2; return 0; }
283
284 if test -n "$base_names_"; then
285 for base_ in $base_names_; do
286 alias "$base_"="$base_$EXEEXT"
287 done
288 fi
289
290 return 0
291}
292
293# Use this function to prepend to PATH an absolute name for each
294# specified, possibly-$initial_cwd_-relative, directory.
295path_prepend_ ()
296{
297 while test $# != 0; do
298 path_dir_=$1
299 case $path_dir_ in
300 '') fail_ "invalid path dir: '$1'";;
301 /*) abs_path_dir_=$path_dir_;;
302 *) abs_path_dir_=`cd "$initial_cwd_/$path_dir_" && echo "$PWD"` \
303 || fail_ "invalid path dir: $path_dir_";;
304 esac
305 case $abs_path_dir_ in
306 *:*) fail_ "invalid path dir: '$abs_path_dir_'";;
307 esac
308 PATH="$abs_path_dir_:$PATH"
309
310 # Create an alias, FOO, for each FOO.exe in this directory.
311 create_exe_shims_ "$abs_path_dir_" \
312 || fail_ "something failed (above): $abs_path_dir_"
313 shift
314 done
315 export PATH
316}
317
318setup_ ()
319{
320 if test "$VERBOSE" = yes; then
321 # Test whether set -x may cause the selected shell to corrupt an
322 # application's stderr. Many do, including zsh-4.3.10 and the /bin/sh
323 # from SunOS 5.11, OpenBSD 4.7 and Irix 5.x and 6.5.
324 # If enabling verbose output this way would cause trouble, simply
325 # issue a warning and refrain.
326 if $gl_set_x_corrupts_stderr_; then
327 warn_ "using SHELL=$SHELL with 'set -x' corrupts stderr"
328 else
329 set -x
330 fi
331 fi
332
333 initial_cwd_=$PWD
334 fail=0
335
336 pfx_=`testdir_prefix_`
337 test_dir_=`mktempd_ "$initial_cwd_" "$pfx_-$ME_.XXXX"` \
338 || fail_ "failed to create temporary directory in $initial_cwd_"
339 cd "$test_dir_"
340
341 # As autoconf-generated configure scripts do, ensure that IFS
342 # is defined initially, so that saving and restoring $IFS works.
343 gl_init_sh_nl_='
344'
345 IFS=" "" $gl_init_sh_nl_"
346
347 # This trap statement, along with a trap on 0 below, ensure that the
348 # temporary directory, $test_dir_, is removed upon exit as well as
349 # upon receipt of any of the listed signals.
350 for sig_ in 1 2 3 13 15; do
351 eval "trap 'Exit $(expr $sig_ + 128)' $sig_"
352 done
353}
354
355# Create a temporary directory, much like mktemp -d does.
356# Written by Jim Meyering.
357#
358# Usage: mktempd_ /tmp phoey.XXXXXXXXXX
359#
360# First, try to use the mktemp program.
361# Failing that, we'll roll our own mktemp-like function:
362# - try to get random bytes from /dev/urandom
363# - failing that, generate output from a combination of quickly-varying
364# sources and gzip. Ignore non-varying gzip header, and extract
365# "random" bits from there.
366# - given those bits, map to file-name bytes using tr, and try to create
367# the desired directory.
368# - make only $MAX_TRIES_ attempts
369
370# Helper function. Print $N pseudo-random bytes from a-zA-Z0-9.
371rand_bytes_ ()
372{
373 n_=$1
374
375 # Maybe try openssl rand -base64 $n_prime_|tr '+/=\012' abcd first?
376 # But if they have openssl, they probably have mktemp, too.
377
378 chars_=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
379 dev_rand_=/dev/urandom
380 if test -r "$dev_rand_"; then
381 # Note: 256-length($chars_) == 194; 3 copies of $chars_ is 186 + 8 = 194.
382 dd ibs=$n_ count=1 if=$dev_rand_ 2>/dev/null \
383 | LC_ALL=C tr -c $chars_ 01234567$chars_$chars_$chars_
384 return
385 fi
386
387 n_plus_50_=`expr $n_ + 50`
388 cmds_='date; date +%N; free; who -a; w; ps auxww; ps ef; netstat -n'
389 data_=` (eval "$cmds_") 2>&1 | gzip `
390
391 # Ensure that $data_ has length at least 50+$n_
392 while :; do
393 len_=`echo "$data_"|wc -c`
394 test $n_plus_50_ -le $len_ && break;
395 data_=` (echo "$data_"; eval "$cmds_") 2>&1 | gzip `
396 done
397
398 echo "$data_" \
399 | dd bs=1 skip=50 count=$n_ 2>/dev/null \
400 | LC_ALL=C tr -c $chars_ 01234567$chars_$chars_$chars_
401}
402
403mktempd_ ()
404{
405 case $# in
406 2);;
407 *) fail_ "Usage: mktempd_ DIR TEMPLATE";;
408 esac
409
410 destdir_=$1
411 template_=$2
412
413 MAX_TRIES_=4
414
415 # Disallow any trailing slash on specified destdir:
416 # it would subvert the post-mktemp "case"-based destdir test.
417 case $destdir_ in
418 /) ;;
419 */) fail_ "invalid destination dir: remove trailing slash(es)";;
420 esac
421
422 case $template_ in
423 *XXXX) ;;
424 *) fail_ \
425 "invalid template: $template_ (must have a suffix of at least 4 X's)";;
426 esac
427
428 # First, try to use mktemp.
429 d=`unset TMPDIR; mktemp -d -t -p "$destdir_" "$template_" 2>/dev/null` \
430 || fail=1
431
432 # The resulting name must be in the specified directory.
433 case $d in "$destdir_"*);; *) fail=1;; esac
434
435 # It must have created the directory.
436 test -d "$d" || fail=1
437
438 # It must have 0700 permissions. Handle sticky "S" bits.
439 perms=`ls -dgo "$d" 2>/dev/null|tr S -` || fail=1
440 case $perms in drwx------*) ;; *) fail=1;; esac
441
442 test $fail = 0 && {
443 echo "$d"
444 return
445 }
446
447 # If we reach this point, we'll have to create a directory manually.
448
449 # Get a copy of the template without its suffix of X's.
450 base_template_=`echo "$template_"|sed 's/XX*$//'`
451
452 # Calculate how many X's we've just removed.
453 template_length_=`echo "$template_" | wc -c`
454 nx_=`echo "$base_template_" | wc -c`
455 nx_=`expr $template_length_ - $nx_`
456
457 err_=
458 i_=1
459 while :; do
460 X_=`rand_bytes_ $nx_`
461 candidate_dir_="$destdir_/$base_template_$X_"
462 err_=`mkdir -m 0700 "$candidate_dir_" 2>&1` \
463 && { echo "$candidate_dir_"; return; }
464 test $MAX_TRIES_ -le $i_ && break;
465 i_=`expr $i_ + 1`
466 done
467 fail_ "$err_"
468}
469
470# If you want to override the testdir_prefix_ function,
471# or to add more utility functions, use this file.
472test -f "$srcdir/init.cfg" \
473 && . "$srcdir/init.cfg"
474
475setup_ "$@"
476# This trap is here, rather than in the setup_ function, because some
477# shells run the exit trap at shell function exit, rather than script exit.
478trap remove_tmp_ 0
Note: See TracBrowser for help on using the repository browser.