Debian Codegraph @

plugins/python-build/test/stubs/stub

raw
1 #!/usr/bin/env bash
2 set -e
3
4 status=0
5 program="${0##*/}"
6 PROGRAM="$(echo "$program" | tr a-z- A-Z_)"
7
8 _STUB_PLAN="${PROGRAM}_STUB_PLAN"
9 _STUB_RUN="${PROGRAM}_STUB_RUN"
10 _STUB_INDEX="${PROGRAM}_STUB_INDEX"
11 _STUB_RESULT="${PROGRAM}_STUB_RESULT"
12 _STUB_END="${PROGRAM}_STUB_END"
13 _STUB_LOG="${PROGRAM}_STUB_LOG"
14
15 [ -n "${!_STUB_LOG}" ] || eval "${_STUB_LOG}"="${BATS_TEST_TMPDIR}/${program}-stub-log"
16 if test -z "${!_STUB_END}"; then echo "$program" "$@" >>"${!_STUB_LOG}"; fi
17
18 [ -e "${!_STUB_PLAN}" ] || exit 1
19 [ -n "${!_STUB_RUN}" ] || eval "${_STUB_RUN}"="${BATS_TEST_TMPDIR}/${program}-stub-run"
20
21
22 # Initialize or load the stub run information.
23 eval "${_STUB_INDEX}"=1
24 eval "${_STUB_RESULT}"=0
25 if test -e "${!_STUB_RUN}"; then source "${!_STUB_RUN}"; fi
26
27
28 # Loop over each line in the plan.
29 index=0
30 while IFS= read -r line; do
31 index=$(($index + 1))
32
33 if [ -z "${!_STUB_END}" ] && [ $index -eq "${!_STUB_INDEX}" ]; then
34 # We found the plan line we're interested in.
35 # Start off by assuming success.
36 result=0
37
38 # Split the line into an array of arguments to
39 # match and a command to run to produce output.
40 command=" $line"
41 if [ "$command" != "${command/ : }" ]; then
42 patterns="${command%% : *}"
43 command="${command#* : }"
44 fi
45
46 # Naively split patterns by whitespace for now.
47 # In the future, use a sed script to split while
48 # respecting quoting.
49 set -f
50 patterns=($patterns)
51 set +f
52 arguments=("$@")
53
54 # Match the expected argument patterns to actual
55 # arguments.
56 for (( i=0; i<${#patterns[@]}; i++ )); do
57 pattern="${patterns[$i]}"
58 argument="${arguments[$i]}"
59
60 case "$argument" in
61 $pattern ) ;;
62 * ) result=1 ;;
63 esac
64 done
65
66 # If the arguments matched, evaluate the command
67 # in a subshell. Otherwise, log the failure.
68 if [ $result -eq 0 ] ; then
69 set +e
70 ( eval "$command" )
71 status="$?"
72 set -e
73 else
74 eval "${_STUB_RESULT}"=1
75 fi
76 fi
77 done < "${!_STUB_PLAN}"
78
79
80 if [ -n "${!_STUB_END}" ]; then
81 # If the number of lines in the plan is larger than
82 # the requested index, we failed.
83 if [ $index -ge "${!_STUB_INDEX}" ]; then
84 eval "${_STUB_RESULT}"=1
85 fi
86 if [ "${!_STUB_RESULT}" -ne 0 ]; then
87 {
88 echo "index: $index; stub index: ${!_STUB_INDEX}"
89 echo "plan:"
90 cat "${!_STUB_PLAN}" || true
91 echo "run:"
92 cat "${!_STUB_RUN}" || true
93 echo "log:"
94 cat "${!_STUB_LOG}" || true
95 } >&2
96 fi
97
98 # Clean up the run file.
99 rm -f "${!_STUB_RUN}"
100 rm -f "${!_STUB_LOG}"
101
102 # Return the result.
103 exit "${!_STUB_RESULT}"
104
105 else
106 # If the requested index is larger than the number
107 # of lines in the plan file, we failed.
108 if [ "${!_STUB_INDEX}" -gt $index ]; then
109 eval "${_STUB_RESULT}"=1
110 fi
111
112 # Write out the run information.
113 { echo "${_STUB_INDEX}=$((${!_STUB_INDEX} + 1))"
114 echo "${_STUB_RESULT}=${!_STUB_RESULT}"
115 } > "${!_STUB_RUN}"
116
117 exit "$status"
118
119 fi