mirror of
https://github.com/Tarrasch/zsh-autoenv.git
synced 2024-11-22 07:20:59 +02:00
Fixes after using setopt nounset
during tests
Also add tests for unset variable with varstash.
This commit is contained in:
parent
d6fbb13451
commit
e567a40592
13
autoenv.zsh
13
autoenv.zsh
@ -41,9 +41,8 @@ autoenv_source_parent() {
|
||||
|
||||
# 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() {
|
||||
@ -126,10 +125,8 @@ zmodload -F zsh/stat b:zstat
|
||||
# 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
|
||||
@ -220,7 +217,7 @@ _autoenv_source() {
|
||||
# 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-- ))
|
||||
@ -276,7 +273,7 @@ _autoenv_chpwd_handler() {
|
||||
_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
|
||||
|
@ -12,7 +12,7 @@ Manually create auth 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 .
|
||||
@ -33,11 +33,11 @@ The last "ENTERED" is because it executed the command.
|
||||
|
||||
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 .
|
||||
@ -56,7 +56,7 @@ Now lets see that it actually checks the shasum value.
|
||||
|
||||
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 .
|
||||
|
@ -1,6 +1,10 @@
|
||||
# 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.
|
||||
@ -8,7 +12,7 @@ echo -n > $AUTOENV_ENV_FILENAME
|
||||
|
||||
# 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.
|
||||
|
@ -36,31 +36,23 @@ Test autounstashing when leaving a directory. {{{
|
||||
|
||||
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
|
||||
@ -71,9 +63,8 @@ Now leave again.
|
||||
$ 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.
|
||||
|
||||
@ -83,7 +74,20 @@ 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
|
||||
|
||||
}}}
|
||||
|
Loading…
Reference in New Issue
Block a user