Skip to content

Commit 7847388

Browse files
authored
Initialize variable definitions that are defined without properties (#966)
## Changes We can debate whether or not variable definitions without properties are valid, but in no case should this panic the CLI. Fixes #934. ## Tests Unit.
1 parent 65dd9c5 commit 7847388

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package mutator
2+
3+
import (
4+
"context"
5+
6+
"github.com/databricks/cli/bundle"
7+
"github.com/databricks/cli/bundle/config/variable"
8+
)
9+
10+
type initializeVariables struct{}
11+
12+
// InitializeVariables initializes nil variables to their corresponding zero values.
13+
func InitializeVariables() bundle.Mutator {
14+
return &initializeVariables{}
15+
}
16+
17+
func (m *initializeVariables) Name() string {
18+
return "InitializeVariables"
19+
}
20+
21+
func (m *initializeVariables) Apply(ctx context.Context, b *bundle.Bundle) error {
22+
vars := b.Config.Variables
23+
for k, v := range vars {
24+
if v == nil {
25+
vars[k] = &variable.Variable{}
26+
}
27+
}
28+
29+
return nil
30+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package mutator_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/databricks/cli/bundle"
8+
"github.com/databricks/cli/bundle/config"
9+
"github.com/databricks/cli/bundle/config/mutator"
10+
"github.com/databricks/cli/bundle/config/variable"
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func TestInitializeVariables(t *testing.T) {
16+
b := &bundle.Bundle{
17+
Config: config.Root{
18+
Variables: map[string]*variable.Variable{
19+
"foo": nil,
20+
"bar": {
21+
Description: "This is a description",
22+
},
23+
},
24+
},
25+
}
26+
err := bundle.Apply(context.Background(), b, mutator.InitializeVariables())
27+
require.NoError(t, err)
28+
assert.NotNil(t, b.Config.Variables["foo"])
29+
assert.NotNil(t, b.Config.Variables["bar"])
30+
assert.Equal(t, "This is a description", b.Config.Variables["bar"].Description)
31+
}
32+
33+
func TestInitializeVariablesWithoutVariables(t *testing.T) {
34+
b := &bundle.Bundle{
35+
Config: config.Root{
36+
Variables: nil,
37+
},
38+
}
39+
err := bundle.Apply(context.Background(), b, mutator.InitializeVariables())
40+
require.NoError(t, err)
41+
assert.Nil(t, b.Config.Variables)
42+
}

bundle/config/mutator/mutator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ func DefaultMutators() []bundle.Mutator {
1010
return []bundle.Mutator{
1111
scripts.Execute(config.ScriptPreInit),
1212
ProcessRootIncludes(),
13+
InitializeVariables(),
1314
DefineDefaultTarget(),
1415
LoadGitDetails(),
1516
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
variables:
2+
a:
3+
b:

bundle/tests/variables_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,15 @@ func TestVariablesTargetsBlockOverrideWithUndefinedVariables(t *testing.T) {
9292
)))
9393
assert.ErrorContains(t, err, "variable c is not defined but is assigned a value")
9494
}
95+
96+
func TestVariablesWithoutDefinition(t *testing.T) {
97+
t.Setenv("BUNDLE_VAR_a", "foo")
98+
t.Setenv("BUNDLE_VAR_b", "bar")
99+
b := load(t, "./variables/without_definition")
100+
err := bundle.Apply(context.Background(), b, mutator.SetVariables())
101+
require.NoError(t, err)
102+
require.True(t, b.Config.Variables["a"].HasValue())
103+
require.True(t, b.Config.Variables["b"].HasValue())
104+
assert.Equal(t, "foo", *b.Config.Variables["a"].Value)
105+
assert.Equal(t, "bar", *b.Config.Variables["b"].Value)
106+
}

0 commit comments

Comments
 (0)