| 1 |
package env |
| 2 |
|
| 3 |
import ( |
| 4 |
"slices" |
| 5 |
"strconv" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
) |
| 11 |
|
| 12 |
func mockEnviron() []string { |
| 13 |
return []string{"TEST_FOO=bar"} |
| 14 |
} |
| 15 |
|
| 16 |
func TestProvider(t *testing.T) { |
| 17 |
type Args struct { |
| 18 |
name string |
| 19 |
delim string |
| 20 |
opt Opt |
| 21 |
key string |
| 22 |
value string |
| 23 |
expKey string |
| 24 |
expValue string |
| 25 |
wantOpt Opt // Expected options for comparison. |
| 26 |
} |
| 27 |
f := func(args Args) { |
| 28 |
t.Run(args.name, func(t *testing.T) { |
| 29 |
gotProvider := Provider(args.delim, args.opt) |
| 30 |
wantProvider := Provider(args.delim, args.wantOpt) |
| 31 |
|
| 32 |
// Config variables. |
| 33 |
assert.Equal(t, wantProvider.delim, gotProvider.delim) |
| 34 |
assert.Equal(t, wantProvider.prefix, gotProvider.prefix) |
| 35 |
|
| 36 |
// Optioonal TransformFunc. |
| 37 |
if args.opt.TransformFunc != nil { |
| 38 |
k, v := gotProvider.transform(args.key, args.value) |
| 39 |
assert.Equal(t, args.expKey, k) |
| 40 |
assert.Equal(t, args.expValue, v) |
| 41 |
} |
| 42 |
|
| 43 |
// Optional EnvironFunc. |
| 44 |
var ( |
| 45 |
wantEnv = wantProvider.environ() |
| 46 |
gotEnv = gotProvider.environ() |
| 47 |
) |
| 48 |
slices.Sort(wantEnv) |
| 49 |
slices.Sort(gotEnv) |
| 50 |
if !slices.Equal(wantEnv, gotEnv) { |
| 51 |
assert.Fail(t, "Env vars not equal (omitted from message for security)", |
| 52 |
"Want len: %d\nGot len:%d", len(wantEnv), len(gotEnv)) |
| 53 |
} |
| 54 |
}) |
| 55 |
} |
| 56 |
|
| 57 |
// Execute the test cases. |
| 58 |
f(Args{name: "No TransformFunc", delim: ".", opt: Opt{Prefix: "TESTVAR_"}, wantOpt: Opt{Prefix: "TESTVAR_"}}) |
| 59 |
f(Args{name: "No Opt", delim: "."}) |
| 60 |
f(Args{name: "Has EnvironFunc", delim: ".", opt: Opt{Prefix: "TEST_", EnvironFunc: mockEnviron}, wantOpt: Opt{Prefix: "TEST_", EnvironFunc: mockEnviron}}) |
| 61 |
f(Args{ |
| 62 |
name: "Has TransformFunc", delim: ".", key: "TestKey", value: "TestVal", expKey: "testkey", expValue: "TestVal", opt: Opt{Prefix: "TESTVAR_", TransformFunc: func(k, v string) (string, any) { |
| 63 |
return strings.ToLower(k), v |
| 64 |
}}, |
| 65 |
wantOpt: Opt{Prefix: "TESTVAR_"}, |
| 66 |
}) |
| 67 |
f(Args{ |
| 68 |
name: "Has TransformFunc. No Prefix", delim: ".", key: "test_key", value: "test_val", expKey: "TEST.KEY", expValue: "test_val", opt: Opt{ |
| 69 |
TransformFunc: func(k, v string) (string, any) { |
| 70 |
return strings.ReplaceAll(strings.ToUpper(k), "_", "."), v |
| 71 |
}}, |
| 72 |
wantOpt: Opt{}, |
| 73 |
}) |
| 74 |
f(Args{ |
| 75 |
name: "Has TransformFunc", delim: ".", key: "TEST_KEY", value: "test_val", expKey: "key", expValue: "prod_val", wantOpt: Opt{Prefix: "TEST_"}, opt: Opt{ |
| 76 |
Prefix: "TEST_", |
| 77 |
TransformFunc: func(key string, value string) (string, any) { |
| 78 |
key = strings.ReplaceAll(strings.TrimPrefix(strings.ToLower(key), "test_"), "_", ".") |
| 79 |
value = strings.ReplaceAll(value, "test", "prod") |
| 80 |
return key, value |
| 81 |
}, |
| 82 |
}, |
| 83 |
}) |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
func TestRead(t *testing.T) { |
| 88 |
type Args struct { |
| 89 |
name string |
| 90 |
key string |
| 91 |
value string |
| 92 |
expKey string |
| 93 |
expValue any |
| 94 |
opt Opt |
| 95 |
delim string |
| 96 |
} |
| 97 |
|
| 98 |
f := func(args Args) { |
| 99 |
t.Run(args.name, func(t *testing.T) { |
| 100 |
t.Setenv(args.key, args.value) |
| 101 |
|
| 102 |
env := Provider(args.delim, args.opt) |
| 103 |
envs, err := env.Read() |
| 104 |
assert.Nil(t, err) |
| 105 |
|
| 106 |
v, ok := envs[args.expKey] |
| 107 |
assert.True(t, ok) |
| 108 |
assert.Equal(t, args.expValue, v) |
| 109 |
}) |
| 110 |
} |
| 111 |
|
| 112 |
// Execute the test cases. |
| 113 |
f(Args{name: "No TransformFunc", key: "TEST_KEY", value: "TEST_VAL", expKey: "TEST_KEY", expValue: "TEST_VAL", delim: ".", opt: Opt{}}) |
| 114 |
f(Args{name: "Path value", key: "TEST_DIR", value: "/test/dir/file", expKey: "TEST_DIR", expValue: "/test/dir/file", delim: "."}) |
| 115 |
f(Args{name: "Empty value", key: "KEY", value: "", expKey: "KEY", expValue: "", delim: ".", opt: Opt{}}) |
| 116 |
f(Args{ |
| 117 |
name: "Has TransformFunc", key: "TEST_KEY", value: "TEST_VAL", expKey: "test.key", expValue: "TEST_VAL", delim: "_", opt: Opt{ |
| 118 |
TransformFunc: func(key string, value string) (string, any) { |
| 119 |
return strings.ReplaceAll(strings.ToLower(key), "_", "."), value |
| 120 |
}, |
| 121 |
}, |
| 122 |
}) |
| 123 |
f(Args{ |
| 124 |
name: "No TransformFunc. Has Prefix.", key: "TEST_KEY", value: "TEST_VAL", expKey: "test.key", expValue: "TEST_VAL", delim: "/", |
| 125 |
opt: Opt{ |
| 126 |
Prefix: "TEST", |
| 127 |
TransformFunc: func(key string, value string) (string, any) { |
| 128 |
return strings.ReplaceAll(strings.ToLower(key), "_", "."), value |
| 129 |
}, |
| 130 |
}, |
| 131 |
}) |
| 132 |
f(Args{ |
| 133 |
name: "Transform key", key: "TEST_DIR", value: "/test/dir/file", expKey: "TEST_DIR", expValue: "_test_dir_file", delim: ".", |
| 134 |
opt: Opt{ |
| 135 |
TransformFunc: func(key string, value string) (string, any) { |
| 136 |
return key, strings.ReplaceAll(strings.ToLower(value), "/", "_") |
| 137 |
}, |
| 138 |
}, |
| 139 |
}) |
| 140 |
f(Args{ |
| 141 |
name: "Has TransformFunc", key: "TEST_KEY", value: "TEST_VAL", expKey: "TEST_OVERRIDE_KEY", expValue: "TEST_OVERRIDE_VAL", delim: ".", opt: Opt{ |
| 142 |
EnvironFunc: func() []string { |
| 143 |
return []string{"TEST_OVERRIDE_KEY=TEST_OVERRIDE_VAL"} |
| 144 |
}, |
| 145 |
}, |
| 146 |
}) |
| 147 |
f(Args{ |
| 148 |
name: "Has int value", key: "TEST_KEY", value: "123", expKey: "test_key", expValue: 123, delim: ".", opt: Opt{ |
| 149 |
EnvironFunc: func() []string { |
| 150 |
return []string{"TEST_KEY=123"} |
| 151 |
}, |
| 152 |
TransformFunc: func(key string, value string) (string, any) { |
| 153 |
if key == "TEST_KEY" { |
| 154 |
intval, err := strconv.Atoi(value) |
| 155 |
if err != nil { |
| 156 |
return "", nil |
| 157 |
} |
| 158 |
return "test_key", intval |
| 159 |
} |
| 160 |
|
| 161 |
return "", "" |
| 162 |
}, |
| 163 |
}, |
| 164 |
}) |
| 165 |
} |