More bugfixes and shellcheck tweaks

This commit is contained in:
Tyler Akins 2023-04-10 11:39:47 -05:00
parent 7604ce3054
commit 3a58ee390e
No known key found for this signature in database
GPG Key ID: 8F3B8C432F4393BD
6 changed files with 47 additions and 20 deletions

View File

@ -3,7 +3,7 @@
cd "$(dirname "$0")" # Go to the script's directory cd "$(dirname "$0")" # Go to the script's directory
declare -A DATA declare -A DATA
DATA=([one]=111 [two]=222) export DATA=([one]=111 [two]=222)
. ../mo . ../mo
cat <<EOF | mo cat <<EOF | mo

View File

@ -1,6 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# #
# This embeds a template in the script without using strange `cat` syntax. # This embeds a template in the script without using strange `cat` syntax.
# shellcheck disable=SC1083 disable=SC1010 disable=SC1054 disable=SC1073 disable=SC1072 disable=SC1056 disable=SC1009
cd "$(dirname "$0")" # Go to the script's directory cd "$(dirname "$0")" # Go to the script's directory

View File

@ -10,20 +10,22 @@ export ARRAY=( AAA BBB CCC )
# Include an external template # Include an external template
INCLUDE() { INCLUDE() {
cat "$MO_FUNCTION_ARGS" # shellcheck disable=SC2031
cat "${MO_FUNCTION_ARGS[0]}"
} }
# Print section title # Print section title
TITLE() { TITLE() {
echo "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" echo "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"
echo "$MO_FUNCTION_ARGS" # shellcheck disable=SC2031
echo "${MO_FUNCTION_ARGS[0]}"
echo "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" echo "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"
} }
cat <<EOF | mo -u cat <<EOF | mo -u
{{TITLE Part 1}} {{TITLE 'Part 1'}}
{{INCLUDE function-args-part1}} {{INCLUDE function-args-part1}}
{{TITLE Part 2}} {{TITLE 'Part 2'}}
{{INCLUDE function-args-part2}} {{INCLUDE function-args-part2}}
EOF EOF

9
mo
View File

@ -154,7 +154,7 @@ mo() (
;; ;;
-*) -*)
mo::error "Unknown option: $arg (See --help for options)" mo::error "Unknown option: $arg (See --help for options)"
;; ;;
*) *)
@ -938,7 +938,7 @@ mo::parseDelimiter() {
mo::chomp moOpen "$moContent" mo::chomp moOpen "$moContent"
moContent=${moContent:${#moOpen}} moContent=${moContent:${#moOpen}}
mo::trim moContent "$moContent" mo::trim moContent "$moContent"
moClose="${moContent%%="$moCloseDelimiter"*}" mo::chomp moClose "${moContent%%="$moCloseDelimiter"*}"
moContent=${moContent#*="$moCloseDelimiter"} moContent=${moContent#*="$moCloseDelimiter"}
mo::debug "Parsing delimiters: $moOpen $moClose" mo::debug "Parsing delimiters: $moOpen $moClose"
@ -1237,6 +1237,7 @@ mo::getArgumentSingleQuote() {
# #
# $1 - Destination variable name, an array with two elements # $1 - Destination variable name, an array with two elements
# $2 - Content # $2 - Content
# $3 - Closing delimiter
# #
# The array has the following elements. # The array has the following elements.
# [0] = argument type, "NAME" or "VALUE" # [0] = argument type, "NAME" or "VALUE"
@ -1536,7 +1537,7 @@ mo::evaluateVariable() {
moCurrent=$3 moCurrent=$3
moResult="" moResult=""
mo::findVariableName moNameParts "$moArg" "$moCurrent" mo::findVariableName moNameParts "$moArg" "$moCurrent"
mo::debug "Evaluate variable ($moArg + $moCurrent): ${moNameParts[*]}" mo::debug "Evaluate variable ($moArg, $moCurrent): ${moNameParts[*]}"
if [[ -z "${moNameParts[1]}" ]]; then if [[ -z "${moNameParts[1]}" ]]; then
if mo::isArray "$moArg"; then if mo::isArray "$moArg"; then
@ -1620,7 +1621,7 @@ mo::findVariableName() {
# $3-$* - Elements to join # $3-$* - Elements to join
# #
# Returns nothing. # Returns nothing.
moJoin() { mo::join() {
local joiner part result target local joiner part result target
target=$1 target=$1

View File

@ -26,6 +26,33 @@ const testOverrides = {
"Lambdas -> Escaping": { "Lambdas -> Escaping": {
skip: "HTML escaping is not supported" skip: "HTML escaping is not supported"
}, },
"Lambdas -> Inverted Section": {
// This one passed mostly by accident. Correcting so the test still
// tests what is was designed to illustrate.
data: {
static: "static",
lambda: {
__tag__: 'code',
bash: 'false'
}
}
},
"Lambdas -> Section - Alternate Delimiters": {
data: {
lambda: {
__tag__: 'code',
bash: 'content=$(cat); echo -n "$content{{planet}}=>|planet|$content"'
}
}
},
"Lambdas -> Section - Multiple Calls": {
data: {
lambda: {
__tag__: 'code',
bash: 'echo -n "__$(cat)__"'
}
}
},
"Partials -> Recursion": { "Partials -> Recursion": {
skip: "Complex objects are not supported and context is reset to the global level, so the recursion will loop forever" skip: "Complex objects are not supported and context is reset to the global level, so the recursion will loop forever"
}, },
@ -158,14 +185,8 @@ function addToEnvironmentObject(name, value) {
return `#${name} is null`; return `#${name} is null`;
} }
// Sometimes the __tag__ property of the code in the lambdas may be if (value.__tag__ === "code") {
// missing. Compensate by detecting commonly defined languages. return `${name}() { ${value.bash || 'echo "NO BASH VERSION OF CODE"'}; }`;
if (value.__tag__ === "code" || (value.ruby && value.php && value.perl)) {
if (value.bash) {
return `${name}() { ${value.bash}; }`;
}
return `${name}() { perl -e 'print ((${value.perl})->("'"$1"'"))'; }`;
} }
return addToEnvironmentObjectConvertedToAssociativeArray(name, value); return addToEnvironmentObjectConvertedToAssociativeArray(name, value);
@ -288,12 +309,12 @@ function runTest(testSet, test) {
test.script = buildScript(test); test.script = buildScript(test);
if (test.skip) { if (test.skip) {
debug("Skipping test:", testSet.fullName, `$(${test.skip})`); debug("Skipping test:", test.fullName, `(${test.skip})`);
return Promise.resolve(); return Promise.resolve();
} }
debug("Running test:", testSet.fullName); debug("Running test:", test.fullName);
return setupEnvironment(test) return setupEnvironment(test)
.then(() => executeScript(test)) .then(() => executeScript(test))
@ -303,6 +324,8 @@ function runTest(testSet, test) {
if (test.isFailure) { if (test.isFailure) {
showFailureDetails(test); showFailureDetails(test);
} else {
debug('Test pass:', test.fullName);
} }
}); });
} }

View File

@ -6,6 +6,6 @@ export person=""
export template="" export template=""
export returnCode=1 export returnCode=1
export arguments=(--something) export arguments=(--something)
export expected=$'ERROR: Unknown option: --something (See --help for options)\n' export expected=$'ERROR: Unknown option: --something (See --help for options)\n'
runTest runTest