Skip to content

Commit

Permalink
Merge pull request #29 from joho/respect_empty_external_env_vars
Browse files Browse the repository at this point in the history
Respect preset empty external env vars
  • Loading branch information
joho authored Mar 28, 2017
2 parents eaf676f + 034acc2 commit 325433c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 8 additions & 1 deletion godotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,15 @@ func loadFile(filename string, overload bool) error {
return err
}

currentEnv := map[string]bool{}
rawEnv := os.Environ()
for _, rawEnvLine := range rawEnv {
key := strings.Split(rawEnvLine, "=")[0]
currentEnv[key] = true
}

for key, value := range envMap {
if os.Getenv(key) == "" || overload {
if !currentEnv[key] || overload {
os.Setenv(key, value)
}
}
Expand Down
2 changes: 2 additions & 0 deletions godotenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,12 @@ func TestLoadDoesNotOverride(t *testing.T) {
// ensure NO overload
presets := map[string]string{
"OPTION_A": "do_not_override",
"OPTION_B": "",
}

expectedValues := map[string]string{
"OPTION_A": "do_not_override",
"OPTION_B": "",
}
loadEnvAndCompareValues(t, Load, envFileName, expectedValues, presets)
}
Expand Down

0 comments on commit 325433c

Please sign in to comment.