-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoml_test.go
231 lines (192 loc) · 5.99 KB
/
toml_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright (c) Christoph Berger. All rights reserved.
// Use of this source code is governed by the BSD (3-Clause)
// License that can be found in the LICENSE.txt file.
//
// This source code may import third-party source code whose
// licenses are provided in the respective license files.
package start
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/laurent22/toml-go"
. "github.com/smartystreets/goconvey/convey"
)
func TestReadTomlFile(t *testing.T) {
Convey("Given a file \"test.toml\" in test/", t, func() {
var tomlDoc toml.Document
var err error
cfg := new(configFile)
Convey("then readTomlFile('./test/test.toml') should find the file", func() {
tomlDoc, err = cfg.readTomlFile("./test/test.toml")
So(err, ShouldBeNil)
Convey("and it should read all test values", func() {
So(tomlDoc.GetString("astring"), ShouldEqual, "From Config File")
So(tomlDoc.GetBool("abool"), ShouldEqual, true)
So(tomlDoc.GetInt("anint"), ShouldEqual, 42)
So(tomlDoc.GetDate("adate").Equal(time.Date(2014, time.August, 17, 9, 25, 0, 0, time.UTC)), ShouldBeTrue)
})
})
})
}
func TestConfigFile(t *testing.T) {
Convey("When passing an absolute path to an existing TOML file to newConfigFile", t, func() {
tomlfile, err := filepath.Abs("test/test.toml")
So(err, ShouldBeNil)
Convey("then newConfigFile loads "+tomlfile+" and returns a new configFile", func() {
cfg, _ := newConfigFile(tomlfile)
So(cfg, ShouldNotBeNil)
})
})
Convey("When passing an absolute directory to newConfigFile", t, func() {
tomlfile, err := filepath.Abs("test")
So(err, ShouldBeNil)
Convey("then newConfigFile loads start.toml from that directory and returns a new configFile", func() {
cfg, _ := newConfigFile(tomlfile)
So(cfg, ShouldNotBeNil)
Convey("and appName() should return start", func() {
So(appName(), ShouldEqual, "start")
})
})
})
Convey("When passing just a file name to newConfigFile", t, func() {
tomlname := "test.toml"
var cfgdir, tomlpath string
cfgdirCreated := false // If the test needs to create the config dir, remember to remove it afterwards
Convey("and the file exists in the config directory", func() {
cfgdir, exists := GetUserConfigDir()
if !exists {
err := os.Mkdir(cfgdir, 0700)
if err != nil {
panic(err)
}
cfgdirCreated = true
}
tomlpath = filepath.Join(cfgdir, tomlname)
_, err := os.Create(tomlpath)
if err != nil {
panic(err)
}
Convey("then newConfigFile should find the file ("+tomlpath+")", func() {
cfg, _ := newConfigFile(tomlname)
So(cfg, ShouldNotBeNil)
})
})
Convey("and the directory is specified by the env var START_CFGPATH", func() {
os.Setenv("START_CFGPATH", "test")
Convey("then newConfigFile should find the file", func() {
cfg, _ := newConfigFile(tomlname)
So(cfg, ShouldNotBeNil)
})
})
Convey("and the file is in the working directory", func() {
pwd, _ := os.Getwd()
tomlpath = filepath.Join(pwd, tomlname)
os.Create(tomlpath)
Convey("then newConfigFile should find the file", func() {
cfg, _ := newConfigFile(tomlname)
So(cfg, ShouldNotBeNil)
})
})
Reset(func() {
os.Remove(tomlpath)
if cfgdirCreated {
os.Remove(cfgdir)
}
os.Setenv("START_CFGPATH", "")
})
})
Convey("When passing no file name to newConfigFile", t, func() {
var cfgdir, tomlpath string
cfgdir_created := false // If the test needs to create the config dir, remember to remove it afterwards
Convey("and the file 'test.toml' exists in the user's config directory", func() {
cfgdir, exists := GetUserConfigDir()
if !exists {
err := os.Mkdir(cfgdir, 0700)
if err != nil {
panic(err)
}
cfgdir_created = true
}
tomlpath = filepath.Join(cfgdir, "test.toml")
_, err := os.Create(tomlpath)
if err != nil {
panic(err)
}
Convey("then newConfigFile should find the file", func() {
cfg, _ := newConfigFile("")
So(cfg, ShouldNotBeNil)
})
})
Convey("and the file is specified by the env var START_CFGPATH", func() {
os.Setenv("START_CFGPATH", "test/test.toml")
Convey("then newConfigFile should find the file", func() {
cfg, _ := newConfigFile("")
So(cfg, ShouldNotBeNil)
})
})
Convey("and the file 'start.toml' is in the working directory", func() {
wd, _ := os.Getwd()
tomlpath = filepath.Join(wd, "start.toml")
os.Create(tomlpath)
Convey("then newConfigFile should find the file", func() {
cfg, _ := newConfigFile("")
So(cfg, ShouldNotBeNil)
})
})
Reset(func() {
os.Remove(tomlpath)
if cfgdir_created {
os.Remove(cfgdir)
}
os.Setenv("START_CFGPATH", "")
})
})
Convey("When passing no file name to newConfigFile", t, func() {
var cfgdir, tomlpath string
cfgdir_created := false // If the test needs to create the config dir, remember to remove it afterwards
Convey("and the file 'test.toml' exists in the config directory", func() {
cfgdir, exists := GetUserConfigDir()
if !exists {
err := os.Mkdir(cfgdir, 0700)
if err != nil {
panic(err)
}
cfgdir_created = true
}
tomlpath = filepath.Join(cfgdir, "test.toml")
_, err := os.Create(tomlpath)
if err != nil {
panic(err)
}
Convey("then newConfigFile should find the file", func() {
cfg, _ := newConfigFile("")
So(cfg, ShouldNotBeNil)
})
})
Convey("and the file is specified by the env var START_CFGPATH", func() {
os.Setenv("START_CFGPATH", "test/test.toml")
Convey("then newConfigFile should find the file", func() {
cfg, _ := newConfigFile("")
So(cfg, ShouldNotBeNil)
})
})
Convey("and the file 'start.toml' is in the working directory", func() {
wd, _ := os.Getwd()
tomlpath = filepath.Join(wd, "start.toml")
os.Create(tomlpath)
Convey("then newConfigFile should find the file", func() {
cfg, _ := newConfigFile("")
So(cfg, ShouldNotBeNil)
})
})
Reset(func() {
os.Remove(tomlpath)
if cfgdir_created {
os.Remove(cfgdir)
}
os.Setenv("START_CFGPATH", "")
})
})
}