Error when unknown keys are encounters during template execution (#766)

## Tests
New unit test and manually
This commit is contained in:
shreyas-goenka 2023-09-14 17:53:20 +02:00 committed by GitHub
parent 953dcb4972
commit 327ab0e598
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 0 deletions

15
internal/init_test.go Normal file
View File

@ -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")
}

View File

@ -0,0 +1,8 @@
{
"properties": {
"foo": {
"type": "string",
"default": "abc"
}
}
}

View File

@ -0,0 +1,3 @@
{{.foo}}
{{.does_not_exist}}
hello, world

View File

@ -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

View File

@ -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/*"}