on 94de6bc help sign in
crush/internal/env/env_test.go
140 lines 3.1 kB history raw
1package env
3import (
4 "strings"
5 "testing"
7 "github.com/stretchr/testify/require"
8)
10func TestOsEnv_Get(t *testing.T) {
11 env := New()
13 // Test getting an existing environment variable
14 t.Setenv("TEST_VAR", "test_value")
16 value := env.Get("TEST_VAR")
17 require.Equal(t, "test_value", value)
19 // Test getting a non-existent environment variable
20 value = env.Get("NON_EXISTENT_VAR")
21 require.Equal(t, "", value)
24func TestOsEnv_Env(t *testing.T) {
25 env := New()
27 envVars := env.Env()
29 // Environment should not be empty in normal circumstances
30 require.NotNil(t, envVars)
31 require.Greater(t, len(envVars), 0)
33 // Each environment variable should be in key=value format
34 for _, envVar := range envVars {
35 require.Contains(t, envVar, "=")
36 }
39func TestNewFromMap(t *testing.T) {
40 testMap := map[string]string{
41 "KEY1": "value1",
42 "KEY2": "value2",
43 }
45 env := NewFromMap(testMap)
46 require.NotNil(t, env)
47 require.IsType(t, &mapEnv{}, env)
50func TestMapEnv_Get(t *testing.T) {
51 testMap := map[string]string{
52 "KEY1": "value1",
53 "KEY2": "value2",
54 }
56 env := NewFromMap(testMap)
58 // Test getting existing keys
59 require.Equal(t, "value1", env.Get("KEY1"))
60 require.Equal(t, "value2", env.Get("KEY2"))
62 // Test getting non-existent key
63 require.Equal(t, "", env.Get("NON_EXISTENT"))
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 }
73 env := NewFromMap(testMap)
74 envVars := env.Env()
76 require.Len(t, envVars, 2)
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 }
86 require.Equal(t, "value1", envMap["KEY1"])
87 require.Equal(t, "value2", envMap["KEY2"])
88 })
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 })
96 t.Run("nil map", func(t *testing.T) {
97 env := NewFromMap(nil)
98 envVars := env.Env()
99 require.Nil(t, envVars)
100 })
103func TestMapEnv_GetEmptyValue(t *testing.T) {
104 testMap := map[string]string{
105 "EMPTY_KEY": "",
106 "NORMAL_KEY": "value",
107 }
109 env := NewFromMap(testMap)
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"))
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 }
122 env := NewFromMap(testMap)
123 envVars := env.Env()
125 require.Len(t, envVars, 2)
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 }
138 require.True(t, found["equals"], "Should handle values with equals signs")
139 require.True(t, found["spaces"], "Should handle values with spaces")