Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer environment variables over local variables #12

Merged
merged 1 commit into from Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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