From fe680f431b2b0481f0ed845252f831218b280ef1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6khan=20=C3=96zelo=C4=9Flu?= <33967642+gozeloglu@users.noreply.github.com> Date: Tue, 1 Feb 2022 10:53:40 +0300 Subject: [PATCH] Add tests for `env` Provider (#134) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add tests for `env` Provider. Signed-off-by: Gökhan Özeloğlu --- providers/env/env_test.go | 151 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 143 insertions(+), 8 deletions(-) diff --git a/providers/env/env_test.go b/providers/env/env_test.go index d4bf05d..2717c71 100644 --- a/providers/env/env_test.go +++ b/providers/env/env_test.go @@ -2,6 +2,7 @@ package env import ( "github.com/stretchr/testify/assert" + "os" "strings" "testing" ) @@ -9,12 +10,15 @@ import ( 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", @@ -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: "", @@ -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) + } }) } } @@ -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) + }) + } +}