Commit ff006982 authored by Josh Bleecher Snyder's avatar Josh Bleecher Snyder Committed by Rob Pike

text/template: don't panic when using AddParseTree with an unparsed template

Fixes #7032.

R=golang-codereviews, r
CC=golang-codereviews
https://golang.org/cl/43960045
parent 8183ed19
...@@ -259,6 +259,18 @@ func TestAddParseTree(t *testing.T) { ...@@ -259,6 +259,18 @@ func TestAddParseTree(t *testing.T) {
} }
} }
// Issue 7032
func TestAddParseTreeToUnparsedTemplate(t *testing.T) {
master := "{{define \"master\"}}{{end}}"
tmpl := New("master")
tree, err := parse.Parse("master", master, "", "", nil)
if err != nil {
t.Fatalf("unexpected parse err: %v", err)
}
masterTree := tree["master"]
tmpl.AddParseTree("master", masterTree) // used to panic
}
func TestRedefinition(t *testing.T) { func TestRedefinition(t *testing.T) {
var tmpl *Template var tmpl *Template
var err error var err error
......
...@@ -105,7 +105,7 @@ func (t *Template) copy(c *common) *Template { ...@@ -105,7 +105,7 @@ func (t *Template) copy(c *common) *Template {
// AddParseTree creates a new template with the name and parse tree // AddParseTree creates a new template with the name and parse tree
// and associates it with t. // and associates it with t.
func (t *Template) AddParseTree(name string, tree *parse.Tree) (*Template, error) { func (t *Template) AddParseTree(name string, tree *parse.Tree) (*Template, error) {
if t.tmpl[name] != nil { if t.common != nil && t.tmpl[name] != nil {
return nil, fmt.Errorf("template: redefinition of template %q", name) return nil, fmt.Errorf("template: redefinition of template %q", name)
} }
nt := t.New(name) nt := t.New(name)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment