Skip to content

Commit

Permalink
loader: read from file if ConfigFile.Content is empty (#315)
Browse files Browse the repository at this point in the history
Fixes #314.

Signed-off-by: Andy Walker <walkeraj@gmail.com>
  • Loading branch information
flowchartsman committed Nov 3, 2022
1 parent 517cf49 commit 918dbe8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
7 changes: 7 additions & 0 deletions loader/loader.go
Expand Up @@ -167,6 +167,13 @@ func Load(configDetails types.ConfigDetails, options ...func(*Options)) (*types.
for i, file := range configDetails.ConfigFiles {
configDict := file.Config
if configDict == nil {
if len(file.Content) == 0 {
content, err := os.ReadFile(file.Filename)
if err != nil {
return nil, err
}
file.Content = content
}
dict, err := parseConfig(file.Content, opts)
if err != nil {
return nil, err
Expand Down
27 changes: 27 additions & 0 deletions loader/loader_test.go
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"testing"
Expand Down Expand Up @@ -270,6 +271,32 @@ func TestLoad(t *testing.T) {
assert.Check(t, is.DeepEqual(sampleConfig.Volumes, actual.Volumes))
}

func TestLoadFromFile(t *testing.T) {
workingDir, err := os.Getwd()
if err != nil {
t.Fatalf("os.Getwd(): %s", err)
}
tmpdir := t.TempDir()
tmpPath := filepath.Join(tmpdir, "Docker-compose.yaml")
if err := os.WriteFile(tmpPath, []byte(sampleYAML), 0444); err != nil {
t.Fatalf("failed to write temporary file: %s", err)
}
actual, err := Load(types.ConfigDetails{
WorkingDir: workingDir,
ConfigFiles: []types.ConfigFile{{
Filename: tmpPath,
}},
Environment: nil,
}, func(options *Options) {
options.SkipNormalization = true
options.SkipConsistencyCheck = true
})
assert.NilError(t, err)
assert.Check(t, is.DeepEqual(serviceSort(sampleConfig.Services), serviceSort(actual.Services)))
assert.Check(t, is.DeepEqual(sampleConfig.Networks, actual.Networks))
assert.Check(t, is.DeepEqual(sampleConfig.Volumes, actual.Volumes))
}

func TestLoadExtensions(t *testing.T) {
actual, err := loadYAML(`
services:
Expand Down

0 comments on commit 918dbe8

Please sign in to comment.