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

Fix inherited env var not found in multi-var .env #221

Closed
Closed
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
3 changes: 3 additions & 0 deletions dotenv/fixtures/inherited-not-found-multi-var.env
@@ -0,0 +1,3 @@
FOO=bar
VARIABLE_NOT_FOUND
BAR=baz
59 changes: 59 additions & 0 deletions dotenv/godotenv_test.go
Expand Up @@ -598,6 +598,65 @@ func TestInheritedEnvVariableNotFoundWithLookup(t *testing.T) {
}
}

func TestInheritedEnvVariableNotFoundMultiVar(t *testing.T) {
envMap, err := Read("fixtures/inherited-not-found-multi-var.env")
if err != nil {
t.Fatal(err)
}

if v, present := envMap["VARIABLE_NOT_FOUND"]; present {
t.Errorf("Expected 'VARIABLE_NOT_FOUND' to not exist, but got %q", v)
}

expectedValues := map[string]string{
"FOO": "bar",
"BAR": "baz",
}
if len(envMap) != len(expectedValues) {
t.Errorf("Expected the size of envMap to be %d, but was %d", len(expectedValues), len(envMap))
}
for key, value := range expectedValues {
if envMap[key] != value {
t.Errorf("Read got one of the keys wrong, [%q]->%q", key, envMap[key])
}
}
}

func TestInheritedEnvVariableNotFoundMultiVarWithLookup(t *testing.T) {
notFoundMap := make(map[string]bool)
envMap, err := ReadWithLookup(func(v string) (string, bool) {
envVar, ok := os.LookupEnv(v)
if !ok {
notFoundMap[v] = true
}
return envVar, ok
}, "fixtures/inherited-not-found-multi-var.env")

if err != nil {
t.Fatal(err)
}

if !notFoundMap["VARIABLE_NOT_FOUND"] {
t.Error("Expected 'VARIABLE_NOT_FOUND' to be part of not found variables")
}
if v, present := envMap["VARIABLE_NOT_FOUND"]; present {
t.Errorf("Expected 'VARIABLE_NOT_FOUND' to not exist, but got %q", v)
}

expectedValues := map[string]string{
"FOO": "bar",
"BAR": "baz",
}
if len(envMap) != len(expectedValues) {
t.Errorf("Expected the size of envMap to be %d, but was %d", len(expectedValues), len(envMap))
}
for key, value := range expectedValues {
if envMap[key] != value {
t.Errorf("Read got one of the keys wrong, [%q]->%q", key, envMap[key])
}
}
}

func TestExpendingEnvironmentWithLookup(t *testing.T) {
rawEnvLine := "TEST=$ME"
expectedValue := "YES"
Expand Down
4 changes: 2 additions & 2 deletions dotenv/parser.go
Expand Up @@ -41,9 +41,9 @@ func parseBytes(src []byte, out map[string]string, lookupFn LookupFn) error {
value, ok := lookupFn(key)
if ok {
out[key] = value
cutset = left
continue
}
cutset = left
continue
}

value, left, err := extractVarValue(left, out, lookupFn)
Expand Down