-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
54 lines (43 loc) · 1.16 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package flex_test
import (
"io/ioutil"
"os"
"path/filepath"
. "gopkg.in/check.v1"
"github.com/niemeyer/flex"
)
var _ = Suite(&ConfigSuite{})
type ConfigSuite struct {
realHome string
tempHome string
confPath string
}
func (s *ConfigSuite) SetUpTest(c *C) {
s.realHome = os.Getenv("HOME")
s.tempHome = c.MkDir()
s.confPath = filepath.Join(s.tempHome, ".flex", "config.yaml")
os.Setenv("HOME", s.tempHome)
os.Mkdir(filepath.Dir(s.confPath), 0700)
}
func (s *ConfigSuite) TearDownTest(c *C) {
os.Setenv("HOME", s.realHome)
}
func (s *ConfigSuite) TestReadConfigMissing(c *C) {
cfg, err := flex.LoadConfig()
c.Assert(err, IsNil)
c.Assert(cfg.TestOption, Equals, "")
}
func (s *ConfigSuite) TestLoadConfig(c *C) {
err := ioutil.WriteFile(s.confPath, []byte("test-option: value"), 0644)
c.Assert(err, IsNil)
cfg, err := flex.LoadConfig()
c.Assert(err, IsNil)
c.Assert(cfg.TestOption, Equals, "value")
}
func (s *ConfigSuite) TestSaveConfig(c *C) {
err := flex.SaveConfig(&flex.Config{TestOption: "value"})
c.Assert(err, IsNil)
data, err := ioutil.ReadFile(s.confPath)
c.Assert(err, IsNil)
c.Assert(string(data), Matches, "(?s)test-option: value\n.*")
}