Skip to content
This repository was archived by the owner on Jan 12, 2022. It is now read-only.

Commit 993ff7a

Browse files
committed
Add multi-line var values test case and update comment test
Signed-off-by: x1unix <[email protected]>
1 parent d9069cd commit 993ff7a

File tree

1 file changed

+58
-25
lines changed

1 file changed

+58
-25
lines changed

godotenv_test.go

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,34 @@ func TestExpanding(t *testing.T) {
271271

272272
}
273273

274+
func TestVariableStringValueSeparator(t *testing.T) {
275+
input := "TEST_URLS=\"stratum+tcp://stratum.antpool.com:3333\nstratum+tcp://stratum.antpool.com:443\""
276+
want := map[string]string{
277+
"TEST_URLS": "stratum+tcp://stratum.antpool.com:3333\nstratum+tcp://stratum.antpool.com:443",
278+
}
279+
got, err := Parse(strings.NewReader(input))
280+
if err != nil {
281+
t.Error(err)
282+
}
283+
284+
if len(got) != len(want) {
285+
t.Fatalf(
286+
"unexpected value:\nwant:\n\t%#v\n\ngot:\n\t%#v", want, got)
287+
}
288+
289+
for k, wantVal := range want {
290+
gotVal, ok := got[k]
291+
if !ok {
292+
t.Fatalf("key %q doesn't present in result", k)
293+
}
294+
if wantVal != gotVal {
295+
t.Fatalf(
296+
"mismatch in %q value:\nwant:\n\t%s\n\ngot:\n\t%s", k,
297+
wantVal, gotVal)
298+
}
299+
}
300+
}
301+
274302
func TestActualEnvVarsAreLeftAlone(t *testing.T) {
275303
os.Clearenv()
276304
os.Setenv("OPTION_A", "actualenv")
@@ -377,33 +405,38 @@ func TestParsing(t *testing.T) {
377405
}
378406

379407
func TestLinesToIgnore(t *testing.T) {
380-
// it 'ignores empty lines' do
381-
// expect(env("\n \t \nfoo=bar\n \nfizz=buzz")).to eql('foo' => 'bar', 'fizz' => 'buzz')
382-
if !isIgnoredLine("\n") {
383-
t.Error("Line with nothing but line break wasn't ignored")
384-
}
385-
386-
if !isIgnoredLine("\r\n") {
387-
t.Error("Line with nothing but windows-style line break wasn't ignored")
388-
}
389-
390-
if !isIgnoredLine("\t\t ") {
391-
t.Error("Line full of whitespace wasn't ignored")
392-
}
393-
394-
// it 'ignores comment lines' do
395-
// expect(env("\n\n\n # HERE GOES FOO \nfoo=bar")).to eql('foo' => 'bar')
396-
if !isIgnoredLine("# comment") {
397-
t.Error("Comment wasn't ignored")
398-
}
399-
400-
if !isIgnoredLine("\t#comment") {
401-
t.Error("Indented comment wasn't ignored")
408+
cases := map[string]struct {
409+
input string
410+
want string
411+
}{
412+
"Line with nothing but line break": {
413+
input: "\n",
414+
},
415+
"Line with nothing but windows-style line break": {
416+
input: "\r\n",
417+
},
418+
"Line full of whitespace": {
419+
input: "\t\t ",
420+
},
421+
"Comment": {
422+
input: "# Comment",
423+
},
424+
"Indented comment": {
425+
input: "\t # comment",
426+
},
427+
"non-ignored value": {
428+
input: `export OPTION_B='\n'`,
429+
want: `export OPTION_B='\n'`,
430+
},
402431
}
403432

404-
// make sure we're not getting false positives
405-
if isIgnoredLine(`export OPTION_B='\n'`) {
406-
t.Error("ignoring a perfectly valid line to parse")
433+
for n, c := range cases {
434+
t.Run(n, func(t *testing.T) {
435+
got := string(getStatementStart([]byte(c.input)))
436+
if got != c.want {
437+
t.Errorf("Expected:\t %q\nGot:\t %q", c.want, got)
438+
}
439+
})
407440
}
408441
}
409442

0 commit comments

Comments
 (0)