From 9b4bbb98c319c668cbbeb4df8b27f4c301ad714c Mon Sep 17 00:00:00 2001 From: "John Barton (joho)" Date: Wed, 31 Jul 2013 12:57:44 +1000 Subject: [PATCH] Clear the env at the start of each integration test. --- godotenv.go | 2 ++ godotenv_test.go | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/godotenv.go b/godotenv.go index 2a3c4fc..18430cc 100644 --- a/godotenv.go +++ b/godotenv.go @@ -42,8 +42,10 @@ func loadFile(filename string) (err error) { } for _, fullLine := range lines { + // fmt.Printf("Line: %v\n", fullLine) if !isIgnoredLine(fullLine) { key, value, err := parseLine(fullLine) + // fmt.Printf("Key: %v Value: %v\n", key, value) if err == nil { os.Setenv(key, value) diff --git a/godotenv_test.go b/godotenv_test.go index 933595c..5d3d328 100644 --- a/godotenv_test.go +++ b/godotenv_test.go @@ -13,6 +13,9 @@ func parseAndCompare(t *testing.T, rawEnvLine string, expectedKey string, expect } func loadEnvAndCompareValues(t *testing.T, envFileName string, expectedValues map[string]string) { + // first up, clear the env + os.Clearenv() + err := Load(envFileName) if err != nil { t.Fatalf("Error loading %v", envFileName) @@ -36,7 +39,7 @@ func TestLoadFileNotFound(t *testing.T) { func TestLoadPlainEnv(t *testing.T) { envFileName := "fixtures/plain.env" - plainValues := map[string]string{ + expectedValues := map[string]string{ "OPTION_A": "1", "OPTION_B": "2", "OPTION_C": "3", @@ -44,7 +47,33 @@ func TestLoadPlainEnv(t *testing.T) { "OPTION_E": "5", } - loadEnvAndCompareValues(t, envFileName, plainValues) + loadEnvAndCompareValues(t, envFileName, expectedValues) +} + +func TestLoadExportedEnv(t *testing.T) { + envFileName := "fixtures/exported.env" + expectedValues := map[string]string{ + "OPTION_A": "2", + "OPTION_B": "\n", + } + + loadEnvAndCompareValues(t, envFileName, expectedValues) +} + +func TestLoadQuotedEnv(t *testing.T) { + envFileName := "fixtures/quoted.env" + expectedValues := map[string]string{ + "OPTION_A": "1", + "OPTION_B": "2", + "OPTION_C": "", + "OPTION_D": "\n", + "OPTION_E": "1", + "OPTION_F": "2", + "OPTION_G": "", + "OPTION_H": "\n", + } + + loadEnvAndCompareValues(t, envFileName, expectedValues) } func TestParsing(t *testing.T) { @@ -69,6 +98,7 @@ func TestParsing(t *testing.T) { // parses export keyword parseAndCompare(t, "export OPTION_A=2", "OPTION_A", "2") + parseAndCompare(t, "export OPTION_B='\n'", "OPTION_B", "\n") // it 'expands newlines in quoted strings' do // expect(env('FOO="bar\nbaz"')).to eql('FOO' => "bar\nbaz")