From 4ac462dbaa5afa7aa957ff6c230c2b9fc0e850fa Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Wed, 5 Jun 2019 13:24:00 +1000 Subject: [PATCH] Fail when xargs fails --- binstub | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/binstub b/binstub index 7bc934d..96554cc 100755 --- a/binstub +++ b/binstub @@ -19,7 +19,7 @@ _STUB_DEBUG="${PROGRAM}_STUB_DEBUG" debug() { if [ -n "${!_STUB_DEBUG}" ] ; then - echo "bats-mock($program): $@" >&${!_STUB_DEBUG} + echo "bats-mock($program): $*" >&${!_STUB_DEBUG} fi } @@ -32,13 +32,13 @@ eval "${_STUB_RESULT}"=0 [ ! -e "${!_STUB_RUN}" ] || source "${!_STUB_RUN}" if [ -z "${!_STUB_END}" ] && [ -n "${!_STUB_DEBUG}" ]; then - debug "got $program $@" >&${!_STUB_DEBUG} + debug "got $program $*" >&${!_STUB_DEBUG} fi # Loop over each line in the plan. index=0 while IFS= read -r line; do - index=$(($index + 1)) + index=$((index + 1)) # if [ -n "${!_STUB_DEBUG}" ]; then # echo "bats-mock: [idx $index, want ${!_STUB_INDEX}] $line" >&${!_STUB_DEBUG} @@ -60,10 +60,18 @@ while IFS= read -r line; do arguments=("$@") parsed_patterns=() - # xargs respects quoted substrings + # Check we can run xargs on the patterns. We use xargs to parse quoted substrings + # without having to use eval + if ! xargs_output="$(xargs printf '%b\0' <<< "${patterns}")" ; then + debug "xargs failed to parse ${patterns}" + eval "${_STUB_RESULT}"=2 + break + fi + + # Tokenize on null-bytes from xargs above while IFS= read -rd '' token; do parsed_patterns+=("$token") - done < <(xargs printf '%b\0' <<< "${patterns}") + done <<< "$xargs_output" # debug "patterns [${#parsed_patterns[@]}] = $(printf "'%q' " "${parsed_patterns[@]}")" # debug "arguments [${#arguments[@]}] = $(printf "'%q' " "${arguments[@]}")" @@ -81,6 +89,15 @@ while IFS= read -r line; do fi done + # Check if there are unmatched arguments + if [[ ${#arguments[@]} -gt ${#parsed_patterns[@]} ]] ; then + idx="${#parsed_patterns[@]}" + argument="${arguments[$idx]}" + debug "$(printf "unexpected argument '%q' at idx %d" "$argument" "$idx")" + result=2 + break + fi + # If the arguments matched, evaluate the command # in a subshell. Otherwise, log the failure. if [ $result -eq 0 ] ; then