diff --git a/binstub b/binstub index d1169a2..43f6afc 100755 --- a/binstub +++ b/binstub @@ -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" @@ -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 @@ -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 @@ -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 diff --git a/stub.bash b/stub.bash index 8528a10..0a71cf4 100644 --- a/stub.bash +++ b/stub.bash @@ -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