Fixing empty variable failure for --fail-not-set

This commit is contained in:
Alexey Maslennikov 2017-06-16 15:59:57 +02:00
parent 2b611b8f90
commit 31b2faf135
5 changed files with 23 additions and 5 deletions

12
mo
View File

@ -758,7 +758,7 @@ moShow() {
echo -n "$moJoined" echo -n "$moJoined"
else else
# shellcheck disable=SC2031 # shellcheck disable=SC2031
if [[ -z "$MO_FAIL_ON_UNSET" ]] || moTest "$1"; then if [[ -z "$MO_FAIL_ON_UNSET" ]] || moTestVarSet "$1"; then
echo -n "${!1}" echo -n "${!1}"
else else
echo "Env variable not set: $1" >&2 echo "Env variable not set: $1" >&2
@ -880,6 +880,16 @@ moTest() {
return 1 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. # Internal: Trim the leading whitespace only.
# #

View File

@ -2,7 +2,7 @@
cd "${0%/*}" cd "${0%/*}"
unset __NO_SUCH_VAR unset __NO_SUCH_VAR
../mo --fail-not-set ./fail-not-set-file.template 2>&1 POPULATED="words" EMPTY="" ../mo --fail-not-set ./fail-not-set-file.template 2>&1
if [[ $? -ne 1 ]]; then if [[ $? -ne 1 ]]; then
echo "Did not return 1" echo "Did not return 1"

View File

@ -1 +1,3 @@
* {{__NO_SUCH_VAR}} Populated: {{POPULATED}};
Empty: {{EMPTY}};
Unset: {{__NO_SUCH_VAR}};

View File

@ -1 +1,3 @@
This will fail: Env variable not set: __NO_SUCH_VAR Populated: words;
Empty: ;
Unset: Env variable not set: __NO_SUCH_VAR

View File

@ -2,7 +2,11 @@
cd "${0%/*}" cd "${0%/*}"
unset __NO_SUCH_VAR unset __NO_SUCH_VAR
echo "This will fail: {{__NO_SUCH_VAR}}" | ../mo --fail-not-set 2>&1 POPULATED="words" EMPTY="" ../mo --fail-not-set 2>&1 <<EOF
Populated: {{POPULATED}};
Empty: {{EMPTY}};
Unset: {{__NO_SUCH_VAR}};
EOF
if [[ $? -ne 1 ]]; then if [[ $? -ne 1 ]]; then
echo "Did not return 1" echo "Did not return 1"