Skip to content

Commit fe8c6c2

Browse files
mislaveregon
authored andcommitted
Improve test output for failed stubs
1 parent 5d0b957 commit fe8c6c2

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

test/stubs/stub

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ _STUB_RESULT="${PROGRAM}_STUB_RESULT"
1414
_STUB_END="${PROGRAM}_STUB_END"
1515
_STUB_DEBUG="${PROGRAM}_STUB_DEBUG"
1616

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

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

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

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

test/test_helper.bash

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,17 @@ unstub() {
6666

6767
export "${prefix}_STUB_END"=1
6868

69+
local stub_was_invoked=
70+
[ -e "${TMP}/${program}-stub-run" ] && stub_was_invoked=1
71+
6972
local STATUS=0
7073
"$path" || STATUS="$?"
7174

75+
local debug_var="${prefix}_STUB_DEBUG"
76+
if [ $STATUS -ne 0 ] && [ -z "${!debug_var}" ] && [ -n "$stub_was_invoked" ]; then
77+
echo "unstub $program: re-run test with ${debug_var}=3 to log \`$program' invocations" >&2
78+
fi
79+
7280
rm -f "$path"
7381
rm -f "${TMP}/${program}-stub-plan" "${TMP}/${program}-stub-run"
7482
return "$STATUS"

0 commit comments

Comments
 (0)