-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv_retractor_test.go
More file actions
105 lines (95 loc) · 2.65 KB
/
env_retractor_test.go
File metadata and controls
105 lines (95 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package envrtr
import (
"fmt"
"os"
"testing"
)
func TestNewEnvRetractor(t *testing.T) {
systemEnvValues := systemEnvValues()
envApplyValues(systemEnvValues)
defer envRollbackValues(systemEnvValues)
envValues := envValues()
r := NewEnvRetractor(envValues).(*EnvRetractor)
for name, tmpVal := range envValues {
t.Run(fmt.Sprintf("state_for_var_%s_with_val_%s", name, tmpVal), func(t *testing.T) {
valSet, ok := r.values[name]
if !ok {
t.Errorf("variable '%s' not exists in result", name)
return
}
if valSet.tmp != tmpVal {
t.Errorf(
"not correct tmp value for variable '%s'. Expected '%s', got '%s'",
name,
tmpVal,
valSet.original,
)
}
originalVal, ok := systemEnvValues[name]
if !ok {
t.Fatalf("variable '%s' not exists in systemEnvValues", name)
}
if valSet.original != originalVal {
t.Errorf(
"not correct original value for variable '%s'. Expected '%s', got '%s'",
name,
originalVal,
valSet.original,
)
}
if originalVal == "" && valSet.present != false {
t.Errorf("not correct preset value for variable '%s'. Expected false, got true", name)
}
})
}
}
func TestEnvRetractor_PullOut(t *testing.T) {
systemEnvValues := systemEnvValues()
envApplyValues(systemEnvValues)
defer envRollbackValues(systemEnvValues)
envValues := envValues()
NewEnvRetractor(envValues).PullOut()
for name, expectedVal := range envValues {
t.Run(fmt.Sprintf("var_%s_expected_val_%s", name, expectedVal), func(t *testing.T) {
val := os.Getenv(name)
if val != expectedVal {
t.Errorf("not correct value for variable '%s'. Expected '%s', got '%s'", name, expectedVal, val)
}
})
}
}
func TestEnvRetractor_Retract(t *testing.T) {
systemEnvValues := systemEnvValues()
envApplyValues(systemEnvValues)
defer envRollbackValues(systemEnvValues)
envValues := envValues()
r := NewEnvRetractor(envValues).PullOut()
r.Retract()
for name, expectedVal := range systemEnvValues {
t.Run(fmt.Sprintf("var_%s_expected_val_%s", name, expectedVal), func(t *testing.T) {
val, present := os.LookupEnv(name)
if expectedVal == "" && present != false {
t.Errorf("variable '%s' must be unset but present", name)
}
if val != expectedVal {
t.Errorf("not correct value for variable '%s'. Expected '%s', got '%s'", name, expectedVal, val)
}
})
}
}
func envValues() map[string]string {
return map[string]string{
"TEST_ENV": "value",
"TEST_ENV2": "value2_new",
"TEST_ENV4": "value4_new",
}
}
func systemEnvValues() map[string]string {
return map[string]string{
"TEST_ENV": "value",
"TEST_ENV2": "value2",
"TEST_ENV3": "value3",
"TEST_ENV4": "",
"TEST_ENV5": "",
}
}