Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when unknown keys are encounters during template execution #766

Merged
merged 10 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions internal/init_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"properties": {
"foo": {
"type": "string",
"default": "abc"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{.foo}}
{{.does_not_exist}}
hello, world
18 changes: 18 additions & 0 deletions libs/template/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"slices"
"sort"
"strings"
Expand Down Expand Up @@ -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 {
Expand All @@ -112,6 +119,17 @@ 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 "", fmt.Errorf("variable %q not defined", matches[1])
}
return "", err
}
return result.String(), nil
Expand Down
12 changes: 12 additions & 0 deletions libs/template/renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/*"}
Expand Down