postmodern/chruby — pull request #301 by nathanl (2014-12-03 16:40:57+00:00)
Title: zsh: switch rubies after changing pwd, not before

I suspect this PR will be rejected as "you misunderstand" or will need improving. Here's the problem I'm trying to solve: when I change folders, it appears that `auto.sh` tries to switch Rubies using the folder I'm leaving, not the folder I'm entering.

## Setup

- use zsh
- `mkdir -p switching_test/sub`
- `echo 'ruby-top' > switching_test/.ruby-version`
- `echo 'ruby-sub' > switching_test/sub/.ruby-version`

## Tests

- `cd switching_test`
- Expected result: `chruby: unknown Ruby: ruby-top`
- Actual result: no error
- `cd sub`
- Expected result: `chruby: unknown Ruby: ruby-sub`
- Actual result: `chruby: unknown Ruby: ruby-top`

## Investigation

At the top of the `chruby()` function, I added `echo "chruby called in $PWD"`. This confirmed for me that `chruby()` was being called in the directory I'm switching from, not to.

[This documentation on hook functions](http://zsh.sourceforge.net/Doc/Release/Functions.html#Hook-Functions) made me think that `chpwd` might be the right hook, rather than `preexec`. Changing it does fix my problem; hence this PR.

## Misgivings

- You list "does not hook CD" as an anti-feature. Do you consider this hooking CD?
- It seems too obvious. Probably there's a reason you want to execute with every command? It does appear to me that the bash section executes `chruby_auto` **after** the user's command (using `&&`), not before...
- I'm not sure how to run the tests properly, much less add one; `test/runner` gives me a bunch of errors with backtraces like `/usr/share/shunit2/shunit2:.:129: no such file or directory: /usr/share/shunit2/shunit2 \n test/chruby_auto_test.sh:zsh:130: use 'exit' to exit.` I installed `chruby` with homebrew and it appears to be looking in the installation folder for `shunit2`, but I don't know why.

So: thoughts?