Skip to content

Commit 2a3ce93

Browse files
aksiksindeloof
authored andcommitted
read config file content if missing
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 <[email protected]>
1 parent 1aa0c8a commit 2a3ce93

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

loader/loader.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,17 @@ func projectName(details types.ConfigDetails, opts *Options) (string, error) {
484484
if !projectNameImperativelySet {
485485
var pjNameFromConfigFile string
486486
for _, configFile := range details.ConfigFiles {
487-
yml, err := ParseYAML(configFile.Content)
487+
content := configFile.Content
488+
if content == nil {
489+
// This can be hit when Filename is set but Content is not. One
490+
// example is when using ToConfigFiles().
491+
d, err := os.ReadFile(configFile.Filename)
492+
if err != nil {
493+
return "", fmt.Errorf("failed to read file %q: %w", configFile.Filename, err)
494+
}
495+
content = d
496+
}
497+
yml, err := ParseYAML(content)
488498
if err != nil {
489499
// HACK: the way that loading is currently structured, this is
490500
// a duplicative parse just for the `name`. if it fails, we

loader/loader_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ services:
405405
}, func(options *Options) {
406406
options.SkipNormalization = true
407407
options.SkipConsistencyCheck = true
408+
options.SetProjectName("project", true)
408409
})
409410
assert.NilError(t, err)
410411
assert.Assert(t, is.Len(actual.Services, 2))
@@ -2854,6 +2855,46 @@ networks:
28542855
assert.ErrorContains(t, err, "service redis declares mutually exclusive `network_mode` and `networks`")
28552856
}
28562857

2858+
func TestLoadEmptyContent(t *testing.T) {
2859+
yaml := `name: load-multi-docs
2860+
services:
2861+
test:
2862+
image: nginx:latest`
2863+
tmpPath := filepath.Join(t.TempDir(), "docker-compose.yaml")
2864+
if err := os.WriteFile(tmpPath, []byte(yaml), 0o644); err != nil {
2865+
t.Fatalf("failed to write temporary file: %s", err)
2866+
}
2867+
_, err := Load(types.ConfigDetails{
2868+
ConfigFiles: []types.ConfigFile{
2869+
{
2870+
Filename: tmpPath,
2871+
},
2872+
},
2873+
})
2874+
if err != nil {
2875+
t.Fatal(err)
2876+
}
2877+
}
2878+
2879+
func TestLoadEmptyContent_MissingProject(t *testing.T) {
2880+
yaml := `
2881+
services:
2882+
test:
2883+
image: nginx:latest`
2884+
tmpPath := filepath.Join(t.TempDir(), "docker-compose.yaml")
2885+
if err := os.WriteFile(tmpPath, []byte(yaml), 0o644); err != nil {
2886+
t.Fatalf("failed to write temporary file: %s", err)
2887+
}
2888+
_, err := Load(types.ConfigDetails{
2889+
ConfigFiles: []types.ConfigFile{
2890+
{
2891+
Filename: tmpPath,
2892+
},
2893+
},
2894+
})
2895+
assert.ErrorContains(t, err, "project name must not be empty")
2896+
}
2897+
28572898
func TestLoadUnitBytes(t *testing.T) {
28582899
project, err := loadYAML(`
28592900
name: load-unit-bytes
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
include:
22
- compose-include-cycle.yaml
33

4+
name: project
45
services:
56
foo:
67
image: foo

0 commit comments

Comments
 (0)