-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathload_test.go
141 lines (135 loc) · 2.87 KB
/
load_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 copre
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type TestConfigAllSources struct {
A string `json:"a" env:"A" flag:"a"`
B string `json:"b" env:"B" flag:"b"`
C string `json:"c" env:"C" flag:"c"`
D string `json:"d" env:"D" flag:"d"`
}
func TestLoaderPrecendence(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
tests := map[string]struct {
JSON string
Flags map[string]string
EnvVars map[string]string
Input TestConfigAllSources
Expected TestConfigAllSources
}{
"file<flag<env": {
JSON: `{
"a": "file",
"b": "file",
"c": "file"
}`,
Flags: map[string]string{
"b": "flag",
"c": "flag",
},
EnvVars: map[string]string{
"C": "env",
},
Input: TestConfigAllSources{
D: "struct",
},
Expected: TestConfigAllSources{
A: "file",
B: "flag",
C: "env",
D: "struct",
},
},
"env<file<flag": {
EnvVars: map[string]string{
"A": "env",
"B": "env",
"C": "env",
},
JSON: `{
"b": "file",
"c": "file"
}`,
Flags: map[string]string{
"c": "flag",
},
Input: TestConfigAllSources{
D: "struct",
},
Expected: TestConfigAllSources{
A: "env",
B: "file",
C: "flag",
D: "struct",
},
},
"flag<env<file": {
Flags: map[string]string{
"a": "flag",
"b": "flag",
"c": "flag",
},
EnvVars: map[string]string{
"B": "env",
"C": "env",
},
JSON: `{ "c": "file" }`,
Input: TestConfigAllSources{
D: "struct",
},
Expected: TestConfigAllSources{
A: "flag",
B: "env",
C: "file",
D: "struct",
},
},
}
for precendence, test := range tests {
buildOrder := strings.Split(precendence, "<")
t.Run(precendence, func(t *testing.T) {
// Create config file
tf, err := ioutil.TempFile("", "test")
require.NoError(err)
defer os.Remove(tf.Name())
_, err = tf.WriteString(test.JSON)
require.NoError(err)
// Prepare FlagSet
f := pflag.NewFlagSet("test", pflag.ContinueOnError)
args := []string{}
for key, value := range test.Flags {
f.String(key, "", "")
args = append(args, fmt.Sprintf("--%s=%s", key, value))
}
_ = f.Parse(args)
// Setup environment
prefix := strings.ToUpper(strings.ReplaceAll(precendence, "<", "_"))
for key, value := range test.EnvVars {
os.Setenv(prefix+"_"+key, value)
}
result := test.Input
loaders := []Loader{}
for _, phase := range buildOrder {
if phase == "file" {
loaders = append(loaders, File(tf.Name(), json.Unmarshal))
} else if phase == "flag" {
loaders = append(loaders, FlagSet(f))
} else {
loaders = append(loaders, Env(WithPrefix(prefix)))
}
}
err = Load(&result, loaders...)
require.NoError(err)
assert.Equal(test.Expected, result)
})
}
}