Also add tests for unset variable with varstash.
# Internal functions. {{{
# Internal: stack of entered (and handled) directories. {{{
-_autoenv_stack_entered=()
+typeset -a _autoenv_stack_entered
typeset -A _autoenv_stack_entered_mtime
-_autoenv_stack_entered_mtime=()
# Add an entry to the stack, and remember its mtime.
_autoenv_stack_entered_add() {
# A fixed hash value can be given as 2nd arg, but is used with tests only.
_autoenv_hash_pair() {
local env_file=${1:A}
- local env_shasum
- if [[ -n $2 ]]; then
- env_shasum=$2
- else
+ local env_shasum=${2:-}
+ if [[ -z $env_shasum ]]; then
if ! [[ -e $env_file ]]; then
echo "Missing file argument for _autoenv_hash_pair!" >&2
return 1
# Change to directory of env file, source it and cd back.
local new_dir=$PWD
builtin cd -q $_autoenv_envfile_dir
- _autoenv_debug "== SOURCE: ${bold_color}$env_file${reset_color}\n PWD: $PWD"
+ _autoenv_debug "== SOURCE: ${bold_color:-}$env_file${reset_color:-}\n PWD: $PWD"
(( _autoenv_debug_indent++ ))
source $env_file
(( _autoenv_debug_indent-- ))
_autoenv_source $env_file_leave leave $prev_dir
fi
- # Unstash any autostash'd stuff.
+ # Unstash any autostashed stuff.
varstash_dir=$prev_dir autounstash
_autoenv_stack_entered_remove $prev_file
Now try to make it accept it
- $ unset _autoenv_stack_entered
+ $ _autoenv_stack_entered=()
$ rm $AUTOENV_ENV_FILENAME
$ _autoenv_ask_for_yes() { echo "yes" }
$ cd .
Now lets see that it actually checks the shasum value.
- $ unset _autoenv_stack_entered
+ $ _autoenv_stack_entered=()
$ cd .
ENTERED
- $ unset _autoenv_stack_entered
+ $ _autoenv_stack_entered=()
$ rm $AUTOENV_ENV_FILENAME
$ test_autoenv_add_to_env $PWD/.env mischief
$ cd .
Now, will it take no for an answer?
- $ unset _autoenv_stack_entered
+ $ _autoenv_stack_entered=()
$ rm $AUTOENV_ENV_FILENAME
$ _autoenv_ask_for_yes() { echo "no"; return 1 }
$ cd .
# Ensure we have our mocked out AUTOENV_ENV_FILENAME
# (via .zshenv).
+# Treat unset variables as errors.
+# Not handled in varstash yet.
+# setopt nounset
+
[[ $AUTOENV_ENV_FILENAME[0,4] == '/tmp' ]] || return 1
# Reset any authentication.
# Add file $1 (with optional hash $2) to authentication file.
test_autoenv_add_to_env() {
- _autoenv_hash_pair $1 $2 >> $AUTOENV_ENV_FILENAME
+ _autoenv_hash_pair $1 ${2:-} >> $AUTOENV_ENV_FILENAME
}
# Add enter and leave env files to authentication file.
Setup:
+ $ unset VAR
$ cd sub
ENTER
$ echo 'echo ENTER; autostash VAR=changed' > $AUTOENV_FILE_ENTER
$ echo 'echo LEAVE; echo "no explicit call to autounstash"' > $AUTOENV_FILE_LEAVE
$ test_autoenv_auth_env_files
-$VAR is empty:
+$VAR is unset:
- $ echo VAR:$VAR
- VAR:
+ $ echo VAR_set:$+VAR
+ VAR_set:0
-Set it:
+Trigger the autostashing in the enter file.
- $ VAR=orig
$ cd ..
LEAVE
no explicit call to autounstash
-
-Leaving the directory keeps it intact - nothing had been stashed yet.
-
- $ echo $VAR
- orig
-
-Enter the dir, trigger the autostashing.
-
$ cd sub
ENTER
$ echo $VAR
$ cd ..
LEAVE
no explicit call to autounstash
- $ echo $VAR
- orig
-
+ $ echo VAR_set:$+VAR
+ VAR_set:0
Remove the leave file, auto-unstashing should still happen.
$ echo $VAR
changed
$ cd ..
+ $ echo VAR_set:$+VAR
+ VAR_set:0
+
+And once again where a value gets restored.
+
+ $ VAR=orig_2
$ echo $VAR
- orig
+ orig_2
+ $ cd sub
+ ENTER
+ $ echo $VAR
+ changed
+ $ cd ..
+ $ echo $VAR
+ orig_2
}}}