mirror of https://github.com/databricks/cli.git
Error when unknown keys are encounters during template execution (#766)
## Tests New unit test and manually
This commit is contained in:
parent
953dcb4972
commit
327ab0e598
|
@ -0,0 +1,15 @@
|
|||
package internal
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestAccBundleInitErrorOnUnknownFields(t *testing.T) {
|
||||
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
|
||||
|
||||
tmpDir := t.TempDir()
|
||||
_, _, err := RequireErrorRun(t, "bundle", "init", "./testdata/init/field-does-not-exist", "--output-dir", tmpDir)
|
||||
assert.EqualError(t, err, "failed to compute file content for bar.tmpl. variable \"does_not_exist\" not defined")
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"properties": {
|
||||
"foo": {
|
||||
"type": "string",
|
||||
"default": "abc"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
{{.foo}}
|
||||
{{.does_not_exist}}
|
||||
hello, world
|
|
@ -8,6 +8,7 @@ import (
|
|||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
|
@ -102,6 +103,12 @@ func (r *renderer) executeTemplate(templateDefinition string) (string, error) {
|
|||
return "", err
|
||||
}
|
||||
|
||||
// The template execution will error instead of printing <no value> on unknown
|
||||
// map keys if the "missingkey=error" option is set.
|
||||
// We do this here instead of doing this once for r.baseTemplate because
|
||||
// the Template.Clone() method does not clone options.
|
||||
tmpl = tmpl.Option("missingkey=error")
|
||||
|
||||
// Parse the template text
|
||||
tmpl, err = tmpl.Parse(templateDefinition)
|
||||
if err != nil {
|
||||
|
@ -112,6 +119,20 @@ func (r *renderer) executeTemplate(templateDefinition string) (string, error) {
|
|||
result := strings.Builder{}
|
||||
err = tmpl.Execute(&result, r.config)
|
||||
if err != nil {
|
||||
// Parse and return a more readable error for missing values that are used
|
||||
// by the template definition but are not provided in the passed config.
|
||||
target := &template.ExecError{}
|
||||
if errors.As(err, target) {
|
||||
captureRegex := regexp.MustCompile(`map has no entry for key "(.*)"`)
|
||||
matches := captureRegex.FindStringSubmatch(target.Err.Error())
|
||||
if len(matches) != 2 {
|
||||
return "", err
|
||||
}
|
||||
return "", template.ExecError{
|
||||
Name: target.Name,
|
||||
Err: fmt.Errorf("variable %q not defined", matches[1]),
|
||||
}
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
return result.String(), nil
|
||||
|
|
|
@ -189,6 +189,18 @@ My email is {{template "email"}}
|
|||
assert.Contains(t, statement, `My email is hrithik.roshan@databricks.com`)
|
||||
}
|
||||
|
||||
func TestRendererExecuteTemplateWithUnknownProperty(t *testing.T) {
|
||||
templateText := `{{.does_not_exist}}`
|
||||
|
||||
r := renderer{
|
||||
config: map[string]any{},
|
||||
baseTemplate: template.New("base"),
|
||||
}
|
||||
|
||||
_, err := r.executeTemplate(templateText)
|
||||
assert.ErrorContains(t, err, "variable \"does_not_exist\" not defined")
|
||||
}
|
||||
|
||||
func TestRendererIsSkipped(t *testing.T) {
|
||||
|
||||
skipPatterns := []string{"a*", "*yz", "def", "a/b/*"}
|
||||
|
|
Loading…
Reference in New Issue