mirror of
https://github.com/golang/go
synced 2024-11-23 22:50:05 -07:00
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
This commit is contained in:
parent
8183ed19b9
commit
ff006982c3
@ -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) {
|
||||
var tmpl *Template
|
||||
var err error
|
||||
|
@ -105,7 +105,7 @@ func (t *Template) copy(c *common) *Template {
|
||||
// AddParseTree creates a new template with the name and parse tree
|
||||
// and associates it with t.
|
||||
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)
|
||||
}
|
||||
nt := t.New(name)
|
||||
|
Loading…
Reference in New Issue
Block a user