From 9ec71cc6c8895835970179bbc61547dd0db77dbd Mon Sep 17 00:00:00 2001 From: "John Barton (joho)" Date: Wed, 31 Jul 2013 14:25:10 +1000 Subject: [PATCH] Make sure we don't overwrite existing ENV vars. --- godotenv.go | 2 +- godotenv_test.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/godotenv.go b/godotenv.go index a210f4e..91151fe 100644 --- a/godotenv.go +++ b/godotenv.go @@ -34,7 +34,7 @@ func loadFile(filename string) (err error) { if !isIgnoredLine(fullLine) { key, value, err := parseLine(fullLine) - if err == nil { + if err == nil && os.Getenv(key) == "" { os.Setenv(key, value) } } diff --git a/godotenv_test.go b/godotenv_test.go index 2f096e7..e6ec8d9 100644 --- a/godotenv_test.go +++ b/godotenv_test.go @@ -83,6 +83,16 @@ func TestLoadQuotedEnv(t *testing.T) { loadEnvAndCompareValues(t, envFileName, expectedValues) } +func TestActualEnvVarsAreLeftAlone(t *testing.T) { + os.Clearenv() + os.Setenv("OPTION_A", "actualenv") + _ = Load("fixtures/plain.env") + + if os.Getenv("OPTION_A") != "actualenv" { + t.Error("An ENV var set earlier was overwritten") + } +} + func TestParsing(t *testing.T) { // unquoted values parseAndCompare(t, "FOO=bar", "FOO", "bar")