7 "github.com/stretchr/testify/require"
10func TestOsEnv_Get(t *testing.T) {
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) {
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, "=")
39func TestNewFromMap(t *testing.T) {
40 testMap := map[string]string{
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{
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{
73 env := NewFromMap(testMap)
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]
86 require.Equal(t, "value1", envMap["KEY1"])
87 require.Equal(t, "value2", envMap["KEY2"])
90 t.Run("empty map", func(t *testing.T) {
91 env := NewFromMap(map[string]string{})
93 require.Nil(t, envVars)
96 t.Run("nil map", func(t *testing.T) {
97 env := NewFromMap(nil)
99 require.Nil(t, envVars)
103func TestMapEnv_GetEmptyValue(t *testing.T) {
104 testMap := map[string]string{
106 "NORMAL_KEY": "value",
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",
122 env := NewFromMap(testMap)
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
133 if envVar == "KEY_WITH_SPACES=value with spaces" {
134 found["spaces"] = true
138 require.True(t, found["equals"], "Should handle values with equals signs")
139 require.True(t, found["spaces"], "Should handle values with spaces")
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list