2023-08-17 15:22:32 +00:00
|
|
|
package mutator_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/config"
|
|
|
|
"github.com/databricks/cli/bundle/config/mutator"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSelectDefaultTargetNoTargets(t *testing.T) {
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &bundle.Bundle{
|
2023-08-17 15:22:32 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Targets: map[string]*config.Target{},
|
|
|
|
},
|
|
|
|
}
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, mutator.SelectDefaultTarget())
|
|
|
|
assert.ErrorContains(t, diags.Error(), "no targets defined")
|
2023-08-17 15:22:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSelectDefaultTargetSingleTargets(t *testing.T) {
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &bundle.Bundle{
|
2023-08-17 15:22:32 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Targets: map[string]*config.Target{
|
|
|
|
"foo": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, mutator.SelectDefaultTarget())
|
|
|
|
assert.NoError(t, diags.Error())
|
2023-11-15 14:03:36 +00:00
|
|
|
assert.Equal(t, "foo", b.Config.Bundle.Target)
|
2023-08-17 15:22:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSelectDefaultTargetNoDefaults(t *testing.T) {
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &bundle.Bundle{
|
2023-08-17 15:22:32 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Targets: map[string]*config.Target{
|
|
|
|
"foo": {},
|
|
|
|
"bar": {},
|
|
|
|
"qux": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, mutator.SelectDefaultTarget())
|
|
|
|
assert.ErrorContains(t, diags.Error(), "please specify target")
|
2023-08-17 15:22:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSelectDefaultTargetNoDefaultsWithNil(t *testing.T) {
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &bundle.Bundle{
|
2023-08-17 15:22:32 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Targets: map[string]*config.Target{
|
|
|
|
"foo": nil,
|
|
|
|
"bar": nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, mutator.SelectDefaultTarget())
|
|
|
|
assert.ErrorContains(t, diags.Error(), "please specify target")
|
2023-08-17 15:22:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSelectDefaultTargetMultipleDefaults(t *testing.T) {
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &bundle.Bundle{
|
2023-08-17 15:22:32 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Targets: map[string]*config.Target{
|
|
|
|
"foo": {Default: true},
|
|
|
|
"bar": {Default: true},
|
|
|
|
"qux": {Default: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, mutator.SelectDefaultTarget())
|
|
|
|
assert.ErrorContains(t, diags.Error(), "multiple targets are marked as default")
|
2023-08-17 15:22:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSelectDefaultTargetSingleDefault(t *testing.T) {
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &bundle.Bundle{
|
2023-08-17 15:22:32 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Targets: map[string]*config.Target{
|
|
|
|
"foo": {},
|
|
|
|
"bar": {Default: true},
|
|
|
|
"qux": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, mutator.SelectDefaultTarget())
|
|
|
|
assert.NoError(t, diags.Error())
|
2023-11-15 14:03:36 +00:00
|
|
|
assert.Equal(t, "bar", b.Config.Bundle.Target)
|
2023-08-17 15:22:32 +00:00
|
|
|
}
|