1#! /bin/sh
2# Run a test script of the coreutils test scripts, picking up the right
3# interpreter (i.e., perl or the shell) and the right flags for it (e.g.,
4# perl '-T' flag for perl scripts that must run in tainted mode).
5#
6# Copyright (C) 2011-2012 Free Software Foundation, Inc.
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20#
21
22# ---------------------------------- #
23#  Readonly variables and functions  #
24# ---------------------------------- #
25
26# Help to avoid typo-related bugs.
27set -u
28
29me=shell-or-perl
30
31fatal_ ()
32{
33  echo "$me: $*" >&2
34  # Exit with status '99' to inform the testsuite harness that an
35  # hard error occurred.
36  exit 99
37}
38
39print_help_ ()
40{
41  cat <<EOH
42Usage: $me [--help] [--srcdir DIR] [--shell SHELL-CMD] [--perl PERL-CMD]
43       [--test-name NAME-WITHOUT-VPATH] TEST-SCRIPT [ARGS..]
44EOH
45}
46
47# ---------------- #
48#  Option parsing  #
49# ---------------- #
50
51assign_optarg_to_var='
52  test $# -gt 1 || fatal_ "option '\''$1'\'' requires an argument"
53  eval "$var=\$2"
54  shift'
55
56srcdir=${srcdir-.}
57cu_PERL=${PERL-perl}
58cu_SHELL=/bin/sh # Getting $SHELL from the environment is dangerous.
59test_name=
60while test $# -gt 0; do
61  var=
62  case $1 in
63    --help) print_help_; exit $?;;
64    --shell) var=cu_SHELL;;
65    --perl) var=cu_PERL;;
66    --srcdir) var=srcdir;;
67    --test-name) var=test_name;;
68    --) shift; break;;
69    -*) fatal_ "unknown option '$1'";;
70    *) break;;
71  esac
72  test -z "$var" || eval "$assign_optarg_to_var"
73  shift
74done
75
76unset assign_optarg_to_var var
77
78case $# in
79  0) fatal_ "missing argument";;
80  *) test_script=$1; shift;;
81esac
82
83test -z "$test_name" && test_name=$test_script
84
85# --------------------- #
86#  Run the test script  #
87# --------------------- #
88
89test -f "$test_script" && test -r "$test_script" \
90  || fatal_ "test script '$test_script' does not exist, or isn't readable"
91
92read shebang_line < "$test_script" \
93  || fatal_ "cannot read from the test script '$test_script'"
94
95case $shebang_line in
96'#!/usr/bin/perl'*)
97  # The test is a perl script.
98  if $cu_PERL -e 'use warnings' > /dev/null 2>&1; then
99    # Perl is available, see if we must run the test with taint
100    # mode on or not.
101    case $shebang_line in *\ -T*) T_=T;; *) T_=;; esac
102    # Now run it.
103    exec $cu_PERL -w$T_ -I"$srcdir" -MCoreutils -MCuSkip \
104                  -M"CuTmpdir qw($test_name)" \
105                  -- "$test_script" ${1+"$@"}
106  else
107    # Perl is not available, skip the test.
108    echo "$test_name: skip: no usable version of Perl found"
109    exit 77
110  fi
111  ;;
112*)
113  # Assume the test is a shell script.
114  exec $cu_SHELL "$test_script" ${1+"$@"}
115esac
116
117# ------------- #
118#  Not reached  #
119# ------------- #
120
121fatal_ "dead code reached"
122