Skip to content

Commit

Permalink
fixed env test
Browse files Browse the repository at this point in the history
  • Loading branch information
dan13ram committed Jun 28, 2024
1 parent 7d0c42a commit 76c2102
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
16 changes: 13 additions & 3 deletions config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 8 additions & 4 deletions config/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 76c2102

Please sign in to comment.