Skip to content

Commit

Permalink
read config file content if missing
Browse files Browse the repository at this point in the history
This ensures that project name validation occurs in cases where the
content is missing (e.g., when constructed via ToConfigFiles).

Signed-off-by: Assil Ksiksi <assil@ksiksi.net>
  • Loading branch information
aksiksi authored and ndeloof committed Dec 21, 2023
1 parent 1aa0c8a commit 2a3ce93
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
12 changes: 11 additions & 1 deletion loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,17 @@ func projectName(details types.ConfigDetails, opts *Options) (string, error) {
if !projectNameImperativelySet {
var pjNameFromConfigFile string
for _, configFile := range details.ConfigFiles {
yml, err := ParseYAML(configFile.Content)
content := configFile.Content
if content == nil {
// This can be hit when Filename is set but Content is not. One
// example is when using ToConfigFiles().
d, err := os.ReadFile(configFile.Filename)
if err != nil {
return "", fmt.Errorf("failed to read file %q: %w", configFile.Filename, err)
}
content = d
}
yml, err := ParseYAML(content)
if err != nil {
// HACK: the way that loading is currently structured, this is
// a duplicative parse just for the `name`. if it fails, we
Expand Down
41 changes: 41 additions & 0 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ services:
}, func(options *Options) {
options.SkipNormalization = true
options.SkipConsistencyCheck = true
options.SetProjectName("project", true)
})
assert.NilError(t, err)
assert.Assert(t, is.Len(actual.Services, 2))
Expand Down Expand Up @@ -2854,6 +2855,46 @@ networks:
assert.ErrorContains(t, err, "service redis declares mutually exclusive `network_mode` and `networks`")
}

func TestLoadEmptyContent(t *testing.T) {
yaml := `name: load-multi-docs
services:
test:
image: nginx:latest`
tmpPath := filepath.Join(t.TempDir(), "docker-compose.yaml")
if err := os.WriteFile(tmpPath, []byte(yaml), 0o644); err != nil {
t.Fatalf("failed to write temporary file: %s", err)
}
_, err := Load(types.ConfigDetails{
ConfigFiles: []types.ConfigFile{
{
Filename: tmpPath,
},
},
})
if err != nil {
t.Fatal(err)
}
}

func TestLoadEmptyContent_MissingProject(t *testing.T) {
yaml := `
services:
test:
image: nginx:latest`
tmpPath := filepath.Join(t.TempDir(), "docker-compose.yaml")
if err := os.WriteFile(tmpPath, []byte(yaml), 0o644); err != nil {
t.Fatalf("failed to write temporary file: %s", err)
}
_, err := Load(types.ConfigDetails{
ConfigFiles: []types.ConfigFile{
{
Filename: tmpPath,
},
},
})
assert.ErrorContains(t, err, "project name must not be empty")
}

func TestLoadUnitBytes(t *testing.T) {
project, err := loadYAML(`
name: load-unit-bytes
Expand Down
1 change: 1 addition & 0 deletions loader/testdata/compose-include-cycle.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
include:
- compose-include-cycle.yaml

name: project
services:
foo:
image: foo

0 comments on commit 2a3ce93

Please sign in to comment.