github.com/searchspring/haus@v0.1.8-0.20200414161854-a7ca8bb9ea93/app/config_test.go (about) 1 package haus 2 3 import ( 4 "os" 5 "io/ioutil" 6 "testing" 7 8 "gopkg.in/yaml.v2" 9 ) 10 11 func TestReadConfig(t *testing.T) { 12 13 // Test missing config 14 _,err := ReadConfig("bogusbogus","","master", "./hauscfg", make(map[string]string) ) 15 if err == nil { 16 t.Error("Expected missing file error, didn't get it.") 17 } 18 19 // Test broken config 20 _,err = ReadConfig("config.go","", "master", "./hauscfg", make(map[string]string) ) 21 if err == nil { 22 t.Error("Expected yaml error, didn't get it.") 23 } 24 25 // Test normal config 26 config,err := ReadConfig("../haus.yml","", "master", "./hauscfg", make(map[string]string) ) 27 if err != nil { 28 t.Error(err) 29 } 30 v := config.Name 31 if v != "Your Name" { 32 t.Error("Expected Your Name, got ", v) 33 } 34 v = config.Email 35 if v != "email@address.com" { 36 t.Error("Expected email@address.com, got ",v) 37 } 38 39 // Test user config with normal config 40 testpath_usrcfg, err := ioutil.TempDir("", "haus_usrcfg") 41 if err != nil { 42 t.Error(err) 43 } 44 defer os.RemoveAll(testpath_usrcfg) 45 46 usrCfg := map[string]string{ 47 "name": "Testy Testerson", 48 "email": "Test@test.com", 49 "path": "/my/test/path", 50 "hausrepo": "https://git@github.com/searchspring/haus.git", 51 } 52 yaml,err := yaml.Marshal(usrCfg) 53 if err != nil { 54 t.Error(err) 55 } 56 usrcfgfile := testpath_usrcfg+"/.hauscfg.yml" 57 err = ioutil.WriteFile(usrcfgfile, yaml, 0644) 58 if err != nil { 59 t.Error(err) 60 } 61 62 // Test autocheckout 63 testpath, err := ioutil.TempDir("", "haus") 64 if err != nil { 65 t.Error(err) 66 } 67 defer os.RemoveAll(testpath) 68 pwd,err := os.Getwd() 69 if err != nil { 70 t.Error(err) 71 } 72 err = os.Chdir(testpath) 73 if err != nil { 74 t.Error(err) 75 } 76 defer os.Chdir(pwd) 77 78 config,err = ReadConfig("bogus", usrcfgfile, "master", "./hauscfg", make(map[string]string)) 79 if err != nil { 80 t.Error(err) 81 } 82 83 // Test with config 84 if config.Name != "Testy Testerson" { 85 t.Error("User config failed, expected Testy Testerson, got ",config.Name) 86 } 87 88 config,err = ReadConfig("./haus.yml", usrcfgfile, "master", "./hauscfg", make(map[string]string) ) 89 if err != nil { 90 t.Error(err) 91 } 92 93 if config.Name != "Testy Testerson" { 94 t.Error("User config failed, expected Testy Testerson, got ",config.Name) 95 } 96 }