-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
5 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package mutator | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/databricks/cli/bundle" | ||
"github.com/databricks/cli/bundle/config/variable" | ||
) | ||
|
||
type initializeVariables struct{} | ||
|
||
// InitializeVariables initializes nil variables to their corresponding zero values. | ||
func InitializeVariables() bundle.Mutator { | ||
return &initializeVariables{} | ||
} | ||
|
||
func (m *initializeVariables) Name() string { | ||
return "InitializeVariables" | ||
} | ||
|
||
func (m *initializeVariables) Apply(ctx context.Context, b *bundle.Bundle) error { | ||
vars := b.Config.Variables | ||
for k, v := range vars { | ||
if v == nil { | ||
vars[k] = &variable.Variable{} | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
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/databricks/cli/bundle/config/variable" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestInitializeVariables(t *testing.T) { | ||
b := &bundle.Bundle{ | ||
Config: config.Root{ | ||
Variables: map[string]*variable.Variable{ | ||
"foo": nil, | ||
"bar": { | ||
Description: "This is a description", | ||
}, | ||
}, | ||
}, | ||
} | ||
err := bundle.Apply(context.Background(), b, mutator.InitializeVariables()) | ||
require.NoError(t, err) | ||
assert.NotNil(t, b.Config.Variables["foo"]) | ||
assert.NotNil(t, b.Config.Variables["bar"]) | ||
assert.Equal(t, "This is a description", b.Config.Variables["bar"].Description) | ||
} | ||
|
||
func TestInitializeVariablesWithoutVariables(t *testing.T) { | ||
b := &bundle.Bundle{ | ||
Config: config.Root{ | ||
Variables: nil, | ||
}, | ||
} | ||
err := bundle.Apply(context.Background(), b, mutator.InitializeVariables()) | ||
require.NoError(t, err) | ||
assert.Nil(t, b.Config.Variables) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
variables: | ||
a: | ||
b: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters