Tarrasch / zsh-autoenv

Autoenv for zsh
693 stars 30 forks source link

Temporarily add chpwd handler to precmd hooks upon loading plugin #98

Closed AndydeCleyre closed 2 years ago

AndydeCleyre commented 2 years ago

This replaces directly calling the handler at this stage, which may not have the functions it needs defined by that time.

Fixes #97

Also breaks something with varstash, so this is still a WIP

AndydeCleyre commented 2 years ago
--- tests/varstash_export.t
+++ tests/varstash_export.t.err
@@ -36,14 +36,11 @@
 Activate autoenv in the subshell.

   $ $TESTSHELL -c "$TEST_SOURCE_AUTOENV; echo \${MYVAR}; echo \$MYEXPORT"
-  ENTER
-  changed
+
   changed_export

 "autounstash" should handle the exported variables.

   $ $TESTSHELL -c "$TEST_SOURCE_AUTOENV; cd ..; echo \${MYVAR:-empty}; echo \$MYEXPORT"
-  ENTER
-  LEAVE
   empty
-  orig_export
+  changed_export
AndydeCleyre commented 2 years ago

So in those TESTSHELL invocations, the usual precmd hooks are not run before the commands. ~That seems to me more of a quirk of the testing setup than expected behavior.~

The following change gets the tests to pass. I know this project hasn't had a lot of activity lately, but @blueyed, can you please let me know what you think about any of this?

diff --git a/tests/varstash_export.t b/tests/varstash_export.t
index 9936d7e..c9a8744 100644
--- a/tests/varstash_export.t
+++ b/tests/varstash_export.t
@@ -35,14 +35,14 @@ The variable is not available in a subshell, only the exported one.

 Activate autoenv in the subshell.

-  $ $TESTSHELL -c "$TEST_SOURCE_AUTOENV; echo \${MYVAR}; echo \$MYEXPORT"
+  $ $TESTSHELL -c "$TEST_SOURCE_AUTOENV; for hook ( precmd \$precmd_functions ) { if (( \$+functions[\$hook] )) \$hook }; echo \${MYVAR}; echo \$MYEXPORT"
   ENTER
   changed
   changed_export

 "autounstash" should handle the exported variables.

-  $ $TESTSHELL -c "$TEST_SOURCE_AUTOENV; cd ..; echo \${MYVAR:-empty}; echo \$MYEXPORT"
+  $ $TESTSHELL -c "$TEST_SOURCE_AUTOENV; for hook ( precmd \$precmd_functions ) { if (( \$+functions[\$hook] )) \$hook }; cd ..; echo \${MYVAR:-empty}; echo \$MYEXPORT"
   ENTER
   LEAVE
   empty

EDIT: I see guess this isn't a safe method if precmd hooks aren't run in subshells like this.

AndydeCleyre commented 2 years ago

I'm coming to think this is a much worse idea than just ensuring I define the functions I need before sourcing the autoenv plugin.