Skip to content

Commit c6b9dbe

Browse files
author
Michael Gunkoff
committed
fix returned error when time.Duration has incorrect value, more tests
1 parent 7712c23 commit c6b9dbe

2 files changed

Lines changed: 69 additions & 1 deletion

File tree

env.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func Load(settings any) error {
114114

115115
engine.Field.durationValue, err = time.ParseDuration(engine.Field.envValue)
116116
if err != nil {
117-
return err
117+
return incorrectFieldValueError(engine.Field.envTag)
118118
}
119119
engine.Field.int64Value = engine.Field.durationValue.Nanoseconds()
120120
} else {

env_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,26 @@ type stringSliceConfig struct {
124124
StringSlice []string `env:"STRING_SLICE"`
125125
}
126126

127+
type Uint8Struct struct {
128+
Value uint8 `env:"UINT8"`
129+
}
130+
131+
type BadUint8Struct struct {
132+
Value uint8 `env:"BAD_UINT8"`
133+
}
134+
135+
type Float64Struct struct {
136+
Value float64 `env:"FLOAT"`
137+
}
138+
139+
type BadFloat64Struct struct {
140+
Value float64 `env:"BAD_FLOAT"`
141+
}
142+
143+
type badSettingsWithDuration struct {
144+
Timeout time.Duration `env:"BAD_TIMEOUT"`
145+
}
146+
127147
// setupTestEnvironment sets up all required environment variables for testing
128148
func setupTestEnvironment(t *testing.T) {
129149
t.Helper()
@@ -149,6 +169,14 @@ func setupTestEnvironment(t *testing.T) {
149169
"EMAIL": "email@example.com",
150170
"BIG_PORT": "25060",
151171
"STRING_SLICE": "a,b,c",
172+
173+
// new env vars for the uint8/float64 tests
174+
"UINT8": "200", // valid uint8
175+
"BAD_UINT8": "999", // out of uint8 range -> should fail
176+
"BAD_FLOAT": "notafloat", // invalid float -> should fail
177+
178+
// new env var for bad duration test
179+
"BAD_TIMEOUT": "notaduration", // invalid duration -> should fail
152180
}
153181

154182
for key, value := range envVars {
@@ -349,6 +377,43 @@ func TestLoad(t *testing.T) {
349377
expectError: false,
350378
description: "Should handle string slice from comma-separated values",
351379
},
380+
{
381+
name: "settings_with_uint8_ok",
382+
settings: &Uint8Struct{},
383+
wantErr: nil,
384+
expectError: false,
385+
description: "Should handle uint8 type correctly",
386+
},
387+
{
388+
name: "settings_with_uint8_fail",
389+
settings: &BadUint8Struct{},
390+
wantErr: NewIncorrectFieldValueError("BAD_UINT8"),
391+
expectError: true,
392+
description: "Should fail when uint8 value is out of range",
393+
},
394+
{
395+
name: "settings_with_float64_ok_explicit",
396+
settings: &Float64Struct{},
397+
wantErr: nil,
398+
expectError: false,
399+
description: "Should handle float64 type correctly (explicit struct)",
400+
},
401+
{
402+
name: "settings_with_float64_fail",
403+
settings: &BadFloat64Struct{},
404+
wantErr: NewIncorrectFieldValueError("BAD_FLOAT"),
405+
expectError: true,
406+
description: "Should fail when float64 cannot be parsed",
407+
},
408+
409+
// new failing duration test
410+
{
411+
name: "settings_with_bad_duration_should_fail",
412+
settings: &badSettingsWithDuration{},
413+
wantErr: NewIncorrectFieldValueError("BAD_TIMEOUT"),
414+
expectError: true,
415+
description: "Should fail when duration cannot be parsed",
416+
},
352417
}
353418

354419
for _, tt := range tests {
@@ -437,6 +502,9 @@ func createFreshInstance(original any) any {
437502
return &pigPort{}
438503
case *stringSliceConfig:
439504
return &stringSliceConfig{}
505+
// new case for the bad duration struct
506+
case *badSettingsWithDuration:
507+
return &badSettingsWithDuration{}
440508
default:
441509
return original
442510
}

0 commit comments

Comments
 (0)