env_test.go 3.1 KB

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