diff --git a/dotenv/fixtures/inherited-not-found-multi-var.env b/dotenv/fixtures/inherited-not-found-multi-var.env new file mode 100644 index 00000000..198dd0f2 --- /dev/null +++ b/dotenv/fixtures/inherited-not-found-multi-var.env @@ -0,0 +1,3 @@ +FOO=bar +VARIABLE_NOT_FOUND +BAR=baz diff --git a/dotenv/godotenv_test.go b/dotenv/godotenv_test.go index 7fa718fd..fada7230 100644 --- a/dotenv/godotenv_test.go +++ b/dotenv/godotenv_test.go @@ -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" diff --git a/dotenv/parser.go b/dotenv/parser.go index e657c8df..228655a9 100644 --- a/dotenv/parser.go +++ b/dotenv/parser.go @@ -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)