mirror of https://github.com/databricks/cli.git
fix and added execute template tests
This commit is contained in:
parent
3626588511
commit
95d4da6dc0
|
@ -36,7 +36,7 @@ func TestTemplateInitializationForDevConfig(t *testing.T) {
|
||||||
|
|
||||||
// materialize the template
|
// materialize the template
|
||||||
cmd := root.RootCmd
|
cmd := root.RootCmd
|
||||||
cmd.SetArgs([]string{"bundle", "init", filepath.FromSlash("testdata/init/templateDefinition"), "--target-dir", tmp})
|
cmd.SetArgs([]string{"bundle", "init", filepath.FromSlash("testdata/init/templateDefinition"), "--target-dir", tmp, "--config-file", filepath.Join(tmp, "config.json")})
|
||||||
err = cmd.Execute()
|
err = cmd.Execute()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,6 @@ import (
|
||||||
|
|
||||||
// Executes the template by applying config on it. Returns the materialized config
|
// Executes the template by applying config on it. Returns the materialized config
|
||||||
// as a string
|
// as a string
|
||||||
// TODO: test this function
|
|
||||||
func executeTemplate(config map[string]any, templateDefinition string) (string, error) {
|
func executeTemplate(config map[string]any, templateDefinition string) (string, error) {
|
||||||
// configure template with helper functions
|
// configure template with helper functions
|
||||||
tmpl, err := template.New("").Funcs(HelperFuncs).Parse(templateDefinition)
|
tmpl, err := template.New("").Funcs(HelperFuncs).Parse(templateDefinition)
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
package template
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestExecuteTemplate(t *testing.T) {
|
||||||
|
templateText :=
|
||||||
|
`"{{.count}} items are made of {{.Material}}".
|
||||||
|
{{if eq .Animal "sheep" }}
|
||||||
|
Sheep wool is the best!
|
||||||
|
{{else}}
|
||||||
|
{{.Animal}} wool is not too bad...
|
||||||
|
{{end}}
|
||||||
|
`
|
||||||
|
statement, err := executeTemplate(map[string]any{
|
||||||
|
"Material": "wool",
|
||||||
|
"count": 1,
|
||||||
|
"Animal": "sheep",
|
||||||
|
}, templateText)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "\"1 items are made of wool\".\n\nSheep wool is the best!\n\n", statement)
|
||||||
|
|
||||||
|
statement, err = executeTemplate(map[string]any{
|
||||||
|
"Material": "wool",
|
||||||
|
"count": 1,
|
||||||
|
"Animal": "cat",
|
||||||
|
}, templateText)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "\"1 items are made of wool\".\n\ncat wool is not too bad...\n\n", statement)
|
||||||
|
}
|
Loading…
Reference in New Issue