Skip to content

Commit

Permalink
add config tests (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktr authored Feb 17, 2018
1 parent e33da1a commit ded70da
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
21 changes: 12 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/mitchellh/mapstructure"
)

const (
var (
localConfigName = ".evans.toml"
)

Expand Down Expand Up @@ -110,9 +110,14 @@ func init() {
}
}

func setupConfig(c *Config) {
c.REPL.Server = c.Server
c.Env.Server = c.Server
}

func Get() *Config {
var config Config
err := mapstructure.Decode(mConfig.Get(), &config)
var global Config
err := mapstructure.Decode(mConfig.Get(), &global)
if err != nil {
panic(err)
}
Expand All @@ -124,19 +129,17 @@ func Get() *Config {

// if local config missing, return global config
if local == nil {
return &config
setupConfig(&global)
return &global
}

ic, err := mapstruct.Map(&config, local)
ic, err := mapstruct.Map(&global, local)
if err != nil {
panic(err)
}

c := ic.(*Config)

// TODO: use more better method
c.REPL.Server = c.Server
c.Env.Server = c.Server
setupConfig(c)

return c
}
Expand Down
35 changes: 35 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package config

import (
"io/ioutil"
"testing"

"github.com/stretchr/testify/require"
)

func TestConfig_Get(t *testing.T) {
checkValues := func() {
c := Get()
_ = c.REPL.Server.Host
_ = c.Env.Server.Host
}

t.Run("no local config", func(t *testing.T) {
checkValues()
})

t.Run("has local config", func(t *testing.T) {
f, err := ioutil.TempFile("", "")
require.NoError(t, err)
defer f.Close()

backup := localConfigName
defer func() {
localConfigName = backup
}()

localConfigName = f.Name()

checkValues()
})
}
2 changes: 1 addition & 1 deletion meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package meta

const (
Name = "evans"
Version = "0.2.6"
Version = "0.2.7"
)

0 comments on commit ded70da

Please sign in to comment.