func WithWorkingDirectory(path string, f func()) {
	if _, err := os.Stat(path); os.IsNotExist(err) {
		panic(err)
	}
	originalPath, err := os.Getwd()
	if err != nil {
		panic(err)
	}
	if err := os.Chdir(path); err != nil {
		panic(err)
	}
	f()
	if err := os.Chdir(originalPath); err != nil {
		panic(err)
	}
}
WithWorkingDirectory executes the passed function within the context of
the passed working directory path. If the directory does not exist, panic.
package devtest
import (
	"bytes"
	"flag"
	"fmt"
	"io"
	"log"
	"os"
	"path/filepath"
	"runtime"
	"strings"
	"testing"
	"github.com/puma/puma-dev/homedir"
	"github.com/stretchr/testify/assert"
)
var (
	appSymlinkHome      = "~/.puma-dev"
	DebugLoggingEnabled = os.Getenv("DEBUG_LOG") == "1"
	StubbedArgs         = make(map[string]int)
	_, b, _, _  = runtime.Caller(0)
	ProjectRoot = filepath.Join(filepath.Dir(b), "..", "..")
)
func SetFlagOrFail(t *testing.T, flagName string, flagValue string) {
	if err := flag.Set(flagName, flagValue); err != nil {
		assert.Fail(t, err.Error())
	}
}
func LogDebugf(msg string, vars ...interface{}) {
	if DebugLoggingEnabled {
		log.Printf(strings.Join([]string{"[DEBUG]", msg}, " "), vars...)
	}
}
func StubCommandLineArgs(args ...string) {
	for _, arg := range args {
		StubbedArgs[arg] += 1
	}
	for arg := range StubbedArgs {
		stubbedFlagName := strings.Replace(arg, "-", "", -1)
		if fl := flag.Lookup(stubbedFlagName); fl != nil {
			oldValue := fl.Value.String()
			if err := flag.Set(fl.Name, fl.DefValue); err == nil {
				LogDebugf("reset flag %s to %s (was %s)", fl.Name, fl.Value.String(), oldValue)
			}
		}
	}
	os.Args = append([]string{os.Args[0]}, args...)
	flag.Parse()
}
func EnsurePumaDevDirectory() {
	path, err := homedir.Expand(appSymlinkHome)
	if err != nil {
		panic(err)
	}
	if fi, err := os.Stat(path); err == nil && fi.IsDir() {
		return
	}
	if err := os.Mkdir(path, 0755); err != nil {
		panic(err)
	}
}
func WithStdoutCaptured(f func()) string {
	osStdout := os.Stdout
	r, w, err := os.Pipe()
	if err != nil {
		panic(err)
	}
	os.Stdout = w
	outC := make(chan string)
	go func() {
		var buf bytes.Buffer
		if _, err := io.Copy(&buf, r); err != nil {
			panic(err)
		}
		outC <- buf.String()
	}()
	f()
	w.Close()
	os.Stdout = osStdout
	out := <-outC
	return out
}
func RemoveDirectoryOrFail(t *testing.T, path string) {
	if !DirExists(path) {
		assert.FailNow(t, fmt.Sprintf("%s does not exist", path))
	}
	if err := os.RemoveAll(path); err != nil {
		assert.Fail(t, err.Error())
	} else {
		LogDebugf("removed %s\n", path)
	}
}
func MakeDirectoryOrFail(t *testing.T, path string) func() {
	if err := os.MkdirAll(path, 0755); err != nil {
		assert.Fail(t, err.Error())
	}
	return func() {
		RemoveDirectoryOrFail(t, path)
	}
}