Pass shellcheck, more specs are handled, preserve function whitespace

This closes #49.
This commit is contained in:
Tyler Akins
2023-04-10 08:10:14 -05:00
parent e0e9189355
commit 7604ce3054
46 changed files with 308 additions and 273 deletions

View File

@@ -2,8 +2,8 @@
cd "${0%/*}" || exit 1
. ../run-tests
thing="Works"
template="{{&thing}}"
expected="Works"
export thing="Works"
export template="{{&thing}}"
export expected="Works"
runTest

View File

@@ -2,7 +2,7 @@
cd "${0%/*}" || exit 1
. ../run-tests
repo=( "resque" "hub" "rip" )
export repo=( "resque" "hub" "rip" )
template() {
cat <<EOF
{{#repo}}

View File

@@ -6,6 +6,7 @@ declare -A repo
repo[resque]="Resque"
repo[hub]="Hub"
repo[rip]="Rip"
export repo
template() {
cat <<EOF
{{#repo}}

View File

@@ -2,7 +2,7 @@
cd "${0%/*}" || exit 1
. ../run-tests
template="Wor{{!comment}}ks"
expected="Works"
export template="Wor{{!comment}}ks"
export expected="Works"
runTest

View File

@@ -10,10 +10,6 @@ run through multiple
lines}}.</h1>
EOF
}
expected() {
cat <<EOF
<h1>Today.</h1>
EOF
}
export expected=$'<h1>Today.</h1>\n'
runTest

View File

@@ -2,7 +2,7 @@
cd "${0%/*}" || exit 1
. ../run-tests
template="Wor{{! comment }}ks"
expected="Works"
export template="Wor{{! comment }}ks"
export expected="Works"
runTest

View File

@@ -2,9 +2,9 @@
cd "${0%/*}" || exit 1
. ../run-tests
thing="Wor"
thing2="ks"
template="{{thing thing2}}"
expected="Works"
export thing="Wor"
export thing2="ks"
export template="{{thing thing2}}"
export expected="Works"
runTest

View File

@@ -2,8 +2,8 @@
cd "${0%/*}" || exit 1
. ../run-tests
thing="Works"
template="{{=| |=}}|thing|"
expected="Works"
export thing="Works"
export template="{{=| |=}}|thing|"
export expected="Works"
runTest

View File

@@ -2,9 +2,9 @@
cd "${0%/*}" || exit 1
. ../run-tests
arguments=(-- --help)
returnCode=1
template=""
expected="cat: --help: No such file or directory"$'\n'
export arguments=(--fail-on-file -- --help)
export returnCode=1
export template=""
export expected=$'ERROR: No such file: --help\n'
runTest

View File

@@ -2,7 +2,7 @@
cd "${0%/*}" || exit 1
. ../run-tests
template='{{"Works"}}'
expected="Works"
export template='{{"Works"}}'
export expected="Works"
runTest

View File

@@ -3,10 +3,10 @@ cd "${0%/*}" || exit 1
. ../run-tests
unset __NO_SUCH_VAR
POPULATED="words"
EMPTY=""
arguments=(--fail-not-set)
returnCode=1
export POPULATED="words"
export EMPTY=""
export arguments=(--fail-not-set)
export returnCode=1
template() {
cat <<EOF
@@ -15,10 +15,6 @@ Empty: {{EMPTY}};
Unset: {{__NO_SUCH_VAR}};
EOF
}
expected() {
cat <<EOF
ERROR: Environment variable not set: __NO_SUCH_VAR
EOF
}
export expected=$'ERROR: Environment variable not set: __NO_SUCH_VAR\n'
runTest

View File

@@ -5,14 +5,9 @@ cd "${0%/*}" || exit 1
failFunction() {
false
}
arguments=(--fail-on-function)
returnCode=1
template="Fail on function? {{failFunction}}"
expected() {
cat <<EOF
ERROR: Function 'failFunction' with args () failed with status code 1
EOF
}
export arguments=(--fail-on-function)
export returnCode=1
export template="Fail on function? {{failFunction}}"
export expected=$'ERROR: Function failed with status code 1: "failFunction"\n'
runTest

View File

@@ -2,9 +2,9 @@
cd "${0%/*}" || exit 1
. ../run-tests
USER=j.doe
ADMIN=false
arguments=(--false)
export USER=j.doe
export ADMIN=false
export arguments=(--false)
template() {
cat <<EOF
The user {{USER}} exists.
@@ -13,10 +13,6 @@ WRONG - should not be an admin.
{{/ADMIN}}
EOF
}
expected() {
cat <<EOF
The user j.doe exists.
EOF
}
export expected=$'The user j.doe exists.\n'
runTest

View File

@@ -2,8 +2,8 @@
cd "${0%/*}" || exit 1
. ../run-tests
USER=j.doe
ADMIN=false
export USER=j.doe
export ADMIN=false
MO_FALSE_IS_EMPTY=yeppers
template() {
cat <<EOF
@@ -13,10 +13,6 @@ WRONG - should not be an admin.
{{/ADMIN}}
EOF
}
expected() {
cat <<EOF
The user j.doe exists.
EOF
}
export expected=$'The user j.doe exists.\n'
runTest

View File

@@ -2,7 +2,7 @@
cd "${0%/*}" || exit 1
. ../run-tests
person=""
export person=""
template() {
cat <<EOF
Shown.
@@ -11,10 +11,6 @@ Shown.
{{/person}}
EOF
}
expected() {
cat <<EOF
Shown.
EOF
}
export expected=$'Shown.\n'
runTest

View File

@@ -2,9 +2,10 @@
cd "${0%/*}" || exit 1
. ../run-tests
name=Willy
export name=Willy
wrapped() {
# This eats the newline in the content
# Wrapping 'cat' in a subshell eats the trailing whitespace
# The echo adds a newline, which is preserved.
echo "<b>$(cat)</b>"
}
template() {
@@ -15,10 +16,6 @@ template() {
... this is the last line.
EOF
}
expected() {
cat <<EOF
<b> Willy is awesome.</b>... this is the last line.
EOF
}
export expected=$'<b> Willy is awesome.</b>\n... this is the last line.\n'
runTest

View File

@@ -2,7 +2,7 @@
cd "${0%/*}" || exit 1
. ../run-tests
name=Willy
export name=Willy
MO_ALLOW_FUNCTION_ARGUMENTS=true
pipeTo() {

View File

@@ -3,8 +3,10 @@ cd "${0%/*}" || exit 1
. ../run-tests
testArgs() {
local args=$(declare -p MO_FUNCTION_ARGS)
echo "${args#*=}"
local args
# shellcheck disable=SC2031
args=$(declare -p MO_FUNCTION_ARGS)
echo -n "${args#*=}"
}
template() {
cat <<EOF

View File

@@ -2,8 +2,8 @@
cd "${0%/*}" || exit 1
. ../run-tests
STR=abc
DATA=(111 222)
export STR=abc
export DATA=(111 222)
template() {
cat <<EOF
Issue #7

View File

@@ -2,10 +2,9 @@
cd "${0%/*}" || exit 1
. ../run-tests
template=""
arguments=(--help)
export arguments=(--help)
expected() {
cat <<EOF
cat <<'EOF'
Mo is a mustache template rendering software written in bash. It inserts
environment variables into templates.
@@ -21,19 +20,59 @@ Simple usage:
Options:
--allow-function-arguments
Permit functions to be called with additional arguments. Otherwise,
the only way to get access to the arguments is to use the
MO_FUNCTION_ARGS environment variable.
-d, --debug
Enable debug logging to stderr.
-u, --fail-not-set
Fail upon expansion of an unset variable.
Fail upon expansion of an unset variable. Will silently ignore by
default. Alternately, set MO_FAIL_ON_UNSET to a non-empty value.
-x, --fail-on-function
Fail when a function returns a non-zero status code.
Fail when a function returns a non-zero status code instead of
silently ignoring it. Alternately, set MO_FAIL_ON_FUNCTION to a
non-empty value.
-f, --fail-on-file
Fail when a file (from command-line or partial) does not exist.
Alternately, set MO_FAIL_ON_FILE to a non-empty value.
-e, --false
Treat the string "false" as empty for conditionals.
Treat the string "false" as empty for conditionals. Alternately,
set MO_FALSE_IS_EMPTY to a non-empty value.
-h, --help
This message.
-s=FILE, --source=FILE
Load FILE into the environment before processing templates.
Can be used multiple times.
-d, --debug
Enable debug logging to stderr.
-- Indicate the end of options. All arguments after this will be
treated as filenames only. Use when filenames may start with
hyphens.
Mo uses the following environment variables:
MO_ALLOW_FUNCTION_ARGUMENTS - When set to a non-empty value, this allows
functions referenced in templates to receive additional options and
arguments.
MO_DEBUG - When set to a non-empty value, additional debug information is
written to stderr.
MO_FUNCTION_ARGS - Arguments passed to the function.
MO_FAIL_ON_FILE - If a filename from the command-line is missing or a
partial does not exist, abort with an error.
MO_FAIL_ON_FUNCTION - If a function returns a non-zero status code, abort
with an error.
MO_FAIL_ON_UNSET - When set to a non-empty value, expansion of an unset env
variable will be aborted with an error.
MO_FALSE_IS_EMPTY - When set to a non-empty value, the string "false" will
be treated as an empty value for the purposes of conditionals.
MO_ORIGINAL_COMMAND - Used to find the `mo` program in order to generate a
help message.
Mo is under a MIT style licence with an additional non-advertising clause.
See LICENSE.md for the full text.
This is open source! Please feel free to contribute.
https://github.com/tests-always-included/mo
MO_VERSION=3.0.0
EOF

View File

@@ -2,7 +2,7 @@
cd "${0%/*}" || exit 1
. ../run-tests
thisIsTrue=true
export thisIsTrue=true
template() {
cat <<EOF
With spacing

View File

@@ -2,8 +2,8 @@
cd "${0%/*}" || exit 1
. ../run-tests
boolean=true
template=$' | {{#boolean}} {{! Important Whitespace }}\n {{/boolean}} | \n'
expected=$' | \n | \n'
export boolean=true
export template=$' | {{#boolean}} {{! Important Whitespace }}\n {{/boolean}} | \n'
export expected=$' | \n | \n'
runTest

View File

@@ -2,14 +2,10 @@
cd "${0%/*}" || exit 1
. ../run-tests
person=""
template=""
returnCode=1
arguments=(--something)
expected() {
cat <<EOF
cat: --something: No such file or directory
EOF
}
export person=""
export template=""
export returnCode=1
export arguments=(--something)
export expected=$'ERROR: Unknown option: --something (See --help for options)\n'
runTest

View File

@@ -2,7 +2,7 @@
cd "${0%/*}" || exit 1
. ../run-tests
repo=()
export repo=()
template() {
cat <<EOF
{{#repo}}
@@ -13,10 +13,6 @@ template() {
{{/repo}}
EOF
}
expected() {
cat <<EOF
No repos :(
EOF
}
export expected=$' No repos :(\n'
runTest

View File

@@ -2,13 +2,15 @@
cd "${0%/*}" || exit 1
. ../run-tests
a=foo
b=wrong
export a=foo
export b=wrong
declare -A sec
sec=([b]="bar")
export sec
declare -A c
c=([d]="baz")
template="{{#sec}}{{a}} {{b}} {{c.d}}{{/sec}}"
expected="foo bar baz"
export c
export template="{{#sec}}{{a}} {{b}} {{c.d}}{{/sec}}"
export expected="foo bar baz"
runTest

View File

@@ -2,8 +2,8 @@
cd "${0%/*}" || exit 1
. ../run-tests
name="Chris"
company="<b>GitHub</b>"
export name="Chris"
export company="<b>GitHub</b>"
template() {
cat <<EOF
* .{{name}}.

View File

@@ -2,7 +2,7 @@
cd "${0%/*}" || exit 1
. ../run-tests
multilineData=$'line 1\nline 2'
export multilineData=$'line 1\nline 2'
template() {
cat <<EOF
Partial:
@@ -26,7 +26,10 @@ Indented:
line 1
line 2
EOF
# This one looks odd, but if you check the spec spec/specs/partials.yaml, name "Standalone Indentation" (mirrors "standalone-indentation" in tests/), then the spec clearly shows that the indentation is applied before rendering.
# This one looks odd, but if you check the spec spec/specs/partials.yaml,
# name "Standalone Indentation" (mirrors "standalone-indentation" in
# tests/), then the spec clearly shows that the indentation is applied
# before rendering.
}
runTest

View File

@@ -2,12 +2,12 @@
cd "${0%/*}" || exit 1
. ../run-tests
USER=jwerle
GENDER=male
THING=apple
COLOR=red
PERSON=tobi
ADJECTIVE=cool
export USER=jwerle
export GENDER=male
export THING=apple
export COLOR=red
export PERSON=tobi
export ADJECTIVE=cool
template() {
cat <<EOF
{{! this is a comment }}

View File

@@ -2,7 +2,7 @@
cd "${0%/*}" || exit 1
. ../run-tests
template="Works"
expected="Works"
export template="Works"
export expected="Works"
runTest

View File

@@ -2,7 +2,7 @@
cd "${0%/*}" || exit 1
. ../run-tests
names=( "Tyler" )
export names=( "Tyler" )
template() {
cat <<EOF
<h2>Names</h2>

9
tests/partial-bad-file Executable file
View File

@@ -0,0 +1,9 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
# This file intentionally does not exist
export template="{{>fixtures/partial-bad-file.partial}}"
export expected=""
runTest

View File

@@ -2,8 +2,9 @@
cd "${0%/*}" || exit 1
. ../run-tests
returnCode=1
person=""
export returnCode=1
export arguments=(--fail-on-file)
export person=""
template() {
cat <<EOF
Won't be there: {{> fixtures/partial-missing.partial}}
@@ -11,7 +12,7 @@ EOF
}
expected() {
cat <<EOF
cat: partial-missing.partial: No such file or directory
ERROR: No such file: partial-missing.partial
EOF
}

View File

@@ -2,7 +2,7 @@
cd "${0%/*}" || exit 1
. ../run-tests
template="{{'Works'}}"
expected="Works"
export template="{{'Works'}}"
export expected="Works"
runTest

View File

@@ -2,8 +2,8 @@
cd "${0%/*}" || exit 1
. ../run-tests
thing="Works"
template="{{thing}}"
expected="Works"
export thing="Works"
export template="{{thing}}"
export expected="Works"
runTest

View File

@@ -2,7 +2,7 @@
cd "${0%/*}" || exit 1
. ../run-tests
arguments=(--source=fixtures/source.vars)
export arguments=(--source=fixtures/source.vars)
template() {
cat <<EOF
{{VAR}}

View File

@@ -2,13 +2,9 @@
cd "${0%/*}" || exit 1
. ../run-tests
arguments=(--source=invalid)
returnCode=1
template="Do not display this"
expected() {
cat <<EOF
No such file: invalid
EOF
}
export arguments=(--source=invalid)
export returnCode=1
export template="Do not display this"
export expected=$'No such file: invalid\n'
runTest

View File

@@ -2,7 +2,7 @@
cd "${0%/*}" || exit 1
. ../run-tests
arguments=(--source=fixtures/source-multiple-1.vars --source=fixtures/source-multiple-2.vars)
export arguments=(--source=fixtures/source-multiple-1.vars --source=fixtures/source-multiple-2.vars)
template() {
cat <<EOF
A: {{A}}

View File

@@ -2,9 +2,9 @@
cd "${0%/*}" || exit 1
. ../run-tests
arguments=(--source=)
returnCode=1
template="Do not display this"
expected=$'No such file: \n'
export arguments=(--source=)
export returnCode=1
export template="Do not display this"
export expected=$'No such file: \n'
runTest

View File

@@ -2,7 +2,7 @@
cd "${0%/*}" || exit 1
. ../run-tests
content=$'<\n->'
export content=$'<\n->'
template() {
cat <<EOF
\

View File

@@ -2,8 +2,8 @@
cd "${0%/*}" || exit 1
. ../run-tests
thing="Works"
template="{{{thing}}}"
expected="Works"
export thing="Works"
export template="{{{thing}}}"
export expected="Works"
runTest

View File

@@ -2,10 +2,10 @@
cd "${0%/*}" || exit 1
. ../run-tests
NAME="Chris"
VALUE=10000
TAXED_VALUE=6000
IN_CA=true
export NAME="Chris"
export VALUE=10000
export TAXED_VALUE=6000
export IN_CA=true
template() {
cat <<EOF
Hello {{NAME}}

View File

@@ -3,8 +3,8 @@ cd "${0%/*}" || exit 1
. ../run-tests
declare -A a
a=()
template="o{{#a.b}}WRONG{{/a.b}}k"
expected="ok"
export a=()
export template="o{{#a.b}}WRONG{{/a.b}}k"
export expected="ok"
runTest

View File

@@ -2,8 +2,8 @@
cd "${0%/*}" || exit 1
. ../run-tests
foo=bar
template="{{#foo}}{{.}} is {{foo}}{{/foo}}"
expected="bar is bar"
export foo=bar
export template="{{#foo}}{{.}} is {{foo}}{{/foo}}"
export expected="bar is bar"
runTest