Skip to content

Commit

Permalink
Port stub_repeated from ruby-build 784d7b7
Browse files Browse the repository at this point in the history
Was initially added 784d7b7118ac31a2fffe4d7036b06d736dbb2468
  • Loading branch information
jasonkarns committed Jun 4, 2024
1 parent 2737aec commit c5a7654
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 6 additions & 4 deletions binstub
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#!/usr/bin/env bash
set -e

status=0
status=127
program="${0##*/}"
PROGRAM="$(echo "$program" | tr a-z- A-Z_)"

_STUB_PLAN="${PROGRAM}_STUB_PLAN"
_STUB_RUN="${PROGRAM}_STUB_RUN"
_STUB_INDEX="${PROGRAM}_STUB_INDEX"
_STUB_NOINDEX="${PROGRAM}_STUB_NOINDEX"
_STUB_RESULT="${PROGRAM}_STUB_RESULT"
_STUB_END="${PROGRAM}_STUB_END"
_STUB_DEBUG="${PROGRAM}_STUB_DEBUG"
Expand All @@ -31,7 +32,7 @@ index=0
while IFS= read -r line; do
index=$(($index + 1))

if [ -z "${!_STUB_END}" ] && [ $index -eq "${!_STUB_INDEX}" ]; then
if [[ -z "${!_STUB_END}" && -n "${!_STUB_NOINDEX}" || $index -eq "${!_STUB_INDEX}" ]]; then
# We found the plan line we're interested in.
# Start off by assuming success.
result=0
Expand Down Expand Up @@ -71,7 +72,8 @@ while IFS= read -r line; do
( eval "$command" )
status="$?"
set -e
else
[ -z "${!_STUB_NOINDEX}" ] || break
elif [ -z "${!_STUB_NOINDEX}" ]; then
eval "${_STUB_RESULT}"=1
fi
fi
Expand All @@ -94,7 +96,7 @@ if [ -n "${!_STUB_END}" ]; then
else
# If the requested index is larger than the number
# of lines in the plan file, we failed.
if [ "${!_STUB_INDEX}" -gt $index ]; then
if [[ -z "${!_STUB_NOINDEX}" && "${!_STUB_INDEX}" -gt $index ]]; then
eval "${_STUB_RESULT}"=1
fi

Expand Down
8 changes: 8 additions & 0 deletions stub.bash
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ stub() {
for arg in "$@"; do printf "%s\n" "$arg" >> "${BATS_MOCK_TMPDIR}/${program}-stub-plan"; done
}

stub_repeated() {
local program="$1"
# shellcheck disable=SC2155
local prefix="$(echo "$program" | tr a-z- A-Z_)"
export "${prefix}_STUB_NOINDEX"=1
stub "$@"
}

unstub() {
local program="$1"
local prefix
Expand Down

0 comments on commit c5a7654

Please sign in to comment.