-
Notifications
You must be signed in to change notification settings - Fork 1
/
env_test.go
170 lines (157 loc) · 3.99 KB
/
env_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
package copre
import (
"net"
"os"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type TestConfigEnvOptions struct {
A []byte `env:"NOPREFIX_A,noprefix"`
B []byte `env:"B,hex"`
C []byte `env:",base64"`
}
func TestEnvOptions(t *testing.T) {
require := require.New(t)
expected := TestConfigEnvOptions{
A: []byte{1, 1},
B: []byte{255},
C: []byte("1"),
}
prefix := "ENV_OPTIONS"
os.Setenv("NOPREFIX_A", "1,1")
os.Setenv(prefix+"_B", "FF")
os.Setenv(prefix+"_C", "MQ==")
result := TestConfigEnvOptions{}
err := Load(&result, Env(WithPrefix(prefix), ComputeEnvKey(UpperSnakeCase)))
require.NoError(err)
require.Equal(expected, result)
}
type TestConfigEnvOptionsUnsupported struct {
A string `env:"A,hex"`
}
func TestEnvOptionUnsupported(t *testing.T) {
result := TestConfigEnvOptionsUnsupported{}
err := Load(&result, Env())
require.Error(t, err)
}
func TestConvertScalars(t *testing.T) {
assert := assert.New(t)
input := "1"
conversions := []interface{}{
"1",
uint8(1),
uint16(1),
uint32(1),
uint64(1),
uint(1),
int8(1),
int16(1),
int32(1),
int64(1),
int(1),
float32(1.0),
float64(1.0),
true,
}
for _, expected := range conversions {
t.Logf("converting to %T", expected)
converted, err := convertString(reflect.TypeOf(expected), input)
assert.NoError(err)
assert.Equal(expected, converted)
}
}
func TestConvertSlices(t *testing.T) {
assert := assert.New(t)
input := "1,0"
conversions := []interface{}{
[]string{"1", "0"},
[]uint8{1, 0},
[]uint16{1, 0},
[]uint32{1, 0},
[]uint64{1, 0},
[]uint{1, 0},
[]int8{1, 0},
[]int16{1, 0},
[]int32{1, 0},
[]int64{1, 0},
[]int{1, 0},
[]float32{1.0, 0.0},
[]float64{1.0, 0.0},
[]bool{true, false},
}
for _, expected := range conversions {
t.Logf("converting to %T", expected)
converted, err := convertString(reflect.TypeOf(expected), input)
assert.NoError(err)
assert.Equal(expected, converted)
}
}
func TestConvertMaps(t *testing.T) {
assert := assert.New(t)
input := "0=1,1=0"
conversions := []interface{}{
map[string]uint8{"0": 1, "1": 0},
map[string]int{"0": 1, "1": 0},
map[string]float32{"0": 1.0, "1": 0.0},
map[bool]bool{false: true, true: false},
map[uint64]string{0: "1", 1: "0"},
}
for _, expected := range conversions {
t.Logf("converting to %T", expected)
converted, err := convertString(reflect.TypeOf(expected), input)
assert.NoError(err)
assert.Equal(expected, converted)
}
}
func TestConvertNetTypes(t *testing.T) {
assert := assert.New(t)
conversions := map[string]interface{}{
"127.0.0.1": net.IPv4(127, 0, 0, 1),
"255.255.255.0": net.IPv4Mask(255, 255, 255, 0),
"192.168.0.0/16": net.IPNet{IP: net.IPv4(192, 168, 0, 0), Mask: net.IPv4Mask(255, 255, 0, 0)},
}
for input, expected := range conversions {
t.Logf("converting to %T", expected)
converted, err := convertString(reflect.TypeOf(expected), input)
assert.NoError(err)
assert.Equal(expected, converted)
}
}
func TestConvertTimeDuration(t *testing.T) {
assert := assert.New(t)
conversions := map[string]interface{}{
"5s": 5 * time.Second,
}
for input, expected := range conversions {
converted, err := convertString(reflect.TypeOf(expected), input)
assert.NoError(err)
assert.Equal(expected, converted)
}
}
func TestConvertHexBase64(t *testing.T) {
assert := assert.New(t)
conversions := map[string]interface{}{
"FF": convertBytesHexMarker{255},
"MQ==": convertBytesBase64Marker{49},
}
for input, expected := range conversions {
converted, err := convertString(reflect.TypeOf(expected), input)
assert.NoError(err)
// The types don't match and we only have a single element, so let's use ElementsMatch as workaround
assert.ElementsMatch(expected, converted)
}
}
func TestConvertErrors(t *testing.T) {
assert := assert.New(t)
input := "1"
conversions := []interface{}{
struct{}{},
}
for _, expected := range conversions {
_, err := convertString(reflect.TypeOf(expected), input)
assert.Error(err)
}
}