2022-12-15 20:28:14 +00:00
|
|
|
package mutator_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/config"
|
|
|
|
"github.com/databricks/cli/bundle/config/mutator"
|
2022-12-15 20:28:14 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSelectDefaultEnvironmentNoEnvironments(t *testing.T) {
|
|
|
|
bundle := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Environments: map[string]*config.Environment{},
|
|
|
|
},
|
|
|
|
}
|
2023-05-24 12:45:19 +00:00
|
|
|
err := mutator.SelectDefaultEnvironment().Apply(context.Background(), bundle)
|
2022-12-15 20:28:14 +00:00
|
|
|
assert.ErrorContains(t, err, "no environments defined")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSelectDefaultEnvironmentSingleEnvironments(t *testing.T) {
|
|
|
|
bundle := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Environments: map[string]*config.Environment{
|
|
|
|
"foo": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-05-24 12:45:19 +00:00
|
|
|
err := mutator.SelectDefaultEnvironment().Apply(context.Background(), bundle)
|
2022-12-15 20:28:14 +00:00
|
|
|
assert.NoError(t, err)
|
2023-05-24 12:45:19 +00:00
|
|
|
assert.Equal(t, "foo", bundle.Config.Bundle.Environment)
|
2022-12-15 20:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSelectDefaultEnvironmentNoDefaults(t *testing.T) {
|
|
|
|
bundle := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Environments: map[string]*config.Environment{
|
|
|
|
"foo": {},
|
|
|
|
"bar": {},
|
|
|
|
"qux": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-05-24 12:45:19 +00:00
|
|
|
err := mutator.SelectDefaultEnvironment().Apply(context.Background(), bundle)
|
2022-12-15 20:28:14 +00:00
|
|
|
assert.ErrorContains(t, err, "please specify environment")
|
|
|
|
}
|
|
|
|
|
2022-12-22 14:31:32 +00:00
|
|
|
func TestSelectDefaultEnvironmentNoDefaultsWithNil(t *testing.T) {
|
|
|
|
bundle := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Environments: map[string]*config.Environment{
|
|
|
|
"foo": nil,
|
|
|
|
"bar": nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-05-24 12:45:19 +00:00
|
|
|
err := mutator.SelectDefaultEnvironment().Apply(context.Background(), bundle)
|
2022-12-22 14:31:32 +00:00
|
|
|
assert.ErrorContains(t, err, "please specify environment")
|
|
|
|
}
|
|
|
|
|
2022-12-15 20:28:14 +00:00
|
|
|
func TestSelectDefaultEnvironmentMultipleDefaults(t *testing.T) {
|
|
|
|
bundle := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Environments: map[string]*config.Environment{
|
|
|
|
"foo": {Default: true},
|
|
|
|
"bar": {Default: true},
|
|
|
|
"qux": {Default: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-05-24 12:45:19 +00:00
|
|
|
err := mutator.SelectDefaultEnvironment().Apply(context.Background(), bundle)
|
2022-12-15 20:28:14 +00:00
|
|
|
assert.ErrorContains(t, err, "multiple environments are marked as default")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSelectDefaultEnvironmentSingleDefault(t *testing.T) {
|
|
|
|
bundle := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Environments: map[string]*config.Environment{
|
|
|
|
"foo": {},
|
|
|
|
"bar": {Default: true},
|
|
|
|
"qux": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-05-24 12:45:19 +00:00
|
|
|
err := mutator.SelectDefaultEnvironment().Apply(context.Background(), bundle)
|
2022-12-15 20:28:14 +00:00
|
|
|
assert.NoError(t, err)
|
2023-05-24 12:45:19 +00:00
|
|
|
assert.Equal(t, "bar", bundle.Config.Bundle.Environment)
|
2022-12-15 20:28:14 +00:00
|
|
|
}
|