mirror of
https://github.com/tests-always-included/mo.git
synced 2024-12-18 16:27:52 +00:00
Merge pull request #15 from maslennikov/master
Adding an option to fail upon unset env variables
This commit is contained in:
commit
e6787e71d5
1
.gitignore
vendored
1
.gitignore
vendored
@ -4,3 +4,4 @@ diagnostic.test
|
||||
tests/*.diff
|
||||
spec/
|
||||
spec-runner/
|
||||
node_modules/
|
||||
|
35
mo
35
mo
@ -13,6 +13,7 @@
|
||||
#/
|
||||
#/ mo [--false] [--help] [--source=FILE] filenames...
|
||||
#/
|
||||
#/ --fail-not-set - Fail upon expansion of an unset variable.
|
||||
#/ --false - Treat the string "false" as empty for conditionals.
|
||||
#/ --help - This message.
|
||||
#/ --source=FILE - Load FILE into the environment before processing templates.
|
||||
@ -28,6 +29,8 @@
|
||||
# Public: Template parser function. Writes templates to stdout.
|
||||
#
|
||||
# $0 - Name of the mo file, used for getting the help message.
|
||||
# --fail-not-set - Fail upon expansion of an unset variable. Default behavior
|
||||
# is to silently ignore and expand into empty string.
|
||||
# --false - Treat "false" as an empty value. You may set the
|
||||
# MO_FALSE_IS_EMPTY environment variable instead to a non-empty
|
||||
# value to enable this behavior.
|
||||
@ -40,6 +43,8 @@
|
||||
#
|
||||
# Mo uses the following environment variables:
|
||||
#
|
||||
# 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.
|
||||
@ -69,6 +74,11 @@ mo() (
|
||||
exit 0
|
||||
;;
|
||||
|
||||
--fail-not-set)
|
||||
# shellcheck disable=SC2030
|
||||
MO_FAIL_ON_UNSET=true
|
||||
;;
|
||||
|
||||
--false)
|
||||
# shellcheck disable=SC2030
|
||||
MO_FALSE_IS_EMPTY=true
|
||||
@ -686,7 +696,7 @@ moParse() {
|
||||
# Returns nothing.
|
||||
moPartial() {
|
||||
# Namespace variables here to prevent conflicts.
|
||||
local moContent moFilename moIndent moPartial moStandalone
|
||||
local moContent moFilename moIndent moPartial moStandalone moUnindented
|
||||
|
||||
if moIsStandalone moStandalone "$2" "$4" "$5"; then
|
||||
moStandalone=( $moStandalone )
|
||||
@ -705,16 +715,17 @@ moPartial() {
|
||||
(
|
||||
# TODO: Remove dirname and use a function instead
|
||||
cd "$(dirname -- "$moFilename")" || exit 1
|
||||
moIndentLines moPartial "$moIndent" "$(
|
||||
moUnindented="$(
|
||||
moLoadFile moPartial "${moFilename##*/}"
|
||||
moParse "${moPartial}" "$6" true
|
||||
|
||||
# Fix bash handling of subshells and keep trailing whitespace.
|
||||
# This is removed in moIndentLines.
|
||||
echo -n "."
|
||||
)"
|
||||
)" || exit 1
|
||||
moIndentLines moPartial "$moIndent" "$moUnindented"
|
||||
echo -n "$moPartial"
|
||||
)
|
||||
) || exit 1
|
||||
|
||||
local "$1" && moIndirect "$1" "$moContent"
|
||||
}
|
||||
@ -746,7 +757,13 @@ moShow() {
|
||||
eval moJoin moJoined "," "\${$1[@]}"
|
||||
echo -n "$moJoined"
|
||||
else
|
||||
# shellcheck disable=SC2031
|
||||
if [[ -z "$MO_FAIL_ON_UNSET" ]] || moTestVarSet "$1"; then
|
||||
echo -n "${!1}"
|
||||
else
|
||||
echo "Env variable not set: $1" >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
else
|
||||
# Further subindexes are disallowed
|
||||
@ -863,6 +880,16 @@ moTest() {
|
||||
return 1
|
||||
}
|
||||
|
||||
# Internal: Determine if a variable is assigned, even if it is assigned an empty
|
||||
# value.
|
||||
#
|
||||
# $1 - Variable name to check.
|
||||
#
|
||||
# Returns true (0) if the variable is set, 1 if the variable is unset.
|
||||
moTestVarSet() {
|
||||
[[ "${!1-a}" == "${!1-b}" ]]
|
||||
}
|
||||
|
||||
|
||||
# Internal: Trim the leading whitespace only.
|
||||
#
|
||||
|
1
tests/fail-not-set-file.expected
Normal file
1
tests/fail-not-set-file.expected
Normal file
@ -0,0 +1 @@
|
||||
Env variable not set: __NO_SUCH_VAR
|
9
tests/fail-not-set-file.sh
Executable file
9
tests/fail-not-set-file.sh
Executable file
@ -0,0 +1,9 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
cd "${0%/*}"
|
||||
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
|
3
tests/fail-not-set-file.template
Normal file
3
tests/fail-not-set-file.template
Normal file
@ -0,0 +1,3 @@
|
||||
Populated: {{POPULATED}};
|
||||
Empty: {{EMPTY}};
|
||||
Unset: {{__NO_SUCH_VAR}};
|
3
tests/fail-not-set.expected
Normal file
3
tests/fail-not-set.expected
Normal file
@ -0,0 +1,3 @@
|
||||
Populated: words;
|
||||
Empty: ;
|
||||
Unset: Env variable not set: __NO_SUCH_VAR
|
13
tests/fail-not-set.sh
Executable file
13
tests/fail-not-set.sh
Executable file
@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
cd "${0%/*}"
|
||||
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
|
@ -11,6 +11,7 @@ Simple usage:
|
||||
|
||||
mo [--false] [--help] [--source=FILE] filenames...
|
||||
|
||||
--fail-not-set - Fail upon expansion of an unset variable.
|
||||
--false - Treat the string "false" as empty for conditionals.
|
||||
--help - This message.
|
||||
--source=FILE - Load FILE into the environment before processing templates.
|
||||
|
Loading…
Reference in New Issue
Block a user