mirror of
https://github.com/tests-always-included/mo.git
synced 2024-12-18 16:27:52 +00:00
Enabling a "false is empty" style check.
This is based off of pull request #10 from @athieriot. Thanks for the idea! I've implemented it as a flag and an environment variable that can be enabled. I've also added another test to handle the environment variable.
This commit is contained in:
parent
ea76dc468b
commit
1c03949107
42
mo
42
mo
@ -8,6 +8,14 @@
|
|||||||
#/ conditionally display content or iterate over the values of an array.
|
#/ conditionally display content or iterate over the values of an array.
|
||||||
#/
|
#/
|
||||||
#/ Learn more about mustache templates at https://mustache.github.io/
|
#/ Learn more about mustache templates at https://mustache.github.io/
|
||||||
|
#/
|
||||||
|
#/ 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.
|
||||||
#
|
#
|
||||||
# Mo is under a MIT style licence with an additional non-advertising clause.
|
# Mo is under a MIT style licence with an additional non-advertising clause.
|
||||||
# See LICENSE.md for the full text.
|
# See LICENSE.md for the full text.
|
||||||
@ -20,6 +28,9 @@
|
|||||||
# Public: Template parser function. Writes templates to stdout.
|
# Public: Template parser function. Writes templates to stdout.
|
||||||
#
|
#
|
||||||
# $0 - Name of the mo file, used for getting the help message.
|
# $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.
|
# --help - Display a help message.
|
||||||
# --source=FILE - Source a file into the environment before processint
|
# --source=FILE - Source a file into the environment before processint
|
||||||
# template files.
|
# template files.
|
||||||
@ -27,6 +38,14 @@
|
|||||||
# use this when filenames may start with two hyphens.
|
# use this when filenames may start with two hyphens.
|
||||||
# $@ - Filenames to parse.
|
# $@ - Filenames to parse.
|
||||||
#
|
#
|
||||||
|
# Mo uses the following environment variables:
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
#
|
||||||
# Returns nothing.
|
# Returns nothing.
|
||||||
mo() (
|
mo() (
|
||||||
# This function executes in a subshell so IFS is reset.
|
# This function executes in a subshell so IFS is reset.
|
||||||
@ -45,11 +64,15 @@ mo() (
|
|||||||
files=("${files[@]}" "$arg")
|
files=("${files[@]}" "$arg")
|
||||||
else
|
else
|
||||||
case "$arg" in
|
case "$arg" in
|
||||||
-h|--h|--he|--hel|--help)
|
-h|--h|--he|--hel|--help|-\?)
|
||||||
moUsage "$0"
|
moUsage "$0"
|
||||||
exit 0
|
exit 0
|
||||||
;;
|
;;
|
||||||
|
|
||||||
|
--false)
|
||||||
|
MO_FALSE_IS_EMPTY=true
|
||||||
|
;;
|
||||||
|
|
||||||
--source=*)
|
--source=*)
|
||||||
f2source="${1#--source=}"
|
f2source="${1#--source=}"
|
||||||
|
|
||||||
@ -775,15 +798,19 @@ moStandaloneDenied() {
|
|||||||
|
|
||||||
|
|
||||||
# Internal: Determines if the named thing is a function or if it is a
|
# Internal: Determines if the named thing is a function or if it is a
|
||||||
# non-empty environment variable.
|
# non-empty environment variable. When MO_FALSE_IS_EMPTY is set to a
|
||||||
|
# non-empty value, then "false" is also treated is an empty value.
|
||||||
#
|
#
|
||||||
# Do not use variables without prefixes here if possible as this needs to
|
# Do not use variables without prefixes here if possible as this needs to
|
||||||
# check if any name exists in the environment
|
# check if any name exists in the environment
|
||||||
#
|
#
|
||||||
# $1 - Name of environment variable or function
|
# $1 - Name of environment variable or function
|
||||||
# $2 - Current value (our context)
|
# $2 - Current value (our context)
|
||||||
|
# MO_FALSE_IS_EMPTY - When set to a non-empty value, this will say the
|
||||||
|
# string value "false" is empty.
|
||||||
#
|
#
|
||||||
# Returns 0 if the name is not empty, 1 otherwise.
|
# Returns 0 if the name is not empty, 1 otherwise. When MO_FALSE_IS_EMPTY
|
||||||
|
# is set, this returns 1 if the name is "false".
|
||||||
moTest() {
|
moTest() {
|
||||||
# Test for functions
|
# Test for functions
|
||||||
moIsFunction "$1" && return 0
|
moIsFunction "$1" && return 0
|
||||||
@ -792,6 +819,10 @@ moTest() {
|
|||||||
# Arrays must have at least 1 element
|
# Arrays must have at least 1 element
|
||||||
eval '[[ "${#'"$1"'[@]}" -gt 0 ]]' && return 0
|
eval '[[ "${#'"$1"'[@]}" -gt 0 ]]' && return 0
|
||||||
else
|
else
|
||||||
|
# If MO_FALSE_IS_EMPTY is set, then return 1 if the value of
|
||||||
|
# the variable is "false".
|
||||||
|
[[ ! -z "${MO_FALSE_IS_EMPTY-}" ]] && [[ "${!1-}" == "false" ]] && return 1
|
||||||
|
|
||||||
# Environment variables must not be empty
|
# Environment variables must not be empty
|
||||||
[[ ! -z "${!1}" ]] && return 0
|
[[ ! -z "${!1}" ]] && return 0
|
||||||
fi
|
fi
|
||||||
@ -855,10 +886,13 @@ moTrimWhitespace() {
|
|||||||
#
|
#
|
||||||
# Returns nothing.
|
# Returns nothing.
|
||||||
moUsage() {
|
moUsage() {
|
||||||
grep '^#/' "$1" | cut -c 4-
|
grep '^#/' "${MO_ORIGINAL_COMMAND}" | cut -c 4-
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Save the original command's path for usage later
|
||||||
|
MO_ORIGINAL_COMMAND="$(cd "${BASH_SOURCE%/*}"; pwd)/${BASH_SOURCE##*/}"
|
||||||
|
|
||||||
# If sourced, load all functions.
|
# If sourced, load all functions.
|
||||||
# If executed, perform the actions as expected.
|
# If executed, perform the actions as expected.
|
||||||
if [[ "$0" == "$BASH_SOURCE" ]] || ! [[ -n "$BASH_SOURCE" ]]; then
|
if [[ "$0" == "$BASH_SOURCE" ]] || ! [[ -n "$BASH_SOURCE" ]]; then
|
||||||
|
@ -8,6 +8,7 @@ FAIL=0
|
|||||||
|
|
||||||
for TEST in tests/*.expected; do
|
for TEST in tests/*.expected; do
|
||||||
BASE="${TEST%.expected}"
|
BASE="${TEST%.expected}"
|
||||||
|
MO_FALSE_IS_EMPTY=
|
||||||
|
|
||||||
echo -n "$BASE ... "
|
echo -n "$BASE ... "
|
||||||
|
|
||||||
|
1
tests/false-is-empty-arg.expected
Normal file
1
tests/false-is-empty-arg.expected
Normal file
@ -0,0 +1 @@
|
|||||||
|
The user j.doe exists.
|
4
tests/false-is-empty-arg.sh
Executable file
4
tests/false-is-empty-arg.sh
Executable file
@ -0,0 +1,4 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
cd "${0%/*}"
|
||||||
|
USER=j.doe ADMIN=false ../mo --false false-is-empty-arg.template
|
4
tests/false-is-empty-arg.template
Normal file
4
tests/false-is-empty-arg.template
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
The user {{USER}} exists.
|
||||||
|
{{#ADMIN}}
|
||||||
|
WRONG - should not be an admin.
|
||||||
|
{{/ADMIN}}
|
2
tests/false-is-empty-env.env
Normal file
2
tests/false-is-empty-env.env
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
MO_FALSE_IS_EMPTY=yeppers
|
||||||
|
someFalseValue=false
|
1
tests/false-is-empty-env.expected
Normal file
1
tests/false-is-empty-env.expected
Normal file
@ -0,0 +1 @@
|
|||||||
|
Works
|
4
tests/false-is-empty-env.template
Normal file
4
tests/false-is-empty-env.template
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
Works
|
||||||
|
{{#someFalseValue}}
|
||||||
|
Never shown!
|
||||||
|
{{/someFalseValue}}
|
@ -6,3 +6,11 @@ environment variable. You can use {{#VARIABLE}}content{{/VARIABLE}} to
|
|||||||
conditionally display content or iterate over the values of an array.
|
conditionally display content or iterate over the values of an array.
|
||||||
|
|
||||||
Learn more about mustache templates at https://mustache.github.io/
|
Learn more about mustache templates at https://mustache.github.io/
|
||||||
|
|
||||||
|
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.
|
||||||
|
Loading…
Reference in New Issue
Block a user