github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/internal/config/testing.go (about) 1 package config 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 "path/filepath" 8 ) 9 10 func StubBackupConfig() func() { 11 orig := BackupConfigFile 12 BackupConfigFile = func(_ string) error { 13 return nil 14 } 15 16 return func() { 17 BackupConfigFile = orig 18 } 19 } 20 21 func StubWriteConfig(wc io.Writer, wh io.Writer) func() { 22 orig := WriteConfigFile 23 WriteConfigFile = func(fn string, data []byte) error { 24 switch filepath.Base(fn) { 25 case "config.yml": 26 _, err := wc.Write(data) 27 return err 28 case "hosts.yml": 29 _, err := wh.Write(data) 30 return err 31 default: 32 return fmt.Errorf("write to unstubbed file: %q", fn) 33 } 34 } 35 return func() { 36 WriteConfigFile = orig 37 } 38 } 39 40 func stubConfig(main, hosts string) func() { 41 orig := ReadConfigFile 42 ReadConfigFile = func(fn string) ([]byte, error) { 43 switch filepath.Base(fn) { 44 case "config.yml": 45 if main == "" { 46 return []byte(nil), os.ErrNotExist 47 } else { 48 return []byte(main), nil 49 } 50 case "hosts.yml": 51 if hosts == "" { 52 return []byte(nil), os.ErrNotExist 53 } else { 54 return []byte(hosts), nil 55 } 56 default: 57 return []byte(nil), fmt.Errorf("read from unstubbed file: %q", fn) 58 } 59 60 } 61 return func() { 62 ReadConfigFile = orig 63 } 64 }