Commit 4985ee3d authored by Rob Pike's avatar Rob Pike

text/template: fix nil error on redefinition

Fixes #2720.

R=golang-dev, dsymonds
CC=golang-dev
https://golang.org/cl/5545072
parent f4ad8c1c
...@@ -9,6 +9,7 @@ package template ...@@ -9,6 +9,7 @@ package template
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"strings"
"testing" "testing"
"text/template/parse" "text/template/parse"
) )
...@@ -257,3 +258,17 @@ func TestAddParseTree(t *testing.T) { ...@@ -257,3 +258,17 @@ func TestAddParseTree(t *testing.T) {
t.Errorf("expected %q got %q", "broot", b.String()) t.Errorf("expected %q got %q", "broot", b.String())
} }
} }
func TestRedefinition(t *testing.T) {
var tmpl *Template
var err error
if tmpl, err = New("tmpl1").Parse(`{{define "test"}}foo{{end}}`); err != nil {
t.Fatalf("parse 1: %v", err)
}
if _, err = tmpl.New("tmpl2").Parse(`{{define "test"}}bar{{end}}`); err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), "redefinition") {
t.Fatalf("expected redefinition error; got %v", err)
}
}
...@@ -198,7 +198,7 @@ func (t *Template) associate(new *Template) error { ...@@ -198,7 +198,7 @@ func (t *Template) associate(new *Template) error {
name := new.name name := new.name
if old := t.tmpl[name]; old != nil { if old := t.tmpl[name]; old != nil {
oldIsEmpty := parse.IsEmptyTree(old.Root) oldIsEmpty := parse.IsEmptyTree(old.Root)
newIsEmpty := parse.IsEmptyTree(new.Root) newIsEmpty := new.Tree != nil && parse.IsEmptyTree(new.Root)
if !oldIsEmpty && !newIsEmpty { if !oldIsEmpty && !newIsEmpty {
return fmt.Errorf("template: redefinition of template %q", name) return fmt.Errorf("template: redefinition of template %q", 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