-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_test.go
161 lines (134 loc) · 3.28 KB
/
env_test.go
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package env
import (
"fmt"
"os"
"testing"
)
// TestLoad tests Load function.
func TestLoad(t *testing.T) {
os.Clearenv()
if err := os.Setenv("KEY_0", "default"); err != nil {
t.Error(err)
}
// Load env-file.
if err := Load("./fixtures/variables.env"); err != nil {
t.Error(err)
}
// Variable update protection.
if os.Getenv("KEY_0") != "default" {
t.Error("the existing variable has been overwritten")
}
// Setting a new variable.
if Get("KEY_1") != "value_1" {
t.Error("data wasn't loaded")
}
// Expand test.
if v := Get("KEY_2"); v != "default01" { // KEY_0 not overwritten
t.Errorf("expected value `default01` but `%s`.", v)
}
}
// TestLoadSafe tests LoadSafe function.
func TestLoadSafe(t *testing.T) {
os.Clearenv()
if err := Set("KEY_0", "default"); err != nil {
t.Error(err)
}
// Load env-file.
if err := LoadSafe("./fixtures/variables.env"); err != nil {
t.Error(err)
}
// Expand test.
// LoadSafe don't expand vars.
if v := os.Getenv("KEY_2"); v != "${KEY_0}01" {
t.Errorf("expected value `${KEY_0}01` but `%s`.", v)
}
}
// TestUpdate tests Update function.
func TestUpdate(t *testing.T) {
os.Clearenv()
if err := Set("KEY_0", "default"); err != nil {
t.Error(err)
}
// Load env-file.
if err := Update("./fixtures/variables.env"); err != nil {
t.Error(err)
}
// Variable update protection.
if Get("KEY_0") == "default" {
t.Error("existing variable has not overwritten")
}
// Setting a new variable.
if Get("KEY_1") != "value_1" {
t.Error("data didn't loaded")
}
// Expand test.
// KEY_0 not overwritten.
if v := Get("KEY_2"); v != "value_001" {
t.Errorf("expected value `value_001` but `%s`.", v)
}
}
// TestUpdateSafe tests UpdateSafe function.
func TestUpdateSafe(t *testing.T) {
os.Clearenv()
if err := Set("KEY_0", "default"); err != nil {
t.Error(err)
}
// Load env-file.
if err := UpdateSafe("./fixtures/variables.env"); err != nil {
t.Error(err)
}
// Expand test.
// UpdateSafe don't expand vars.
if v := Get("KEY_2"); v != "${KEY_0}01" {
t.Errorf("expected value `${KEY_0}01` but `%s`.", v)
}
}
// TestExists tests Exists function.
func TestExist(t *testing.T) {
tests := [][]string{
{"KEY_0", "default"},
{"KEY_1", "default"},
}
os.Clearenv()
for _, item := range tests {
if err := os.Setenv(item[0], item[1]); err != nil {
t.Error(err)
}
}
// Variables is exists.
if !Exists("KEY_0") || !Exists("KEY_0", "KEY_1") {
t.Error("expected value `true` but `false`")
}
// Variables doesn't exists.
if Exists("KEY_2") || Exists("KEY_0", "KEY_1", "KEY_2") {
t.Error("expected value `false` but `true`")
}
}
// TestSave tests Save function.
func TestSave(t *testing.T) {
data := struct {
Host string `env:"HOST"`
Port int `env:"PORT"`
}{
Host: "localhost",
Port: 8080,
}
// Save object.
os.Clearenv()
Save("/tmp/.env", "", data)
// Not chanage environment.
if h, p := os.Getenv("HOST"), os.Getenv("PORT"); h != "" || p != "" {
t.Error("doesn't have to change the environment")
}
// Load object.
if err := Load("/tmp/.env"); err != nil {
t.Error(err)
}
h, p := os.Getenv("HOST"), os.Getenv("PORT")
if h != data.Host {
t.Errorf("expected `%s` but `%s`", data.Host, h)
}
if p != fmt.Sprint(data.Port) {
t.Errorf("expected `%d` but `%s`", data.Port, fmt.Sprint(data.Port))
}
}