-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathfill_environments_test.go
More file actions
173 lines (132 loc) · 3.15 KB
/
fill_environments_test.go
File metadata and controls
173 lines (132 loc) · 3.15 KB
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
package goconfig
import (
"os"
"testing"
"time"
)
func TestFillEnvironments(t *testing.T) {
c := struct {
MyBoolTrue bool
MyBoolFalse bool
MyString string
MyFloat64 float64
MyFloat32 float32
MyInt64 int64
MyInt32 int32
MyInt int
MyUint64 uint64
MyUint32 uint32
MyUint uint
MyEmpty string
MyStruct struct {
MyItem string
}
MyDurationNano time.Duration
MyDurationString time.Duration
}{}
os.Setenv("MYBOOLTRUE", "true")
os.Setenv("MYBOOLFALSE", "false")
os.Setenv("MYSTRING", "Hello world")
os.Setenv("MYFLOAT64", "1.23")
os.Setenv("MYFLOAT32", "3.21")
os.Setenv("MYINT64", "123")
os.Setenv("MYINT32", "321")
os.Setenv("MYINT", "8888")
os.Setenv("MYUINT64", "64")
os.Setenv("MYUINT32", "32")
os.Setenv("MYUINT", "4444")
os.Setenv("MYSTRUCT_MYITEM", "nested")
os.Setenv("MYEMPTY", "")
os.Setenv("MYDURATIONNANO", "15000000000")
os.Setenv("MYDURATIONSTRING", "15s")
err := FillEnvironments(&c)
AssertNil(t, err)
if c.MyBoolTrue != true {
t.Error("MyBoolTrue should be true")
}
if c.MyBoolFalse != false {
t.Error("MyBoolFalse should be false")
}
if c.MyString != "Hello world" {
t.Error("MyString should be 'Hello World'")
}
if c.MyFloat64 != 1.23 {
t.Error("MyFloat64 should be 1.23")
}
if c.MyFloat32 != 3.21 {
t.Error("MyFloat32 should be 3.21")
}
if c.MyInt64 != 123 {
t.Error("MyInt64 should be 123")
}
if c.MyInt32 != 321 {
t.Error("MyInt32 should be 321")
}
if c.MyInt != 8888 {
t.Error("MyInt should be 8888")
}
if c.MyUint64 != 64 {
t.Error("MyUint64 should be 64")
}
if c.MyUint32 != 32 {
t.Error("MyUint32 should be 32")
}
if c.MyUint != 4444 {
t.Error("MyUint should be 4444")
}
if c.MyEmpty != "" {
t.Error("MyEmpty should be ''")
}
if c.MyStruct.MyItem != "nested" {
t.Error("MyStruct.MyItem should be 'nested'")
}
if c.MyDurationNano.String() != "15s" {
t.Error("MyDurationNano should be '15s'")
}
if c.MyDurationString.String() != "15s" {
t.Error("MyDurationString should be '15s'")
}
}
func TestFillEnvironmentsWithArray(t *testing.T) {
c := struct {
MyStringArray []string
}{
[]string{"four", "five", "six"},
}
os.Setenv("MYSTRINGARRAY", `["one", "two", "three"]`)
err := FillEnvironments(&c)
AssertNil(t, err)
AssertEqual(t, c.MyStringArray, []string{"one", "two", "three"})
}
func TestFillEnvironmentsWithArrayMalformed(t *testing.T) {
c := struct {
MyStringArray []string
}{}
os.Setenv("MYSTRINGARRAY", `}`)
err := FillEnvironments(&c)
AssertNotNil(t, err)
AssertEqual(t, err.Error(), "'MYSTRINGARRAY' should be a JSON"+
" array: invalid character '}' looking for beginning of value")
}
func TestFillEnvironmentsEmptyOverride(t *testing.T) {
c := struct {
Value string
}{Value: "default"}
os.Setenv("VALUE", "")
err := FillEnvironments(&c)
AssertNil(t, err)
AssertEqual(t, c.Value, "")
}
func TestFillEnvironmentsWithPointerStruct(t *testing.T) {
type nested struct {
Value string
}
c := struct {
Nested *nested
}{}
os.Setenv("NESTED_VALUE", "hello")
err := FillEnvironments(&c)
AssertNil(t, err)
AssertNotNil(t, c.Nested)
AssertEqual(t, c.Nested.Value, "hello")
}