From 76c210288199f9557441667f283ae1b13ce8203d Mon Sep 17 00:00:00 2001 From: dan13ram Date: Fri, 28 Jun 2024 13:18:22 +0530 Subject: [PATCH] fixed env test --- config/env.go | 16 +++++++++++++--- config/env_test.go | 12 ++++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/config/env.go b/config/env.go index 5d43324..17b29be 100644 --- a/config/env.go +++ b/config/env.go @@ -158,12 +158,22 @@ func getStringArrayEnv(key string) []string { func getArrayLengthEnv(key string) int { env := os.Environ() - var val int + var length int + seen := make(map[int]bool) for _, e := range env { pair := strings.Split(e, "=") if strings.HasPrefix(pair[0], key) { - val++ + vals := strings.Split(strings.TrimPrefix(pair[0], key), "_") + log.Println(vals) + val, err := strconv.Atoi(vals[1]) + if err != nil { + return 0 + } + if !seen[val] { + length++ + seen[val] = true + } } } - return val + return length } diff --git a/config/env_test.go b/config/env_test.go index fec6acb..c53199b 100644 --- a/config/env_test.go +++ b/config/env_test.go @@ -86,11 +86,13 @@ func TestStringArrayEnv(t *testing.T) { func TestGetArrayLengthEnv(t *testing.T) { t.Run("Valid env variable", func(t *testing.T) { - os.Setenv("TEST_VAR_ONE", "test1") - os.Setenv("TEST_VAR_TWO", "test2") + os.Setenv("TEST_VAR_0_ONE", "test1") + os.Setenv("TEST_VAR_0_TWO", "test1") + os.Setenv("TEST_VAR_1", "test2") - defer os.Unsetenv("TEST_VAR_ONE") - defer os.Unsetenv("TEST_VAR_TWO") + defer os.Unsetenv("TEST_VAR_0_ONE") + defer os.Unsetenv("TEST_VAR_0_TWO") + defer os.Unsetenv("TEST_VAR_1") val := getArrayLengthEnv("TEST_VAR") assert.Equal(t, 2, val) @@ -166,7 +168,9 @@ NUM_ETHEREUM_NETWORKS=2 envContent := ` NUM_ETHEREUM_NETWORKS=2 ETHEREUM_NETWORKS_0_CONFIRMATIONS=1 +ETHEREUM_NETWORKS_0_START_BLOCK_NUMBER=1 ETHEREUM_NETWORKS_1_CONFIRMATIONS=2 +ETHEREUM_NETWORKS_1_START_BLOCK_NUMBER=1 COSMOS_NETWORK_CONFIRMATIONS=3 ` err := os.WriteFile(".test.env", []byte(envContent), 0644)