6
6
"os"
7
7
"reflect"
8
8
"testing"
9
+ "strings"
9
10
)
10
11
11
12
var noopPresets = make (map [string ]string )
@@ -161,7 +162,7 @@ func TestLoadExportedEnv(t *testing.T) {
161
162
envFileName := "fixtures/exported.env"
162
163
expectedValues := map [string ]string {
163
164
"OPTION_A" : "2" ,
164
- "OPTION_B" : "\n " ,
165
+ "OPTION_B" : "\\ n" ,
165
166
}
166
167
167
168
loadEnvAndCompareValues (t , Load , envFileName , expectedValues , noopPresets )
@@ -182,7 +183,7 @@ func TestLoadQuotedEnv(t *testing.T) {
182
183
"OPTION_A" : "1" ,
183
184
"OPTION_B" : "2" ,
184
185
"OPTION_C" : "" ,
185
- "OPTION_D" : "\n " ,
186
+ "OPTION_D" : "\\ n" ,
186
187
"OPTION_E" : "1" ,
187
188
"OPTION_F" : "2" ,
188
189
"OPTION_G" : "" ,
@@ -193,7 +194,7 @@ func TestLoadQuotedEnv(t *testing.T) {
193
194
loadEnvAndCompareValues (t , Load , envFileName , expectedValues , noopPresets )
194
195
}
195
196
196
- func TestSubstituitions (t * testing.T ) {
197
+ func TestSubstitutions (t * testing.T ) {
197
198
envFileName := "fixtures/substitutions.env"
198
199
expectedValues := map [string ]string {
199
200
"OPTION_A" : "1" ,
@@ -206,6 +207,70 @@ func TestSubstituitions(t *testing.T) {
206
207
loadEnvAndCompareValues (t , Load , envFileName , expectedValues , noopPresets )
207
208
}
208
209
210
+ func TestExpanding (t * testing.T ) {
211
+ tests := []struct {
212
+ name string
213
+ input string
214
+ expected map [string ]string
215
+ }{
216
+ {
217
+ "expands variables found in values" ,
218
+ "FOO=test\n BAR=$FOO" ,
219
+ map [string ]string {"FOO" : "test" , "BAR" : "test" },
220
+ },
221
+ {
222
+ "parses variables wrapped in brackets" ,
223
+ "FOO=test\n BAR=${FOO}bar" ,
224
+ map [string ]string {"FOO" : "test" , "BAR" : "testbar" },
225
+ },
226
+ {
227
+ "expands undefined variables to an empty string" ,
228
+ "BAR=$FOO" ,
229
+ map [string ]string {"BAR" : "" },
230
+ },
231
+ {
232
+ "expands variables in double quoted strings" ,
233
+ "FOO=test\n BAR=\" quote $FOO\" " ,
234
+ map [string ]string {"FOO" : "test" , "BAR" : "quote test" },
235
+ },
236
+ {
237
+ "does not expand variables in single quoted strings" ,
238
+ "BAR='quote $FOO'" ,
239
+ map [string ]string {"BAR" : "quote $FOO" },
240
+ },
241
+ {
242
+ "does not expand escaped variables" ,
243
+ `FOO="foo\$BAR"` ,
244
+ map [string ]string {"FOO" : "foo$BAR" },
245
+ },
246
+ {
247
+ "does not expand escaped variables" ,
248
+ `FOO="foo\${BAR}"` ,
249
+ map [string ]string {"FOO" : "foo${BAR}" },
250
+ },
251
+ {
252
+ "does not expand escaped variables" ,
253
+ "FOO=test\n BAR=\" foo\\ ${FOO} ${FOO}\" " ,
254
+ map [string ]string {"FOO" : "test" , "BAR" : "foo${FOO} test" },
255
+ },
256
+ }
257
+
258
+ for _ , tt := range tests {
259
+ t .Run (tt .name , func (t * testing.T ) {
260
+ env , err := Parse (strings .NewReader (tt .input ))
261
+ if err != nil {
262
+ t .Errorf ("Error: %s" , err .Error ())
263
+ }
264
+ for k , v := range tt .expected {
265
+ if strings .Compare (env [k ], v ) != 0 {
266
+ t .Errorf ("Expected: %s, Actual: %s" , v , env [k ])
267
+ }
268
+ }
269
+ })
270
+ }
271
+
272
+ }
273
+
209
274
func TestActualEnvVarsAreLeftAlone (t * testing.T ) {
210
275
os .Clearenv ()
211
276
os .Setenv ("OPTION_A" , "actualenv" )
@@ -247,7 +312,7 @@ func TestParsing(t *testing.T) {
247
312
248
313
// parses export keyword
249
314
parseAndCompare (t , "export OPTION_A=2" , "OPTION_A" , "2" )
250
- parseAndCompare (t , `export OPTION_B='\n'` , "OPTION_B" , "\n " )
315
+ parseAndCompare (t , `export OPTION_B='\n'` , "OPTION_B" , "\\ n" )
251
316
252
317
// it 'expands newlines in quoted strings' do
253
318
// expect(env('FOO="bar\nbaz"')).to eql('FOO' => "bar\nbaz")
0 commit comments