Skip to content

Commit

Permalink
loader: fix a panic
Browse files Browse the repository at this point in the history
fixes #471

Signed-off-by: Nick Santos <[email protected]>
  • Loading branch information
nicks committed Oct 13, 2023
1 parent 2d32c3f commit 175c6aa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ func projectName(details types.ConfigDetails, opts *Options) (string, error) {
}

// TODO(milas): this should probably ALWAYS set (overriding any existing)
if _, ok := details.Environment[consts.ComposeProjectName]; !ok && projectName != "" {
if _, ok := details.Environment[consts.ComposeProjectName]; !ok && projectName != "" && details.Environment != nil {
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 @@ -2869,3 +2869,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 175c6aa

Please sign in to comment.