mirror of
https://github.com/tests-always-included/mo.git
synced 2024-12-18 16:27:52 +00:00
Attempting to address shortcomings and whitespace issues
This commit is contained in:
parent
8e9fd680d4
commit
febd3467c8
18
README.md
18
README.md
@ -189,6 +189,21 @@ myfunc() {
|
||||
```
|
||||
|
||||
|
||||
Environment Variables and Functions
|
||||
-----------------------------------
|
||||
|
||||
There are several functions and variables used to process templates. `mo` reserves variables that start with `MO_` for variables exposing data or configuration, functions starting with `mo::`, and local variables starting with `mo[A-Z]`. You are welcome to use internal functions, though only ones that are marked as "Public" should not change their interface. Scripts may also read any of the variables.
|
||||
|
||||
* `MO_ALLOW_FUNCTION_ARGUMENTS` - When set to a non-empty value, this allows functions referenced in templates to receive additional options and arguments. This puts the content from the template directly into an eval statement. Use with extreme care.
|
||||
* `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_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_VERSION` - Version of `mo`.
|
||||
|
||||
|
||||
Concessions
|
||||
-----------
|
||||
|
||||
@ -200,9 +215,8 @@ Pull requests to solve the following issues would be helpful.
|
||||
### Mustache Syntax
|
||||
|
||||
* Dotted names are supported but only for associative arrays (Bash 4). See [`demo/associative-arrays`](demo/associative-arrays) for an example.
|
||||
* There's no "top level" object, so `echo '{.}' | ./mo` does not do anything useful. In other languages you can say the data for the template is a string and in `mo` the data is always the environment. Luckily this type of usage is rare and `{.}` works great when iterating over an array.
|
||||
* There's no "top level" object, so `echo '{{.}}' | ./mo` does not do anything useful. In other languages you can say the data for the template is a string and in `mo` the data is always the environment. Luckily this type of usage is rare and `{{.}}` works great when iterating over an array.
|
||||
* HTML encoding is not built into `mo`. `{{{var}}}`, `{{&var}}` and `{{var}}` all do the same thing. `echo '{{TEST}}' | TEST='<b>' mo` will give you "`<b>`" instead of "`>b<`".
|
||||
* You can not change the delimiters.
|
||||
|
||||
|
||||
### General Scripting Issues
|
||||
|
137
run-tests
137
run-tests
@ -1,45 +1,116 @@
|
||||
#!/usr/bin/env bash
|
||||
testCase() {
|
||||
echo "Input: $1"
|
||||
echo "Expected: $2"
|
||||
}
|
||||
|
||||
cd "${0%/*}" || exit 1
|
||||
indirect() {
|
||||
unset -v "$1"
|
||||
printf -v "$1" '%s' "$2"
|
||||
}
|
||||
|
||||
# shellcheck disable=SC1091
|
||||
. ./mo
|
||||
PASS=0
|
||||
FAIL=0
|
||||
getValue() {
|
||||
local name temp len hardSpace
|
||||
|
||||
for TEST in tests/*.expected; do
|
||||
export BASE="${TEST%.expected}"
|
||||
export MO_FALSE_IS_EMPTY=
|
||||
name=$2
|
||||
hardSpace=" "
|
||||
|
||||
echo -n "$BASE ... "
|
||||
if declare -f "$name" &> /dev/null; then
|
||||
temp=$("$name"; echo -n "$hardSpace")
|
||||
len=$((${#temp} - 1))
|
||||
|
||||
(
|
||||
if [[ -f "${BASE}.sh" ]]; then
|
||||
# Run a shell script if one exists
|
||||
"${BASE}.sh"
|
||||
else
|
||||
# Fall back to using .env and .template
|
||||
# shellcheck disable=SC1090
|
||||
. "${BASE}.env"
|
||||
echo "Do not read this input" | mo "${BASE}.template"
|
||||
if [[ "${temp:$len}" == "$hardSpace" ]]; then
|
||||
temp=${temp:0:$len}
|
||||
fi
|
||||
) | diff -U5 - "${TEST}" > "${BASE}.diff"
|
||||
|
||||
statusCode=$?
|
||||
|
||||
if [[ $statusCode -ne 0 ]]; then
|
||||
echo "FAIL (status code $statusCode)"
|
||||
FAIL=$(( FAIL + 1 ))
|
||||
else
|
||||
echo "ok"
|
||||
PASS=$(( PASS + 1 ))
|
||||
rm "${BASE}.diff"
|
||||
temp=${!name}
|
||||
fi
|
||||
done
|
||||
|
||||
echo ""
|
||||
echo "Pass: $PASS"
|
||||
echo "Fail: $FAIL"
|
||||
if [[ $FAIL -gt 0 ]]; then
|
||||
local "$1" && indirect "$1" "$temp"
|
||||
}
|
||||
|
||||
runTest() (
|
||||
local testTemplate testExpected testActual hardSpace len testReturnCode testFail
|
||||
|
||||
hardSpace=" "
|
||||
. ../mo
|
||||
|
||||
getValue testTemplate template
|
||||
getValue testExpected expected
|
||||
|
||||
testActual=$(echo -n "$testTemplate" | mo ${arguments[@]+"${arguments[@]}"} 2>&1; echo -n "$hardSpace$?")
|
||||
testReturnCode=${testActual##*$hardSpace}
|
||||
testActual=${testActual%$hardSpace*}
|
||||
testFail=false
|
||||
|
||||
if [[ "$testActual" != "$testExpected" ]]; then
|
||||
echo "Failure"
|
||||
echo "Expected:"
|
||||
echo "$testExpected"
|
||||
echo "Actual:"
|
||||
echo "$testActual"
|
||||
|
||||
if [[ -n "${MO_DEBUG-}" ]]; then
|
||||
declare -p testExpected
|
||||
declare -p testActual
|
||||
fi
|
||||
|
||||
testFail=true
|
||||
fi
|
||||
|
||||
if [[ "$testReturnCode" != "$returnCode" ]]; then
|
||||
echo "Expected return code $returnCode, but got $testReturnCode"
|
||||
testFail=true
|
||||
fi
|
||||
|
||||
if [[ "$testFail" == "true" ]]; then
|
||||
return 1
|
||||
fi
|
||||
|
||||
return 0
|
||||
)
|
||||
|
||||
runTestFile() (
|
||||
local file=$1
|
||||
|
||||
echo "Test: $file"
|
||||
"$file"
|
||||
)
|
||||
|
||||
runTests() (
|
||||
PASS=0
|
||||
FAIL=0
|
||||
|
||||
if [[ $# -gt 0 ]]; then
|
||||
for TEST in "$@"; do
|
||||
runTestFile "$TEST" && PASS=$((PASS + 1)) || FAIL=$((FAIL + 1))
|
||||
done
|
||||
else
|
||||
cd "${0%/*}"
|
||||
for TEST in tests/*; do
|
||||
if [[ -f "$TEST" ]]; then
|
||||
runTestFile "$TEST" && PASS=$((PASS + 1)) || FAIL=$((FAIL + 1))
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "Pass: $PASS"
|
||||
echo "Fail: $FAIL"
|
||||
|
||||
if [[ $FAIL -gt 0 ]]; then
|
||||
exit 1
|
||||
fi
|
||||
)
|
||||
|
||||
# Clear test related variables
|
||||
template="Template not defined"
|
||||
expected="Expected not defined"
|
||||
returnCode=0
|
||||
arguments=()
|
||||
|
||||
# If sourced, load functions.
|
||||
# If executed, perform the actions as expected.
|
||||
if [[ "$0" == "${BASH_SOURCE[0]}" ]] || [[ -z "${BASH_SOURCE[0]}" ]]; then
|
||||
runTests ${@+"${@}"}
|
||||
fi
|
||||
|
9
tests/ampersand
Executable file
9
tests/ampersand
Executable file
@ -0,0 +1,9 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
thing="Works"
|
||||
template="{{&thing}}"
|
||||
expected="Works"
|
||||
|
||||
runTest
|
21
tests/array
Executable file
21
tests/array
Executable file
@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
repo=( "resque" "hub" "rip" )
|
||||
template() {
|
||||
cat <<EOF
|
||||
{{#repo}}
|
||||
<b>{{@key}} - {{.}}</b>
|
||||
{{/repo}}
|
||||
EOF
|
||||
}
|
||||
expected() {
|
||||
cat <<EOF
|
||||
<b>0 - resque</b>
|
||||
<b>1 - hub</b>
|
||||
<b>2 - rip</b>
|
||||
EOF
|
||||
}
|
||||
|
||||
runTest
|
@ -1 +0,0 @@
|
||||
repo=( "resque" "hub" "rip" )
|
@ -1,3 +0,0 @@
|
||||
<b>0 - resque</b>
|
||||
<b>1 - hub</b>
|
||||
<b>2 - rip</b>
|
@ -1,3 +0,0 @@
|
||||
{{#repo}}
|
||||
<b>{{@key}} - {{.}}</b>
|
||||
{{/repo}}
|
24
tests/assoc-array
Executable file
24
tests/assoc-array
Executable file
@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
declare -A repo
|
||||
repo[resque]="Resque"
|
||||
repo[hub]="Hub"
|
||||
repo[rip]="Rip"
|
||||
template() {
|
||||
cat <<EOF
|
||||
{{#repo}}
|
||||
<b>{{@key}} - {{.}}</b>
|
||||
{{/repo}}
|
||||
EOF
|
||||
}
|
||||
expected() {
|
||||
cat <<EOF
|
||||
<b>hub - Hub</b>
|
||||
<b>rip - Rip</b>
|
||||
<b>resque - Resque</b>
|
||||
EOF
|
||||
}
|
||||
|
||||
runTest
|
@ -1,4 +0,0 @@
|
||||
declare -A repo
|
||||
repo[resque]="Resque"
|
||||
repo[hub]="Hub"
|
||||
repo[rip]="Rip"
|
@ -1,3 +0,0 @@
|
||||
<b>hub - Hub</b>
|
||||
<b>rip - Rip</b>
|
||||
<b>resque - Resque</b>
|
@ -1,3 +0,0 @@
|
||||
{{#repo}}
|
||||
<b>{{@key}} - {{.}}</b>
|
||||
{{/repo}}
|
8
tests/comment
Executable file
8
tests/comment
Executable file
@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
template="Wor{{!comment}}ks"
|
||||
expected="Works"
|
||||
|
||||
runTest
|
19
tests/comment-newline
Executable file
19
tests/comment-newline
Executable file
@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
template() {
|
||||
cat <<EOF
|
||||
<h1>Today{{! ignore me
|
||||
and this can
|
||||
run through multiple
|
||||
lines}}.</h1>
|
||||
EOF
|
||||
}
|
||||
expected() {
|
||||
cat <<EOF
|
||||
<h1>Today.</h1>
|
||||
EOF
|
||||
}
|
||||
|
||||
runTest
|
@ -1 +0,0 @@
|
||||
<h1>Today.</h1>
|
@ -1,4 +0,0 @@
|
||||
<h1>Today{{! ignore me
|
||||
and this can
|
||||
run through multiple
|
||||
lines}}.</h1>
|
8
tests/comment-with-spaces
Executable file
8
tests/comment-with-spaces
Executable file
@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
template="Wor{{! comment }}ks"
|
||||
expected="Works"
|
||||
|
||||
runTest
|
@ -1 +0,0 @@
|
||||
<h1>Today.</h1>
|
@ -1 +0,0 @@
|
||||
<h1>Today{{! ignore me }}.</h1>
|
10
tests/concatenated-variables
Executable file
10
tests/concatenated-variables
Executable file
@ -0,0 +1,10 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
thing="Wor"
|
||||
thing2="ks"
|
||||
template="{{thing thing2}}"
|
||||
expected="Works"
|
||||
|
||||
runTest
|
9
tests/delimiters
Executable file
9
tests/delimiters
Executable file
@ -0,0 +1,9 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
thing="Works"
|
||||
template="{{=| |=}}|thing|"
|
||||
expected="Works"
|
||||
|
||||
runTest
|
9
tests/double-hyphen
Executable file
9
tests/double-hyphen
Executable file
@ -0,0 +1,9 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
arguments=(-- --help)
|
||||
template=""
|
||||
expected="cat: --help: No such file or directory"$'\n'
|
||||
|
||||
runTest
|
@ -1 +0,0 @@
|
||||
cat: --help: No such file or directory
|
@ -1,5 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# This should display a message indicating that the file --help
|
||||
# could not be found. It should not display a help messsage.
|
||||
cd "${0%/*}" || exit 1
|
||||
../mo -- --help 2>&1
|
8
tests/double-quote
Executable file
8
tests/double-quote
Executable file
@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
template='{{"Works"}}'
|
||||
expected="Works"
|
||||
|
||||
runTest
|
24
tests/fail-not-set
Executable file
24
tests/fail-not-set
Executable file
@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
unset __NO_SUCH_VAR
|
||||
POPULATED="words"
|
||||
EMPTY=""
|
||||
arguments=(--fail-not-set)
|
||||
returnCode=1
|
||||
|
||||
template() {
|
||||
cat <<EOF
|
||||
Populated: {{POPULATED}};
|
||||
Empty: {{EMPTY}};
|
||||
Unset: {{__NO_SUCH_VAR}};
|
||||
EOF
|
||||
}
|
||||
expected() {
|
||||
cat <<EOF
|
||||
ERROR: Environment variable not set: __NO_SUCH_VAR
|
||||
EOF
|
||||
}
|
||||
|
||||
runTest
|
@ -1 +0,0 @@
|
||||
Env variable not set: __NO_SUCH_VAR
|
@ -1,9 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
cd "${0%/*}" || exit 1
|
||||
unset __NO_SUCH_VAR
|
||||
POPULATED="words" EMPTY="" ../mo --fail-not-set ./fail-not-set-file.template 2>&1
|
||||
|
||||
if [[ $? -ne 1 ]]; then
|
||||
echo "Did not return 1"
|
||||
fi
|
@ -1,3 +0,0 @@
|
||||
Populated: {{POPULATED}};
|
||||
Empty: {{EMPTY}};
|
||||
Unset: {{__NO_SUCH_VAR}};
|
@ -1,3 +0,0 @@
|
||||
Populated: words;
|
||||
Empty: ;
|
||||
Unset: Env variable not set: __NO_SUCH_VAR
|
@ -1,13 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
cd "${0%/*}" || exit 1
|
||||
unset __NO_SUCH_VAR
|
||||
POPULATED="words" EMPTY="" ../mo --fail-not-set 2>&1 <<EOF
|
||||
Populated: {{POPULATED}};
|
||||
Empty: {{EMPTY}};
|
||||
Unset: {{__NO_SUCH_VAR}};
|
||||
EOF
|
||||
|
||||
if [[ $? -ne 1 ]]; then
|
||||
echo "Did not return 1"
|
||||
fi
|
18
tests/fail-on-function
Executable file
18
tests/fail-on-function
Executable file
@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
runTest
|
@ -1 +0,0 @@
|
||||
Fail on function? Function 'failFunction' with args () failed with status code 1
|
@ -1,18 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
cd "${0%/*}" || exit 1
|
||||
|
||||
failFunction() {
|
||||
false
|
||||
}
|
||||
|
||||
# Must be sourced to use functions
|
||||
# shellcheck disable=SC1091
|
||||
. ../mo
|
||||
mo --fail-on-function 2>&1 <<EOF
|
||||
Fail on function? {{failFunction}}
|
||||
EOF
|
||||
|
||||
if [[ $? -ne 1 ]]; then
|
||||
echo "Did not return 1"
|
||||
fi
|
22
tests/false-is-empty-arg
Executable file
22
tests/false-is-empty-arg
Executable file
@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
USER=j.doe
|
||||
ADMIN=false
|
||||
arguments=(--false)
|
||||
template() {
|
||||
cat <<EOF
|
||||
The user {{USER}} exists.
|
||||
{{#ADMIN}}
|
||||
WRONG - should not be an admin.
|
||||
{{/ADMIN}}
|
||||
EOF
|
||||
}
|
||||
expected() {
|
||||
cat <<EOF
|
||||
The user j.doe exists.
|
||||
EOF
|
||||
}
|
||||
|
||||
runTest
|
@ -1 +0,0 @@
|
||||
The user j.doe exists.
|
@ -1,4 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
cd "${0%/*}" || exit 1
|
||||
USER=j.doe ADMIN=false ../mo --false false-is-empty-arg.template
|
@ -1,4 +0,0 @@
|
||||
The user {{USER}} exists.
|
||||
{{#ADMIN}}
|
||||
WRONG - should not be an admin.
|
||||
{{/ADMIN}}
|
22
tests/false-is-empty-env
Executable file
22
tests/false-is-empty-env
Executable file
@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
USER=j.doe
|
||||
ADMIN=false
|
||||
MO_FALSE_IS_EMPTY=yeppers
|
||||
template() {
|
||||
cat <<EOF
|
||||
The user {{USER}} exists.
|
||||
{{#ADMIN}}
|
||||
WRONG - should not be an admin.
|
||||
{{/ADMIN}}
|
||||
EOF
|
||||
}
|
||||
expected() {
|
||||
cat <<EOF
|
||||
The user j.doe exists.
|
||||
EOF
|
||||
}
|
||||
|
||||
runTest
|
@ -1,2 +0,0 @@
|
||||
MO_FALSE_IS_EMPTY=yeppers
|
||||
someFalseValue=false
|
@ -1 +0,0 @@
|
||||
Works
|
@ -1,4 +0,0 @@
|
||||
Works
|
||||
{{#someFalseValue}}
|
||||
Never shown!
|
||||
{{/someFalseValue}}
|
20
tests/false-list
Executable file
20
tests/false-list
Executable file
@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
person=""
|
||||
template() {
|
||||
cat <<EOF
|
||||
Shown.
|
||||
{{#person}}
|
||||
Never shown!
|
||||
{{/person}}
|
||||
EOF
|
||||
}
|
||||
expected() {
|
||||
cat <<EOF
|
||||
Shown.
|
||||
EOF
|
||||
}
|
||||
|
||||
runTest
|
@ -1 +0,0 @@
|
||||
person=""
|
@ -1 +0,0 @@
|
||||
Shown.
|
@ -1,4 +0,0 @@
|
||||
Shown.
|
||||
{{#person}}
|
||||
Never shown!
|
||||
{{/person}}
|
24
tests/function
Executable file
24
tests/function
Executable file
@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
name=Willy
|
||||
wrapped() {
|
||||
# This eats the newline in the content
|
||||
echo "<b>$(cat)</b>"
|
||||
}
|
||||
template() {
|
||||
cat <<EOF
|
||||
{{#wrapped}}
|
||||
{{name}} is awesome.
|
||||
{{/wrapped}}
|
||||
... this is the last line.
|
||||
EOF
|
||||
}
|
||||
expected() {
|
||||
cat <<EOF
|
||||
<b> Willy is awesome.</b>... this is the last line.
|
||||
EOF
|
||||
}
|
||||
|
||||
runTest
|
35
tests/function-args
Executable file
35
tests/function-args
Executable file
@ -0,0 +1,35 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
name=Willy
|
||||
MO_ALLOW_FUNCTION_ARGUMENTS=true
|
||||
|
||||
pipeTo() {
|
||||
cat | "$1"
|
||||
}
|
||||
|
||||
testArgs() {
|
||||
printf "%d" "$#"
|
||||
|
||||
# Display all arguments
|
||||
printf " %q" ${@+"$@"}
|
||||
}
|
||||
template() {
|
||||
cat <<EOF
|
||||
No args: {{testArgs}} - done
|
||||
One arg: {{testArgs one}} - done
|
||||
Getting name in a string: {{testArgs "The name is $name"}} - done
|
||||
Reverse this: {{#pipeTo rev}}abcde{{/pipeTo}}
|
||||
EOF
|
||||
}
|
||||
expected() {
|
||||
cat <<EOF
|
||||
No args: 0 '' - done
|
||||
One arg: 1 one - done
|
||||
Getting name in a string: 1 The\ name\ is\ Willy - done
|
||||
Reverse this: edcba
|
||||
EOF
|
||||
}
|
||||
|
||||
runTest
|
25
tests/function-args-read
Executable file
25
tests/function-args-read
Executable file
@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
testArgs() {
|
||||
echo "$MO_FUNCTION_ARGS"
|
||||
}
|
||||
template() {
|
||||
cat <<EOF
|
||||
No args: [{{testArgs}}] - done
|
||||
One arg: [{{testArgs one}}] - done
|
||||
Multiple arguments: [{{testArgs aa bb cc 'x' " ! {[_.| }}] - done
|
||||
Evil: [{{testArgs bla; cat /etc/issue}}] - done
|
||||
EOF
|
||||
}
|
||||
expected() {
|
||||
cat <<EOF
|
||||
No args: [] - done
|
||||
One arg: [one] - done
|
||||
Multiple arguments: [aa bb cc 'x' " ! {[_.|] - done
|
||||
Evil: [bla; cat /etc/issue] - done
|
||||
EOF
|
||||
}
|
||||
|
||||
runTest
|
@ -1,3 +0,0 @@
|
||||
testArgs() {
|
||||
echo "$MO_FUNCTION_ARGS"
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
No args: [] - done
|
||||
One arg: [one] - done
|
||||
Multiple arguments: [aa bb cc 'x' " ! {[_.|] - done
|
||||
Evil: [bla; cat /etc/issue] - done
|
@ -1,4 +0,0 @@
|
||||
No args: [{{testArgs}}] - done
|
||||
One arg: [{{testArgs one}}] - done
|
||||
Multiple arguments: [{{testArgs aa bb cc 'x' " ! {[_.| }}] - done
|
||||
Evil: [{{testArgs bla; cat /etc/issue}}] - done
|
@ -1,13 +0,0 @@
|
||||
name=Willy
|
||||
MO_ALLOW_FUNCTION_ARGUMENTS=true
|
||||
|
||||
pipeTo() {
|
||||
cat | "$1"
|
||||
}
|
||||
|
||||
testArgs() {
|
||||
printf "%d" "$#"
|
||||
|
||||
# Display all arguments
|
||||
printf " %q" ${@+"$@"}
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
No args: 0 '' - done
|
||||
One arg: 1 one - done
|
||||
Getting name in a string: 1 The\ name\ is\ Willy - done
|
||||
Reverse this: edcba
|
@ -1,4 +0,0 @@
|
||||
No args: {{testArgs}} - done
|
||||
One arg: {{testArgs one}} - done
|
||||
Getting name in a string: {{testArgs "The name is $name"}} - done
|
||||
Reverse this: {{#pipeTo rev}}abcde{{/pipeTo}}
|
@ -1,5 +0,0 @@
|
||||
name=Willy
|
||||
wrapped() {
|
||||
# This eats the newline in the content
|
||||
echo "<b>$(cat)</b>"
|
||||
}
|
@ -1 +0,0 @@
|
||||
<b> Willy is awesome.</b>... this is the last line.
|
@ -1,4 +0,0 @@
|
||||
{{#wrapped}}
|
||||
{{name}} is awesome.
|
||||
{{/wrapped}}
|
||||
... this is the last line.
|
28
tests/globals-in-loop
Executable file
28
tests/globals-in-loop
Executable file
@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
STR=abc
|
||||
DATA=(111 222)
|
||||
template() {
|
||||
cat <<EOF
|
||||
Issue #7
|
||||
{{STR}}
|
||||
{{#DATA}}
|
||||
Item: {{.}}
|
||||
String: {{STR}}
|
||||
{{/DATA}}
|
||||
EOF
|
||||
}
|
||||
expected() {
|
||||
cat <<EOF
|
||||
Issue #7
|
||||
abc
|
||||
Item: 111
|
||||
String: abc
|
||||
Item: 222
|
||||
String: abc
|
||||
EOF
|
||||
}
|
||||
|
||||
runTest
|
@ -1,2 +0,0 @@
|
||||
STR=abc
|
||||
DATA=(111 222)
|
@ -1,6 +0,0 @@
|
||||
Issue #7
|
||||
abc
|
||||
Item: 111
|
||||
String: abc
|
||||
Item: 222
|
||||
String: abc
|
@ -1,6 +0,0 @@
|
||||
Issue #7
|
||||
{{STR}}
|
||||
{{#DATA}}
|
||||
Item: {{.}}
|
||||
String: {{STR}}
|
||||
{{/DATA}}
|
14
tests/help.expected → tests/help
Normal file → Executable file
14
tests/help.expected → tests/help
Normal file → Executable file
@ -1,3 +1,11 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
template=""
|
||||
arguments=(--help)
|
||||
expected() {
|
||||
cat <<EOF
|
||||
Mo is a mustache template rendering software written in bash. It inserts
|
||||
environment variables into templates.
|
||||
|
||||
@ -25,4 +33,8 @@ Options:
|
||||
Load FILE into the environment before processing templates.
|
||||
Can be used multiple times.
|
||||
|
||||
MO_VERSION=2.4.1
|
||||
MO_VERSION=3.0.0
|
||||
EOF
|
||||
}
|
||||
|
||||
runTest
|
@ -1,4 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
cd "${0%/*}" || exit 1
|
||||
../mo --help
|
56
tests/indented-partials
Executable file
56
tests/indented-partials
Executable file
@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
thisIsTrue=true
|
||||
template() {
|
||||
cat <<EOF
|
||||
With spacing
|
||||
{{> fixtures/indented-partials.partial}}
|
||||
|
||||
{{> fixtures/indented-partials.partial}}
|
||||
|
||||
Without spacing
|
||||
{{> fixtures/indented-partials.partial}}
|
||||
{{> fixtures/indented-partials.partial}}
|
||||
|
||||
With text
|
||||
{{> fixtures/indented-partials.partial}}
|
||||
text
|
||||
{{> fixtures/indented-partials.partial}}
|
||||
|
||||
In a conditional
|
||||
{{#thisIsTrue}}
|
||||
{{> fixtures/indented-partials.partial}}
|
||||
{{/thisIsTrue}}
|
||||
EOF
|
||||
}
|
||||
expected() {
|
||||
cat <<EOF
|
||||
With spacing
|
||||
first line
|
||||
second line
|
||||
|
||||
first line
|
||||
second line
|
||||
|
||||
Without spacing
|
||||
first line
|
||||
second line
|
||||
first line
|
||||
second line
|
||||
|
||||
With text
|
||||
first line
|
||||
second line
|
||||
text
|
||||
first line
|
||||
second line
|
||||
|
||||
In a conditional
|
||||
first line
|
||||
second line
|
||||
EOF
|
||||
}
|
||||
|
||||
runTest
|
@ -1 +0,0 @@
|
||||
thisIsTrue=true
|
@ -1,23 +0,0 @@
|
||||
With spacing
|
||||
first line
|
||||
second line
|
||||
|
||||
first line
|
||||
second line
|
||||
|
||||
Without spacing
|
||||
first line
|
||||
second line
|
||||
first line
|
||||
second line
|
||||
|
||||
With text
|
||||
first line
|
||||
second line
|
||||
text
|
||||
first line
|
||||
second line
|
||||
|
||||
In a conditional
|
||||
first line
|
||||
second line
|
@ -1,18 +0,0 @@
|
||||
With spacing
|
||||
{{> indented-partials.partial}}
|
||||
|
||||
{{> indented-partials.partial}}
|
||||
|
||||
Without spacing
|
||||
{{> indented-partials.partial}}
|
||||
{{> indented-partials.partial}}
|
||||
|
||||
With text
|
||||
{{> indented-partials.partial}}
|
||||
text
|
||||
{{> indented-partials.partial}}
|
||||
|
||||
In a conditional
|
||||
{{#thisIsTrue}}
|
||||
{{> indented-partials.partial}}
|
||||
{{/thisIsTrue}}
|
14
tests/invalid-option
Executable file
14
tests/invalid-option
Executable file
@ -0,0 +1,14 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
person=""
|
||||
template=""
|
||||
arguments=(--something)
|
||||
expected() {
|
||||
cat <<EOF
|
||||
cat: --something: No such file or directory
|
||||
EOF
|
||||
}
|
||||
|
||||
runTest
|
@ -1 +0,0 @@
|
||||
cat: --something: No such file or directory
|
@ -1,5 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# This should display a message indicating that the file --something
|
||||
# could not be found.
|
||||
cd "${0%/*}" || exit 1
|
||||
../mo --something 2>&1
|
22
tests/inverted
Executable file
22
tests/inverted
Executable file
@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
repo=()
|
||||
template() {
|
||||
cat <<EOF
|
||||
{{#repo}}
|
||||
<b>{{.}}</b>
|
||||
{{/repo}}
|
||||
{{^repo}}
|
||||
No repos :(
|
||||
{{/repo}}
|
||||
EOF
|
||||
}
|
||||
expected() {
|
||||
cat <<EOF
|
||||
No repos :(
|
||||
EOF
|
||||
}
|
||||
|
||||
runTest
|
@ -1 +0,0 @@
|
||||
repo=()
|
@ -1,2 +0,0 @@
|
||||
|
||||
No repos :(
|
@ -1,6 +0,0 @@
|
||||
{{#repo}}
|
||||
<b>{{.}}</b>
|
||||
{{/repo}}
|
||||
{{^repo}}
|
||||
No repos :(
|
||||
{{/repo}}
|
24
tests/miss
Executable file
24
tests/miss
Executable file
@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
name="Chris"
|
||||
company="<b>GitHub</b>"
|
||||
template() {
|
||||
cat <<EOF
|
||||
* .{{name}}.
|
||||
* .{{age}}.
|
||||
* .{{company}}.
|
||||
* .{{{company}}}.
|
||||
EOF
|
||||
}
|
||||
expected() {
|
||||
cat <<EOF
|
||||
* .Chris.
|
||||
* ..
|
||||
* .<b>GitHub</b>.
|
||||
* .<b>GitHub</b>.
|
||||
EOF
|
||||
}
|
||||
|
||||
runTest
|
@ -1,2 +0,0 @@
|
||||
name="Chris"
|
||||
company="<b>GitHub</b>"
|
@ -1,4 +0,0 @@
|
||||
* .Chris.
|
||||
* ..
|
||||
* .<b>GitHub</b>.
|
||||
* .<b>GitHub</b>.
|
@ -1,4 +0,0 @@
|
||||
* .{{name}}.
|
||||
* .{{age}}.
|
||||
* .{{company}}.
|
||||
* .{{{company}}}.
|
31
tests/multi-line-partial
Executable file
31
tests/multi-line-partial
Executable file
@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
multilineData=$'line 1\nline 2'
|
||||
template() {
|
||||
cat <<EOF
|
||||
Partial:
|
||||
|
||||
{{> fixtures/multi-line-partial.partial}}
|
||||
|
||||
Indented:
|
||||
|
||||
{{> fixtures/multi-line-partial.partial}}
|
||||
EOF
|
||||
}
|
||||
expected() {
|
||||
cat <<EOF
|
||||
Partial:
|
||||
|
||||
line 1
|
||||
line 2
|
||||
|
||||
Indented:
|
||||
|
||||
line 1
|
||||
line 2
|
||||
EOF
|
||||
}
|
||||
|
||||
runTest
|
@ -1 +0,0 @@
|
||||
multilineData=$'line 1\nline 2'
|
@ -1,9 +0,0 @@
|
||||
Partial:
|
||||
|
||||
line 1
|
||||
line 2
|
||||
|
||||
Indented:
|
||||
|
||||
line 1
|
||||
line 2
|
@ -1,7 +0,0 @@
|
||||
Partial:
|
||||
|
||||
{{> multi-line-partial.partial}}
|
||||
|
||||
Indented:
|
||||
|
||||
{{> multi-line-partial.partial}}
|
33
tests/mush
Executable file
33
tests/mush
Executable file
@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
USER=jwerle
|
||||
GENDER=male
|
||||
THING=apple
|
||||
COLOR=red
|
||||
PERSON=tobi
|
||||
ADJECTIVE=cool
|
||||
template() {
|
||||
cat <<EOF
|
||||
{{! this is a comment }}
|
||||
|
||||
{{USER}} is {{GENDER}}
|
||||
{{THING}} is {{COLOR}}
|
||||
{{PERSON}} is {{ADJECTIVE}}
|
||||
{{USER}} is friends with {{PERSON}}
|
||||
{{var}} {{value}}
|
||||
EOF
|
||||
}
|
||||
expected() {
|
||||
cat <<EOF
|
||||
|
||||
jwerle is male
|
||||
apple is red
|
||||
tobi is cool
|
||||
jwerle is friends with tobi
|
||||
|
||||
EOF
|
||||
}
|
||||
|
||||
runTest
|
@ -1,6 +0,0 @@
|
||||
USER=jwerle
|
||||
GENDER=male
|
||||
THING=apple
|
||||
COLOR=red
|
||||
PERSON=tobi
|
||||
ADJECTIVE=cool
|
@ -1,6 +0,0 @@
|
||||
|
||||
jwerle is male
|
||||
apple is red
|
||||
tobi is cool
|
||||
jwerle is friends with tobi
|
||||
|
@ -1,7 +0,0 @@
|
||||
{{! this is a comment }}
|
||||
|
||||
{{USER}} is {{GENDER}}
|
||||
{{THING}} is {{COLOR}}
|
||||
{{PERSON}} is {{ADJECTIVE}}
|
||||
{{USER}} is friends with {{PERSON}}
|
||||
{{var}} {{value}}
|
8
tests/no-content
Executable file
8
tests/no-content
Executable file
@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
template="Works"
|
||||
expected="Works"
|
||||
|
||||
runTest
|
22
tests/partial
Executable file
22
tests/partial
Executable file
@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
names=( "Tyler" )
|
||||
template() {
|
||||
cat <<EOF
|
||||
<h2>Names</h2>
|
||||
{{#names}}
|
||||
{{> partial.partial}}
|
||||
{{/names}}
|
||||
EOF
|
||||
}
|
||||
expected() {
|
||||
cat <<EOF
|
||||
<h2>Names</h2>
|
||||
<strong>Tyler</strong>
|
||||
|
||||
EOF
|
||||
}
|
||||
|
||||
runTest
|
18
tests/partial-missing
Executable file
18
tests/partial-missing
Executable file
@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env bash
|
||||
cd "${0%/*}" || exit 1
|
||||
. ../run-tests
|
||||
|
||||
returnCode=1
|
||||
person=""
|
||||
template() {
|
||||
cat <<EOF
|
||||
Won't be there: {{> fixtures/partial-missing.partial}}
|
||||
EOF
|
||||
}
|
||||
expected() {
|
||||
cat <<EOF
|
||||
cat: partial-missing.partial: No such file or directory
|
||||
EOF
|
||||
}
|
||||
|
||||
runTest
|
@ -1 +0,0 @@
|
||||
cat: partial-missing.partial: No such file or directory
|
@ -1,8 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
cd "${0%/*}" || exit 1
|
||||
../mo partial-missing.template 2>&1
|
||||
|
||||
if [[ $? -ne 1 ]]; then
|
||||
echo "Did not return 1"
|
||||
fi
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user