-
Notifications
You must be signed in to change notification settings - Fork 1
/
apply_env_overrides_test.go
116 lines (104 loc) · 2.51 KB
/
apply_env_overrides_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
package config
import (
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestToCamelCase(t *testing.T) {
t.Parallel()
tests := []struct {
name string
value string
expect string
}{
{
name: "snake case",
value: "snake_case",
expect: "SnakeCase",
},
{
name: "space case",
value: "space case",
expect: "Space case",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := ToCamelCase(tt.value)
if tt.expect != actual {
t.Errorf("mismatch %s %s", tt.expect, actual)
}
})
}
}
func TestApplyEnvOverridesToSlice(t *testing.T) {
t.Parallel()
type Payload struct {
Addr string `envconfig:"ADDR"`
Timeout time.Duration `envconfig:"TIMEOUT"`
}
tests := []struct {
name string
prefix string
value interface{}
expect interface{}
envs []string
err error
}{
{
name: "no prefix",
err: ErrPrefixRequired,
},
{
name: "not a pointer",
prefix: "PREFIX_S",
value: []Payload{},
err: ErrNotPointer,
},
{
name: "not a slice",
prefix: "PREFIX_S",
value: new(int),
err: ErrNotSlice,
},
{
name: "fail on time.ParseDuration",
prefix: "PREFIX_S",
value: new([]Payload),
envs: []string{"PREFIX_S_0_ADDR", "localhost", "PREFIX_S_0_TIMEOUT", "localhost"},
err: errors.New(`failed to parse value "localhost" as time.Duration type`),
},
{
name: "ok",
prefix: "PREFIX_S",
value: new([]Payload),
expect: new([]Payload),
},
{
name: "fill with env values",
prefix: "PREFIX_S",
value: new([]Payload),
expect: &[]Payload{{Addr: "localhost", Timeout: time.Minute}, {Timeout: 2 * time.Minute}},
envs: []string{"PREFIX_S_0_ADDR", "localhost", "PREFIX_S_0_TIMEOUT", "1m", "PREFIX_S_1_TIMEOUT", "2m"},
},
{
name: "merge with env values",
prefix: "PREFIX_S",
value: &[]Payload{{Timeout: 10 * time.Minute}, {Timeout: 20 * time.Minute}, {Timeout: 30 * time.Minute}},
expect: &[]Payload{{Addr: "localhost", Timeout: time.Minute}, {Timeout: 2 * time.Minute}, {Timeout: 30 * time.Minute}},
envs: []string{"PREFIX_S_0_ADDR", "localhost", "PREFIX_S_0_TIMEOUT", "1m", "PREFIX_S_1_TIMEOUT", "2m"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer envs{}.set(tt.envs...).unset()
err := applyEnvOverridesToSlice(tt.prefix, tt.value)
assert.Equal(t, tt.err, err, tt.name)
if tt.err != nil && err != nil {
return
}
assert.Equal(t, tt.expect, tt.value, tt.name)
})
}
}