Fix exit codes posix shell wrapper (#27012)

* Correct exit code in sh wrapper

* Fix tests

* SC2069
This commit is contained in:
Harmen Stoppels 2021-10-29 10:10:22 +02:00 committed by GitHub
parent f610b506cd
commit 49034abd76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 6 deletions

View File

@ -75,7 +75,7 @@ spack env create spack_test_2_env
# Ensure that we uninstall b on exit # Ensure that we uninstall b on exit
cleanup() { cleanup() {
echo "Removing test environment before exiting." echo "Removing test environment before exiting."
spack env deactivate 2>&1 > /dev/null spack env deactivate > /dev/null 2>&1
spack env rm -y spack_test_env spack_test_2_env spack env rm -y spack_test_env spack_test_2_env
title "Cleanup" title "Cleanup"
@ -149,14 +149,17 @@ contains "usage: spack env deactivate " spack env deactivate --help
title 'Testing activate and deactivate together' title 'Testing activate and deactivate together'
echo "Testing 'spack env activate spack_test_env'" echo "Testing 'spack env activate spack_test_env'"
succeeds spack env activate spack_test_env
spack env activate spack_test_env spack env activate spack_test_env
is_set SPACK_ENV is_set SPACK_ENV
echo "Testing 'spack env deactivate'" echo "Testing 'spack env deactivate'"
succeeds spack env deactivate
spack env deactivate spack env deactivate
is_not_set SPACK_ENV is_not_set SPACK_ENV
echo "Testing 'spack env activate spack_test_env'" echo "Testing 'spack env activate spack_test_env'"
succeeds spack env activate spack_test_env
spack env activate spack_test_env spack env activate spack_test_env
is_set SPACK_ENV is_set SPACK_ENV
@ -165,6 +168,7 @@ despacktivate
is_not_set SPACK_ENV is_not_set SPACK_ENV
echo "Testing 'spack env activate --prompt spack_test_env'" echo "Testing 'spack env activate --prompt spack_test_env'"
succeeds spack env activate --prompt spack_test_env
spack env activate --prompt spack_test_env spack env activate --prompt spack_test_env
is_set SPACK_ENV is_set SPACK_ENV
is_set SPACK_OLD_PS1 is_set SPACK_OLD_PS1
@ -175,13 +179,21 @@ is_not_set SPACK_ENV
is_not_set SPACK_OLD_PS1 is_not_set SPACK_OLD_PS1
echo "Testing 'spack env activate --temp'" echo "Testing 'spack env activate --temp'"
succeeds spack env activate --temp
spack env activate --temp spack env activate --temp
is_set SPACK_ENV is_set SPACK_ENV
succeeds spack env deactivate
spack env deactivate spack env deactivate
is_not_set SPACK_ENV is_not_set SPACK_ENV
echo "Testing spack env activate repeatedly" echo "Testing spack env activate repeatedly"
spack env activate spack_test_env spack env activate spack_test_env
succeeds spack env activate spack_test_2_env
spack env activate spack_test_2_env spack env activate spack_test_2_env
contains "spack_test_2_env" sh -c 'echo $PATH' contains "spack_test_2_env" sh -c 'echo $PATH'
does_not_contain "spack_test_env" sh -c 'echo $PATH' does_not_contain "spack_test_env" sh -c 'echo $PATH'
despacktivate
echo "Correct error exit codes for activate and deactivate"
fails spack env activate nonexisiting_environment
fails spack env deactivate

View File

@ -136,7 +136,8 @@ _spack_shell_wrapper() {
command spack env activate "$@" command spack env activate "$@"
else else
# Actual call to activate: source the output. # Actual call to activate: source the output.
eval $(command spack $_sp_flags env activate --sh "$@") stdout="$(command spack $_sp_flags env activate --sh "$@")" || return
eval "$stdout"
fi fi
;; ;;
deactivate) deactivate)
@ -157,7 +158,8 @@ _spack_shell_wrapper() {
command spack env deactivate -h command spack env deactivate -h
else else
# No args: source the output of the command. # No args: source the output of the command.
eval $(command spack $_sp_flags env deactivate --sh) stdout="$(command spack $_sp_flags env deactivate --sh)" || return
eval "$stdout"
fi fi
;; ;;
*) *)
@ -183,8 +185,8 @@ _spack_shell_wrapper() {
# Args contain --sh, --csh, or -h/--help: just execute. # Args contain --sh, --csh, or -h/--help: just execute.
command spack $_sp_flags $_sp_subcommand "$@" command spack $_sp_flags $_sp_subcommand "$@"
else else
eval $(command spack $_sp_flags $_sp_subcommand --sh "$@" || \ stdout="$(command spack $_sp_flags $_sp_subcommand --sh "$@")" || return
echo "return 1") # return 1 if spack command fails eval "$stdout"
fi fi
;; ;;
*) *)
@ -363,7 +365,8 @@ if [ -z "${SPACK_SKIP_MODULES+x}" ]; then
_spack_pathadd PATH "${_sp_module_bin}" _spack_pathadd PATH "${_sp_module_bin}"
fi; fi;
else else
eval `spack --print-shell-vars sh` stdout="$(command spack --print-shell-vars sh)" || return
eval "$stdout"
fi; fi;