Fixed full variable override detection (#1787)

## Changes
Fixes #1786 

## Tests
All valid override combinations are added as test cases
This commit is contained in:
Andrew Nester 2024-09-25 14:35:16 +02:00 committed by GitHub
parent 3d9decdda9
commit b3a3071086
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 114 additions and 8 deletions

View File

@ -418,22 +418,43 @@ func isFullVariableOverrideDef(v dyn.Value) bool {
return false return false
} }
// If the map has more than 2 keys, it is not a full variable override. // If the map has more than 3 keys, it is not a full variable override.
if mv.Len() > 2 { if mv.Len() > 3 {
return false return false
} }
// If the map has 2 keys, one of them should be "default" and the other is "type" // If the map has 3 keys, they should be "description", "type" and "default" or "lookup"
if mv.Len() == 3 {
if _, ok := mv.GetByString("type"); ok {
if _, ok := mv.GetByString("description"); ok {
if _, ok := mv.GetByString("default"); ok {
return true
}
}
}
return false
}
// If the map has 2 keys, one of them should be "default" or "lookup" and the other is "type" or "description"
if mv.Len() == 2 { if mv.Len() == 2 {
if _, ok := mv.GetByString("type"); !ok { if _, ok := mv.GetByString("type"); ok {
return false if _, ok := mv.GetByString("default"); ok {
return true
}
} }
if _, ok := mv.GetByString("default"); !ok { if _, ok := mv.GetByString("description"); ok {
return false if _, ok := mv.GetByString("default"); ok {
return true
}
if _, ok := mv.GetByString("lookup"); ok {
return true
}
} }
return true return false
} }
for _, keyword := range variableKeywords { for _, keyword := range variableKeywords {

View File

@ -6,6 +6,7 @@ import (
"testing" "testing"
"github.com/databricks/cli/bundle/config/variable" "github.com/databricks/cli/bundle/config/variable"
"github.com/databricks/cli/libs/dyn"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -169,3 +170,87 @@ func TestRootMergeTargetOverridesWithVariables(t *testing.T) {
assert.Equal(t, "complex var", root.Variables["complex"].Description) assert.Equal(t, "complex var", root.Variables["complex"].Description)
} }
func TestIsFullVariableOverrideDef(t *testing.T) {
testCases := []struct {
value dyn.Value
expected bool
}{
{
value: dyn.V(map[string]dyn.Value{
"type": dyn.V("string"),
"default": dyn.V("foo"),
"description": dyn.V("foo var"),
}),
expected: true,
},
{
value: dyn.V(map[string]dyn.Value{
"type": dyn.V("string"),
"lookup": dyn.V("foo"),
"description": dyn.V("foo var"),
}),
expected: false,
},
{
value: dyn.V(map[string]dyn.Value{
"type": dyn.V("string"),
"default": dyn.V("foo"),
}),
expected: true,
},
{
value: dyn.V(map[string]dyn.Value{
"type": dyn.V("string"),
"lookup": dyn.V("foo"),
}),
expected: false,
},
{
value: dyn.V(map[string]dyn.Value{
"description": dyn.V("string"),
"default": dyn.V("foo"),
}),
expected: true,
},
{
value: dyn.V(map[string]dyn.Value{
"description": dyn.V("string"),
"lookup": dyn.V("foo"),
}),
expected: true,
},
{
value: dyn.V(map[string]dyn.Value{
"default": dyn.V("foo"),
}),
expected: true,
},
{
value: dyn.V(map[string]dyn.Value{
"lookup": dyn.V("foo"),
}),
expected: true,
},
{
value: dyn.V(map[string]dyn.Value{
"type": dyn.V("string"),
}),
expected: false,
},
{
value: dyn.V(map[string]dyn.Value{
"type": dyn.V("string"),
"default": dyn.V("foo"),
"description": dyn.V("foo var"),
"lookup": dyn.V("foo"),
}),
expected: false,
},
}
for i, tc := range testCases {
assert.Equal(t, tc.expected, isFullVariableOverrideDef(tc.value), "test case %d", i)
}
}