diff --git a/Makefile b/Makefile index 7b1b23f..f59c91d 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ TESTS:=$(wildcard tests/*.t) uniq = $(if $1,$(firstword $1) $(call uniq,$(filter-out $(firstword $1),$1))) _TESTS_REL_AND_ABS:=$(call uniq,$(abspath $(TESTS)) $(TESTS)) $(_TESTS_REL_AND_ABS): - ZDOTDIR="${CURDIR}/tests" cram --shell=zsh -v $@ + cram --shell=zsh -v $@ .PHONY: $(_TESTS_REL_AND_ABS) .PHONY: itest test diff --git a/autoenv.zsh b/autoenv.zsh index 561a336..c569ef8 100644 --- a/autoenv.zsh +++ b/autoenv.zsh @@ -74,11 +74,24 @@ _autoenv_stack_entered_remove() { } # Is the given entry already in the stack? +# This checks for the env_file ($1) as-is and with symlinks resolved. _autoenv_stack_entered_contains() { local env_file=$1 + local f i if (( ${+_autoenv_stack_entered[(r)${env_file}]} )); then # Entry is in stack. - if [[ $_autoenv_stack_entered_mtime[$env_file] == $(_autoenv_get_file_mtime $env_file) ]]; then + f=$env_file + else + for i in $_autoenv_stack_entered; do + if [[ ${i:A} == ${env_file:A} ]]; then + # Entry is in stack (compared with resolved symlinks). + f=$i + break + fi + done + fi + if [[ -n $f ]]; then + if [[ $_autoenv_stack_entered_mtime[$f] == $(_autoenv_get_file_mtime $f) ]]; then # Entry has the expected mtime. return fi @@ -275,8 +288,8 @@ _autoenv_chpwd_handler() { if [[ $AUTOENV_HANDLE_LEAVE == 1 ]] && (( $#_autoenv_stack_entered )); then local prev_file prev_dir for prev_file in ${_autoenv_stack_entered}; do - prev_dir=${prev_file:A:h} - if ! [[ ${PWD:A}/ == ${prev_dir}/* ]]; then + prev_dir=${prev_file:h} + if ! [[ ${PWD}/ == ${prev_dir}/* ]]; then local env_file_leave=$prev_dir/$AUTOENV_FILE_LEAVE if _autoenv_check_authorized_env_file $env_file_leave; then _autoenv_source $env_file_leave leave $prev_dir diff --git a/tests/cwd.t b/tests/cwd.t index ee586b4..e0ae132 100644 --- a/tests/cwd.t +++ b/tests/cwd.t @@ -33,5 +33,8 @@ Check that symlinked dirs get handled correctly. $ cd sub_linked ENTERED: PWD:sub_linked pwd:sub_linked from:cwd.t to:sub_linked event:enter $ cd sub2 - ENTERED: PWD:sub2 pwd:sub2 from:sub_linked to:sub2 event:enter - $ cd . + + $ cd ../.. + LEFT: PWD:cwd.t pwd:cwd.t from:sub2 to:cwd.t event:leave + $ cd sub_linked/sub2 + ENTERED: PWD:sub2 pwd:sub2 from:cwd.t to:sub2 event:enter diff --git a/tests/leave.t b/tests/leave.t index 227da71..fb7ee37 100644 --- a/tests/leave.t +++ b/tests/leave.t @@ -88,3 +88,28 @@ Test that .env is sourced only once with AUTOENV_HANDLE_LEAVE=0. ENTERED $ cd .. $ cd sub + + +Test that "leave" is not triggered when entering an outside dir via symlink. + + $ AUTOENV_HANDLE_LEAVE=1 + $ cd .. + LEFT + $ mkdir outside + $ cd outside + $ echo 'echo ENTERED outside: PWD:${PWD:t} pwd:${${"$(pwd)"}:t} from:${autoenv_from_dir:t} to:${autoenv_to_dir:t} event:${autoenv_event}' > .env + $ echo 'echo LEFT outside: PWD:${PWD:t} pwd:${${"$(pwd)"}:t} from:${autoenv_from_dir:t} to:${autoenv_to_dir:t} event:${autoenv_event}' > .env.leave + $ test_autoenv_auth_env_files + + $ cd .. + $ ln -s ../outside sub/symlink + $ cd sub + ENTERED + $ cd symlink + ENTERED outside: PWD:symlink pwd:symlink from:sub to:symlink event:enter + + $ cd ../.. + LEFT + LEFT outside: PWD:leave.t pwd:leave.t from:symlink to:leave.t event:leave + $ cd sub/symlink + ENTERED outside: PWD:symlink pwd:symlink from:leave.t to:symlink event:enter