-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_layer_test.go
83 lines (72 loc) · 2.17 KB
/
file_layer_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
package config
import (
"bytes"
"io"
"os"
"reflect"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestFileLayer(t *testing.T) {
Convey("FileLayer test ", t, func() {
// Make sure there is two ile available in the tmp file system
tmp := os.TempDir()
dir := tmp + "/config_f"
So(os.MkdirAll(dir, 0744), ShouldBeNil)
path := dir + "/test.json"
path2 := dir + "/invalid.json"
buf := bytes.NewBufferString(`{"slicestr":["a","b","c"],"str" : "string_data","bool" : true,"integer" : 10 ,"nested" : {"key1" : "string","key2" : 100}}`)
bufInvalid := bytes.NewBufferString(`invalid{json}[]`)
f, err := os.Create(path)
So(err, ShouldBeNil)
_, err = io.Copy(f, buf)
So(err, ShouldBeNil)
f2, err := os.Create(path2)
So(err, ShouldBeNil)
_, err = io.Copy(f2, bufInvalid)
So(err, ShouldBeNil)
defer func() {
_ = os.Remove(path)
_ = os.Remove(path2)
}()
So(f.Close(), ShouldBeNil)
So(f2.Close(), ShouldBeNil)
Convey("Check if the file is loaded correctly ", func() {
fl := NewFileLayer(path)
o := New()
errAdd := o.AddLayer(fl)
So(errAdd, ShouldBeNil)
So(o.GetStringDefault("str", ""), ShouldEqual, "string_data")
So(o.GetStringDefault("nested.key1", ""), ShouldEqual, "string")
So(o.GetIntDefault("nested.key2", 0), ShouldEqual, 100)
So(o.GetBoolDefault("bool", false), ShouldBeTrue)
So(reflect.DeepEqual(o.GetStringSlice("slicestr"), []string{"a", "b", "c"}), ShouldBeTrue)
a := New() // Just for test load again
So(a.AddLayer(fl), ShouldBeNil)
})
Convey("Check for the invalid ext", func() {
f, errCheck := os.Create(path + ".invalid_ext")
So(errCheck, ShouldBeNil)
defer func() {
_ = os.Remove(path + ".invalid_ext")
}()
So(f.Close(), ShouldBeNil)
fl := NewFileLayer(path + ".invalid_ext")
o := New()
errCheck = o.AddLayer(fl)
So(errCheck, ShouldNotBeNil)
})
Convey("Check for the invalid file content", func() {
fl := NewFileLayer(path2)
o := New()
err = o.AddLayer(fl)
So(err, ShouldNotBeNil)
})
Convey("Check for the invalid file", func() {
fl := NewFileLayer(path + "imnothere.json")
o := New()
err = o.AddLayer(fl)
So(err, ShouldNotBeNil)
})
})
}