Skip to content

Commit 7d98404

Browse files
committed
stub: -unnecessary indirection
1 parent bf7d3b0 commit 7d98404

1 file changed

Lines changed: 45 additions & 40 deletions

File tree

test/stubs/stub

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ program="${0##*/}"
77
PROGRAM="$(echo "$program" | tr a-z- A-Z_)"
88
[ -n "$TMPDIR" ] || TMPDIR="/tmp"
99

10-
_STUB_PLAN="${PROGRAM}_STUB_PLAN"
11-
_STUB_RUN="${PROGRAM}_STUB_RUN"
12-
_STUB_INDEX="${PROGRAM}_STUB_INDEX"
13-
_STUB_RESULT="${PROGRAM}_STUB_RESULT"
14-
_STUB_END="${PROGRAM}_STUB_END"
15-
_STUB_LOG="${PROGRAM}_STUB_LOG"
10+
STUB_PLAN="${PROGRAM}_STUB_PLAN"
11+
STUB_PLAN="${!STUB_PLAN}"
12+
13+
STUB_RUN="${PROGRAM}_STUB_RUN"
14+
STUB_RUN="${!STUB_RUN:-${TMPDIR}/${program}-stub-run}"
15+
STUB_INDEX=
16+
STUB_RESULT=
17+
18+
STUB_END="${PROGRAM}_STUB_END"
19+
STUB_END="${!STUB_END}"
20+
21+
STUB_LOG="${PROGRAM}_STUB_LOG"
22+
STUB_LOG="${!STUB_LOG:-${TMPDIR}/${program}-stub-log}"
1623

1724

1825
STUB_LOCKFILE="${TMPDIR}/${program}-stub.lock"
@@ -46,47 +53,42 @@ acquire_lock() {
4653

4754
acquire_lock
4855

49-
[ -n "${!_STUB_LOG}" ] || eval "${_STUB_LOG}"="${TMPDIR}/${program}-stub-log"
50-
51-
if test -z "${!_STUB_END}"; then echo "$program" "$@" >>"${!_STUB_LOG}"; fi
52-
53-
[ -e "${!_STUB_PLAN}" ] || exit 1
54-
[ -n "${!_STUB_RUN}" ] || eval "${_STUB_RUN}"="${TMPDIR}/${program}-stub-run"
56+
if [[ -z $STUB_END ]]; then echo "$program" "$@" >>"$STUB_LOG"; fi
5557

58+
[[ -e $STUB_PLAN ]] || exit 1
5659

5760
# Initialize or load the stub run information.
5861
read_runfile() {
59-
if test -e "${!_STUB_RUN}"; then source "${!_STUB_RUN}"; fi
62+
if [[ -e $STUB_RUN ]]; then source "$STUB_RUN"; fi
6063
}
6164
write_runfile() {
6265
{
6366
local i
64-
echo "${_STUB_INDEX}=${!_STUB_INDEX}"
65-
echo "${_STUB_RESULT}=${!_STUB_RESULT}"
67+
echo "STUB_INDEX=$STUB_INDEX"
68+
echo "STUB_RESULT=$STUB_RESULT"
6669
echo "STUB_RUNCOUNTS=()"
6770
for i in ${!STUB_RUNCOUNTS[@]}; do
6871
echo "STUB_RUNCOUNTS[$i]=${STUB_RUNCOUNTS[$i]}"
6972
done
70-
} > "${!_STUB_RUN}"
73+
} > "$STUB_RUN"
7174
}
7275
update_runfile_index() {
73-
( eval "${_STUB_INDEX}=$((${!_STUB_INDEX} + 1))"
76+
( STUB_INDEX=$((STUB_INDEX + 1))
7477
write_runfile
7578
)
7679
}
7780
update_runfile_result() {
7881
(
7982
# Another stubs may have run while we were running payload
8083
# So we need to merge possible state changes
81-
local our_result="${!_STUB_RESULT}"
84+
local our_result="$STUB_RESULT"
8285
local -a our_runcounts
8386
array_copy STUB_RUNCOUNTS our_runcounts
8487

8588
read_runfile
8689

8790
# merge our match_result and their match_result, with failure taking precedence
88-
local new_result=$(( $our_result | ${!_STUB_RESULT} ))
89-
eval "${_STUB_RESULT}=\$new_result"
91+
STUB_RESULT=$(( our_result | STUB_RESULT ))
9092

9193
# 3-way merge STUB_RUNCOUNTS (their changes),
9294
# our_runcounts (our changes) and initial_runcounts (base)
@@ -118,16 +120,16 @@ array_copy() {
118120
eval "$dest=$data"
119121
}
120122

121-
eval "${_STUB_INDEX}"=1
122-
eval "${_STUB_RESULT}"=0
123+
STUB_INDEX=1
124+
STUB_RESULT=0
123125
declare -a STUB_RUNCOUNTS
124126
read_runfile
125127
declare -a initial_runcounts
126128
array_copy STUB_RUNCOUNTS initial_runcounts
127129

128-
# !_STUB_END is set externally to trigger verification mode for `unstub'
130+
# ${PROGRAM}_STUB_END envvar is set externally to trigger verification mode for `unstub'
129131
# Execution mode
130-
if [ -z "${!_STUB_END}" ]; then
132+
if [[ -z $STUB_END ]]; then
131133

132134
# Loop over each line in the plan.
133135
regular_command_index=0
@@ -146,7 +148,7 @@ if [ -z "${!_STUB_END}" ]; then
146148
# Also keep track of no-order commands for the purpose of run count tracking
147149
if [[ -n $line_flag_regular ]]; then
148150
regular_command_index=$(($regular_command_index + 1))
149-
if [[ $regular_command_index -ne ${!_STUB_INDEX} ]]; then
151+
if [[ $regular_command_index -ne $STUB_INDEX ]]; then
150152
continue;
151153
fi
152154
else
@@ -202,15 +204,18 @@ if [ -z "${!_STUB_END}" ]; then
202204
break
203205
fi
204206

205-
done < "${!_STUB_PLAN}"
207+
done < "$STUB_PLAN"
206208

207209
#If we never matched anything, we failed.
208210
if [[ $match_result -eq 1 ]]; then
209-
eval "${_STUB_RESULT}"=1
211+
STUB_RESULT=1
212+
213+
#This also means that we never released the lock
214+
# before running the payload
215+
else
216+
acquire_lock
210217
fi
211-
212218
# Write out the match_result information.
213-
acquire_lock
214219
update_runfile_result
215220
release_lock
216221

@@ -219,15 +224,15 @@ if [ -z "${!_STUB_END}" ]; then
219224
fi
220225

221226
# Verification mode (`unstub')
222-
if [ -n "${!_STUB_END}" ]; then
227+
if [[ -n $STUB_END ]]; then
223228

224229
# `unstub' is supposed to run after any stubs are finished
225230
release_lock
226231

227232
# If the number of regular commands in the plan is larger than
228233
# the final regular_command_index, we failed.
229-
if [ "$(grep -Ee '^-' "${!_STUB_PLAN}" | wc -l )" -ge "${!_STUB_INDEX}" ]; then
230-
eval "${_STUB_RESULT}"=1
234+
if [[ $(grep -Ee '^-' "$STUB_PLAN" | wc -l ) -ge $STUB_INDEX ]]; then
235+
STUB_RESULT=1
231236
fi
232237

233238
# If no-order commands weren't executed exactly once
@@ -253,25 +258,25 @@ if [ -n "${!_STUB_END}" ]; then
253258
( -n $line_flag_multiple && \
254259
(( STUB_RUNCOUNTS[no_order_command_index] < 1 )) ) ]]
255260
then
256-
eval "${_STUB_RESULT}"=1
261+
STUB_RESULT=1
257262
fi
258263

259-
done < "${!_STUB_PLAN}"
264+
done < "$STUB_PLAN"
260265

261-
if [ "${!_STUB_RESULT}" -ne 0 ]; then
266+
if [[ $STUB_RESULT -ne 0 ]]; then
262267
{
263268
echo "plan:"
264-
cat "${!_STUB_PLAN}" || true
269+
cat "$STUB_PLAN" || true
265270
echo "log:"
266-
cat "${!_STUB_LOG}" || true
271+
cat "$STUB_LOG" || true
267272
} >&2
268273
fi
269274

270275
# Clean up the run file.
271-
rm -f "${!_STUB_RUN}"
272-
rm -f "${!_STUB_LOG}"
276+
rm -f "$STUB_RUN"
277+
rm -f "$STUB_LOG"
273278

274279
# Return the run result.
275-
exit "${!_STUB_RESULT}"
280+
exit "$STUB_RESULT"
276281

277282
fi

0 commit comments

Comments
 (0)