| 1 |
#!/bin/sh |
| 2 |
# |
| 3 |
# INSTALL_TEST_SH, sh-kind test launcher for install non-reg suite |
| 4 |
# Copyright (C) 2017-2022 Xavier Delaruelle |
| 5 |
# |
| 6 |
# This program is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 2 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
########################################################################## |
| 20 |
|
| 21 |
progpath=$0 |
| 22 |
progdir=$(dirname "$progpath") |
| 23 |
[ -z "$progdir" ] && progdir='.' |
| 24 |
ret=0 |
| 25 |
|
| 26 |
# print message on stderr then exit |
| 27 |
echo_error() { |
| 28 |
echo -e "ERROR: $1" >&2 |
| 29 |
exit 1 |
| 30 |
} |
| 31 |
|
| 32 |
if [ $# -lt 4 ]; then |
| 33 |
# print usage message |
| 34 |
echo_error "Usage: $progpath initfile mode shell command" |
| 35 |
fi |
| 36 |
|
| 37 |
initfile=$1 |
| 38 |
shift |
| 39 |
mode=$1 |
| 40 |
shift |
| 41 |
sh=$1 |
| 42 |
shift |
| 43 |
cmdlist=${*} |
| 44 |
|
| 45 |
# get shell kind and options for sublaunch |
| 46 |
shname=$(basename "$sh") |
| 47 |
if [ "$shname" = 'ksh93' ]; then |
| 48 |
shname='ksh' |
| 49 |
fi |
| 50 |
case "$shname" in |
| 51 |
bash|ksh|zsh) |
| 52 |
shkind='sh' |
| 53 |
;; |
| 54 |
tcsh) |
| 55 |
shkind='csh' |
| 56 |
;; |
| 57 |
*) |
| 58 |
shkind=$shname |
| 59 |
esac |
| 60 |
case "$shkind" in |
| 61 |
# ignore global csh system init file to avoid side effect |
| 62 |
csh) |
| 63 |
shopts='-f' |
| 64 |
;; |
| 65 |
*) |
| 66 |
shopts='' |
| 67 |
esac |
| 68 |
|
| 69 |
# init module via autoinit cmd if modulecmd.tcl is initfile |
| 70 |
if [ "$(basename "${initfile}")" = 'modulecmd.tcl' ]; then |
| 71 |
autoinit=0 |
| 72 |
else |
| 73 |
autoinit=1 |
| 74 |
fi |
| 75 |
|
| 76 |
if [ $autoinit -eq 0 ] && [ ! -x "$initfile" ]; then |
| 77 |
echo_error "Cannot execute $initfile" |
| 78 |
elif [ $autoinit -ne 0 ] && [ ! -r "$initfile" ]; then |
| 79 |
echo_error "Cannot read $initfile" |
| 80 |
fi |
| 81 |
|
| 82 |
# source module init file if first step of given mode |
| 83 |
if [ "$mode" = 'top' ] || [ "$mode" = 'sub' ] || [ "$mode" = 'subsub' ]; then |
| 84 |
if [ $autoinit -eq 0 ]; then |
| 85 |
eval "$("$initfile" "$shname" autoinit)" |
| 86 |
else |
| 87 |
. "$initfile" |
| 88 |
fi |
| 89 |
fi |
| 90 |
|
| 91 |
# execute command list or perform recursive call |
| 92 |
case "$mode" in |
| 93 |
top|launch) |
| 94 |
# enable alias resolution on bash for tests |
| 95 |
if [ "$shname" = 'bash' ]; then |
| 96 |
shopt -s expand_aliases |
| 97 |
# enable word splitting for the below loop on zsh |
| 98 |
elif [ "$shname" = 'zsh' ]; then |
| 99 |
setopt sh_word_split |
| 100 |
fi |
| 101 |
IFS=';' |
| 102 |
for cmd in ${cmdlist}; do |
| 103 |
unset IFS |
| 104 |
eval "$cmd" |
| 105 |
ret=$((ret+$?)) |
| 106 |
IFS=';' |
| 107 |
done |
| 108 |
unset IFS |
| 109 |
;; |
| 110 |
sub|sublaunch) |
| 111 |
# shellcheck disable=SC2086 |
| 112 |
$sh $shopts "$progdir/install_test_${shkind}" "$initfile" launch "$sh" "$cmdlist" |
| 113 |
ret=$? |
| 114 |
;; |
| 115 |
subsub) |
| 116 |
# shellcheck disable=SC2086 |
| 117 |
$sh $shopts "$progdir/install_test_${shkind}" "$initfile" sublaunch "$sh" "$cmdlist" |
| 118 |
ret=$? |
| 119 |
;; |
| 120 |
*) |
| 121 |
echo_error "Unrecognized mode option. Valid mode are 'top', 'sub', 'subsub'" |
| 122 |
esac |
| 123 |
|
| 124 |
# throw command exit code |
| 125 |
exit $ret |
| 126 |
|
| 127 |
# vim:set tabstop=3 shiftwidth=3 expandtab autoindent: |