env_test.go

  1package env
  2
  3import (
  4	"strings"
  5	"testing"
  6
  7	"github.com/stretchr/testify/require"
  8)
  9
 10func TestOsEnv_Get(t *testing.T) {
 11	env := &osEnv{}
 12
 13	// Test getting an existing environment variable
 14	t.Setenv("TEST_VAR", "test_value")
 15
 16	value := env.Get("TEST_VAR")
 17	require.Equal(t, "test_value", value)
 18
 19	// Test getting a non-existent environment variable
 20	value = env.Get("NON_EXISTENT_VAR")
 21	require.Equal(t, "", value)
 22}
 23
 24func TestOsEnv_Env(t *testing.T) {
 25	env := &osEnv{}
 26
 27	envVars := env.Env()
 28
 29	// Environment should not be empty in normal circumstances
 30	require.NotNil(t, envVars)
 31	require.Greater(t, len(envVars), 0)
 32
 33	// Each environment variable should be in key=value format
 34	for _, envVar := range envVars {
 35		require.Contains(t, envVar, "=")
 36	}
 37}
 38
 39func TestNewFromMap(t *testing.T) {
 40	testMap := map[string]string{
 41		"KEY1": "value1",
 42		"KEY2": "value2",
 43	}
 44
 45	env := NewFromMap(testMap)
 46	require.NotNil(t, env)
 47	require.IsType(t, &mapEnv{}, env)
 48}
 49
 50func TestMapEnv_Get(t *testing.T) {
 51	testMap := map[string]string{
 52		"KEY1": "value1",
 53		"KEY2": "value2",
 54	}
 55
 56	env := NewFromMap(testMap)
 57
 58	// Test getting existing keys
 59	require.Equal(t, "value1", env.Get("KEY1"))
 60	require.Equal(t, "value2", env.Get("KEY2"))
 61
 62	// Test getting non-existent key
 63	require.Equal(t, "", env.Get("NON_EXISTENT"))
 64}
 65
 66func TestMapEnv_Env(t *testing.T) {
 67	t.Run("with values", func(t *testing.T) {
 68		testMap := map[string]string{
 69			"KEY1": "value1",
 70			"KEY2": "value2",
 71		}
 72
 73		env := NewFromMap(testMap)
 74		envVars := env.Env()
 75
 76		require.Len(t, envVars, 2)
 77
 78		// Convert to map for easier testing (order is not guaranteed)
 79		envMap := make(map[string]string)
 80		for _, envVar := range envVars {
 81			parts := strings.SplitN(envVar, "=", 2)
 82			require.Len(t, parts, 2)
 83			envMap[parts[0]] = parts[1]
 84		}
 85
 86		require.Equal(t, "value1", envMap["KEY1"])
 87		require.Equal(t, "value2", envMap["KEY2"])
 88	})
 89
 90	t.Run("empty map", func(t *testing.T) {
 91		env := NewFromMap(map[string]string{})
 92		envVars := env.Env()
 93		require.NotNil(t, envVars)
 94		require.Len(t, envVars, 0)
 95	})
 96
 97	t.Run("nil map", func(t *testing.T) {
 98		env := NewFromMap(nil)
 99		envVars := env.Env()
100		require.NotNil(t, envVars)
101		require.Len(t, envVars, 0)
102	})
103}
104
105func TestMapEnv_GetEmptyValue(t *testing.T) {
106	testMap := map[string]string{
107		"EMPTY_KEY":  "",
108		"NORMAL_KEY": "value",
109	}
110
111	env := NewFromMap(testMap)
112
113	// Test that empty values are returned correctly
114	require.Equal(t, "", env.Get("EMPTY_KEY"))
115	require.Equal(t, "value", env.Get("NORMAL_KEY"))
116}
117
118func TestMapEnv_EnvFormat(t *testing.T) {
119	testMap := map[string]string{
120		"KEY_WITH_EQUALS": "value=with=equals",
121		"KEY_WITH_SPACES": "value with spaces",
122	}
123
124	env := NewFromMap(testMap)
125	envVars := env.Env()
126
127	require.Len(t, envVars, 2)
128
129	// Check that the format is correct even with special characters
130	found := make(map[string]bool)
131	for _, envVar := range envVars {
132		if envVar == "KEY_WITH_EQUALS=value=with=equals" {
133			found["equals"] = true
134		}
135		if envVar == "KEY_WITH_SPACES=value with spaces" {
136			found["spaces"] = true
137		}
138	}
139
140	require.True(t, found["equals"], "Should handle values with equals signs")
141	require.True(t, found["spaces"], "Should handle values with spaces")
142}