]> Sergey Matveev's repositories - zsh-autoenv.git/commitdiff
Fixes after using `setopt nounset` during tests
authorDaniel Hahler <git@thequod.de>
Mon, 8 Dec 2014 20:37:25 +0000 (21:37 +0100)
committerDaniel Hahler <git@thequod.de>
Mon, 8 Dec 2014 20:37:25 +0000 (21:37 +0100)
Also add tests for unset variable with varstash.

autoenv.zsh
tests/autoenv.t
tests/setup.sh
tests/varstash.t

index f87f350ba99bed31f9add25f27b4e8529e3af7a0..db77553cdaaff4ec5593a719906671934a577f35 100644 (file)
@@ -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
index 19d0592d95f28c9f90c27b14da00db1b55455907..701e3b463967d21abb56e773929655f6d8ad343d 100644 (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 .
index 90bfc510b7a37ba88641af01bcc4a022459cedef..5c6ae9eb6173c7eb31c9145e0b1f0fa357fb17b9 100644 (file)
@@ -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.
index 30a45dd8f618bf1515e99ac7c3111c74843a2226..cdb47a8d30894e9e9fb34471a5e86571941335a7 100644 (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
 
 }}}