Skip to content

Commit

Permalink
Add tests for env Provider (#134)
Browse files Browse the repository at this point in the history
Add tests for `env` Provider.
Signed-off-by: Gökhan Özeloğlu <gozeloglu@gmail.com>
  • Loading branch information
gozeloglu committed Feb 1, 2022
1 parent f600f4d commit fe680f4
Showing 1 changed file with 143 additions and 8 deletions.
151 changes: 143 additions & 8 deletions providers/env/env_test.go
Expand Up @@ -2,19 +2,23 @@ package env

import (
"github.com/stretchr/testify/assert"
"os"
"strings"
"testing"
)

func TestProvider(t *testing.T) {

testCases := []struct {
name string
prefix string
delim string
cb func(key string, value string) (string, interface{})
cbInput func(key string) string
want *Env
name string
prefix string
delim string
key string
value string
expKey string
expValue string
cb func(key string) string
want *Env
}{
{
name: "Nil cb",
Expand All @@ -25,6 +29,22 @@ func TestProvider(t *testing.T) {
delim: ".",
},
},
{
name: "Simple cb",
prefix: "TESTVAR_",
delim: ".",
key: "TestKey",
value: "TestVal",
expKey: "testkey",
expValue: "TestVal",
cb: func(key string) string {
return strings.ToLower(key)
},
want: &Env{
prefix: "TESTVAR_",
delim: ".",
},
},
{
name: "Empty string nil cb",
prefix: "",
Expand All @@ -34,12 +54,31 @@ func TestProvider(t *testing.T) {
delim: ".",
},
},
{
name: "Cb is given",
prefix: "",
delim: ".",
key: "test_key",
value: "test_val",
expKey: "TEST.KEY",
expValue: "test_val",
cb: func(key string) string {
return strings.Replace(strings.ToUpper(key), "_", ".", -1)
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := Provider(tc.prefix, tc.delim, tc.cbInput)
assert.Equal(t, tc.want, got)
gotProvider := Provider(tc.prefix, tc.delim, tc.cb)
if tc.cb == nil {
assert.Equal(t, tc.want, gotProvider)
}
if tc.cb != nil {
k, v := gotProvider.cb(tc.key, tc.value)
assert.Equal(t, tc.expKey, k)
assert.Equal(t, tc.expValue, v)
}
})
}
}
Expand Down Expand Up @@ -123,3 +162,99 @@ func TestProviderWithValue(t *testing.T) {
})
}
}

func TestRead(t *testing.T) {
testCases := []struct {
name string
key string
value string
expKey string
expValue string
env *Env
}{
{
name: "No cb",
key: "TEST_KEY",
value: "TEST_VAL",
expKey: "TEST_KEY",
expValue: "TEST_VAL",
env: &Env{
delim: ".",
},
},
{
name: "cb given",
key: "TEST_KEY",
value: "TEST_VAL",
expKey: "test.key",
expValue: "TEST_VAL",
env: &Env{
delim: "_",
cb: func(key string, value string) (string, interface{}) {
return strings.Replace(strings.ToLower(key), "_", ".", -1), value
},
},
},
{
name: "No cb - prefix given",
key: "TEST_KEY",
value: "TEST_VAL",
expKey: "test.key",
expValue: "TEST_VAL",
env: &Env{
prefix: "TEST",
delim: "/",
cb: func(key string, value string) (string, interface{}) {
return strings.Replace(strings.ToLower(key), "_", ".", -1), value
},
},
},
{
name: "Path value",
key: "TEST_DIR",
value: "/test/dir/file",
expKey: "TEST_DIR",
expValue: "/test/dir/file",
env: &Env{
delim: ".",
},
},
{
name: "Replace value with underscore",
key: "TEST_DIR",
value: "/test/dir/file",
expKey: "TEST_DIR",
expValue: "_test_dir_file",
env: &Env{
delim: ".",
cb: func(key string, value string) (string, interface{}) {
return key, strings.Replace(strings.ToLower(value), "/", "_", -1)
},
},
},
{
name: "Empty value",
key: "KEY",
value: "",
expKey: "KEY",
expValue: "",
env: &Env{
delim: ".",
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := os.Setenv(tc.key, tc.value)
assert.Nil(t, err)
defer os.Unsetenv(tc.key)

envs, err := tc.env.Read()
assert.Nil(t, err)
v, ok := envs[tc.expKey]
assert.True(t, ok)
assert.Equal(t, tc.expValue, v)
})
}
}

0 comments on commit fe680f4

Please sign in to comment.