Skip to content
This repository was archived by the owner on Jan 12, 2022. It is now read-only.

Commit 586bb51

Browse files
authored
Merge pull request #7 from ulyssessouza/inherited-vars2
Fix inherited for multi variables in file
2 parents 32d4136 + 13811c1 commit 586bb51

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

fixtures/inherited-multi-var.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
foo=bar
2+
VAR_TO_BE_LOADED_FROM_OS_ENV
3+
bar=baz
File renamed without changes.

godotenv_test.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,14 +506,16 @@ func TestRoundtrip(t *testing.T) {
506506
}
507507
}
508508

509-
func TestInheritedEnvVariable(t *testing.T) {
509+
func TestInheritedEnvVariablSameSize(t *testing.T) {
510510
const envKey = "VAR_TO_BE_LOADED_FROM_OS_ENV"
511511
const envVal = "SOME_RANDOM_VALUE"
512512
os.Setenv(envKey, envVal)
513513

514-
envFileName := "fixtures/inherited.env"
514+
envFileName := "fixtures/inherited-multi-var.env"
515515
expectedValues := map[string]string{
516516
envKey: envVal,
517+
"foo": "bar",
518+
"bar": "baz",
517519
}
518520

519521
envMap, err := ReadWithLookup(os.LookupEnv, envFileName)
@@ -525,7 +527,31 @@ func TestInheritedEnvVariable(t *testing.T) {
525527
}
526528
for key, value := range expectedValues {
527529
if envMap[key] != value {
528-
t.Error("Read got one of the keys wrong")
530+
t.Errorf("Read got one of the keys wrong, [%q]->%q", key, envMap[key])
531+
}
532+
}
533+
}
534+
535+
func TestInheritedEnvVariablSingleVar(t *testing.T) {
536+
const envKey = "VAR_TO_BE_LOADED_FROM_OS_ENV"
537+
const envVal = "SOME_RANDOM_VALUE"
538+
os.Setenv(envKey, envVal)
539+
540+
envFileName := "fixtures/inherited-single-var.env"
541+
expectedValues := map[string]string{
542+
envKey: envVal,
543+
}
544+
545+
envMap, err := ReadWithLookup(os.LookupEnv, envFileName)
546+
if err != nil {
547+
t.Error("Error reading file")
548+
}
549+
if len(envMap) != len(expectedValues) {
550+
t.Error("Didn't get the right size map back")
551+
}
552+
for key, value := range expectedValues {
553+
if envMap[key] != value {
554+
t.Errorf("Read got one of the keys wrong, [%q]->%q", key, envMap[key])
529555
}
530556
}
531557
}

parser.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ func parseBytes(src []byte, out map[string]string, lookupFn LookupFn) error {
2525
break
2626
}
2727

28-
key, left, err := locateKeyName(cutset)
28+
key, left, inherited, err := locateKeyName(cutset)
2929
if err != nil {
3030
return err
3131
}
3232
if strings.Contains(key, " ") {
3333
return errors.New("key cannot contain a space")
3434
}
3535

36-
if left == nil {
36+
if inherited {
3737
if lookupFn == nil {
3838
lookupFn = noLookupFn
3939
}
@@ -82,7 +82,7 @@ func getStatementStart(src []byte) []byte {
8282
}
8383

8484
// locateKeyName locates and parses key name and returns rest of slice
85-
func locateKeyName(src []byte) (key string, cutset []byte, err error) {
85+
func locateKeyName(src []byte) (key string, cutset []byte, inherited bool, err error) {
8686
// trim "export" and space at beginning
8787
src = bytes.TrimLeftFunc(bytes.TrimPrefix(src, []byte(exportPrefix)), isSpace)
8888

@@ -100,6 +100,7 @@ loop:
100100
// library also supports yaml-style value declaration
101101
key = string(src[0:i])
102102
offset = i + 1
103+
inherited = char == '\n'
103104
break loop
104105
case '_':
105106
default:
@@ -108,20 +109,20 @@ loop:
108109
continue
109110
}
110111

111-
return "", nil, fmt.Errorf(
112+
return "", nil, inherited, fmt.Errorf(
112113
`unexpected character %q in variable name near %q`,
113114
string(char), string(src))
114115
}
115116
}
116117

117118
if len(src) == 0 {
118-
return "", nil, errors.New("zero length string")
119+
return "", nil, inherited, errors.New("zero length string")
119120
}
120121

121122
// trim whitespace
122123
key = strings.TrimRightFunc(key, unicode.IsSpace)
123124
cutset = bytes.TrimLeftFunc(src[offset:], isSpace)
124-
return key, cutset, nil
125+
return key, cutset, inherited, nil
125126
}
126127

127128
// extractVarValue extracts variable value and returns rest of slice

0 commit comments

Comments
 (0)