9
9
"time"
10
10
11
11
"github.com/stretchr/testify/assert"
12
+ "github.com/stretchr/testify/require"
12
13
"golift.io/cnfg"
13
14
)
14
15
@@ -32,46 +33,48 @@ type testSubConfig struct {
32
33
}
33
34
34
35
// A few tests hit this.
35
- func testUnmarshalFileValues (assert * assert.Assertions , config * testStruct , err error , from string ) {
36
+ func testUnmarshalFileValues (t * testing.T , assert * assert.Assertions , config * testStruct , err error , from string ) {
37
+ t .Helper ()
38
+
36
39
from += " "
37
40
38
- assert . Nil ( err , "there should not be an error reading the test file" )
41
+ require . NoError ( t , err , "there should not be an error reading the test file" )
39
42
// PointerSlice
40
- assert .Equal ( 1 , len ( config .PointerSlice ) , from + "pointerslice is too short" )
43
+ assert .Len ( config .PointerSlice , 1 , from + "pointerslice is too short" )
41
44
assert .EqualValues (true , config .PointerSlice [0 ].Bool , from + "the boolean was true" )
42
45
assert .EqualValues (123.4567 , * config .PointerSlice [0 ].FloatP , from + "the float64 was set to 123.4567" )
43
46
assert .EqualValues (0 , config .PointerSlice [0 ].Int , from + "int was not set so should be zero" )
44
47
assert .Nil (config .PointerSlice [0 ].StringP , from + "the string pointer was not set so should remain nil" )
45
48
46
49
// StructSlice
47
- assert .Equal ( 1 , len ( config .StructSlice ) , from + "pointerslice is too short" )
50
+ assert .Len ( config .StructSlice , 1 , from + "pointerslice is too short" )
48
51
assert .EqualValues (false , config .StructSlice [0 ].Bool , from + "the boolean was missing and should be false" )
49
- assert .Nil (config .StructSlice [0 ].FloatP , from + "the float64 was missing and should be nil" )
52
+ assert .Nil (config .StructSlice [0 ].FloatP , from + "the float64 was missing and should be nil 1 " )
50
53
assert .EqualValues (123 , config .StructSlice [0 ].Int , from + "int was set to 123" )
51
54
assert .EqualValues ("foo" , * config .StructSlice [0 ].StringP , from + "the string was set to foo" )
52
55
53
56
// Struct
54
57
assert .EqualValues (false , config .Struct .Bool , from + "the boolean was false and should be false" )
55
- assert .Nil (config .Struct .FloatP , from + "the float64 was missing and should be nil" )
58
+ assert .Nil (config .Struct .FloatP , from + "the float64 was missing and should be nil 2 " )
56
59
assert .EqualValues (0 , config .Struct .Int , from + "int was not set and must be 0" )
57
60
assert .Nil (config .Struct .StringP , from + "the string was missing and should be nil" )
58
61
59
62
// PointerStruct
60
63
assert .NotNil (config .PointerStruct , from + "the pointer struct has values and must not be nil" )
61
64
assert .EqualValues (false , config .PointerStruct .Bool , from + "the boolean was missing and should be false" )
62
- assert .Nil (config .PointerStruct .FloatP , from + "the float64 was missing and should be nil" )
65
+ assert .Nil (config .PointerStruct .FloatP , from + "the float64 was missing and should be nil 3 " )
63
66
assert .EqualValues (0 , config .PointerStruct .Int , from + "int was not set and must be 0" )
64
67
assert .EqualValues ("foo2" , * config .PointerStruct .StringP , from + "the string was set to foo2" )
65
68
66
69
// PointerSlice2
67
- assert .Equal ( 0 , len ( config .PointerSlice2 ) , from + "pointerslice2 is too long" )
70
+ assert .Empty ( config .PointerSlice2 , from + "pointerslice2 is too long" )
68
71
// StructSlice2
69
- assert .Equal ( 0 , len ( config .StructSlice2 ) , from + "structslice2 is too long" )
72
+ assert .Empty ( config .StructSlice2 , from + "structslice2 is too long" )
70
73
// Struct2
71
- assert .EqualValues (false , config .Struct2 .Bool , from + "this must be zero value" )
72
- assert .Nil (config .Struct2 .FloatP , from + "this must be zero value" )
73
- assert .EqualValues (0 , config .Struct2 .Int , from + "this must be zero value" )
74
- assert .Nil (config .Struct2 .StringP , from + "this must be zero value" )
74
+ assert .EqualValues (false , config .Struct2 .Bool , from + "this must be zero value 1 " )
75
+ assert .Nil (config .Struct2 .FloatP , from + "this must be zero value 2 " )
76
+ assert .EqualValues (0 , config .Struct2 .Int , from + "this must be zero value 3 " )
77
+ assert .Nil (config .Struct2 .StringP , from + "this must be zero value 4 " )
75
78
// PointerStruct2
76
79
assert .Nil (config .PointerStruct2 , from + "pointer struct 2 must be nil" )
77
80
}
@@ -96,19 +99,19 @@ func TestBrokenENV(t *testing.T) { //nolint:paralleltest // cannot parallel env
96
99
c := & testBroken {}
97
100
worked , err := cnfg .UnmarshalENV (c , "TEST" )
98
101
99
- assert . NotNil ( err , "an error must be returned for an unsupported type" )
102
+ require . Error ( t , err , "an error must be returned for an unsupported type" )
100
103
assert .False (worked )
101
104
102
105
config := & testBroken2 {}
103
106
worked , err = cnfg .UnmarshalENV (config , "TEST" )
104
107
105
- assert . NotNil ( err , "an error must be returned for an unsupported map type" )
108
+ require . Error ( t , err , "an error must be returned for an unsupported map type" )
106
109
assert .False (worked )
107
110
108
111
config2 := & testBroken3 {}
109
112
worked , err = cnfg .UnmarshalENV (config2 , "TEST" )
110
113
111
- assert . NotNil ( err , "an error must be returned for an unsupported map type" )
114
+ require . Error ( t , err , "an error must be returned for an unsupported map type" )
112
115
assert .False (worked )
113
116
}
114
117
@@ -133,7 +136,7 @@ func TestUnmarshalENVerrors(t *testing.T) { //nolint:paralleltest // cannot para
133
136
config := tester {}
134
137
worked , err := cnfg .UnmarshalENV (& config , "YO" )
135
138
136
- assert . Nil ( err , "maps are supported and must not produce an error" )
139
+ require . NoError ( t , err , "maps are supported and must not produce an error" )
137
140
assert .Empty (os .Getenv ("YO_WORKS_foo2string" ), "delenv must delete the environment variable" )
138
141
assert .Empty (os .Getenv ("YO_WORKS_foostring" ), "delenv must delete the environment variable" )
139
142
assert .True (worked )
@@ -161,7 +164,7 @@ func TestUnmarshalENVerrors(t *testing.T) { //nolint:paralleltest // cannot para
161
164
config2 := tester2 {HasStuff : []map [string ]string {{"freesoda" : "at-pops" }, {"a" : "v" }}}
162
165
worked , err = cnfg .UnmarshalENV (& config2 , "MORE" )
163
166
164
- assert . Nil ( err , "map slices are supported and must not produce an error" )
167
+ require . NoError ( t , err , "map slices are supported and must not produce an error" )
165
168
assert .True (worked )
166
169
167
170
f := * config2 .NotBroken2 [0 ]
@@ -179,7 +182,7 @@ func TestUnmarshalENV(t *testing.T) { //nolint:paralleltest // cannot parallel e
179
182
c := & testStruct {}
180
183
ok , err := cnfg .UnmarshalENV (c , "PRE" )
181
184
182
- assert . Nil ( err , "there must not be an error when parsing no variables" )
185
+ require . NoError ( t , err , "there must not be an error when parsing no variables" )
183
186
assert .False (ok , "there are no environment variables set, so ok should be false" )
184
187
testThingENV (t , assert )
185
188
testOscureENV (t , assert )
@@ -188,7 +191,7 @@ func TestUnmarshalENV(t *testing.T) { //nolint:paralleltest // cannot parallel e
188
191
f := true
189
192
g := & f
190
193
_ , err = cnfg .UnmarshalENV (g , "OOO" )
191
- assert . NotNil ( err , "unmarshaling a non-struct pointer must produce an error" )
194
+ require . Error ( t , err , "unmarshaling a non-struct pointer must produce an error" )
192
195
}
193
196
194
197
func testThingENV (t * testing.T , assert * assert.Assertions ) {
@@ -207,11 +210,11 @@ func testThingENV(t *testing.T, assert *assert.Assertions) {
207
210
208
211
ok , err := cnfg .UnmarshalENV (config , "PRE" )
209
212
assert .True (ok , "ok must be true since things must be parsed" )
210
- testUnmarshalFileValues (assert , config , err , "testThingENV" )
213
+ testUnmarshalFileValues (t , assert , config , err , "testThingENV" )
211
214
// do it again, and we should get the same result
212
215
ok , err = cnfg .UnmarshalENV (config , "PRE" )
213
216
assert .True (ok , "ok must be true since things must be parsed" )
214
- testUnmarshalFileValues (assert , config , err , "testThingENV" )
217
+ testUnmarshalFileValues (t , assert , config , err , "testThingENV" )
215
218
}
216
219
217
220
func testOscureENV (t * testing.T , assert * assert.Assertions ) {
@@ -238,13 +241,13 @@ func testOscureENV(t *testing.T, assert *assert.Assertions) {
238
241
testit := func () {
239
242
ok , err := cnfg .UnmarshalENV (config , "OB" )
240
243
assert .True (ok , "ok must be true since things must be parsed" )
241
- assert . Nil ( err )
244
+ require . NoError ( t , err )
242
245
243
- assert .EqualValues ( 2 , len ( config .FloatSlice ) )
246
+ assert .Len ( config .FloatSlice , 2 )
244
247
assert .EqualValues (- 5 , config .FloatSlice [0 ])
245
248
assert .EqualValues (8 , config .FloatSlice [1 ])
246
249
247
- assert .EqualValues ( 2 , len ( config .UintSliceP ) )
250
+ assert .Len ( config .UintSliceP , 2 )
248
251
assert .EqualValues (12 , * config .UintSliceP [0 ])
249
252
assert .EqualValues (22 , * config .UintSliceP [1 ])
250
253
@@ -254,9 +257,9 @@ func testOscureENV(t *testing.T, assert *assert.Assertions) {
254
257
weirdo := * config .Weirdo
255
258
wut := * config .Wut
256
259
257
- assert .EqualValues ( 1 , len ( weirdo ) )
260
+ assert .Len ( weirdo , 1 )
258
261
assert .EqualValues (- 1 , weirdo [0 ])
259
- assert .EqualValues ( 1 , len ( wut ) )
262
+ assert .Len ( wut , 1 )
260
263
assert .True (wut [0 ].Bool )
261
264
}
262
265
@@ -289,7 +292,7 @@ func testSpecialENV(t *testing.T, assert *assert.Assertions) {
289
292
worked , err := (& cnfg.ENV {Pfx : "TEST" }).Unmarshal (config )
290
293
291
294
assert .True (worked , "ok must be true since things must be parsed" )
292
- assert . Nil ( err )
295
+ require . NoError ( t , err )
293
296
assert .Equal (time .Minute , config .Dur )
294
297
assert .Equal (time .Second , config .CDur .Duration )
295
298
assert .Equal ("golift.io" , config .Sub .URL .Host , "the url wasn't parsed properly" )
@@ -302,5 +305,5 @@ func testSpecialENV(t *testing.T, assert *assert.Assertions) {
302
305
worked , err = (& cnfg.ENV {Pfx : "TEST" }).Unmarshal (config )
303
306
304
307
assert .False (worked , "cannot parse an invalid time" )
305
- assert . NotNil ( err , "cannot parse an invalid time" )
308
+ require . Error ( t , err , "cannot parse an invalid time" )
306
309
}
0 commit comments