| 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-2020 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. |
| 27 | set -u |
| 28 | |
| 29 | me=shell-or-perl |
| 30 | |
| 31 | fatal_ () |
| 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 | |
| 39 | print_help_ () |
| 40 | { |
| 41 | cat <<EOH |
| 42 | Usage: $me [--help] [--srcdir DIR] [--shell SHELL-CMD] [--perl PERL-CMD] |
| 43 | [--test-name NAME-WITHOUT-VPATH] TEST-SCRIPT [ARGS..] |
| 44 | EOH |
| 45 | } |
| 46 | |
| 47 | # ---------------- # |
| 48 | # Option parsing # |
| 49 | # ---------------- # |
| 50 | |
| 51 | assign_optarg_to_var=' |
| 52 | test $# -gt 1 || fatal_ "option '\''$1'\'' requires an argument" |
| 53 | eval "$var=\$2" |
| 54 | shift' |
| 55 | |
| 56 | srcdir=${srcdir-.} |
| 57 | cu_PERL=${PERL-perl} |
| 58 | cu_SHELL=/bin/sh # Getting $SHELL from the environment is dangerous. |
| 59 | test_name= |
| 60 | while 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 |
| 74 | done |
| 75 | |
| 76 | unset assign_optarg_to_var var |
| 77 | |
| 78 | case $# in |
| 79 | 0) fatal_ "missing argument";; |
| 80 | *) test_script=$1; shift;; |
| 81 | esac |
| 82 | |
| 83 | test -z "$test_name" && test_name=$test_script |
| 84 | |
| 85 | # --------------------- # |
| 86 | # Run the test script # |
| 87 | # --------------------- # |
| 88 | |
| 89 | test -f "$test_script" && test -r "$test_script" \ |
| 90 | || fatal_ "test script '$test_script' does not exist, or isn't readable" |
| 91 | |
| 92 | read shebang_line < "$test_script" \ |
| 93 | || fatal_ "cannot read from the test script '$test_script'" |
| 94 | |
| 95 | case $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+"$@"} |
| 115 | esac |
| 116 | |
| 117 | # ------------- # |
| 118 | # Not reached # |
| 119 | # ------------- # |
| 120 | |
| 121 | fatal_ "dead code reached" |