Debian Codegraph @

env_test.go

raw symbols SCIP
1 package babyenv
2
3 import (
4 "os"
5 "strconv"
6 "testing"
7 )
8
9 func TestParse(t *testing.T) {
10 type config struct {
11 A bool `env:"A"`
12 B string `env:"B"`
13 C int `env:"C"`
14 D []byte `env:"D"`
15 E int64 `env:"E"`
16 }
17
18 a := true
19 b := "xxx"
20 c := 16
21 d := []byte("yyy")
22 var e int64 = 64
23
24 os.Setenv("A", strconv.FormatBool(a))
25 os.Setenv("B", b)
26 os.Setenv("C", strconv.FormatInt(int64(c), 10))
27 os.Setenv("D", string(d))
28 os.Setenv("E", strconv.FormatInt(e, 10))
29
30 var cfg config
31 if err := Parse(&cfg); err != nil {
32 t.Errorf("error while parsing: %v", err)
33 return
34 }
35
36 if !cfg.A {
37 t.Errorf("failed parsing bool; expected %#v, got %#v", a, cfg.A)
38 }
39 if cfg.B != b {
40 t.Errorf("failed parsing string; expected %#v, got %#v", b, cfg.B)
41 }
42 if cfg.C != c {
43 t.Errorf("failed parsing int; expected %#v, got %#v", c, cfg.C)
44 }
45 if cfg.D == nil {
46 t.Errorf("failed parsing byte[]; expected %#v, got nil", d)
47 } else if string(cfg.D) != string(d) {
48 t.Errorf("failed parsing []byte; expected %#v, got %#v", d, cfg.D)
49 }
50 if cfg.E != e {
51 t.Errorf("failed parsing int64; expected %#v, got %#v", c, cfg.E)
52 }
53 }
54
55 func TestParseWithDefaults(t *testing.T) {
56 type config struct {
57 A bool `env:"A" default:"true"`
58 B string `env:"B" default:"xxx"`
59 C int `env:"C" default:"16"`
60 D []byte `env:"D" default:"yyy"`
61 E int64 `env:"E" default:"64"`
62 }
63
64 a := true
65 b := "xxx"
66 c := 16
67 d := []byte("yyy")
68 var e int64 = 64
69
70 os.Unsetenv("A")
71 os.Unsetenv("B")
72 os.Unsetenv("C")
73 os.Unsetenv("D")
74 os.Unsetenv("E")
75
76 var cfg config
77 if err := Parse(&cfg); err != nil {
78 t.Errorf("error while parsing: %v", err)
79 return
80 }
81
82 if cfg.A != a {
83 t.Errorf("failed parsing bool; expected %#v, got %#v", a, cfg.A)
84 }
85 if cfg.B != b {
86 t.Errorf("failed parsing string; expected %#v, got %#v", b, cfg.B)
87 }
88 if cfg.C != c {
89 t.Errorf("failed parsing int; expected %#v, got %#v", c, cfg.C)
90 }
91 if cfg.D == nil {
92 t.Errorf("failed parsing byte[]; expected %#v, got nil", d)
93 } else if string(cfg.D) != string(d) {
94 t.Errorf("failed parsing []byte; expected %#v, got %#v", d, cfg.D)
95 }
96 if cfg.E != e {
97 t.Errorf("failed parsing int64; expected %#v, got %#v", e, cfg.E)
98 }
99 }
100
101 func TestParsePointers(t *testing.T) {
102 type config struct {
103 A *bool `env:"A"`
104 B *string `env:"B"`
105 C *int `env:"C"`
106 D *[]byte `env:"D"`
107 E *int64 `env:"E"`
108 }
109
110 a := true
111 b := "xxx"
112 c := 16
113 d := []byte("yyy")
114 var e int64 = 64
115
116 os.Setenv("A", strconv.FormatBool(a))
117 os.Setenv("B", b)
118 os.Setenv("C", strconv.FormatInt(int64(c), 10))
119 os.Setenv("D", string(d))
120 os.Setenv("E", strconv.FormatInt(e, 10))
121
122 var cfg config
123 if err := Parse(&cfg); err != nil {
124 t.Errorf("error while parsing: %v", err)
125 return
126 }
127
128 if cfg.A == nil {
129 t.Errorf("failed parsing *bool; expected %#v, got nil", a)
130 } else if *cfg.A != a {
131 t.Errorf("failed parsing *bool; expected %#v, got %#v", a, *cfg.A)
132 }
133
134 if cfg.B == nil {
135 t.Errorf("failed parsing *string; expected %#v, got nil", b)
136 } else if *cfg.B != b {
137 t.Errorf("failed parsing *string; expected %#v, got %#v", b, *cfg.B)
138 }
139
140 if cfg.C == nil {
141 t.Errorf("failed parsing *int; expected %#v, got nil", c)
142 } else if *cfg.C != c {
143 t.Errorf("failed parsing *int; expected %#v, got %#v", c, *cfg.C)
144 }
145
146 if cfg.D == nil {
147 t.Errorf("failed parsing *[]byte; expected %#v, got nil", d)
148 } else if string(*cfg.D) != string(d) {
149 t.Errorf("failed parsing *[]byte; expected %#v, got %#v", d, *cfg.D)
150 }
151
152 if cfg.E == nil {
153 t.Errorf("failed parsing *int64; expected %#v, got nil", e)
154 } else if *cfg.E != e {
155 t.Errorf("failed parsing *int64; expected %#v, got %#v", e, *cfg.E)
156 }
157 }
158
159 func TestParsePointersWithDefaults(t *testing.T) {
160 type config struct {
161 A *bool `env:"A" default:"true"`
162 B *string `env:"B" default:"xxx"`
163 C *int `env:"C" default:"16"`
164 D *[]byte `env:"D" default:"yyy"`
165 }
166
167 a := true
168 b := "xxx"
169 c := 16
170 d := []byte("yyy")
171
172 os.Unsetenv("A")
173 os.Unsetenv("B")
174 os.Unsetenv("C")
175 os.Unsetenv("D")
176
177 var cfg config
178 if err := Parse(&cfg); err != nil {
179 t.Errorf("error while parsing: %v", err)
180 return
181 }
182
183 if cfg.A == nil {
184 t.Errorf("failed parsing *bool; expected %#v, got nil", a)
185 } else if *cfg.A != a {
186 t.Errorf("failed parsing *bool; expected %#v, got %#v", a, *cfg.A)
187 }
188
189 if cfg.B == nil {
190 t.Errorf("failed parsing *string; expected %#v, got nil", b)
191 } else if *cfg.B != b {
192 t.Errorf("failed parsing *string; expected %#v, got %#v", b, *cfg.B)
193 }
194
195 if cfg.C == nil {
196 t.Errorf("failed parsing *int; expected %#v, got nil", c)
197 } else if *cfg.C != c {
198 t.Errorf("failed parsing *int; expected %#v, got %#v", c, *cfg.C)
199 }
200
201 if cfg.D == nil {
202 t.Errorf("failed parsing *[]byte; expected %#v, got nil", d)
203 } else if string(*cfg.D) != string(d) {
204 t.Errorf("failed parsing *[]byte; expected %#v, got %#v", d, *cfg.D)
205 }
206 }
207
208 func TestRequiredFlag(t *testing.T) {
209 type config struct {
210 A bool `env:"A,required"`
211 }
212
213 os.Unsetenv("A")
214
215 var cfg config
216 if err := Parse(&cfg); err == nil {
217 t.Errorf("expected an error because of an unfulfilled 'require' flag")
218 }
219 }
220
221 func TestUnexportedFieldBehavior(t *testing.T) {
222 type a struct {
223 a bool
224 }
225
226 type b struct {
227 b bool `env:"b"`
228 }
229
230 var aEnv a
231 if err := Parse(&aEnv); err != nil {
232 t.Errorf("received an unexpected error while parsing a struct with an unexported field with no 'env' tag: %v", err)
233 }
234
235 var bEnv b
236 if err := Parse(&bEnv); err == nil {
237 t.Error("expected an error parsing a field with an 'env' tag on an unexported struct")
238 }
239 }