-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoconf_test.go
141 lines (122 loc) · 2.42 KB
/
goconf_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
package goconf
import (
"os"
"testing"
"io/ioutil"
"github.com/stretchr/testify/require"
)
type SampleA struct {
A string
B *SampleB
CamelCase bool
ManualOverride1 string `envconfig:"manual_override_1"`
SplitWord1 string `split_words:"true"`
ID string
DefaultValue string
}
type SampleB struct {
C int `envconfig:"GO_C"`
D []int `envconfig:"GO_D"`
E []int
}
var expSample = &SampleA{
A: "foo",
B: &SampleB{
C: 9,
D: []int{1, 2, 3},
},
CamelCase: true,
ManualOverride1: "foobar",
SplitWord1: "hello world",
ID: "123456",
DefaultValue: "default",
}
var bytes = []byte(`
a: foo
camelcase: true
b:
c: 9
d:
- 1
- 2
- 3
manualoverride1: "foobar"
splitword1: "hello world"
id: 123456
`)
func TestEnv(t *testing.T) {
os.Setenv("GO_A", "foo")
os.Setenv("GO_CAMELCASE", "true")
os.Setenv("GO_ID", "123456")
os.Setenv("GO_D", "1,2,3")
os.Setenv("GO_C", "9")
os.Setenv("GO_SPLIT_WORD1", "hello world")
os.Setenv("GO_MANUAL_OVERRIDE_1", "foobar")
sample := &SampleA{A: "baz", DefaultValue: "default"}
err := Parse(sample, WithEnv("go"))
require.NoError(t, err)
require.EqualValues(t, expSample, sample)
}
func TestYaml(t *testing.T) {
sample := &SampleA{DefaultValue: "default"}
err := Parse(sample, WithYamlFromBytes(bytes))
require.NoError(t, err)
require.EqualValues(t, expSample, sample)
}
func TestYamlFromFile(t *testing.T) {
err := ioutil.WriteFile("config1.yml", bytes, 0777)
require.NoError(t, err)
sample := &SampleA{DefaultValue: "default"}
err = Parse(sample, WithYaml("config1.yml"))
require.NoError(t, err)
require.EqualValues(t, expSample, sample)
}
func TestCombile(t *testing.T) {
os.Setenv("GOO_A", "baz")
cbytes := []byte(`
a: foo
b:
e:
- 3
- 3
- 3
manualoverride1: "foobar"
`)
cfile := []byte(`
a: "bar"
b:
e:
- 4
- 4
- 4
`)
cexp := &SampleA{
A: "baz",
B: &SampleB{
C: 9,
D: []int{1, 2, 3},
E: []int{4, 4, 4},
},
ManualOverride1: "foobar",
DefaultValue: "default",
}
sample := &SampleA{
A: "000",
B: &SampleB{
C: 9,
D: []int{0, 0, 0},
E: []int{0, 0, 0},
},
ManualOverride1: "111",
DefaultValue: "default",
}
err := ioutil.WriteFile("config2.yml", cfile, 0777)
require.NoError(t, err)
err = Parse(sample,
WithYamlFromBytes(cbytes),
WithYaml("config2.yml"),
WithEnv("goo"),
)
require.NoError(t, err)
require.EqualValues(t, cexp, sample)
}