gildas/puppet-homebrew — pull request #13 by dominics (2014-12-17 02:18:20+00:00)
Title: Chdir to execute in a directory the user can access

Brew needs to be able to retrieve information about the current working directory when it runs.

If we're running puppet as root (e.g. from something like cron), and we're managing homebrew for a non-root user account, it's very likely we'll be running with a current working directory that isn't readable by the homebrew user (e.g. `/var/root`). This means that when we execute a brew command, brew fails with errors about not being able to access the CWD. In effect, the current version of the providers are doing this:

```
# cd /var/root # (or any dir inaccessible to someuser)
# sudo -u someuser brew list --versions
```

Which results in:

> shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
chdir: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
chdir: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
The current working directory doesn't exist, cannot proceed.

In puppet, these errors look like:

> Could not prefetch package provider 'brew': Could not list packages: Execution of '/usr/local/bin/brew list --versions' returned 1: shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied

In this PR, we change the providers to `cd` to the home dir of the user we'll be executing brew as just before they execute Brew commands. (The assumption is that a user's own homedir is accessible to them.)