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

Read should behave as Load does #237

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fixtures/read_overrides.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OPTION_A=99
4 changes: 3 additions & 1 deletion godotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ func Read(filenames ...string) (envMap map[string]string, err error) {
}

for key, value := range individualEnvMap {
envMap[key] = value
if _, ok := envMap[key]; !ok {
envMap[key] = value
}
}
}

Expand Down
32 changes: 24 additions & 8 deletions godotenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,23 @@ func TestLoadDoesNotOverride(t *testing.T) {
"OPTION_A": "do_not_override",
"OPTION_B": "",
}

loadEnvAndCompareValues(t, Load, envFileName, expectedValues, presets)

}

func TestReadDoesNotOverride(t *testing.T) {
envFileName := "fixtures/plain.env"
overrideEnvName := "fixtures/read_overrides.env"

envMap, err := Read(envFileName, overrideEnvName)
if err != nil {
t.Error("Error reading file")
}

if envMap["OPTION_A"] != "1" {
t.Error("Read falsely overrode OPTION_A")
}
}

func TestOverloadDoesOverride(t *testing.T) {
Expand Down Expand Up @@ -588,42 +604,42 @@ func TestWhitespace(t *testing.T) {
}{
"Leading whitespace": {
input: " A=a\n",
key: "A",
key: "A",
value: "a",
},
"Leading tab": {
input: "\tA=a\n",
key: "A",
key: "A",
value: "a",
},
"Leading mixed whitespace": {
input: " \t \t\n\t \t A=a\n",
key: "A",
key: "A",
value: "a",
},
"Leading whitespace before export": {
input: " \t\t export A=a\n",
key: "A",
key: "A",
value: "a",
},
"Trailing whitespace": {
input: "A=a \t \t\n",
key: "A",
key: "A",
value: "a",
},
"Trailing whitespace with export": {
input: "export A=a\t \t \n",
key: "A",
key: "A",
value: "a",
},
"No EOL": {
input: "A=a",
key: "A",
key: "A",
value: "a",
},
"Trailing whitespace with no EOL": {
input: "A=a ",
key: "A",
key: "A",
value: "a",
},
}
Expand Down
Loading