Skip to content

Commit

Permalink
goenv-init: Fix use of optional variables that should not crash the c…
Browse files Browse the repository at this point in the history
…alling shell if unset

Just use the ${variable:-} syntax to accept unset variables
  • Loading branch information
Salamandar committed Dec 26, 2024
1 parent 8ea5cab commit 57e68c2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
6 changes: 3 additions & 3 deletions libexec/goenv-init
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ EOL
cat <<EOL
export GOENV_SHELL=${shell}
export GOENV_ROOT=${GOENV_ROOT}
if [ -z "\${GOENV_RC_FILE}" ]; then
if [ -z "\${GOENV_RC_FILE:-}" ]; then
GOENV_RC_FILE="\${HOME}/.goenvrc"
fi
if [ -e "\${GOENV_RC_FILE}" ]; then
if [ -e "\${GOENV_RC_FILE:-}" ]; then
source "\${GOENV_RC_FILE}"
fi
if [ "\${PATH#*\$GOENV_ROOT/shims}" = "\${PATH}" ]; then
if [ "\${GOENV_PATH_ORDER}" = "front" ] ; then
if [ "\${GOENV_PATH_ORDER:-}" = "front" ] ; then
export PATH="\${GOENV_ROOT}/shims:\${PATH}"
else
export PATH="\${PATH}:\${GOENV_ROOT}/shims"
Expand Down
25 changes: 12 additions & 13 deletions test/goenv-init.bats
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ OUT

assert_line 0 'export GOENV_SHELL=bash'
assert_line 1 "export GOENV_ROOT=$GOENV_ROOT"
assert_line 2 'if [ -z "${GOENV_RC_FILE}" ]; then'
assert_line 2 'if [ -z "${GOENV_RC_FILE:-}" ]; then'
assert_line 3 ' GOENV_RC_FILE="${HOME}/.goenvrc"'
assert_line 4 'fi'
assert_line 5 'if [ -e "${GOENV_RC_FILE}" ]; then'
assert_line 5 'if [ -e "${GOENV_RC_FILE:-}" ]; then'
assert_line 6 ' source "${GOENV_RC_FILE}"'
assert_line 7 'fi'
assert_line 8 'if [ "${PATH#*$GOENV_ROOT/shims}" = "${PATH}" ]; then'
assert_line 9 ' if [ "${GOENV_PATH_ORDER}" = "front" ] ; then'
assert_line 9 ' if [ "${GOENV_PATH_ORDER:-}" = "front" ] ; then'
assert_line 10 ' export PATH="${GOENV_ROOT}/shims:${PATH}"'
assert_line 11 ' else'
assert_line 12 ' export PATH="${PATH}:${GOENV_ROOT}/shims"'
Expand Down Expand Up @@ -173,14 +173,14 @@ OUT

assert_line 0 'export GOENV_SHELL=zsh'
assert_line 1 "export GOENV_ROOT=$GOENV_ROOT"
assert_line 2 'if [ -z "${GOENV_RC_FILE}" ]; then'
assert_line 2 'if [ -z "${GOENV_RC_FILE:-}" ]; then'
assert_line 3 ' GOENV_RC_FILE="${HOME}/.goenvrc"'
assert_line 4 'fi'
assert_line 5 'if [ -e "${GOENV_RC_FILE}" ]; then'
assert_line 5 'if [ -e "${GOENV_RC_FILE:-}" ]; then'
assert_line 6 ' source "${GOENV_RC_FILE}"'
assert_line 7 'fi'
assert_line 8 'if [ "${PATH#*$GOENV_ROOT/shims}" = "${PATH}" ]; then'
assert_line 9 ' if [ "${GOENV_PATH_ORDER}" = "front" ] ; then'
assert_line 9 ' if [ "${GOENV_PATH_ORDER:-}" = "front" ] ; then'
assert_line 10 ' export PATH="${GOENV_ROOT}/shims:${PATH}"'
assert_line 11 ' else'
assert_line 12 ' export PATH="${PATH}:${GOENV_ROOT}/shims"'
Expand Down Expand Up @@ -244,14 +244,14 @@ OUT

assert_line 0 'export GOENV_SHELL=ksh'
assert_line 1 "export GOENV_ROOT=$GOENV_ROOT"
assert_line 2 'if [ -z "${GOENV_RC_FILE}" ]; then'
assert_line 2 'if [ -z "${GOENV_RC_FILE:-}" ]; then'
assert_line 3 ' GOENV_RC_FILE="${HOME}/.goenvrc"'
assert_line 4 'fi'
assert_line 5 'if [ -e "${GOENV_RC_FILE}" ]; then'
assert_line 5 'if [ -e "${GOENV_RC_FILE:-}" ]; then'
assert_line 6 ' source "${GOENV_RC_FILE}"'
assert_line 7 'fi'
assert_line 8 'if [ "${PATH#*$GOENV_ROOT/shims}" = "${PATH}" ]; then'
assert_line 9 ' if [ "${GOENV_PATH_ORDER}" = "front" ] ; then'
assert_line 9 ' if [ "${GOENV_PATH_ORDER:-}" = "front" ] ; then'
assert_line 10 ' export PATH="${GOENV_ROOT}/shims:${PATH}"'
assert_line 11 ' else'
assert_line 12 ' export PATH="${PATH}:${GOENV_ROOT}/shims"'
Expand Down Expand Up @@ -281,14 +281,14 @@ OUT
# NOTE: This is very likely to be invalid for your specific shell
assert_line 0 'export GOENV_SHELL=magicshell'
assert_line 1 "export GOENV_ROOT=$GOENV_ROOT"
assert_line 2 'if [ -z "${GOENV_RC_FILE}" ]; then'
assert_line 2 'if [ -z "${GOENV_RC_FILE:-}" ]; then'
assert_line 3 ' GOENV_RC_FILE="${HOME}/.goenvrc"'
assert_line 4 'fi'
assert_line 5 'if [ -e "${GOENV_RC_FILE}" ]; then'
assert_line 5 'if [ -e "${GOENV_RC_FILE:-}" ]; then'
assert_line 6 ' source "${GOENV_RC_FILE}"'
assert_line 7 'fi'
assert_line 8 'if [ "${PATH#*$GOENV_ROOT/shims}" = "${PATH}" ]; then'
assert_line 9 ' if [ "${GOENV_PATH_ORDER}" = "front" ] ; then'
assert_line 9 ' if [ "${GOENV_PATH_ORDER:-}" = "front" ] ; then'
assert_line 10 ' export PATH="${GOENV_ROOT}/shims:${PATH}"'
assert_line 11 ' else'
assert_line 12 ' export PATH="${PATH}:${GOENV_ROOT}/shims"'
Expand All @@ -311,4 +311,3 @@ OUT

assert_success
}

0 comments on commit 57e68c2

Please sign in to comment.