~peter-pearse/ubuntu/natty/diffutils/prop001

« back to all changes in this revision

Viewing changes to tests/init.sh

  • Committer: Bazaar Package Importer
  • Author(s): Santiago Vila
  • Date: 2010-05-04 20:38:00 UTC
  • mfrom: (2.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20100504203800-f67xd9rsa9xl9qqj
Tags: 1:3.0-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# source this file; set up for tests
 
2
 
 
3
# Copyright (C) 2009, 2010 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=.}
 
25
#   . "$srcdir/init.sh"; path_prepend_ .
 
26
#   Execute some commands.
 
27
#   Note that these commands are executed in a subdirectory, therefore you
 
28
#   need to prepend "../" to relative filenames in the build directory.
 
29
#   Set the exit code 0 for success, 77 for skipped, or 1 or other for failure.
 
30
#   Use the skip_ and fail_ functions to print a diagnostic and then exit
 
31
#   with the corresponding exit code.
 
32
#   Exit $?
 
33
 
 
34
# Executing a test that uses this file
 
35
# ====================================
 
36
#
 
37
# Running a single test:
 
38
#   $ make check TESTS=test-foo.sh
 
39
#
 
40
# Running a single test, with verbose output:
 
41
#   $ make check TESTS=test-foo.sh VERBOSE=yes
 
42
#
 
43
# Running a single test, with single-stepping:
 
44
#   1. Go into a sub-shell:
 
45
#   $ bash
 
46
#   2. Set relevant environment variables from TESTS_ENVIRONMENT in the
 
47
#      Makefile:
 
48
#   $ export srcdir=../../tests # this is an example
 
49
#   3. Execute the commands from the test, copy&pasting them one by one:
 
50
#   $ . "$srcdir/init.sh"; path_prepend_ .
 
51
#   ...
 
52
#   4. Finally
 
53
#   $ exit
 
54
 
 
55
# We require $(...) support unconditionally.
 
56
# We require a few additional shell features only when $EXEEXT is nonempty,
 
57
# in order to support automatic $EXEEXT emulation:
 
58
# - hyphen-containing alias names
 
59
# - we prefer to use ${var#...} substitution, rather than having
 
60
#   to work around lack of support for that feature.
 
61
# The following code attempts to find a shell with support for these features
 
62
# and re-exec's it.  If not, it skips the current test.
 
63
 
 
64
gl_shell_test_script_='
 
65
test $(echo y) = y || exit 1
 
66
test -z "$EXEEXT" && exit 0
 
67
shopt -s expand_aliases
 
68
alias a-b="echo zoo"
 
69
v=abx
 
70
     test ${v%x} = ab \
 
71
  && test ${v#a} = bx \
 
72
  && test $(a-b) = zoo
 
73
'
 
74
 
 
75
if test "x$1" = "x--no-reexec"; then
 
76
  shift
 
77
else
 
78
  for re_shell_ in "${CONFIG_SHELL:-no_shell}" /bin/sh bash dash zsh pdksh fail
 
79
  do
 
80
    test "$re_shell_" = no_shell && continue
 
81
    test "$re_shell_" = fail && skip_ failed to find an adequate shell
 
82
    if "$re_shell_" -c "$gl_shell_test_script_" 2>/dev/null; then
 
83
      exec "$re_shell_" "$0" --no-reexec "$@"
 
84
      echo "$ME_: exec failed" 1>&2
 
85
      exit 127
 
86
    fi
 
87
  done
 
88
fi
 
89
 
 
90
test -n "$EXEEXT" && shopt -s expand_aliases
 
91
 
 
92
# We use a trap below for cleanup.  This requires us to go through
 
93
# hoops to get the right exit status transported through the handler.
 
94
# So use `Exit STATUS' instead of `exit STATUS' inside of the tests.
 
95
# Turn off errexit here so that we don't trip the bug with OSF1/Tru64
 
96
# sh inside this function.
 
97
Exit () { set +e; (exit $1); exit $1; }
 
98
 
 
99
# Print warnings (e.g., about skipped and failed tests) to this file number.
 
100
# Override by defining to say, 9, in init.cfg, and putting say,
 
101
# "export ...ENVVAR_SETTINGS...; exec 9>&2; $(SHELL)" in the definition
 
102
# of TESTS_ENVIRONMENT in your tests/Makefile.am file.
 
103
# This is useful when using automake's parallel tests mode, to print
 
104
# the reason for skip/failure to console, rather than to the .log files.
 
105
: ${stderr_fileno_=2}
 
106
 
 
107
warn_() { echo "$@" 1>&$stderr_fileno_; }
 
108
fail_() { warn_ "$ME_: failed test: $@"; Exit 1; }
 
109
skip_() { warn_ "$ME_: skipped test: $@"; Exit 77; }
 
110
framework_failure_() { warn_ "$ME_: set-up failure: $@"; Exit 1; }
 
111
 
 
112
# This is a stub function that is run upon trap (upon regular exit and
 
113
# interrupt).  Override it with a per-test function, e.g., to unmount
 
114
# a partition, or to undo any other global state changes.
 
115
cleanup_() { :; }
 
116
 
 
117
if ( diff --version < /dev/null 2>&1 | grep GNU ) 2>&1 > /dev/null; then
 
118
  compare() { diff -u "$@"; }
 
119
elif ( cmp --version < /dev/null 2>&1 | grep GNU ) 2>&1 > /dev/null; then
 
120
  compare() { cmp -s "$@"; }
 
121
else
 
122
  compare() { cmp "$@"; }
 
123
fi
 
124
 
 
125
# An arbitrary prefix to help distinguish test directories.
 
126
testdir_prefix_() { printf gt; }
 
127
 
 
128
# Run the user-overridable cleanup_ function, remove the temporary
 
129
# directory and exit with the incoming value of $?.
 
130
remove_tmp_()
 
131
{
 
132
  __st=$?
 
133
  cleanup_
 
134
  # cd out of the directory we're about to remove
 
135
  cd "$initial_cwd_" || cd / || cd /tmp
 
136
  chmod -R u+rwx "$test_dir_"
 
137
  # If removal fails and exit status was to be 0, then change it to 1.
 
138
  rm -rf "$test_dir_" || { test $__st = 0 && __st=1; }
 
139
  exit $__st
 
140
}
 
141
 
 
142
# Given a directory name, DIR, if every entry in it that matches *.exe
 
143
# contains only the specified bytes (see the case stmt below), then print
 
144
# a space-separated list of those names and return 0.  Otherwise, don't
 
145
# print anything and return 1.  Naming constraints apply also to DIR.
 
146
find_exe_basenames_()
 
147
{
 
148
  feb_dir_=$1
 
149
  feb_fail_=0
 
150
  feb_result_=
 
151
  feb_sp_=
 
152
  for feb_file_ in $feb_dir_/*.exe; do
 
153
    case $feb_file_ in
 
154
      *[!-a-zA-Z/0-9_.+]*) feb_fail_=1; break;;
 
155
      *) # Remove leading file name components as well as the .exe suffix.
 
156
         feb_file_=${feb_file_##*/}
 
157
         feb_file_=${feb_file_%.exe}
 
158
         feb_result_="$feb_result_$feb_sp_$feb_file_";;
 
159
    esac
 
160
    feb_sp_=' '
 
161
  done
 
162
  test $feb_fail_ = 0 && printf %s "$feb_result_"
 
163
  return $feb_fail_
 
164
}
 
165
 
 
166
# Consider the files in directory, $1.
 
167
# For each file name of the form PROG.exe, create an alias named
 
168
# PROG that simply invokes PROG.exe, then return 0.  If any selected
 
169
# file name or the directory name, $1, contains an unexpected character,
 
170
# define no function and return 1.
 
171
create_exe_shims_()
 
172
{
 
173
  case $EXEEXT in
 
174
    '') return 0 ;;
 
175
    .exe) ;;
 
176
    *) echo "$0: unexpected \$EXEEXT value: $EXEEXT" 1>&2; return 1 ;;
 
177
  esac
 
178
 
 
179
  base_names_=`find_exe_basenames_ $1` \
 
180
    || { echo "$0 (exe_shim): skipping directory: $1" 1>&2; return 1; }
 
181
 
 
182
  if test -n "$base_names_"; then
 
183
    for base_ in $base_names_; do
 
184
      alias "$base_"="$base_$EXEEXT"
 
185
    done
 
186
  fi
 
187
 
 
188
  return 0
 
189
}
 
190
 
 
191
# Use this function to prepend to PATH an absolute name for each
 
192
# specified, possibly-$initial_cwd_-relative, directory.
 
193
path_prepend_()
 
194
{
 
195
  while test $# != 0; do
 
196
    path_dir_=$1
 
197
    case $path_dir_ in
 
198
      '') fail_ "invalid path dir: '$1'";;
 
199
      /*) abs_path_dir_=$path_dir_;;
 
200
      *) abs_path_dir_=`cd "$initial_cwd_/$path_dir_" && echo "$PWD"` \
 
201
           || fail_ "invalid path dir: $path_dir_";;
 
202
    esac
 
203
    case $abs_path_dir_ in
 
204
      *:*) fail_ "invalid path dir: '$abs_path_dir_'";;
 
205
    esac
 
206
    PATH="$abs_path_dir_:$PATH"
 
207
 
 
208
    # Create an alias, FOO, for each FOO.exe in this directory.
 
209
    create_exe_shims_ "$abs_path_dir_" \
 
210
      || fail_ "something failed (above): $abs_path_dir_"
 
211
    shift
 
212
  done
 
213
  export PATH
 
214
}
 
215
 
 
216
setup_()
 
217
{
 
218
  test "$VERBOSE" = yes && set -x
 
219
 
 
220
  initial_cwd_=$PWD
 
221
  ME_=`expr "./$0" : '.*/\(.*\)$'`
 
222
 
 
223
  pfx_=`testdir_prefix_`
 
224
  test_dir_=`mktempd_ "$initial_cwd_" "$pfx_-$ME_.XXXX"` \
 
225
    || fail_ "failed to create temporary directory in $initial_cwd_"
 
226
  cd "$test_dir_"
 
227
 
 
228
  # These trap statements ensure that the temporary directory, $test_dir_,
 
229
  # is removed upon exit as well as upon receipt of any of the listed signals.
 
230
  trap remove_tmp_ 0
 
231
  for sig_ in 1 2 3 13 15; do
 
232
    eval "trap 'Exit $(expr $sig_ + 128)' $sig_"
 
233
  done
 
234
}
 
235
 
 
236
# Create a temporary directory, much like mktemp -d does.
 
237
# Written by Jim Meyering.
 
238
#
 
239
# Usage: mktempd_ /tmp phoey.XXXXXXXXXX
 
240
#
 
241
# First, try to use the mktemp program.
 
242
# Failing that, we'll roll our own mktemp-like function:
 
243
#  - try to get random bytes from /dev/urandom
 
244
#  - failing that, generate output from a combination of quickly-varying
 
245
#      sources and gzip.  Ignore non-varying gzip header, and extract
 
246
#      "random" bits from there.
 
247
#  - given those bits, map to file-name bytes using tr, and try to create
 
248
#      the desired directory.
 
249
#  - make only $MAX_TRIES_ attempts
 
250
 
 
251
# Helper function.  Print $N pseudo-random bytes from a-zA-Z0-9.
 
252
rand_bytes_()
 
253
{
 
254
  n_=$1
 
255
 
 
256
  # Maybe try openssl rand -base64 $n_prime_|tr '+/=\012' abcd first?
 
257
  # But if they have openssl, they probably have mktemp, too.
 
258
 
 
259
  chars_=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
 
260
  dev_rand_=/dev/urandom
 
261
  if test -r "$dev_rand_"; then
 
262
    # Note: 256-length($chars_) == 194; 3 copies of $chars_ is 186 + 8 = 194.
 
263
    dd ibs=$n_ count=1 if=$dev_rand_ 2>/dev/null \
 
264
      | LC_ALL=C tr -c $chars_ 01234567$chars_$chars_$chars_
 
265
    return
 
266
  fi
 
267
 
 
268
  n_plus_50_=`expr $n_ + 50`
 
269
  cmds_='date; date +%N; free; who -a; w; ps auxww; ps ef; netstat -n'
 
270
  data_=` (eval "$cmds_") 2>&1 | gzip `
 
271
 
 
272
  # Ensure that $data_ has length at least 50+$n_
 
273
  while :; do
 
274
    len_=`echo "$data_"|wc -c`
 
275
    test $n_plus_50_ -le $len_ && break;
 
276
    data_=` (echo "$data_"; eval "$cmds_") 2>&1 | gzip `
 
277
  done
 
278
 
 
279
  echo "$data_" \
 
280
    | dd bs=1 skip=50 count=$n_ 2>/dev/null \
 
281
    | LC_ALL=C tr -c $chars_ 01234567$chars_$chars_$chars_
 
282
}
 
283
 
 
284
mktempd_()
 
285
{
 
286
  case $# in
 
287
  2);;
 
288
  *) fail_ "Usage: $ME DIR TEMPLATE";;
 
289
  esac
 
290
 
 
291
  destdir_=$1
 
292
  template_=$2
 
293
 
 
294
  MAX_TRIES_=4
 
295
 
 
296
  # Disallow any trailing slash on specified destdir:
 
297
  # it would subvert the post-mktemp "case"-based destdir test.
 
298
  case $destdir_ in
 
299
  /) ;;
 
300
  */) fail_ "invalid destination dir: remove trailing slash(es)";;
 
301
  esac
 
302
 
 
303
  case $template_ in
 
304
  *XXXX) ;;
 
305
  *) fail_ "invalid template: $template_ (must have a suffix of at least 4 X's)";;
 
306
  esac
 
307
 
 
308
  fail=0
 
309
 
 
310
  # First, try to use mktemp.
 
311
  d=`unset TMPDIR; mktemp -d -t -p "$destdir_" "$template_" 2>/dev/null` \
 
312
    || fail=1
 
313
 
 
314
  # The resulting name must be in the specified directory.
 
315
  case $d in "$destdir_"*);; *) fail=1;; esac
 
316
 
 
317
  # It must have created the directory.
 
318
  test -d "$d" || fail=1
 
319
 
 
320
  # It must have 0700 permissions.  Handle sticky "S" bits.
 
321
  perms=`ls -dgo "$d" 2>/dev/null|tr S -` || fail=1
 
322
  case $perms in drwx------*) ;; *) fail=1;; esac
 
323
 
 
324
  test $fail = 0 && {
 
325
    echo "$d"
 
326
    return
 
327
  }
 
328
 
 
329
  # If we reach this point, we'll have to create a directory manually.
 
330
 
 
331
  # Get a copy of the template without its suffix of X's.
 
332
  base_template_=`echo "$template_"|sed 's/XX*$//'`
 
333
 
 
334
  # Calculate how many X's we've just removed.
 
335
  template_length_=`echo "$template_" | wc -c`
 
336
  nx_=`echo "$base_template_" | wc -c`
 
337
  nx_=`expr $template_length_ - $nx_`
 
338
 
 
339
  err_=
 
340
  i_=1
 
341
  while :; do
 
342
    X_=`rand_bytes_ $nx_`
 
343
    candidate_dir_="$destdir_/$base_template_$X_"
 
344
    err_=`mkdir -m 0700 "$candidate_dir_" 2>&1` \
 
345
      && { echo "$candidate_dir_"; return; }
 
346
    test $MAX_TRIES_ -le $i_ && break;
 
347
    i_=`expr $i_ + 1`
 
348
  done
 
349
  fail_ "$err_"
 
350
}
 
351
 
 
352
# If you want to override the testdir_prefix_ function,
 
353
# or to add more utility functions, use this file.
 
354
test -f "$srcdir/init.cfg" \
 
355
  && . "$srcdir/init.cfg"
 
356
 
 
357
setup_ "$@"