From 31b2faf1358fb2d3869bd62b26f453f8110d1713 Mon Sep 17 00:00:00 2001 From: Alexey Maslennikov Date: Fri, 16 Jun 2017 15:59:57 +0200 Subject: [PATCH] Fixing empty variable failure for --fail-not-set --- mo | 12 +++++++++++- tests/fail-not-set-file.sh | 2 +- tests/fail-not-set-file.template | 4 +++- tests/fail-not-set.expected | 4 +++- tests/fail-not-set.sh | 6 +++++- 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/mo b/mo index f1d1b31..57f0fba 100755 --- a/mo +++ b/mo @@ -758,7 +758,7 @@ moShow() { echo -n "$moJoined" else # shellcheck disable=SC2031 - if [[ -z "$MO_FAIL_ON_UNSET" ]] || moTest "$1"; then + if [[ -z "$MO_FAIL_ON_UNSET" ]] || moTestVarSet "$1"; then echo -n "${!1}" else echo "Env variable not set: $1" >&2 @@ -880,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. # diff --git a/tests/fail-not-set-file.sh b/tests/fail-not-set-file.sh index 0355219..fe24e85 100755 --- a/tests/fail-not-set-file.sh +++ b/tests/fail-not-set-file.sh @@ -2,7 +2,7 @@ cd "${0%/*}" 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 echo "Did not return 1" diff --git a/tests/fail-not-set-file.template b/tests/fail-not-set-file.template index bd8b3ee..d249abe 100644 --- a/tests/fail-not-set-file.template +++ b/tests/fail-not-set-file.template @@ -1 +1,3 @@ -* {{__NO_SUCH_VAR}} +Populated: {{POPULATED}}; +Empty: {{EMPTY}}; +Unset: {{__NO_SUCH_VAR}}; diff --git a/tests/fail-not-set.expected b/tests/fail-not-set.expected index 06dea8f..6713890 100644 --- a/tests/fail-not-set.expected +++ b/tests/fail-not-set.expected @@ -1 +1,3 @@ -This will fail: Env variable not set: __NO_SUCH_VAR +Populated: words; +Empty: ; +Unset: Env variable not set: __NO_SUCH_VAR diff --git a/tests/fail-not-set.sh b/tests/fail-not-set.sh index d3f9ef7..b0b6b34 100755 --- a/tests/fail-not-set.sh +++ b/tests/fail-not-set.sh @@ -2,7 +2,11 @@ cd "${0%/*}" 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 <