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 := New()
 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 := New()
 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.Nil(t, envVars)
 94	})
 95
 96	t.Run("nil map", func(t *testing.T) {
 97		env := NewFromMap(nil)
 98		envVars := env.Env()
 99		require.Nil(t, envVars)
100	})
101}
102
103func TestMapEnv_GetEmptyValue(t *testing.T) {
104	testMap := map[string]string{
105		"EMPTY_KEY":  "",
106		"NORMAL_KEY": "value",
107	}
108
109	env := NewFromMap(testMap)
110
111	// Test that empty values are returned correctly
112	require.Equal(t, "", env.Get("EMPTY_KEY"))
113	require.Equal(t, "value", env.Get("NORMAL_KEY"))
114}
115
116func TestMapEnv_EnvFormat(t *testing.T) {
117	testMap := map[string]string{
118		"KEY_WITH_EQUALS": "value=with=equals",
119		"KEY_WITH_SPACES": "value with spaces",
120	}
121
122	env := NewFromMap(testMap)
123	envVars := env.Env()
124
125	require.Len(t, envVars, 2)
126
127	// Check that the format is correct even with special characters
128	found := make(map[string]bool)
129	for _, envVar := range envVars {
130		if envVar == "KEY_WITH_EQUALS=value=with=equals" {
131			found["equals"] = true
132		}
133		if envVar == "KEY_WITH_SPACES=value with spaces" {
134			found["spaces"] = true
135		}
136	}
137
138	require.True(t, found["equals"], "Should handle values with equals signs")
139	require.True(t, found["spaces"], "Should handle values with spaces")
140}