template: all text_template settings before parsing (bugfix "nil-pointer dereference") (#3854)

This commit is contained in:
Dominik-K 2018-02-17 08:57:25 +01:00 committed by Brian Brazil
parent a9c93b8918
commit 67c13ba156
2 changed files with 14 additions and 2 deletions

View file

@ -265,8 +265,7 @@ func (te Expander) Expand() (result string, resultErr error) {
} }
}() }()
tmpl, err := text_template.New(te.name).Funcs(te.funcMap).Parse(te.text) tmpl, err := text_template.New(te.name).Funcs(te.funcMap).Option("missingkey=zero").Parse(te.text)
tmpl.Option("missingkey=zero")
if err != nil { if err != nil {
return "", fmt.Errorf("error parsing template %v: %v", te.name, err) return "", fmt.Errorf("error parsing template %v: %v", te.name, err)
} }

View file

@ -31,6 +31,7 @@ type testTemplatesScenario struct {
queryResult promql.Vector queryResult promql.Vector
shouldFail bool shouldFail bool
html bool html bool
errorMsg string
} }
func TestTemplateExpansion(t *testing.T) { func TestTemplateExpansion(t *testing.T) {
@ -45,6 +46,12 @@ func TestTemplateExpansion(t *testing.T) {
text: "{{ 1 }}", text: "{{ 1 }}",
output: "1", output: "1",
}, },
{
// Non-ASCII space (not allowed in text/template, see https://github.com/golang/go/blob/master/src/text/template/parse/lex.go#L98)
text: "{{ }}",
shouldFail: true,
errorMsg: "error parsing template test: template: test:1: unexpected unrecognized character in action: U+00A0 in command",
},
{ {
// HTML escaping. // HTML escaping.
text: "{{ \"<b>\" }}", text: "{{ \"<b>\" }}",
@ -140,18 +147,21 @@ func TestTemplateExpansion(t *testing.T) {
// Unparsable template. // Unparsable template.
text: "{{", text: "{{",
shouldFail: true, shouldFail: true,
errorMsg: "error parsing template test: template: test:1: unexpected unclosed action in command",
}, },
{ {
// Error in function. // Error in function.
text: "{{ query \"missing\" | first }}", text: "{{ query \"missing\" | first }}",
queryResult: promql.Vector{}, queryResult: promql.Vector{},
shouldFail: true, shouldFail: true,
errorMsg: "error executing template test: template: test:1:21: executing \"test\" at <first>: error calling first: first() called on vector with no elements",
}, },
{ {
// Panic. // Panic.
text: "{{ (query \"missing\").banana }}", text: "{{ (query \"missing\").banana }}",
queryResult: promql.Vector{}, queryResult: promql.Vector{},
shouldFail: true, shouldFail: true,
errorMsg: "error executing template test: template: test:1:10: executing \"test\" at <\"missing\">: can't evaluate field banana in type template.queryResult",
}, },
{ {
// Regex replacement. // Regex replacement.
@ -262,6 +272,9 @@ func TestTemplateExpansion(t *testing.T) {
if err == nil { if err == nil {
t.Fatalf("%d. Error not returned from %v", i, s.text) t.Fatalf("%d. Error not returned from %v", i, s.text)
} }
if err.Error() != s.errorMsg {
t.Fatalf("%d. Error message returned is wrong:\n returned: %v\n expected: %v", i, err.Error(), s.errorMsg)
}
continue continue
} }
if err != nil { if err != nil {