From 3d85c69a0bf67adec57b76511ccc5e5b0ba9cdf4 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Fri, 22 Jan 2021 14:54:23 -0800 Subject: html/template: revert "avoid race when escaping updates template" This reverts CLs 274450 and 279492, except for the new tests. The new race test is changed to skip, as it now fails. We can try again for 1.17. Original CL descriptions: html/template: attach functions to namespace The text/template functions are stored in a data structure shared by all related templates, so do the same with the original, unwrapped, functions on the html/template side. html/template: avoid race when escaping updates template For #39807 Fixes #43855 Change-Id: I2ce91321ada06ea496a982aefe170eb5af9ba847 Reviewed-on: https://go-review.googlesource.com/c/go/+/285957 Trust: Ian Lance Taylor Run-TryBot: Ian Lance Taylor TryBot-Result: Go Bot Reviewed-by: Emmanuel Odeke --- src/html/template/exec_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'src/html/template/exec_test.go') diff --git a/src/html/template/exec_test.go b/src/html/template/exec_test.go index cd6b78a1a9..7d1bef1782 100644 --- a/src/html/template/exec_test.go +++ b/src/html/template/exec_test.go @@ -1720,6 +1720,8 @@ var v = "v"; ` func TestEscapeRace(t *testing.T) { + t.Skip("this test currently fails with -race; see issue #39807") + tmpl := New("") _, err := tmpl.New("templ.html").Parse(raceText) if err != nil { @@ -1777,6 +1779,39 @@ func TestRecursiveExecute(t *testing.T) { } } +// recursiveInvoker is for TestRecursiveExecuteViaMethod. +type recursiveInvoker struct { + t *testing.T + tmpl *Template +} + +func (r *recursiveInvoker) Recur() (string, error) { + var sb strings.Builder + if err := r.tmpl.ExecuteTemplate(&sb, "subroutine", nil); err != nil { + r.t.Fatal(err) + } + return sb.String(), nil +} + +func TestRecursiveExecuteViaMethod(t *testing.T) { + tmpl := New("") + top, err := tmpl.New("x.html").Parse(`{{.Recur}}`) + if err != nil { + t.Fatal(err) + } + _, err = tmpl.New("subroutine").Parse(``) + if err != nil { + t.Fatal(err) + } + r := &recursiveInvoker{ + t: t, + tmpl: tmpl, + } + if err := top.Execute(io.Discard, r); err != nil { + t.Fatal(err) + } +} + // Issue 43295. func TestTemplateFuncsAfterClone(t *testing.T) { s := `{{ f . }}` -- cgit v1.3-5-g9baa