Skip to content

Commit

Permalink
test: Add unit-tests for FetchEnv (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
art-tapin committed Apr 28, 2023
1 parent 6f03a37 commit 682136e
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions pkg/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package common

import (
"os"
"testing"
)

Expand Down Expand Up @@ -67,3 +68,97 @@ func TestFind(t *testing.T) {
t.Error("should not have found anything in the slice")
}
}

func TestFetchEnv(t *testing.T) {
t.Run("when env var exists & has a value different from the default value then return the env var value", func(t *testing.T) {
key := "LOG_LEVEL"
defaultVal := "info"
val := "debug"
os.Setenv(key, val)
defer os.Unsetenv(key)

result := FetchEnv(key, defaultVal)

if result != val {
t.Errorf("Expected %v, but got %v", val, result)
}
})
t.Run("when env var exists & has the same value as the default value then return the env var value", func(t *testing.T) {
key := "LOG_LEVEL"
defaultVal := "info"
val := "info"
os.Setenv(key, val)
defer os.Unsetenv(key)

result := FetchEnv(key, defaultVal)

if result != val {
t.Errorf("Expected %v, but got %v", val, result)
}
})
t.Run("when env var exists but has an empty value then return the empty value", func(t *testing.T) {
key := "LOG_MODE"
defaultVal := "production"
val := ""
os.Setenv(key, val)
defer os.Unsetenv(key)

result := FetchEnv(key, defaultVal)

if result != val {
t.Errorf("Expected %v, but got %v", val, result)
}
})
t.Run("when env var does not exist & the default value is used then return the default value", func(t *testing.T) {
key := "LOG_MODE"
defaultVal := "production"

result := FetchEnv(key, defaultVal)

if result != defaultVal {
t.Errorf("Expected %v, but got %v", defaultVal, result)
}
})
t.Run("when key is empty or invalid then return the default value", func(t *testing.T) {
key := ""
defaultVal := "production"

result := FetchEnv(key, defaultVal)

if result != defaultVal {
t.Errorf("Expected %v, but got %v", defaultVal, result)
}
})
t.Run("when a runtime error occurs during execution then return the default value", func(t *testing.T) {
key := "LOG_LEVEL"
defaultVal := "info"

os.Unsetenv(key)

result := FetchEnv(key, defaultVal)

if result != defaultVal {
t.Errorf("Expected %v, but got %v", defaultVal, result)
}
})
t.Run("when default value is an empty string then return an empty string", func(t *testing.T) {
key := "LOG_MODE"
defaultVal := ""

result := FetchEnv(key, defaultVal)

if result != defaultVal {
t.Errorf("Expected %v, but got %v", defaultVal, result)
}
})
t.Run(" when a non-reserved env var name is used as the key then return the default value", func(t *testing.T) {
key := "MY_VAR"
defaultVal := ""

result := FetchEnv(key, defaultVal)

if result != defaultVal {
t.Errorf("Expected %v, but got %v", defaultVal, result)
}
})
}

0 comments on commit 682136e

Please sign in to comment.