Skip to content

Commit

Permalink
Merge pull request #472 from nicks/nicks/panic
Browse files Browse the repository at this point in the history
loader: fix a panic
  • Loading branch information
ndeloof authored Oct 17, 2023
2 parents 726d8b0 + 162cc09 commit f56910a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
13 changes: 9 additions & 4 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,15 @@ func LoadWithContext(ctx context.Context, configDetails types.ConfigDetails, opt
return nil, err
}
opts.projectName = projectName

// TODO(milas): this should probably ALWAYS set (overriding any existing)
if _, ok := configDetails.Environment[consts.ComposeProjectName]; !ok && projectName != "" {
if configDetails.Environment == nil {
configDetails.Environment = map[string]string{}
}
configDetails.Environment[consts.ComposeProjectName] = projectName
}

return load(ctx, configDetails, opts, nil)
}

Expand Down Expand Up @@ -461,10 +470,6 @@ func projectName(details types.ConfigDetails, opts *Options) (string, error) {
return "", InvalidProjectNameErr(projectName)
}

// TODO(milas): this should probably ALWAYS set (overriding any existing)
if _, ok := details.Environment[consts.ComposeProjectName]; !ok && projectName != "" {
details.Environment[consts.ComposeProjectName] = projectName
}
return projectName, nil
}

Expand Down
38 changes: 30 additions & 8 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2246,7 +2246,7 @@ func TestLoadLegacyBoolean(t *testing.T) {
name: load-legacy-boolean
services:
test:
init: yes # used to be a valid YAML bool, removed in YAML 1.2
init: yes # used to be a valid YAML bool, removed in YAML 1.2
`)
assert.NilError(t, err)
assert.Check(t, *actual.Services[0].Init)
Expand Down Expand Up @@ -2276,7 +2276,7 @@ services:
test:
build:
context: .
ssh:
ssh:
key1: value1
`)
assert.NilError(t, err)
Expand All @@ -2294,7 +2294,7 @@ services:
test:
build:
context: .
ssh:
ssh:
- key1=value1
- key2=value2
`)
Expand Down Expand Up @@ -2642,7 +2642,7 @@ include:
env_file: ./testdata/subdir/extra.env
- path: ./testdata/compose-include.yaml
env_file: ./testdata/subdir/extra.env
services:
bar:
Expand Down Expand Up @@ -2767,7 +2767,7 @@ services:
func TestLoadWithNestedResources(t *testing.T) {
config := buildConfigDetails(`
name: test-nested-resources
include:
include:
- remote:nested/compose.yaml
`, nil)
_, err := LoadWithContext(context.Background(), config, func(options *Options) {
Expand Down Expand Up @@ -2808,7 +2808,7 @@ name: load-multi-docs
services:
test:
image: nginx:latest
---
---
services:
test:
image: nginx:override
Expand All @@ -2826,7 +2826,7 @@ services:
image: example/webapp
build: ./webapp
develop:
watch:
watch:
# sync static content
- path: ./webapp/html
action: sync
Expand All @@ -2838,7 +2838,7 @@ services:
image: example/backend
build: ./backend
develop:
watch:
watch:
# rebuild image and recreate service
- path: ./backend/src
action: rebuild
Expand Down Expand Up @@ -2887,3 +2887,25 @@ services:
},
})
}

func TestBadServiceConfig(t *testing.T) {
yaml := `name: scratch
services:
redis:
image: redis:6.2.6-alpine
network_mode: bridge
networks:
gratheon: null
networks:
gratheon:
name: scratch_gratheon
`
_, err := LoadWithContext(context.Background(), types.ConfigDetails{
ConfigFiles: []types.ConfigFile{
{
Content: []byte(yaml),
},
},
})
assert.ErrorContains(t, err, "service redis declares mutually exclusive `network_mode` and `networks`")
}

0 comments on commit f56910a

Please sign in to comment.