ruby-build/test/stubs/stub

131 lines
3.3 KiB
Text
Raw Permalink Normal View History

2012-11-19 17:53:32 -06:00
#!/usr/bin/env bash
set -e
2023-03-06 18:58:00 +01:00
status=127
2012-11-19 17:53:32 -06:00
program="${0##*/}"
PROGRAM="$(echo "$program" | tr a-z- A-Z_)"
2012-11-19 17:53:32 -06:00
[ -n "$TMPDIR" ] || TMPDIR="/tmp"
_STUB_PLAN="${PROGRAM}_STUB_PLAN"
_STUB_RUN="${PROGRAM}_STUB_RUN"
_STUB_INDEX="${PROGRAM}_STUB_INDEX"
2023-03-06 18:58:00 +01:00
_STUB_NOINDEX="${PROGRAM}_STUB_NOINDEX"
2012-11-19 17:53:32 -06:00
_STUB_RESULT="${PROGRAM}_STUB_RESULT"
_STUB_END="${PROGRAM}_STUB_END"
_STUB_DEBUG="${PROGRAM}_STUB_DEBUG"
2025-01-15 13:08:07 +01:00
if [ -n "${!_STUB_DEBUG}" ] && [ -z "${!_STUB_END}" ]; then
echo stub: "$program" "$@" >&${!_STUB_DEBUG}
fi
2012-11-19 17:53:32 -06:00
[ -e "${!_STUB_PLAN}" ] || exit 1
[ -n "${!_STUB_RUN}" ] || eval "${_STUB_RUN}"="${TMPDIR}/${program}-stub-run"
# Initialize or load the stub run information.
eval "${_STUB_INDEX}"=1
eval "${_STUB_RESULT}"=0
2025-01-15 13:08:07 +01:00
# shellcheck disable=SC1090
2012-11-19 17:53:32 -06:00
[ ! -e "${!_STUB_RUN}" ] || source "${!_STUB_RUN}"
# Expose this for stub scripts.
2025-01-15 13:08:07 +01:00
# shellcheck disable=SC2317
inspect_args() {
local arg
local sep=''
for arg; do
if [[ $arg == *' '* ]]; then
printf '%s"%s"' "$sep" "${arg//\"/\\\"}"
elif [[ $arg == *'"'* ]]; then
printf "%s'%s'" "$sep" "$arg"
else
printf '%s%s' "$sep" "$arg"
fi
sep=" "
done
}
2012-11-19 17:53:32 -06:00
# Loop over each line in the plan.
IFS=$'\n' read -d '' -r -a lines < "${!_STUB_PLAN}" || true
2012-11-19 17:53:32 -06:00
index=0
for line in "${lines[@]}"; do
2012-11-19 17:53:32 -06:00
index=$(($index + 1))
2023-03-06 18:58:00 +01:00
if [[ -z "${!_STUB_END}" && -n "${!_STUB_NOINDEX}" || $index -eq "${!_STUB_INDEX}" ]]; then
2012-11-19 17:53:32 -06:00
# We found the plan line we're interested in.
# Start off by assuming success.
result=0
# Split the line into an array of arguments to
# match and a command to run to produce output.
command=" $line"
if [ "$command" != "${command/ : }" ]; then
patterns="${command%% : *}"
command="${command#* : }"
fi
# Naively split patterns by whitespace for now.
# In the future, use a sed script to split while
# respecting quoting.
set -f
2012-11-19 17:53:32 -06:00
patterns=($patterns)
set +f
2012-11-19 17:53:32 -06:00
arguments=("$@")
# Match the expected argument patterns to actual
# arguments.
for (( i=0; i<${#patterns[@]}; i++ )); do
pattern="${patterns[$i]}"
argument="${arguments[$i]}"
case "$argument" in
$pattern ) ;;
* ) result=1 ;;
esac
done
# If the arguments matched, evaluate the command
# in a subshell. Otherwise, log the failure.
if [ $result -eq 0 ] ; then
set +e
( eval "$command" )
2012-11-19 17:53:32 -06:00
status="$?"
set -e
2023-03-06 18:58:00 +01:00
[ -z "${!_STUB_NOINDEX}" ] || break
elif [ -z "${!_STUB_NOINDEX}" ]; then
2012-11-19 17:53:32 -06:00
eval "${_STUB_RESULT}"=1
fi
fi
done
2012-11-19 17:53:32 -06:00
if [ -n "${!_STUB_END}" ]; then
# Clean up the run file.
rm -f "${!_STUB_RUN}"
2025-01-15 13:08:07 +01:00
stub_index_value="${!_STUB_INDEX}"
2012-11-19 17:53:32 -06:00
# If the number of lines in the plan is larger than
# the requested index, we failed.
2025-01-15 13:08:07 +01:00
if [ "$index" -ge "$stub_index_value" ]; then
echo "$program: expected $index invocations, got $((stub_index_value-1))" >&2
2012-11-19 17:53:32 -06:00
eval "${_STUB_RESULT}"=1
fi
# Return the result.
exit "${!_STUB_RESULT}"
else
# If the requested index is larger than the number
# of lines in the plan file, we failed.
2023-03-06 18:58:00 +01:00
if [[ -z "${!_STUB_NOINDEX}" && "${!_STUB_INDEX}" -gt $index ]]; then
2012-11-19 17:53:32 -06:00
eval "${_STUB_RESULT}"=1
fi
# Write out the run information.
{ echo "${_STUB_INDEX}=$((${!_STUB_INDEX} + 1))"
echo "${_STUB_RESULT}=${!_STUB_RESULT}"
} > "${!_STUB_RUN}"
exit "$status"
fi