Skip to content

Commit

Permalink
Improve test output for failed stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav authored and eregon committed Jan 15, 2025
1 parent 5d0b957 commit fe8c6c2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
10 changes: 7 additions & 3 deletions test/stubs/stub
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ _STUB_RESULT="${PROGRAM}_STUB_RESULT"
_STUB_END="${PROGRAM}_STUB_END"
_STUB_DEBUG="${PROGRAM}_STUB_DEBUG"

if [ -n "${!_STUB_DEBUG}" ]; then
echo "$program" "$@" >&${!_STUB_DEBUG}
if [ -n "${!_STUB_DEBUG}" ] && [ -z "${!_STUB_END}" ]; then
echo stub: "$program" "$@" >&${!_STUB_DEBUG}
fi

[ -e "${!_STUB_PLAN}" ] || exit 1
Expand All @@ -25,9 +25,11 @@ fi
# Initialize or load the stub run information.
eval "${_STUB_INDEX}"=1
eval "${_STUB_RESULT}"=0
# shellcheck disable=SC1090
[ ! -e "${!_STUB_RUN}" ] || source "${!_STUB_RUN}"

# Expose this for stub scripts.
# shellcheck disable=SC2317
inspect_args() {
local arg
local sep=''
Expand Down Expand Up @@ -101,9 +103,11 @@ if [ -n "${!_STUB_END}" ]; then
# Clean up the run file.
rm -f "${!_STUB_RUN}"

stub_index_value="${!_STUB_INDEX}"
# If the number of lines in the plan is larger than
# the requested index, we failed.
if [ $index -ge "${!_STUB_INDEX}" ]; then
if [ "$index" -ge "$stub_index_value" ]; then
echo "$program: expected $index invocations, got $((stub_index_value-1))" >&2
eval "${_STUB_RESULT}"=1
fi

Expand Down
8 changes: 8 additions & 0 deletions test/test_helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,17 @@ unstub() {

export "${prefix}_STUB_END"=1

local stub_was_invoked=
[ -e "${TMP}/${program}-stub-run" ] && stub_was_invoked=1

local STATUS=0
"$path" || STATUS="$?"

local debug_var="${prefix}_STUB_DEBUG"
if [ $STATUS -ne 0 ] && [ -z "${!debug_var}" ] && [ -n "$stub_was_invoked" ]; then
echo "unstub $program: re-run test with ${debug_var}=3 to log \`$program' invocations" >&2
fi

rm -f "$path"
rm -f "${TMP}/${program}-stub-plan" "${TMP}/${program}-stub-run"
return "$STATUS"
Expand Down

0 comments on commit fe8c6c2

Please sign in to comment.