Skip to content

Commit

Permalink
Prefer environment variables over local variables (#12)
Browse files Browse the repository at this point in the history
Closes #11
  • Loading branch information
5nord committed Apr 25, 2022
1 parent 27d3877 commit cc6b967
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions fixtures/vars.env
@@ -0,0 +1,5 @@
A="fromFile"
B="$A"

D="$C"
C="fromFile"
4 changes: 4 additions & 0 deletions gotenv.go
Expand Up @@ -220,6 +220,10 @@ func varReplacement(s string, hsq bool, env Env) string {

v := mn[3]

if replace, ok := os.LookupEnv(v); ok {
return replace
}

replace, ok := env[v]
if !ok {
replace = os.Getenv(v)
Expand Down
16 changes: 16 additions & 0 deletions gotenv_test.go
Expand Up @@ -243,6 +243,22 @@ func TestLoad_overriding(t *testing.T) {
os.Clearenv()
}

func TestLoad_overrideVars(t *testing.T) {
os.Setenv("A", "fromEnv")
err := gotenv.Load("fixtures/vars.env")
assert.Nil(t, err)
assert.Equal(t, "fromEnv", os.Getenv("B"))
os.Clearenv()
}

func TestLoad_overrideVars2(t *testing.T) {
os.Setenv("C", "fromEnv")
err := gotenv.Load("fixtures/vars.env")
assert.Nil(t, err)
assert.Equal(t, "fromEnv", os.Getenv("D"))
os.Clearenv()
}

func TestLoad_Env(t *testing.T) {
err := gotenv.Load(".env.invalid")
assert.NotNil(t, err)
Expand Down

0 comments on commit cc6b967

Please sign in to comment.