Make sure we don't overwrite existing ENV vars.

This commit is contained in:
John Barton (joho) 2013-07-31 14:25:10 +10:00
parent 3f6d91c4c0
commit 9ec71cc6c8
2 changed files with 11 additions and 1 deletions

View File

@ -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)
}
}

View File

@ -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")