| 1 |
#!/usr/bin/env bash |
| 2 |
set -e |
| 3 |
|
| 4 |
status=127 |
| 5 |
program="${0##*/}" |
| 6 |
PROGRAM="$(echo "$program" | tr a-z- A-Z_)" |
| 7 |
[ -n "$TMPDIR" ] || TMPDIR="/tmp" |
| 8 |
|
| 9 |
_STUB_PLAN="${PROGRAM}_STUB_PLAN" |
| 10 |
_STUB_RUN="${PROGRAM}_STUB_RUN" |
| 11 |
_STUB_INDEX="${PROGRAM}_STUB_INDEX" |
| 12 |
_STUB_NOINDEX="${PROGRAM}_STUB_NOINDEX" |
| 13 |
_STUB_RESULT="${PROGRAM}_STUB_RESULT" |
| 14 |
_STUB_END="${PROGRAM}_STUB_END" |
| 15 |
_STUB_DEBUG="${PROGRAM}_STUB_DEBUG" |
| 16 |
|
| 17 |
if [ -n "${!_STUB_DEBUG}" ] && [ -z "${!_STUB_END}" ]; then |
| 18 |
echo stub: "$program" "$@" >&${!_STUB_DEBUG} |
| 19 |
fi |
| 20 |
|
| 21 |
[ -e "${!_STUB_PLAN}" ] || exit 1 |
| 22 |
[ -n "${!_STUB_RUN}" ] || eval "${_STUB_RUN}"="${TMPDIR}/${program}-stub-run" |
| 23 |
|
| 24 |
|
| 25 |
# Initialize or load the stub run information. |
| 26 |
eval "${_STUB_INDEX}"=1 |
| 27 |
eval "${_STUB_RESULT}"=0 |
| 28 |
# shellcheck disable=SC1090 |
| 29 |
[ ! -e "${!_STUB_RUN}" ] || source "${!_STUB_RUN}" |
| 30 |
|
| 31 |
# Expose this for stub scripts. |
| 32 |
# shellcheck disable=SC2317 |
| 33 |
inspect_args() { |
| 34 |
local arg |
| 35 |
local sep='' |
| 36 |
for arg; do |
| 37 |
if [[ $arg == *' '* ]]; then |
| 38 |
printf '%s"%s"' "$sep" "${arg//\"/\\\"}" |
| 39 |
elif [[ $arg == *'"'* ]]; then |
| 40 |
printf "%s'%s'" "$sep" "$arg" |
| 41 |
else |
| 42 |
printf '%s%s' "$sep" "$arg" |
| 43 |
fi |
| 44 |
sep=" " |
| 45 |
done |
| 46 |
} |
| 47 |
|
| 48 |
# Loop over each line in the plan. |
| 49 |
IFS=$'\n' read -d '' -r -a lines < "${!_STUB_PLAN}" || true |
| 50 |
index=0 |
| 51 |
for line in "${lines[@]}"; do |
| 52 |
index=$(($index + 1)) |
| 53 |
|
| 54 |
if [[ -z "${!_STUB_END}" && -n "${!_STUB_NOINDEX}" || $index -eq "${!_STUB_INDEX}" ]]; then |
| 55 |
# We found the plan line we're interested in. |
| 56 |
# Start off by assuming success. |
| 57 |
result=0 |
| 58 |
|
| 59 |
# Split the line into an array of arguments to |
| 60 |
# match and a command to run to produce output. |
| 61 |
command=" $line" |
| 62 |
if [ "$command" != "${command/ : }" ]; then |
| 63 |
patterns="${command%% : *}" |
| 64 |
command="${command#* : }" |
| 65 |
fi |
| 66 |
|
| 67 |
# Naively split patterns by whitespace for now. |
| 68 |
# In the future, use a sed script to split while |
| 69 |
# respecting quoting. |
| 70 |
set -f |
| 71 |
patterns=($patterns) |
| 72 |
set +f |
| 73 |
arguments=("$@") |
| 74 |
|
| 75 |
# Match the expected argument patterns to actual |
| 76 |
# arguments. |
| 77 |
for (( i=0; i<${#patterns[@]}; i++ )); do |
| 78 |
pattern="${patterns[$i]}" |
| 79 |
argument="${arguments[$i]}" |
| 80 |
|
| 81 |
case "$argument" in |
| 82 |
$pattern ) ;; |
| 83 |
* ) result=1 ;; |
| 84 |
esac |
| 85 |
done |
| 86 |
|
| 87 |
# If the arguments matched, evaluate the command |
| 88 |
# in a subshell. Otherwise, log the failure. |
| 89 |
if [ $result -eq 0 ] ; then |
| 90 |
set +e |
| 91 |
( eval "$command" ) |
| 92 |
status="$?" |
| 93 |
set -e |
| 94 |
[ -z "${!_STUB_NOINDEX}" ] || break |
| 95 |
elif [ -z "${!_STUB_NOINDEX}" ]; then |
| 96 |
eval "${_STUB_RESULT}"=1 |
| 97 |
fi |
| 98 |
fi |
| 99 |
done |
| 100 |
|
| 101 |
|
| 102 |
if [ -n "${!_STUB_END}" ]; then |
| 103 |
# Clean up the run file. |
| 104 |
rm -f "${!_STUB_RUN}" |
| 105 |
|
| 106 |
stub_index_value="${!_STUB_INDEX}" |
| 107 |
# If the number of lines in the plan is larger than |
| 108 |
# the requested index, we failed. |
| 109 |
if [ "$index" -ge "$stub_index_value" ]; then |
| 110 |
echo "$program: expected $index invocations, got $((stub_index_value-1))" >&2 |
| 111 |
eval "${_STUB_RESULT}"=1 |
| 112 |
fi |
| 113 |
|
| 114 |
# Return the result. |
| 115 |
exit "${!_STUB_RESULT}" |
| 116 |
|
| 117 |
else |
| 118 |
# If the requested index is larger than the number |
| 119 |
# of lines in the plan file, we failed. |
| 120 |
if [[ -z "${!_STUB_NOINDEX}" && "${!_STUB_INDEX}" -gt $index ]]; then |
| 121 |
eval "${_STUB_RESULT}"=1 |
| 122 |
fi |
| 123 |
|
| 124 |
# Write out the run information. |
| 125 |
{ echo "${_STUB_INDEX}=$((${!_STUB_INDEX} + 1))" |
| 126 |
echo "${_STUB_RESULT}=${!_STUB_RESULT}" |
| 127 |
} > "${!_STUB_RUN}" |
| 128 |
|
| 129 |
exit "$status" |
| 130 |
|
| 131 |
fi |