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

Improve test output for failed stubs #2498

Merged
merged 1 commit into from
Jan 15, 2025
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: 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
Loading