Skip to content

Commit

Permalink
Configure text/template to fail when a value is missed
Browse files Browse the repository at this point in the history
By default the html/template rendered an empty string when a variable is
not passed to the template.Execution. The text/template has a different
behavior. By default it prints `<no value>`. With this configuration now
it errors and tells that a variable is missed

Signed-off-by: Gianluca Arbezzano <gianarb92@gmail.com>
  • Loading branch information
Gianluca Arbezzano committed Jan 15, 2021
1 parent c0a6500 commit b416c3a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 8 deletions.
2 changes: 1 addition & 1 deletion workflow/template_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func RenderTemplate(templateID, templateData string, devices []byte) (string, er
return "", err
}

t := template.New("workflow-template")
t := template.New("workflow-template").Option("missingkey=error")
_, err = t.Parse(string(templateData))
if err != nil {
err = errors.Wrapf(err, errTemplateParsing, templateID)
Expand Down
9 changes: 2 additions & 7 deletions workflow/template_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ func TestRenderTemplate(t *testing.T) {
hwAddress []byte
templateID string
templateData string
skip string
expectedError func(t *testing.T, err error)
expectedTemplate string
}{
Expand All @@ -262,14 +261,13 @@ tasks:
{
name: "invalid-hardware-address",
templateData: validTemplate,
skip: "Not sure if this is an expected error or not but this test is not well written",
hwAddress: []byte("{\"invalid_device\":\"08:00:27:00:00:01\"}"),
expectedError: func(t *testing.T, err error) {
if err == nil {
t.Error("expected error, got nil")
}
if !strings.Contains(err.Error(), "invalid hardware address: {\"invalid_device\":\"08:00:27:00:00:01\"}") {
t.Errorf("\nexpected err: %s\ngot: %s", "invalid hardware address: {\"invalid_device\":\"08:00:27:00:00:01\"}", err)
if !strings.Contains(err.Error(), `executing "workflow-template" at <.device_1>: map has no entry for key "device_1"`) {
t.Errorf("\nexpected err: %s\ngot: %s", `executing "workflow-template" at <.device_1>: map has no entry for key "device_1"`, err)
}
},
},
Expand Down Expand Up @@ -306,9 +304,6 @@ tasks:

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.skip != "" {
t.Skip(test.skip)
}
temp, err := RenderTemplate(test.templateID, test.templateData, test.hwAddress)
if test.expectedError != nil {
test.expectedError(t, err)
Expand Down

0 comments on commit b416c3a

Please sign in to comment.