github.com/iwataka/ghq@v0.7.5-0.20160611155400-0aa07ac077a9/helpers_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "os/exec" 8 "path/filepath" 9 "strings" 10 11 "github.com/motemen/ghq/utils" 12 ) 13 14 func NewFakeRunner(dispatch map[string]error) utils.RunFunc { 15 return func(cmd *exec.Cmd) error { 16 cmdString := strings.Join(cmd.Args, " ") 17 for cmdPrefix, err := range dispatch { 18 if strings.Index(cmdString, cmdPrefix) == 0 { 19 return err 20 } 21 } 22 panic(fmt.Sprintf("No fake dispatch found for: %s", cmdString)) 23 } 24 } 25 26 func WithGitconfigFile(configContent string) (func(), error) { 27 tmpdir, err := ioutil.TempDir("", "ghq-test") 28 if err != nil { 29 return nil, err 30 } 31 32 tmpGitconfigFile := filepath.Join(tmpdir, "gitconfig") 33 34 ioutil.WriteFile( 35 tmpGitconfigFile, 36 []byte(configContent), 37 0777, 38 ) 39 40 prevGitConfigEnv := os.Getenv("GIT_CONFIG") 41 os.Setenv("GIT_CONFIG", tmpGitconfigFile) 42 43 return func() { 44 os.Setenv("GIT_CONFIG", prevGitConfigEnv) 45 }, nil 46 }