github.com/gfleury/gobbs@v0.0.0-20200831213239-44ca2b94c1a1/cmd/root_test.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "testing" 8 9 "github.com/gfleury/gobbs/common" 10 check "gopkg.in/check.v1" 11 ) 12 13 func Test(t *testing.T) { check.TestingT(t) } 14 15 type S struct { 16 } 17 18 var _ = check.Suite(&S{}) 19 20 func (s *S) TestInitConfig(c *check.C) { 21 cfgFile = fmt.Sprintf("%s/config.yaml", os.TempDir()) 22 defer os.Remove(cfgFile) 23 // Run once to create file 24 initConfig() 25 26 // Run twice with file already created 27 initConfig() 28 } 29 30 func (s *S) TestPersistConfig(c *check.C) { 31 cfgFile = fmt.Sprintf("%s/config.yaml", os.TempDir()) 32 defer os.Remove(cfgFile) 33 34 host := stashInfo.Host() 35 user := stashInfo.Credential().User() 36 passwd := stashInfo.Credential().Passwd() 37 stashInfo.Credential().New() 38 39 *host = "stash.example.com" 40 *user = "user" 41 *passwd = "password" 42 // Run once to create file 43 initConfig() 44 45 err := persistConfig() 46 c.Assert(err, check.IsNil) 47 48 content, err := ioutil.ReadFile(cfgFile) 49 c.Assert(err, check.IsNil) 50 c.Assert(string(content), check.Equals, ""+"stash.example.com:\n"+" passwd: password\n"+" user: user\n") 51 } 52 53 func (s *S) TestGetContext(c *check.C) { 54 ctx := common.APIClientContext(&stashInfo) 55 56 c.Check(ctx.Value(common.StashInfoKey), check.Equals, &stashInfo) 57 } 58 59 func (s *S) TestConfigWithEnvVars(c *check.C) { 60 os.Setenv("GOBBS_USER", "user") 61 initConfig() 62 c.Assert(common.Config().Get("user"), check.Equals, "user") 63 }