-
Notifications
You must be signed in to change notification settings - Fork 1
/
dump_test.go
360 lines (292 loc) · 6.69 KB
/
dump_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
// Copyright (c) 2019 Dean Jackson <deanishe@deanishe.net>
// MIT Licence applies http://opensource.org/licenses/MIT
package env
import (
"errors"
"net/url"
"os"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// implements encoding.TextMarshaler, always fails.
type badMarshaller string
func (bm badMarshaller) MarshalText() ([]byte, error) {
return nil, errors.New("oops")
}
func (bm badMarshaller) UnmarshalText(data []byte) error {
return errors.New("oops")
}
type BadTarget struct {
Oops badMarshaller
}
type DumpTarget struct {
Ignored string `env:"-"`
Empty string
EmptySlice []string
Nil *string
Zero int
ZeroTime time.Time
ZeroDuration time.Duration
unexported string
String string
Strings []string
Bool bool
Bools []bool
Int int
Ints []int
Int8P *int8 `env:"INT8"`
Ints8P []*int8 `env:"INTS8"`
Int16 int16
Ints16 []int16
Int32 int32
Ints32 []int32
Int64P *int64 `env:"INT64"`
Ints64P []*int64 `env:"INTS64"`
Uint uint
Uints []uint
Uint8P *uint8 `env:"UINT8"`
Uints8P []*uint8 `env:"UINTS8"`
Uint16 uint16
Uints16 []uint16
Uint32 uint32
Uints32 []uint32
Uint64P *uint64 `env:"UINT64"`
Uints64P []*uint64 `env:"UINTS64"`
Float32 float32
Floats32 []float32
Float64 float64
Floats64 []float64
Duration time.Duration
Durations []time.Duration
URLP *url.URL `env:"URL"`
URLSP []*url.URL `env:"URLS"`
// *time.Time implements encoding.TextMarshaler
TimeP *time.Time `env:"TIME"`
TimesP []*time.Time `env:"TIMES"`
Nested *Nested
Unsupported map[string]string
}
func dumpTestValues() (map[string]string, DumpTarget) {
var (
i8_1 int8 = 3
i8_2 int8 = 4
i64_1 int64 = 9
i64_2 int64 = 10
u8_1 uint8 = 3
u8_2 uint8 = 4
u64_1 uint64 = 9
u64_2 uint64 = 10
url1, _ = url.Parse("http://www.example.com")
url2, _ = url.Parse("http://www.example.org")
t1 = time.Now().Truncate(time.Second)
t2 = time.Now().Add(time.Hour).Truncate(time.Second)
)
x := map[string]string{
"EMPTY": "",
"EMPTY_SLICE": "",
"NIL": "",
"ZERO": "0",
"ZERO_TIME": "0001-01-01T00:00:00Z",
"ZERO_DURATION": "0s",
"STRING": "hello",
"STRINGS": "hello,dolly",
"BOOL": "true",
"BOOLS": "true,true",
"INT": "1",
"INTS": "1,2",
"INT8": "3",
"INTS8": "3,4",
"INT16": "5",
"INTS16": "5,6",
"INT32": "7",
"INTS32": "7,8",
"INT64": "9",
"INTS64": "9,10",
"UINT": "1",
"UINTS": "1,2",
"UINT8": "3",
"UINTS8": "3,4",
"UINT16": "5",
"UINTS16": "5,6",
"UINT32": "7",
"UINTS32": "7,8",
"UINT64": "9",
"UINTS64": "9,10",
"FLOAT32": "1.1",
"FLOATS32": "1.1,2.2",
"FLOAT64": "3.3",
"FLOATS64": "3.3,4.4",
"NESTED_STRING": "nested",
"NESTED_NUM": "42",
"DURATION": "1m0s",
"DURATIONS": "1m0s,2m0s",
"URL": "http://www.example.com",
"URLS": "http://www.example.com,http://www.example.org",
"TIME": t1.Format(time.RFC3339),
"TIMES": strings.Join([]string{
t1.Format(time.RFC3339),
t2.Format(time.RFC3339),
}, ","),
}
v := DumpTarget{
Ignored: "ignored",
Empty: "",
unexported: "unexported",
String: "hello",
Strings: []string{"hello", "dolly"},
Bool: true,
Bools: []bool{true, true},
Int: 1,
Ints: []int{1, 2},
Int8P: &i8_1,
Ints8P: []*int8{&i8_1, &i8_2},
Int16: 5,
Ints16: []int16{5, 6},
Int32: 7,
Ints32: []int32{7, 8},
Int64P: &i64_1,
Ints64P: []*int64{&i64_1, &i64_2},
Uint: 1,
Uints: []uint{1, 2},
Uint8P: &u8_1,
Uints8P: []*uint8{&u8_1, &u8_2},
Uint16: 5,
Uints16: []uint16{5, 6},
Uint32: 7,
Uints32: []uint32{7, 8},
Uint64P: &u64_1,
Uints64P: []*uint64{&u64_1, &u64_2},
Float32: 1.1,
Floats32: []float32{1.1, 2.2},
Float64: 3.3,
Floats64: []float64{3.3, 4.4},
Duration: time.Minute,
Durations: []time.Duration{time.Minute, 2 * time.Minute},
URLP: url1,
URLSP: []*url.URL{url1, url2},
TimeP: &t1,
TimesP: []*time.Time{&t1, &t2},
Nested: &Nested{
NestedString: "nested",
NestedNum: 42,
},
Unsupported: map[string]string{
"foo": "bar",
},
}
return x, v
}
func TestDump(t *testing.T) {
x, v := dumpTestValues()
m, err := Dump(v)
assert.NoError(t, err, "dump failed")
assert.Equal(t, x, m, "unexpected result")
// pointer to struct
m, err = Dump(&v)
assert.NoError(t, err, "dump failed")
assert.Equal(t, x, m, "unexpected result")
}
func TestExport(t *testing.T) {
x, v := dumpTestValues()
require.NoError(t, Export(v), "export failed")
for k, v := range x {
assert.Equalf(t, v, os.Getenv(k), "unexpected %q", k)
}
os.Clearenv()
}
func TestIgnoreZeroValues(t *testing.T) {
x := map[string]string{
"STRING": "present",
"INT": "1",
"NESTED_NUM": "2",
}
v := DumpTarget{
String: "present",
Int: 1,
Ints: []int{},
Nested: &Nested{
NestedNum: 2,
},
}
m, err := Dump(v, IgnoreZeroValues)
assert.NoError(t, err, "dump failed")
assert.Equal(t, x, m, "unexpected result")
}
func TestDump_invalidTarget(t *testing.T) {
invalid := []interface{}{
"string",
[]string{},
map[string]string{},
int(10),
}
for _, v := range invalid {
_, err := Dump(v, IgnoreZeroValues)
assert.EqualError(t, err, "not a struct", "dump accepted invalid target")
}
}
func TestDump_badFields(t *testing.T) {
invalid := []interface{}{
BadTarget{Oops: "oops"},
struct {
BadTarget badMarshaller
}{
BadTarget: "oops",
},
struct {
Oops []badMarshaller
}{
[]badMarshaller{
"oops",
"oops",
},
},
struct {
Oops BadTarget
}{
Oops: BadTarget{Oops: "oops"},
},
}
for _, v := range invalid {
_, err := Dump(v)
assert.EqualError(t, err, "oops", "BadTarget succeeded")
}
}
func TestVarNameFunc(t *testing.T) {
fun := func(name string) string {
name = VarName(name)
name = strings.ToLower(name)
return strings.ReplaceAll(name, "_", "-")
}
x := map[string]string{
"nested-string": "nested",
"nested-num": "42",
"duration": "1m0s",
"durations": "1m0s,2m0s",
}
v := DumpTarget{
Duration: time.Minute,
Durations: []time.Duration{time.Minute, 2 * time.Minute},
Nested: &Nested{
NestedString: "nested",
NestedNum: 42,
},
}
vars, err := Dump(v, VarNameFunc(fun), IgnoreZeroValues)
assert.NoError(t, err, "dump failed")
assert.Equal(t, x, vars, "unexpected vars")
}
func TestExport_invalidTarget(t *testing.T) {
invalid := []interface{}{
"string",
[]string{},
map[string]string{},
int(10),
}
for _, v := range invalid {
err := Export(v, IgnoreZeroValues)
assert.EqualError(t, err, "not a struct", "dump accepted invalid target")
}
}