1
0
mirror of https://github.com/Tarrasch/zsh-autoenv.git synced 2024-11-22 15:30:59 +02:00

Fixes after using setopt nounset during tests

Also add tests for unset variable with varstash.
This commit is contained in:
Daniel Hahler 2014-12-08 21:37:25 +01:00
parent d6fbb13451
commit e567a40592
4 changed files with 35 additions and 30 deletions

View File

@ -41,9 +41,8 @@ autoenv_source_parent() {
# Internal functions. {{{ # Internal functions. {{{
# Internal: stack of entered (and handled) directories. {{{ # Internal: stack of entered (and handled) directories. {{{
_autoenv_stack_entered=() typeset -a _autoenv_stack_entered
typeset -A _autoenv_stack_entered_mtime typeset -A _autoenv_stack_entered_mtime
_autoenv_stack_entered_mtime=()
# Add an entry to the stack, and remember its mtime. # Add an entry to the stack, and remember its mtime.
_autoenv_stack_entered_add() { _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. # A fixed hash value can be given as 2nd arg, but is used with tests only.
_autoenv_hash_pair() { _autoenv_hash_pair() {
local env_file=${1:A} local env_file=${1:A}
local env_shasum local env_shasum=${2:-}
if [[ -n $2 ]]; then if [[ -z $env_shasum ]]; then
env_shasum=$2
else
if ! [[ -e $env_file ]]; then if ! [[ -e $env_file ]]; then
echo "Missing file argument for _autoenv_hash_pair!" >&2 echo "Missing file argument for _autoenv_hash_pair!" >&2
return 1 return 1
@ -220,7 +217,7 @@ _autoenv_source() {
# Change to directory of env file, source it and cd back. # Change to directory of env file, source it and cd back.
local new_dir=$PWD local new_dir=$PWD
builtin cd -q $_autoenv_envfile_dir 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++ )) (( _autoenv_debug_indent++ ))
source $env_file source $env_file
(( _autoenv_debug_indent-- )) (( _autoenv_debug_indent-- ))
@ -276,7 +273,7 @@ _autoenv_chpwd_handler() {
_autoenv_source $env_file_leave leave $prev_dir _autoenv_source $env_file_leave leave $prev_dir
fi fi
# Unstash any autostash'd stuff. # Unstash any autostashed stuff.
varstash_dir=$prev_dir autounstash varstash_dir=$prev_dir autounstash
_autoenv_stack_entered_remove $prev_file _autoenv_stack_entered_remove $prev_file

View File

@ -12,7 +12,7 @@ Manually create auth file
Now try to make it accept it Now try to make it accept it
$ unset _autoenv_stack_entered $ _autoenv_stack_entered=()
$ rm $AUTOENV_ENV_FILENAME $ rm $AUTOENV_ENV_FILENAME
$ _autoenv_ask_for_yes() { echo "yes" } $ _autoenv_ask_for_yes() { echo "yes" }
$ cd . $ cd .
@ -33,11 +33,11 @@ The last "ENTERED" is because it executed the command.
Now lets see that it actually checks the shasum value. Now lets see that it actually checks the shasum value.
$ unset _autoenv_stack_entered $ _autoenv_stack_entered=()
$ cd . $ cd .
ENTERED ENTERED
$ unset _autoenv_stack_entered $ _autoenv_stack_entered=()
$ rm $AUTOENV_ENV_FILENAME $ rm $AUTOENV_ENV_FILENAME
$ test_autoenv_add_to_env $PWD/.env mischief $ test_autoenv_add_to_env $PWD/.env mischief
$ cd . $ cd .
@ -56,7 +56,7 @@ Now lets see that it actually checks the shasum value.
Now, will it take no for an answer? Now, will it take no for an answer?
$ unset _autoenv_stack_entered $ _autoenv_stack_entered=()
$ rm $AUTOENV_ENV_FILENAME $ rm $AUTOENV_ENV_FILENAME
$ _autoenv_ask_for_yes() { echo "no"; return 1 } $ _autoenv_ask_for_yes() { echo "no"; return 1 }
$ cd . $ cd .

View File

@ -1,6 +1,10 @@
# Ensure we have our mocked out AUTOENV_ENV_FILENAME # Ensure we have our mocked out AUTOENV_ENV_FILENAME
# (via .zshenv). # (via .zshenv).
# Treat unset variables as errors.
# Not handled in varstash yet.
# setopt nounset
[[ $AUTOENV_ENV_FILENAME[0,4] == '/tmp' ]] || return 1 [[ $AUTOENV_ENV_FILENAME[0,4] == '/tmp' ]] || return 1
# Reset any authentication. # Reset any authentication.
@ -8,7 +12,7 @@ echo -n > $AUTOENV_ENV_FILENAME
# Add file $1 (with optional hash $2) to authentication file. # Add file $1 (with optional hash $2) to authentication file.
test_autoenv_add_to_env() { 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. # Add enter and leave env files to authentication file.

View File

@ -36,31 +36,23 @@ Test autounstashing when leaving a directory. {{{
Setup: Setup:
$ unset VAR
$ cd sub $ cd sub
ENTER ENTER
$ echo 'echo ENTER; autostash VAR=changed' > $AUTOENV_FILE_ENTER $ echo 'echo ENTER; autostash VAR=changed' > $AUTOENV_FILE_ENTER
$ echo 'echo LEAVE; echo "no explicit call to autounstash"' > $AUTOENV_FILE_LEAVE $ echo 'echo LEAVE; echo "no explicit call to autounstash"' > $AUTOENV_FILE_LEAVE
$ test_autoenv_auth_env_files $ test_autoenv_auth_env_files
$VAR is empty: $VAR is unset:
$ echo VAR:$VAR $ echo VAR_set:$+VAR
VAR: VAR_set:0
Set it: Trigger the autostashing in the enter file.
$ VAR=orig
$ cd .. $ cd ..
LEAVE LEAVE
no explicit call to autounstash 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 $ cd sub
ENTER ENTER
$ echo $VAR $ echo $VAR
@ -71,9 +63,8 @@ Now leave again.
$ cd .. $ cd ..
LEAVE LEAVE
no explicit call to autounstash no explicit call to autounstash
$ echo $VAR $ echo VAR_set:$+VAR
orig VAR_set:0
Remove the leave file, auto-unstashing should still happen. Remove the leave file, auto-unstashing should still happen.
@ -83,7 +74,20 @@ Remove the leave file, auto-unstashing should still happen.
$ echo $VAR $ echo $VAR
changed changed
$ cd .. $ cd ..
$ echo VAR_set:$+VAR
VAR_set:0
And once again where a value gets restored.
$ VAR=orig_2
$ echo $VAR $ echo $VAR
orig orig_2
$ cd sub
ENTER
$ echo $VAR
changed
$ cd ..
$ echo $VAR
orig_2
}}} }}}