-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtag_test.go
246 lines (241 loc) · 5.29 KB
/
tag_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
package conf_test
import (
"github.com/rsb/conf"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
func TestParseTag_Success(t *testing.T) {
tests := []struct {
name string
tag string
expected conf.Tag
}{
{
name: "empty",
tag: "",
expected: conf.Tag{},
},
{
name: "minimum info required",
tag: "env:FOO_BAR",
expected: conf.Tag{
EnvVar: "FOO_BAR",
Default: "",
IsDefault: false,
NoPrint: false,
NoPrefix: false,
Required: false,
Mask: false,
},
},
{
name: "env and required only",
tag: "env:FOO_BAR,required",
expected: conf.Tag{
EnvVar: "FOO_BAR",
Default: "",
IsDefault: false,
NoPrint: false,
NoPrefix: false,
Required: true,
Mask: false,
},
},
{
name: "env and mask only",
tag: "env:FOO_BAR,mask",
expected: conf.Tag{
EnvVar: "FOO_BAR",
Default: "",
IsDefault: false,
NoPrint: false,
NoPrefix: false,
Required: false,
Mask: true,
},
},
{
name: "env and no-print only",
tag: "env:FOO_BAR,no-print",
expected: conf.Tag{
EnvVar: "FOO_BAR",
Default: "",
IsDefault: false,
NoPrint: true,
NoPrefix: false,
Required: false,
Mask: false,
},
},
{
name: "env and no-prefix only",
tag: "env:FOO_BAR,no-prefix",
expected: conf.Tag{
EnvVar: "FOO_BAR",
Default: "",
IsDefault: false,
NoPrint: false,
NoPrefix: true,
Required: false,
Mask: false,
},
},
{
name: "env and default only",
tag: "env:FOO_BAR,default:XYZ",
expected: conf.Tag{
EnvVar: "FOO_BAR",
Default: "XYZ",
IsDefault: true,
NoPrint: false,
NoPrefix: false,
Required: false,
Mask: false,
},
},
{
name: "all settings",
tag: "env:FOO_BAR,pstore:xy/z/key,default:XYZ,no-print,mask,no-prefix,required,cli:foo,cli-s:f,cli-u:some usage",
expected: conf.Tag{
EnvVar: "FOO_BAR",
PStoreVar: "xy/z/key",
CLIFlag: "foo",
CLIShort: "f",
CLIUsage: "some usage",
Default: "XYZ",
IsDefault: true,
NoPrint: true,
NoPrefix: true,
Required: true,
Mask: true,
},
},
{
name: "all settings with leading space",
tag: " env:FOO_BAR, default:XYZ, no-print,mask, no-prefix, required",
expected: conf.Tag{
EnvVar: "FOO_BAR",
Default: "XYZ",
IsDefault: true,
NoPrint: true,
NoPrefix: true,
Required: true,
Mask: true,
},
},
{
name: "env with value that has a leading space",
tag: " env: FOO_BAR, default:XYZ, no-print,mask, no-prefix, required",
expected: conf.Tag{
EnvVar: "FOO_BAR",
Default: "XYZ",
IsDefault: true,
NoPrint: true,
NoPrefix: true,
Required: true,
Mask: true,
},
},
{
name: "default map",
tag: "env:FOO_BAR,default:map(keyA|valueA;keyB|valueB),no-print,mask,no-prefix,required",
expected: conf.Tag{
EnvVar: "FOO_BAR",
Default: "keyA:valueA,keyB:valueB",
IsDefault: true,
NoPrint: true,
NoPrefix: true,
Required: true,
Mask: true,
},
},
{
name: "default map spaces are not manipulated",
tag: "env:FOO_BAR,default:map(keyA|valueA; keyB|valueB),no-print,mask,no-prefix,required",
expected: conf.Tag{
EnvVar: "FOO_BAR",
Default: "keyA:valueA, keyB:valueB",
IsDefault: true,
NoPrint: true,
NoPrefix: true,
Required: true,
Mask: true,
},
},
{
name: "default list",
tag: "env:FOO_BAR,default:list(a;b;c;d),no-print,mask,no-prefix,required",
expected: conf.Tag{
EnvVar: "FOO_BAR",
Default: "a,b,c,d",
IsDefault: true,
NoPrint: true,
NoPrefix: true,
Required: true,
Mask: true,
},
},
{
name: "viper value",
tag: "cli:foo-bar,default:some-value",
expected: conf.Tag{
CLIFlag: "foo-bar",
Default: "some-value",
IsDefault: true,
},
},
{
name: "parameter store value",
tag: "pstore:foo-bar,default:some-value",
expected: conf.Tag{
PStoreVar: "foo-bar",
Default: "some-value",
IsDefault: true,
},
},
{
name: "parameter store, env and viper value",
tag: "pstore:foo-bar,default:some-value,cli:foo,env:FOO_BAR",
expected: conf.Tag{
EnvVar: "FOO_BAR",
CLIFlag: "foo",
PStoreVar: "foo-bar",
Default: "some-value",
IsDefault: true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := conf.ParseTag(tt.tag)
require.NoError(t, err, "conf.ParseTag is not expected to fail")
assert.Equal(t, tt.expected, result)
})
}
}
func TestParseTag_Failures(t *testing.T) {
tests := []struct {
name string
tag string
msg string
}{
{
name: "default without a value",
tag: "env:FOO_BAR,default:,required",
msg: `tag ("default") missing a value`,
},
{
name: "env without a value",
tag: "env:,default:SomeValue,required",
msg: `tag ("env") missing a value`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := conf.ParseTag(tt.tag)
require.Error(t, err, "conf.ParseTag is expected to fail")
assert.Contains(t, err.Error(), tt.msg)
})
}
}