Skip to content

Commit

Permalink
setup-env.sh: make setup scripts work with set -u
Browse files Browse the repository at this point in the history
- Add set -u to the setup-env.sh test script

- Refactor lines in setup-env.sh that tested potentially undefined
  variables to use the `[ -z ${var+x} ]` construct
  • Loading branch information
tgamblin committed Jul 5, 2019
1 parent 7ed82f9 commit 5385dfa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
3 changes: 3 additions & 0 deletions share/spack/qa/setup-env-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ unuse() {
# Make sure no environment is active
unset SPACK_ENV

# fail with undefined variables
set -u

# Source setup-env.sh before tests
. share/spack/setup-env.sh

Expand Down
12 changes: 8 additions & 4 deletions share/spack/setup-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ spack() {
emulate -L sh
fi

# accumulate initial flags for main spack command
# accumulate flags meant for the main spack command
# the loop condition is unreadable, but it means:
# while $1 is set (while there are arguments)
# and $1 starts with '-' (and the arguments are flags)
_sp_flags=""
while [ "${1#-}" != "${1}" ]; do
while [ ! -z ${1+x} ] && [ "${1#-}" != "${1}" ]; do
_sp_flags="$_sp_flags $1"
shift
done
Expand All @@ -62,8 +65,9 @@ spack() {
return
fi

# set the subcommand if there is one (if $1 is set)
_sp_subcommand=""
if [ -n "$1" ]; then
if [ ! -z ${1+x} ]; then
_sp_subcommand="$1"
shift
fi
Expand Down Expand Up @@ -102,7 +106,7 @@ spack() {
case $_sp_arg in
activate)
_a="$@"
if [ -z "$1" ] || \
if [ -z ${1+x} ] || \
[ "${_a#*--sh}" != "$_a" ] || \
[ "${_a#*--csh}" != "$_a" ] || \
[ "${_a#*-h}" != "$_a" ];
Expand Down

0 comments on commit 5385dfa

Please sign in to comment.