Handle nil environment (#154)

This commit is contained in:
Pieter Noordhuis 2022-12-22 15:31:32 +01:00 committed by GitHub
parent 198eefcf39
commit 61ef0ba8c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 2 deletions

View File

@ -33,8 +33,8 @@ func (m *selectDefaultEnvironment) Apply(_ context.Context, b *bundle.Bundle) ([
// Multiple environments means we look for the `default` flag. // Multiple environments means we look for the `default` flag.
var defaults []string var defaults []string
for _, name := range names { for name, env := range b.Config.Environments {
if b.Config.Environments[name].Default { if env != nil && env.Default {
defaults = append(defaults, name) defaults = append(defaults, name)
} }
} }

View File

@ -48,6 +48,19 @@ func TestSelectDefaultEnvironmentNoDefaults(t *testing.T) {
assert.ErrorContains(t, err, "please specify environment") assert.ErrorContains(t, err, "please specify environment")
} }
func TestSelectDefaultEnvironmentNoDefaultsWithNil(t *testing.T) {
bundle := &bundle.Bundle{
Config: config.Root{
Environments: map[string]*config.Environment{
"foo": nil,
"bar": nil,
},
},
}
_, err := mutator.SelectDefaultEnvironment().Apply(context.Background(), bundle)
assert.ErrorContains(t, err, "please specify environment")
}
func TestSelectDefaultEnvironmentMultipleDefaults(t *testing.T) { func TestSelectDefaultEnvironmentMultipleDefaults(t *testing.T) {
bundle := &bundle.Bundle{ bundle := &bundle.Bundle{
Config: config.Root{ Config: config.Root{

View File

@ -87,6 +87,11 @@ func (r *Root) Merge(other *Root) error {
func (r *Root) MergeEnvironment(env *Environment) error { func (r *Root) MergeEnvironment(env *Environment) error {
var err error var err error
// Environment may be nil if it's empty.
if env == nil {
return nil
}
if env.Bundle != nil { if env.Bundle != nil {
err = mergo.MergeWithOverwrite(&r.Bundle, env.Bundle) err = mergo.MergeWithOverwrite(&r.Bundle, env.Bundle)
if err != nil { if err != nil {