forked from miguelbranco80/goconfig
-
Notifications
You must be signed in to change notification settings - Fork 80
/
all_test.go
400 lines (329 loc) · 11.6 KB
/
all_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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Copyright 2009 The "config" Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"bufio"
"os"
"reflect"
"strings"
"testing"
)
const (
tmpFilename = "testdata/__test.go"
sourceFilename = "testdata/source.cfg"
targetFilename = "testdata/target.cfg"
)
func testGet(t *testing.T, c *Config, section string, option string,
expected interface{}) {
ok := false
switch expected.(type) {
case string:
v, _ := c.String(section, option)
if v == expected.(string) {
ok = true
}
case int:
v, _ := c.Int(section, option)
if v == expected.(int) {
ok = true
}
case bool:
v, _ := c.Bool(section, option)
if v == expected.(bool) {
ok = true
}
default:
t.Fatalf("Bad test case")
}
if !ok {
v, _ := c.String(section, option)
t.Errorf("Get failure: expected different value for %s %s (expected: [%#v] got: [%#v])", section, option, expected, v)
}
}
// TestInMemory creates configuration representation and run multiple tests in-memory.
func TestInMemory(t *testing.T) {
c := NewDefault()
// == Test empty structure
// should be empty
if len(c.Sections()) != 1 {
t.Errorf("Sections failure: invalid length")
}
// test presence of missing section
if c.HasSection("no-section") {
t.Errorf("HasSection failure: invalid section")
}
// get options for missing section
_, err := c.Options("no-section")
if err == nil {
t.Errorf("Options failure: invalid section")
}
// test presence of option for missing section
if c.HasOption("no-section", "no-option") {
t.Errorf("HasSection failure: invalid/section/option")
}
// get value from missing section/option
_, err = c.String("no-section", "no-option")
if err == nil {
t.Errorf("String failure: got value for missing section/option")
}
// get value from missing section/option
_, err = c.Int("no-section", "no-option")
if err == nil {
t.Errorf("Int failure: got value for missing section/option")
}
// remove missing section
if c.RemoveSection("no-section") {
t.Errorf("RemoveSection failure: removed missing section")
}
// remove missing section/option
if c.RemoveOption("no-section", "no-option") {
t.Errorf("RemoveOption failure: removed missing section/option")
}
// == Fill up structure
// add section
if !c.AddSection("section1") {
t.Errorf("AddSection failure: false on first insert")
}
// re-add same section
if c.AddSection("section1") {
t.Errorf("AddSection failure: true on second insert")
}
// default section always exists
if c.AddSection(DEFAULT_SECTION) {
t.Errorf("AddSection failure: true on default section insert")
}
// add option/value
if !c.AddOption("section1", "option1", "value1") {
t.Errorf("AddOption failure: false on first insert")
}
testGet(t, c, "section1", "option1", "value1") // read it back
// overwrite value
if c.AddOption("section1", "option1", "value2") {
t.Errorf("AddOption failure: true on second insert")
}
testGet(t, c, "section1", "option1", "value2") // read it back again
// remove option/value
if !c.RemoveOption("section1", "option1") {
t.Errorf("RemoveOption failure: false on first remove")
}
// remove again
if c.RemoveOption("section1", "option1") {
t.Errorf("RemoveOption failure: true on second remove")
}
// read it back again
_, err = c.String("section1", "option1")
if err == nil {
t.Errorf("String failure: got value for removed section/option")
}
// remove existing section
if !c.RemoveSection("section1") {
t.Errorf("RemoveSection failure: false on first remove")
}
// remove again
if c.RemoveSection("section1") {
t.Errorf("RemoveSection failure: true on second remove")
}
// == Test types
// add section
if !c.AddSection("section2") {
t.Errorf("AddSection failure: false on first insert")
}
// add number
if !c.AddOption("section2", "test-number", "666") {
t.Errorf("AddOption failure: false on first insert")
}
testGet(t, c, "section2", "test-number", 666) // read it back
// add 'yes' (bool)
if !c.AddOption("section2", "test-yes", "yes") {
t.Errorf("AddOption failure: false on first insert")
}
testGet(t, c, "section2", "test-yes", true) // read it back
// add 'false' (bool)
if !c.AddOption("section2", "test-false", "false") {
t.Errorf("AddOption failure: false on first insert")
}
testGet(t, c, "section2", "test-false", false) // read it back
// == Test cycle
c.AddOption(DEFAULT_SECTION, "opt1", "%(opt2)s")
c.AddOption(DEFAULT_SECTION, "opt2", "%(opt1)s")
_, err = c.String(DEFAULT_SECTION, "opt1")
if err == nil {
t.Errorf("String failure: no error for cycle")
} else if strings.Index(err.Error(), "cycle") < 0 {
t.Errorf("String failure: incorrect error for cycle")
}
}
// TestReadFile creates a 'tough' configuration file and test (read) parsing.
func TestReadFile(t *testing.T) {
file, err := os.Create(tmpFilename)
if err != nil {
t.Fatal("Test cannot run because cannot write temporary file: " + tmpFilename)
}
err = os.Setenv("GO_CONFIGFILE_TEST_ENV_VAR", "configvalue12345")
if err != nil {
t.Fatalf("Test cannot run because cannot set environment variable GO_CONFIGFILE_TEST_ENV_VAR: %#v", err)
}
buf := bufio.NewWriter(file)
buf.WriteString("optionInDefaultSection=true\n")
buf.WriteString("[section-1]\n")
buf.WriteString("option1=value1 ; This is a comment\n")
buf.WriteString("option2 : 2#Not a comment\t#Now this is a comment after a TAB\n")
buf.WriteString(" # Let me put another comment\n")
buf.WriteString("option3= line1\n line2: \n\tline3=v # Comment multiline with := in value\n")
buf.WriteString("; Another comment\n")
buf.WriteString("[" + DEFAULT_SECTION + "]\n")
buf.WriteString("variable1=small\n")
buf.WriteString("variable2=a_part_of_a_%(variable1)s_test\n")
buf.WriteString("[secTION-2]\n")
buf.WriteString("IS-flag-TRUE=Yes\n")
buf.WriteString("[section-1] # comment on section header\n") // continue again [section-1]
buf.WriteString("option4=this_is_%(variable2)s.\n")
buf.WriteString("envoption1=this_uses_${GO_CONFIGFILE_TEST_ENV_VAR}_env\n")
buf.WriteString("optionInDefaultSection=false")
buf.Flush()
file.Close()
c, err := ReadDefault(tmpFilename)
if err != nil {
t.Fatalf("ReadDefault failure: %s", err)
}
// check number of sections
if len(c.Sections()) != 3 {
t.Errorf("Sections failure: wrong number of sections")
}
// check number of options 6 of [section-1] plus 2 of [default]
opts, err := c.Options("section-1")
if len(opts) != 8 {
t.Errorf("Options failure: wrong number of options: %d", len(opts))
}
testGet(t, c, "section-1", "option1", "value1")
testGet(t, c, "section-1", "option2", "2#Not a comment")
testGet(t, c, "section-1", "option3", "line1\nline2:\nline3=v")
testGet(t, c, "section-1", "option4", "this_is_a_part_of_a_small_test.")
testGet(t, c, "section-1", "envoption1", "this_uses_configvalue12345_env")
testGet(t, c, "section-1", "optionInDefaultSection", false)
testGet(t, c, "section-2", "optionInDefaultSection", true)
testGet(t, c, "secTION-2", "IS-flag-TRUE", true) // case-sensitive
}
// TestWriteReadFile tests writing and reading back a configuration file.
func TestWriteReadFile(t *testing.T) {
cw := NewDefault()
// write file; will test only read later on
cw.AddSection("First-Section")
cw.AddOption("First-Section", "option1", "value option1")
cw.AddOption("First-Section", "option2", "2")
cw.AddOption("", "host", "www.example.com")
cw.AddOption(DEFAULT_SECTION, "protocol", "https://")
cw.AddOption(DEFAULT_SECTION, "base-url", "%(protocol)s%(host)s")
cw.AddOption("Another-Section", "useHTTPS", "y")
cw.AddOption("Another-Section", "url", "%(base-url)s/some/path")
cw.WriteFile(tmpFilename, 0644, "Test file for test-case")
// read back file and test
cr, err := ReadDefault(tmpFilename)
if err != nil {
t.Fatalf("ReadDefault failure: %s", err)
}
testGet(t, cr, "First-Section", "option1", "value option1")
testGet(t, cr, "First-Section", "option2", 2)
testGet(t, cr, "Another-Section", "useHTTPS", true)
testGet(t, cr, "Another-Section", "url", "https://www.example.com/some/path")
defer os.Remove(tmpFilename)
}
// TestSectionOptions tests read options in a section without default options.
func TestSectionOptions(t *testing.T) {
cw := NewDefault()
// write file; will test only read later on
cw.AddSection("First-Section")
cw.AddOption("First-Section", "option1", "value option1")
cw.AddOption("First-Section", "option2", "2")
cw.AddOption("", "host", "www.example.com")
cw.AddOption(DEFAULT_SECTION, "protocol", "https://")
cw.AddOption(DEFAULT_SECTION, "base-url", "%(protocol)s%(host)s")
cw.AddOption("Another-Section", "useHTTPS", "y")
cw.AddOption("Another-Section", "url", "%(base-url)s/some/path")
cw.WriteFile(tmpFilename, 0644, "Test file for test-case")
// read back file and test
cr, err := ReadDefault(tmpFilename)
if err != nil {
t.Fatalf("ReadDefault failure: %s", err)
}
options, err := cr.SectionOptions("First-Section")
if err != nil {
t.Fatalf("SectionOptions failure: %s", err)
}
if len(options) != 2 {
t.Fatalf("SectionOptions reads wrong data: %v", options)
}
expected := map[string]bool{
"option1": true,
"option2": true,
}
actual := map[string]bool{}
for _, v := range options {
actual[v] = true
}
if !reflect.DeepEqual(expected, actual) {
t.Fatalf("SectionOptions reads wrong data: %v", options)
}
options, err = cr.SectionOptions(DEFAULT_SECTION)
if err != nil {
t.Fatalf("SectionOptions failure: %s", err)
}
expected = map[string]bool{
"host": true,
"protocol": true,
"base-url": true,
}
actual = map[string]bool{}
for _, v := range options {
actual[v] = true
}
if !reflect.DeepEqual(expected, actual) {
t.Fatalf("SectionOptions reads wrong data: %v", options)
}
defer os.Remove(tmpFilename)
}
// TestMerge tests merging 2 configurations.
func TestMerge(t *testing.T) {
target, error := ReadDefault(targetFilename)
if error != nil {
t.Fatalf("Unable to read target config file '%s'", targetFilename)
}
source, error := ReadDefault(sourceFilename)
if error != nil {
t.Fatalf("Unable to read source config file '%s'", sourceFilename)
}
target.Merge(source)
// Assert whether a regular option was merged from source -> target
if result, _ := target.String(DEFAULT_SECTION, "one"); result != "source1" {
t.Errorf("Expected 'one' to be '1' but instead it was '%s'", result)
}
// Assert that a non-existent option in source was not overwritten
if result, _ := target.String(DEFAULT_SECTION, "five"); result != "5" {
t.Errorf("Expected 'five' to be '5' but instead it was '%s'", result)
}
// Assert that a folded option was correctly unfolded
if result, _ := target.String(DEFAULT_SECTION, "two_+_three"); result != "source2 + source3" {
t.Errorf("Expected 'two_+_three' to be 'source2 + source3' but instead it was '%s'", result)
}
if result, _ := target.String(DEFAULT_SECTION, "four"); result != "4" {
t.Errorf("Expected 'four' to be '4' but instead it was '%s'", result)
}
// Assert that a section option has been merged
if result, _ := target.String("X", "x.one"); result != "sourcex1" {
t.Errorf("Expected '[X] x.one' to be 'sourcex1' but instead it was '%s'", result)
}
if result, _ := target.String("X", "x.four"); result != "x4" {
t.Errorf("Expected '[X] x.four' to be 'x4' but instead it was '%s'", result)
}
}