Skip to content

Commit f56910a

Browse files
authored
Merge pull request #472 from nicks/nicks/panic
loader: fix a panic
2 parents 726d8b0 + 162cc09 commit f56910a

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

loader/loader.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,15 @@ func LoadWithContext(ctx context.Context, configDetails types.ConfigDetails, opt
238238
return nil, err
239239
}
240240
opts.projectName = projectName
241+
242+
// TODO(milas): this should probably ALWAYS set (overriding any existing)
243+
if _, ok := configDetails.Environment[consts.ComposeProjectName]; !ok && projectName != "" {
244+
if configDetails.Environment == nil {
245+
configDetails.Environment = map[string]string{}
246+
}
247+
configDetails.Environment[consts.ComposeProjectName] = projectName
248+
}
249+
241250
return load(ctx, configDetails, opts, nil)
242251
}
243252

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

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

loader/loader_test.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,7 +2246,7 @@ func TestLoadLegacyBoolean(t *testing.T) {
22462246
name: load-legacy-boolean
22472247
services:
22482248
test:
2249-
init: yes # used to be a valid YAML bool, removed in YAML 1.2
2249+
init: yes # used to be a valid YAML bool, removed in YAML 1.2
22502250
`)
22512251
assert.NilError(t, err)
22522252
assert.Check(t, *actual.Services[0].Init)
@@ -2276,7 +2276,7 @@ services:
22762276
test:
22772277
build:
22782278
context: .
2279-
ssh:
2279+
ssh:
22802280
key1: value1
22812281
`)
22822282
assert.NilError(t, err)
@@ -2294,7 +2294,7 @@ services:
22942294
test:
22952295
build:
22962296
context: .
2297-
ssh:
2297+
ssh:
22982298
- key1=value1
22992299
- key2=value2
23002300
`)
@@ -2642,7 +2642,7 @@ include:
26422642
env_file: ./testdata/subdir/extra.env
26432643
- path: ./testdata/compose-include.yaml
26442644
env_file: ./testdata/subdir/extra.env
2645-
2645+
26462646
26472647
services:
26482648
bar:
@@ -2767,7 +2767,7 @@ services:
27672767
func TestLoadWithNestedResources(t *testing.T) {
27682768
config := buildConfigDetails(`
27692769
name: test-nested-resources
2770-
include:
2770+
include:
27712771
- remote:nested/compose.yaml
27722772
`, nil)
27732773
_, err := LoadWithContext(context.Background(), config, func(options *Options) {
@@ -2808,7 +2808,7 @@ name: load-multi-docs
28082808
services:
28092809
test:
28102810
image: nginx:latest
2811-
---
2811+
---
28122812
services:
28132813
test:
28142814
image: nginx:override
@@ -2826,7 +2826,7 @@ services:
28262826
image: example/webapp
28272827
build: ./webapp
28282828
develop:
2829-
watch:
2829+
watch:
28302830
# sync static content
28312831
- path: ./webapp/html
28322832
action: sync
@@ -2838,7 +2838,7 @@ services:
28382838
image: example/backend
28392839
build: ./backend
28402840
develop:
2841-
watch:
2841+
watch:
28422842
# rebuild image and recreate service
28432843
- path: ./backend/src
28442844
action: rebuild
@@ -2887,3 +2887,25 @@ services:
28872887
},
28882888
})
28892889
}
2890+
2891+
func TestBadServiceConfig(t *testing.T) {
2892+
yaml := `name: scratch
2893+
services:
2894+
redis:
2895+
image: redis:6.2.6-alpine
2896+
network_mode: bridge
2897+
networks:
2898+
gratheon: null
2899+
networks:
2900+
gratheon:
2901+
name: scratch_gratheon
2902+
`
2903+
_, err := LoadWithContext(context.Background(), types.ConfigDetails{
2904+
ConfigFiles: []types.ConfigFile{
2905+
{
2906+
Content: []byte(yaml),
2907+
},
2908+
},
2909+
})
2910+
assert.ErrorContains(t, err, "service redis declares mutually exclusive `network_mode` and `networks`")
2911+
}

0 commit comments

Comments
 (0)