Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit ced6fdd

Browse files
authored
Merge pull request #317 from dtan4/robust-env-file
Skip empty line and comment in env_file
2 parents b346e17 + 7810db7 commit ced6fdd

File tree

4 files changed

+69
-10
lines changed

4 files changed

+69
-10
lines changed

config/merge.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,21 @@ func readEnvFile(resourceLookup ResourceLookup, inFile string, serviceData RawSe
125125
scanner := bufio.NewScanner(bytes.NewBuffer(content))
126126
for scanner.Scan() {
127127
line := strings.TrimSpace(scanner.Text())
128-
key := strings.SplitAfter(line, "=")[0]
129128

130-
found := false
131-
for _, v := range vars {
132-
if strings.HasPrefix(v, key) {
133-
found = true
134-
break
129+
if len(line) > 0 && !strings.HasPrefix(line, "#") {
130+
key := strings.SplitAfter(line, "=")[0]
131+
132+
found := false
133+
for _, v := range vars {
134+
if strings.HasPrefix(v, key) {
135+
found = true
136+
break
137+
}
135138
}
136-
}
137139

138-
if !found {
139-
vars = append(vars, line)
140+
if !found {
141+
vars = append(vars, line)
142+
}
140143
}
141144
}
142145

config/merge_fixtures_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"io/ioutil"
55
"path/filepath"
6+
"strings"
67
"testing"
78
)
89

@@ -12,7 +13,7 @@ func TestMergeOnValidFixtures(t *testing.T) {
1213
t.Fatal(err)
1314
}
1415
for _, file := range files {
15-
if file.IsDir() {
16+
if file.IsDir() || !strings.HasSuffix(file.Name(), ".yml") {
1617
continue
1718
}
1819
data, err := ioutil.ReadFile(filepath.Join("testdata", file.Name()))

config/merge_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"io/ioutil"
45
"testing"
56
)
67

@@ -15,6 +16,18 @@ func (n *NullLookup) ResolvePath(path, inFile string) string {
1516
return ""
1617
}
1718

19+
type FileLookup struct {
20+
}
21+
22+
func (f *FileLookup) Lookup(file, relativeTo string) ([]byte, string, error) {
23+
bytes, err := ioutil.ReadFile(file)
24+
return bytes, file, err
25+
}
26+
27+
func (f *FileLookup) ResolvePath(path, inFile string) string {
28+
return ""
29+
}
30+
1831
func TestExtendsInheritImage(t *testing.T) {
1932
_, configV1, _, _, err := Merge(NewServiceConfigs(), nil, &NullLookup{}, "", []byte(`
2033
parent:
@@ -198,6 +211,44 @@ services:
198211
}
199212
}
200213

214+
func TestMergesEnvFile(t *testing.T) {
215+
_, configV1, _, _, err := Merge(NewServiceConfigs(), nil, &FileLookup{}, "", []byte(`
216+
test:
217+
image: foo
218+
env_file:
219+
- testdata/.env
220+
`), nil)
221+
if err != nil {
222+
t.Fatal(err)
223+
}
224+
225+
_, configV2, _, _, err := Merge(NewServiceConfigs(), nil, &FileLookup{}, "", []byte(`
226+
version: '2'
227+
services:
228+
test:
229+
image: foo
230+
env_file:
231+
- testdata/.env
232+
`), nil)
233+
if err != nil {
234+
t.Fatal(err)
235+
}
236+
237+
for _, config := range []map[string]*ServiceConfig{configV1, configV2} {
238+
test := config["test"]
239+
240+
if len(test.Environment) != 2 {
241+
t.Fatal("env_file is not merged", test.Environment)
242+
}
243+
244+
for _, environment := range test.Environment {
245+
if (environment != "FOO=foo") && (environment != "BAR=bar") {
246+
t.Fatal("Empty line and comment should be excluded", environment)
247+
}
248+
}
249+
}
250+
}
251+
201252
func TestRestartNo(t *testing.T) {
202253
_, configV1, _, _, err := Merge(NewServiceConfigs(), nil, &NullLookup{}, "", []byte(`
203254
test:

config/testdata/.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# This is a comment line
2+
3+
FOO=foo
4+
BAR=bar

0 commit comments

Comments
 (0)