diff --git a/.gitignore b/.gitignore index 5a1b4f1..a6047cb 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ diagnostic.test tests/*.diff spec/ spec-runner/ +node_modules/ diff --git a/mo b/mo index b143808..fc168b5 100755 --- a/mo +++ b/mo @@ -13,9 +13,10 @@ #/ #/ mo [--false] [--help] [--source=FILE] filenames... #/ -#/ --false - Treat the string "false" as empty for conditionals. -#/ --help - This message. -#/ --source=FILE - Load FILE into the environment before processing templates. +#/ --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. # # Mo is under a MIT style licence with an additional non-advertising clause. # See LICENSE.md for the full text. @@ -27,19 +28,23 @@ # Public: Template parser function. Writes templates to stdout. # -# $0 - Name of the mo file, used for getting the help message. -# --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. -# --help - Display a help message. -# --source=FILE - Source a file into the environment before processint -# template files. -# -- - Used to indicate the end of options. You may optionally -# use this when filenames may start with two hyphens. -# $@ - Filenames to parse. +# $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. +# --help - Display a help message. +# --source=FILE - Source a file into the environment before processint +# template files. +# -- - Used to indicate the end of options. You may optionally +# use this when filenames may start with two hyphens. +# $@ - Filenames to parse. # # 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 @@ -255,7 +265,7 @@ moIndentLines() { local content fragment len posN posR result trimmed result="" - + #: Remove the period from the end of the string. len=$((${#3} - 1)) content=${3:0:$len} @@ -746,7 +756,13 @@ moShow() { eval moJoin moJoined "," "\${$1[@]}" echo -n "$moJoined" else - echo -n "${!1}" + # shellcheck disable=SC2031 + if [[ -z "$MO_FAIL_ON_UNSET" ]] || moTest "$1"; then + echo -n "${!1}" + else + echo "Env variable not set: $1" >&2 + exit 1 + fi fi else # Further subindexes are disallowed diff --git a/tests/fail-not-set.expected b/tests/fail-not-set.expected new file mode 100644 index 0000000..06dea8f --- /dev/null +++ b/tests/fail-not-set.expected @@ -0,0 +1 @@ +This will fail: Env variable not set: __NO_SUCH_VAR diff --git a/tests/fail-not-set.sh b/tests/fail-not-set.sh new file mode 100755 index 0000000..d3f9ef7 --- /dev/null +++ b/tests/fail-not-set.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +cd "${0%/*}" +unset __NO_SUCH_VAR +echo "This will fail: {{__NO_SUCH_VAR}}" | ../mo --fail-not-set 2>&1 + +if [[ $? -ne 1 ]]; then + echo "Did not return 1" +fi diff --git a/tests/help.expected b/tests/help.expected index 95e8df5..51a740a 100644 --- a/tests/help.expected +++ b/tests/help.expected @@ -11,6 +11,7 @@ Simple usage: mo [--false] [--help] [--source=FILE] filenames... ---false - Treat the string "false" as empty for conditionals. ---help - This message. ---source=FILE - Load FILE into the environment before processing templates. +--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.