Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

goenv-init: Fix use of optional variables that should not crash the calling shell if unset #431

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}

Loading