@@ -5,18 +5,24 @@ import (
5
5
"testing"
6
6
)
7
7
8
+ var noopPresets = make (map [string ]string )
9
+
8
10
func parseAndCompare (t * testing.T , rawEnvLine string , expectedKey string , expectedValue string ) {
9
11
key , value , _ := parseLine (rawEnvLine )
10
12
if key != expectedKey || value != expectedValue {
11
13
t .Errorf ("Expected '%v' to parse as '%v' => '%v', got '%v' => '%v' instead" , rawEnvLine , expectedKey , expectedValue , key , value )
12
14
}
13
15
}
14
16
15
- func loadEnvAndCompareValues (t * testing.T , envFileName string , expectedValues map [string ]string ) {
17
+ func loadEnvAndCompareValues (t * testing.T , loader func ( files ... string ) error , envFileName string , expectedValues map [ string ] string , presets map [string ]string ) {
16
18
// first up, clear the env
17
19
os .Clearenv ()
18
20
19
- err := Load (envFileName )
21
+ for k , v := range presets {
22
+ os .Setenv (k , v )
23
+ }
24
+
25
+ err := loader (envFileName )
20
26
if err != nil {
21
27
t .Fatalf ("Error loading %v" , envFileName )
22
28
}
@@ -38,13 +44,28 @@ func TestLoadWithNoArgsLoadsDotEnv(t *testing.T) {
38
44
}
39
45
}
40
46
47
+ func TestOverloadWithNoArgsOverloadsDotEnv (t * testing.T ) {
48
+ err := Overload ()
49
+ pathError := err .(* os.PathError )
50
+ if pathError == nil || pathError .Op != "open" || pathError .Path != ".env" {
51
+ t .Errorf ("Didn't try and open .env by default" )
52
+ }
53
+ }
54
+
41
55
func TestLoadFileNotFound (t * testing.T ) {
42
56
err := Load ("somefilethatwillneverexistever.env" )
43
57
if err == nil {
44
58
t .Error ("File wasn't found but Load didn't return an error" )
45
59
}
46
60
}
47
61
62
+ func TestOverloadFileNotFound (t * testing.T ) {
63
+ err := Overload ("somefilethatwillneverexistever.env" )
64
+ if err == nil {
65
+ t .Error ("File wasn't found but Overload didn't return an error" )
66
+ }
67
+ }
68
+
48
69
func TestReadPlainEnv (t * testing.T ) {
49
70
envFileName := "fixtures/plain.env"
50
71
expectedValues := map [string ]string {
@@ -71,6 +92,34 @@ func TestReadPlainEnv(t *testing.T) {
71
92
}
72
93
}
73
94
95
+ func TestLoadDoesNotOverride (t * testing.T ) {
96
+ envFileName := "fixtures/plain.env"
97
+
98
+ // ensure NO overload
99
+ presets := map [string ]string {
100
+ "OPTION_A" : "do_not_override" ,
101
+ }
102
+
103
+ expectedValues := map [string ]string {
104
+ "OPTION_A" : "do_not_override" ,
105
+ }
106
+ loadEnvAndCompareValues (t , Load , envFileName , expectedValues , presets )
107
+ }
108
+
109
+ func TestOveroadDoesOverride (t * testing.T ) {
110
+ envFileName := "fixtures/plain.env"
111
+
112
+ // ensure NO overload
113
+ presets := map [string ]string {
114
+ "OPTION_A" : "do_not_override" ,
115
+ }
116
+
117
+ expectedValues := map [string ]string {
118
+ "OPTION_A" : "1" ,
119
+ }
120
+ loadEnvAndCompareValues (t , Overload , envFileName , expectedValues , presets )
121
+ }
122
+
74
123
func TestLoadPlainEnv (t * testing.T ) {
75
124
envFileName := "fixtures/plain.env"
76
125
expectedValues := map [string ]string {
@@ -81,7 +130,7 @@ func TestLoadPlainEnv(t *testing.T) {
81
130
"OPTION_E" : "5" ,
82
131
}
83
132
84
- loadEnvAndCompareValues (t , envFileName , expectedValues )
133
+ loadEnvAndCompareValues (t , Load , envFileName , expectedValues , noopPresets )
85
134
}
86
135
87
136
func TestLoadExportedEnv (t * testing.T ) {
@@ -91,7 +140,7 @@ func TestLoadExportedEnv(t *testing.T) {
91
140
"OPTION_B" : "\n " ,
92
141
}
93
142
94
- loadEnvAndCompareValues (t , envFileName , expectedValues )
143
+ loadEnvAndCompareValues (t , Load , envFileName , expectedValues , noopPresets )
95
144
}
96
145
97
146
func TestLoadEqualsEnv (t * testing.T ) {
@@ -100,7 +149,7 @@ func TestLoadEqualsEnv(t *testing.T) {
100
149
"OPTION_A" : "postgres://localhost:5432/database?sslmode=disable" ,
101
150
}
102
151
103
- loadEnvAndCompareValues (t , envFileName , expectedValues )
152
+ loadEnvAndCompareValues (t , Load , envFileName , expectedValues , noopPresets )
104
153
}
105
154
106
155
func TestLoadQuotedEnv (t * testing.T ) {
@@ -116,7 +165,7 @@ func TestLoadQuotedEnv(t *testing.T) {
116
165
"OPTION_H" : "\n " ,
117
166
}
118
167
119
- loadEnvAndCompareValues (t , envFileName , expectedValues )
168
+ loadEnvAndCompareValues (t , Load , envFileName , expectedValues , noopPresets )
120
169
}
121
170
122
171
func TestActualEnvVarsAreLeftAlone (t * testing.T ) {
0 commit comments