Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port stub_repeated from ruby-build@784d7b7 #42

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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