From 5b553ba1ed12cd66bc346817f07dd9ae280b4b2b Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sat, 15 Nov 2014 20:59:39 +0100 Subject: [PATCH] varstash integration Fixes: https://github.com/Tarrasch/zsh-autoenv/issues/5 --- autoenv.zsh | 22 +++++++++++++++++----- tests/varstash.t | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 tests/varstash.t diff --git a/autoenv.zsh b/autoenv.zsh index bb395e2..a1acc36 100644 --- a/autoenv.zsh +++ b/autoenv.zsh @@ -82,14 +82,26 @@ _dotenv_check_authorized_env_file() { return 0 } +: ${_dotenv_sourced_varstash:=0} +_dotenv_this_dir=${0:A:h} + _dotenv_source() { local env_file=$1 _dotenv_event=$2 - _dotenv_cwd=$PWD + _dotenv_cwd=$3 - builtin cd -q ${env_file:h} - source $env_file + # Source varstash library once. + if [[ $_dotenv_sourced_varstash == 0 ]]; then + source $_dotenv_this_dir/lib/varstash + export _dotenv_sourced_varstash=1 + fi + # varstash_dir=${env_file:h} + + # Change to directory of env file, source it and cd back. + local new_dir=$PWD builtin cd -q $_dotenv_cwd + source $env_file + builtin cd -q $new_dir unset _dotenv_event _dotenv_cwd } @@ -103,7 +115,7 @@ _dotenv_chpwd_handler() { if ! [[ ${PWD}/ == ${prev_dir}/* ]]; then local env_file_leave=$prev_dir/$DOTENV_FILE_LEAVE if _dotenv_check_authorized_env_file $env_file_leave; then - _dotenv_source $env_file_leave leave + _dotenv_source $env_file_leave leave $prev_dir fi # Remove this entry from the stack. _dotenv_stack_entered=(${_dotenv_stack_entered#$prev_dir}) @@ -136,7 +148,7 @@ _dotenv_chpwd_handler() { _dotenv_stack_entered+=(${env_file_dir}) - _dotenv_source $env_file enter + _dotenv_source $env_file enter $PWD } autoload -U add-zsh-hook diff --git a/tests/varstash.t b/tests/varstash.t new file mode 100644 index 0000000..75a4077 --- /dev/null +++ b/tests/varstash.t @@ -0,0 +1,36 @@ +Test varstash integration. + + $ source $TESTDIR/setup.sh + +Setup test environment. + +# Defaults: +# $ DOTENV_FILE_ENTER=.env +# $ DOTENV_FILE_LEAVE=.env.leave +# $ DOTENV_HANDLE_LEAVE=1 + + $ mkdir sub + $ cd sub + $ echo "autostash FOO=baz" > $DOTENV_FILE_ENTER + $ echo "autounstash" > $DOTENV_FILE_LEAVE + +Manually create auth file + + $ echo "$PWD/$DOTENV_FILE_ENTER:$(echo $(<$DOTENV_FILE_ENTER) | shasum)" > $ENV_AUTHORIZATION_FILE + $ echo "$PWD/$DOTENV_FILE_LEAVE:$(echo $(<$DOTENV_FILE_LEAVE) | shasum)" >> $ENV_AUTHORIZATION_FILE + +Set environment variable. + + $ FOO=bar + +Activating the env stashes it and applies a new value. + + $ cd . + $ echo $FOO + baz + +Leaving the directory unstashes it. + + $ cd .. + $ echo $FOO + bar -- 2.44.0