More Bash 4.x compatibility issues found and fixed

This commit is contained in:
Tyler Akins 2023-05-12 08:17:57 -05:00
parent 6cc72acde0
commit 68306c4c6d
No known key found for this signature in database
GPG Key ID: 8F3B8C432F4393BD
4 changed files with 34 additions and 18 deletions

22
mo
View File

@ -1061,7 +1061,7 @@ mo::evaluate() {
;; ;;
*) *)
moStack=("${moStack[@]}" "$1" "$2") moStack=(${moStack[@]+"${moStack[@]}"} "$1" "$2")
;; ;;
esac esac
@ -1077,7 +1077,7 @@ mo::evaluate() {
else else
#: Concatenate #: Concatenate
mo::debug "Concatenating ${#moStack[@]} stack items" mo::debug "Concatenating ${#moStack[@]} stack items"
mo::evaluateListOfSingles moResult "${moStack[@]}" mo::evaluateListOfSingles moResult ${moStack[@]+"${moStack[@]}"}
fi fi
local "$moTarget" && mo::indirect "$moTarget" "$moResult" local "$moTarget" && mo::indirect "$moTarget" "$moResult"
@ -1310,18 +1310,20 @@ mo::evaluateFunction() {
if [[ -n "${MO_ALLOW_FUNCTION_ARGUMENTS-}" ]]; then if [[ -n "${MO_ALLOW_FUNCTION_ARGUMENTS-}" ]]; then
mo::debug "Function arguments are allowed" mo::debug "Function arguments are allowed"
if [[ ${#moArgs[@]} -gt 0 ]]; then
for moTemp in "${moArgs[@]}"; do for moTemp in "${moArgs[@]}"; do
mo::escape moTemp "$moTemp" mo::escape moTemp "$moTemp"
moFunctionCall="$moFunctionCall $moTemp" moFunctionCall="$moFunctionCall $moTemp"
done done
fi fi
fi
mo::debug "Calling function: $moFunctionCall" mo::debug "Calling function: $moFunctionCall"
#: Call the function in a subshell for safety. Employ the trick to preserve #: Call the function in a subshell for safety. Employ the trick to preserve
#: whitespace at the end of the output. #: whitespace at the end of the output.
moContent=$( moContent=$(
export MO_FUNCTION_ARGS=("${moArgs[@]}") export MO_FUNCTION_ARGS=(${moArgs[@]+"${moArgs[@]}"})
echo -n "$moContent" | eval "$moFunctionCall ; moFunctionResult=\$? ; echo -n '.' ; exit \"\$moFunctionResult\"" echo -n "$moContent" | eval "$moFunctionCall ; moFunctionResult=\$? ; echo -n '.' ; exit \"\$moFunctionResult\""
) || { ) || {
moFunctionResult=$? moFunctionResult=$?
@ -1767,7 +1769,7 @@ mo::tokenizeTagContents() {
"$moTerminator"*) "$moTerminator"*)
mo::debug "Found terminator" mo::debug "Found terminator"
local "$1" && mo::indirectArray "$1" "$moTokenCount" "${moResult[@]}" local "$1" && mo::indirectArray "$1" "$moTokenCount" ${moResult[@]+"${moResult[@]}"}
return return
;; ;;
@ -1775,7 +1777,7 @@ mo::tokenizeTagContents() {
#: Do not tokenize the open paren - treat this as RPL #: Do not tokenize the open paren - treat this as RPL
MO_UNPARSED=${MO_UNPARSED:1} MO_UNPARSED=${MO_UNPARSED:1}
mo::tokenizeTagContents moTemp ')' mo::tokenizeTagContents moTemp ')'
moResult=("${moResult[@]}" "${moTemp[@]:1}" PAREN "${moTemp[0]}") moResult=(${moResult[@]+"${moResult[@]}"} "${moTemp[@]:1}" PAREN "${moTemp[0]}")
MO_UNPARSED=${MO_UNPARSED:1} MO_UNPARSED=${MO_UNPARSED:1}
;; ;;
@ -1783,7 +1785,7 @@ mo::tokenizeTagContents() {
#: Do not tokenize the open brace - treat this as RPL #: Do not tokenize the open brace - treat this as RPL
MO_UNPARSED=${MO_UNPARSED:1} MO_UNPARSED=${MO_UNPARSED:1}
mo::tokenizeTagContents moTemp '}' mo::tokenizeTagContents moTemp '}'
moResult=("${moResult[@]}" "${moTemp[@]:1}" BRACE "${moTemp[0]}") moResult=(${moResult[@]+"${moResult[@]}"} "${moTemp[@]:1}" BRACE "${moTemp[0]}")
MO_UNPARSED=${MO_UNPARSED:1} MO_UNPARSED=${MO_UNPARSED:1}
;; ;;
@ -1793,17 +1795,17 @@ mo::tokenizeTagContents() {
"'"*) "'"*)
mo::tokenizeTagContentsSingleQuote moTemp mo::tokenizeTagContentsSingleQuote moTemp
moResult=("${moResult[@]}" "${moTemp[@]}") moResult=(${moResult[@]+"${moResult[@]}"} "${moTemp[@]}")
;; ;;
'"'*) '"'*)
mo::tokenizeTagContentsDoubleQuote moTemp mo::tokenizeTagContentsDoubleQuote moTemp
moResult=("${moResult[@]}" "${moTemp[@]}") moResult=(${moResult[@]+"${moResult[@]}"} "${moTemp[@]}")
;; ;;
*) *)
mo::tokenizeTagContentsName moTemp mo::tokenizeTagContentsName moTemp
moResult=("${moResult[@]}" "${moTemp[@]}") moResult=(${moResult[@]+"${moResult[@]}"} "${moTemp[@]}")
;; ;;
esac esac
@ -1947,7 +1949,7 @@ mo::tokenizeTagContentsSingleQuote() {
# Save the original command's path for usage later # Save the original command's path for usage later
MO_ORIGINAL_COMMAND="$(cd "${BASH_SOURCE[0]%/*}" || exit 1; pwd)/${BASH_SOURCE[0]##*/}" MO_ORIGINAL_COMMAND="$(cd "${BASH_SOURCE[0]%/*}" || exit 1; pwd)/${BASH_SOURCE[0]##*/}"
MO_VERSION="3.0.1" MO_VERSION="3.0.2"
# If sourced, load all functions. # If sourced, load all functions.
# If executed, perform the actions as expected. # If executed, perform the actions as expected.

View File

@ -3,7 +3,8 @@ cd "${0%/*}" || exit 1
. ../run-tests . ../run-tests
declare -A repo declare -A repo
repo[resque]="Resque" # The order of the array elements can be shuffled depending on the version of
# Bash. Keeping this to a minimal set and alphabetized seems to help.
repo[hub]="Hub" repo[hub]="Hub"
repo[rip]="Rip" repo[rip]="Rip"
export repo export repo
@ -18,7 +19,6 @@ expected() {
cat <<EOF cat <<EOF
<b>hub - Hub</b> <b>hub - Hub</b>
<b>rip - Rip</b> <b>rip - Rip</b>
<b>resque - Resque</b>
EOF EOF
} }

View File

@ -6,7 +6,21 @@ testArgs() {
local args local args
# shellcheck disable=SC2031 # shellcheck disable=SC2031
args=$(declare -p MO_FUNCTION_ARGS) args=$(declare -p MO_FUNCTION_ARGS)
echo -n "${args#*=}"
# The output from declare -p could look like these
# declare -a MO_FUNCTION_ARGS=([0]="one")
# declare -ax MO_FUNCTION_ARGS='([0]="one")'
# Trim leading declare statement and variable name
args="${args#*=}"
# If there are any quotes, remove them. The function arguments will always
# be an array.
if [[ "${args:0:1}" == "'" ]]; then
args=${args#\'}
args=${args%\'}
fi
echo -n "$args"
} }
template() { template() {
cat <<EOF cat <<EOF

View File

@ -93,7 +93,7 @@ This is open source! Please feel free to contribute.
https://github.com/tests-always-included/mo https://github.com/tests-always-included/mo
MO_VERSION=3.0.1 MO_VERSION=3.0.2
EOF EOF
} }