Skip to content

Commit 9fa1c43

Browse files
authored
goenv-init: Fix use of optional variables that should not crash the calling shell if unset (#431)
Just use the ${variable:-} syntax to accept unset variables
1 parent 8ea5cab commit 9fa1c43

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

libexec/goenv-init

+3-3
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@ EOL
108108
cat <<EOL
109109
export GOENV_SHELL=${shell}
110110
export GOENV_ROOT=${GOENV_ROOT}
111-
if [ -z "\${GOENV_RC_FILE}" ]; then
111+
if [ -z "\${GOENV_RC_FILE:-}" ]; then
112112
GOENV_RC_FILE="\${HOME}/.goenvrc"
113113
fi
114-
if [ -e "\${GOENV_RC_FILE}" ]; then
114+
if [ -e "\${GOENV_RC_FILE:-}" ]; then
115115
source "\${GOENV_RC_FILE}"
116116
fi
117117
if [ "\${PATH#*\$GOENV_ROOT/shims}" = "\${PATH}" ]; then
118-
if [ "\${GOENV_PATH_ORDER}" = "front" ] ; then
118+
if [ "\${GOENV_PATH_ORDER:-}" = "front" ] ; then
119119
export PATH="\${GOENV_ROOT}/shims:\${PATH}"
120120
else
121121
export PATH="\${PATH}:\${GOENV_ROOT}/shims"

test/goenv-init.bats

+12-13
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ OUT
135135

136136
assert_line 0 'export GOENV_SHELL=bash'
137137
assert_line 1 "export GOENV_ROOT=$GOENV_ROOT"
138-
assert_line 2 'if [ -z "${GOENV_RC_FILE}" ]; then'
138+
assert_line 2 'if [ -z "${GOENV_RC_FILE:-}" ]; then'
139139
assert_line 3 ' GOENV_RC_FILE="${HOME}/.goenvrc"'
140140
assert_line 4 'fi'
141-
assert_line 5 'if [ -e "${GOENV_RC_FILE}" ]; then'
141+
assert_line 5 'if [ -e "${GOENV_RC_FILE:-}" ]; then'
142142
assert_line 6 ' source "${GOENV_RC_FILE}"'
143143
assert_line 7 'fi'
144144
assert_line 8 'if [ "${PATH#*$GOENV_ROOT/shims}" = "${PATH}" ]; then'
145-
assert_line 9 ' if [ "${GOENV_PATH_ORDER}" = "front" ] ; then'
145+
assert_line 9 ' if [ "${GOENV_PATH_ORDER:-}" = "front" ] ; then'
146146
assert_line 10 ' export PATH="${GOENV_ROOT}/shims:${PATH}"'
147147
assert_line 11 ' else'
148148
assert_line 12 ' export PATH="${PATH}:${GOENV_ROOT}/shims"'
@@ -173,14 +173,14 @@ OUT
173173

174174
assert_line 0 'export GOENV_SHELL=zsh'
175175
assert_line 1 "export GOENV_ROOT=$GOENV_ROOT"
176-
assert_line 2 'if [ -z "${GOENV_RC_FILE}" ]; then'
176+
assert_line 2 'if [ -z "${GOENV_RC_FILE:-}" ]; then'
177177
assert_line 3 ' GOENV_RC_FILE="${HOME}/.goenvrc"'
178178
assert_line 4 'fi'
179-
assert_line 5 'if [ -e "${GOENV_RC_FILE}" ]; then'
179+
assert_line 5 'if [ -e "${GOENV_RC_FILE:-}" ]; then'
180180
assert_line 6 ' source "${GOENV_RC_FILE}"'
181181
assert_line 7 'fi'
182182
assert_line 8 'if [ "${PATH#*$GOENV_ROOT/shims}" = "${PATH}" ]; then'
183-
assert_line 9 ' if [ "${GOENV_PATH_ORDER}" = "front" ] ; then'
183+
assert_line 9 ' if [ "${GOENV_PATH_ORDER:-}" = "front" ] ; then'
184184
assert_line 10 ' export PATH="${GOENV_ROOT}/shims:${PATH}"'
185185
assert_line 11 ' else'
186186
assert_line 12 ' export PATH="${PATH}:${GOENV_ROOT}/shims"'
@@ -244,14 +244,14 @@ OUT
244244

245245
assert_line 0 'export GOENV_SHELL=ksh'
246246
assert_line 1 "export GOENV_ROOT=$GOENV_ROOT"
247-
assert_line 2 'if [ -z "${GOENV_RC_FILE}" ]; then'
247+
assert_line 2 'if [ -z "${GOENV_RC_FILE:-}" ]; then'
248248
assert_line 3 ' GOENV_RC_FILE="${HOME}/.goenvrc"'
249249
assert_line 4 'fi'
250-
assert_line 5 'if [ -e "${GOENV_RC_FILE}" ]; then'
250+
assert_line 5 'if [ -e "${GOENV_RC_FILE:-}" ]; then'
251251
assert_line 6 ' source "${GOENV_RC_FILE}"'
252252
assert_line 7 'fi'
253253
assert_line 8 'if [ "${PATH#*$GOENV_ROOT/shims}" = "${PATH}" ]; then'
254-
assert_line 9 ' if [ "${GOENV_PATH_ORDER}" = "front" ] ; then'
254+
assert_line 9 ' if [ "${GOENV_PATH_ORDER:-}" = "front" ] ; then'
255255
assert_line 10 ' export PATH="${GOENV_ROOT}/shims:${PATH}"'
256256
assert_line 11 ' else'
257257
assert_line 12 ' export PATH="${PATH}:${GOENV_ROOT}/shims"'
@@ -281,14 +281,14 @@ OUT
281281
# NOTE: This is very likely to be invalid for your specific shell
282282
assert_line 0 'export GOENV_SHELL=magicshell'
283283
assert_line 1 "export GOENV_ROOT=$GOENV_ROOT"
284-
assert_line 2 'if [ -z "${GOENV_RC_FILE}" ]; then'
284+
assert_line 2 'if [ -z "${GOENV_RC_FILE:-}" ]; then'
285285
assert_line 3 ' GOENV_RC_FILE="${HOME}/.goenvrc"'
286286
assert_line 4 'fi'
287-
assert_line 5 'if [ -e "${GOENV_RC_FILE}" ]; then'
287+
assert_line 5 'if [ -e "${GOENV_RC_FILE:-}" ]; then'
288288
assert_line 6 ' source "${GOENV_RC_FILE}"'
289289
assert_line 7 'fi'
290290
assert_line 8 'if [ "${PATH#*$GOENV_ROOT/shims}" = "${PATH}" ]; then'
291-
assert_line 9 ' if [ "${GOENV_PATH_ORDER}" = "front" ] ; then'
291+
assert_line 9 ' if [ "${GOENV_PATH_ORDER:-}" = "front" ] ; then'
292292
assert_line 10 ' export PATH="${GOENV_ROOT}/shims:${PATH}"'
293293
assert_line 11 ' else'
294294
assert_line 12 ' export PATH="${PATH}:${GOENV_ROOT}/shims"'
@@ -311,4 +311,3 @@ OUT
311311

312312
assert_success
313313
}
314-

0 commit comments

Comments
 (0)