@@ -3,7 +3,6 @@ package config
33import (
44 "errors"
55 "github.com/stretchr/testify/require"
6- "os"
76 "reflect"
87 "testing"
98)
@@ -44,42 +43,26 @@ func (prefixValidator) Validate() error {
4443
4544func TestFromEnv (t * testing.T ) {
4645 subtests := []struct {
47- name string
48- env map [string ]string
49- options EnvOptions
50- io Validator
51- error bool
46+ name string
47+ opts EnvOptions
48+ io Validator
49+ error bool
5250 }{
53- {"nil" , nil , EnvOptions {}, nil , true },
54- {"nonptr" , nil , EnvOptions {}, simpleValidator {}, true },
55- {"nilptr" , nil , EnvOptions {}, (* simpleValidator )(nil ), true },
56- {"defaulterr" , nil , EnvOptions {}, new (nonStructValidator ), true },
57- {"parseeerr" , map [string ]string {"FOO" : "bar" }, EnvOptions {}, & simpleValidator {}, true },
58- {"invalid" , map [string ]string {"FOO" : "23" }, EnvOptions {}, & simpleValidator {}, true },
59- {"simple" , map [string ]string {"FOO" : "42" }, EnvOptions {}, & simpleValidator {42 }, false },
60- {"default" , nil , EnvOptions {}, & defaultValidator {42 }, false },
61- {"override" , map [string ]string {"FOO" : "23" }, EnvOptions {}, & defaultValidator {23 }, false },
62- {"prefix" , map [string ]string {"PREFIX_FOO" : "42" }, EnvOptions {Prefix : "PREFIX_" }, & simpleValidator {42 }, false },
63- {"nested" , map [string ]string {"PREFIX_FOO" : "42" }, EnvOptions {}, & prefixValidator {simpleValidator {42 }}, false },
64- }
65-
66- allEnv := make (map [string ]struct {})
67- for _ , st := range subtests {
68- for k := range st .env {
69- allEnv [k ] = struct {}{}
70- }
51+ {"nil" , EnvOptions {}, nil , true },
52+ {"nonptr" , EnvOptions {}, simpleValidator {}, true },
53+ {"nilptr" , EnvOptions {}, (* simpleValidator )(nil ), true },
54+ {"defaulterr" , EnvOptions {}, new (nonStructValidator ), true },
55+ {"parseeerr" , EnvOptions {Environment : map [string ]string {"FOO" : "bar" }}, & simpleValidator {}, true },
56+ {"invalid" , EnvOptions {Environment : map [string ]string {"FOO" : "23" }}, & simpleValidator {}, true },
57+ {"simple" , EnvOptions {Environment : map [string ]string {"FOO" : "42" }}, & simpleValidator {42 }, false },
58+ {"default" , EnvOptions {}, & defaultValidator {42 }, false },
59+ {"override" , EnvOptions {Environment : map [string ]string {"FOO" : "23" }}, & defaultValidator {23 }, false },
60+ {"prefix" , EnvOptions {Environment : map [string ]string {"PREFIX_FOO" : "42" }, Prefix : "PREFIX_" }, & simpleValidator {42 }, false },
61+ {"nested" , EnvOptions {Environment : map [string ]string {"PREFIX_FOO" : "42" }}, & prefixValidator {simpleValidator {42 }}, false },
7162 }
7263
7364 for _ , st := range subtests {
7465 t .Run (st .name , func (t * testing.T ) {
75- for k := range allEnv {
76- require .NoError (t , os .Unsetenv (k ))
77- }
78-
79- for k , v := range st .env {
80- require .NoError (t , os .Setenv (k , v ))
81- }
82-
8366 var actual Validator
8467 if vActual := reflect .ValueOf (st .io ); vActual != (reflect.Value {}) {
8568 if vActual .Kind () == reflect .Ptr && ! vActual .IsNil () {
@@ -89,7 +72,7 @@ func TestFromEnv(t *testing.T) {
8972 actual = vActual .Interface ().(Validator )
9073 }
9174
92- if err := FromEnv (actual , st .options ); st .error {
75+ if err := FromEnv (actual , st .opts ); st .error {
9376 require .Error (t , err )
9477 } else {
9578 require .NoError (t , err )
0 commit comments