]> Cypherpunks repositories - gostls13.git/commitdiff
text/template: clone options when cloning templates
authorSean Liao <sean@liao.dev>
Sat, 10 May 2025 11:18:32 +0000 (12:18 +0100)
committerSean Liao <sean@liao.dev>
Mon, 12 May 2025 19:37:51 +0000 (12:37 -0700)
Fixes #43022

Change-Id: I727b86ea0ebfff06f82c909457479c2afb9106dc
Reviewed-on: https://go-review.googlesource.com/c/go/+/671615
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Rob Pike <r@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/text/template/multi_test.go
src/text/template/template.go

index 63cd3f74b270a60e85787ae7b47488365bf15a9e..1e4f61b3f1bc4a08c6c346902dc229ecb707bfb1 100644 (file)
@@ -210,6 +210,7 @@ const (
        cloneText2 = `{{define "b"}}b{{end}}`
        cloneText3 = `{{define "c"}}root{{end}}`
        cloneText4 = `{{define "c"}}clone{{end}}`
+       cloneText5 = `{{define "e"}}{{.Foo}}{{end}}`
 )
 
 func TestClone(t *testing.T) {
@@ -222,6 +223,8 @@ func TestClone(t *testing.T) {
        if err != nil {
                t.Fatal(err)
        }
+       root.Parse(cloneText5)
+       root.Option("missingkey=error")
        clone := Must(root.Clone())
        // Add variants to both.
        _, err = root.Parse(cloneText3)
@@ -259,6 +262,14 @@ func TestClone(t *testing.T) {
        if b.String() != "bclone" {
                t.Errorf("expected %q got %q", "bclone", b.String())
        }
+       b.Reset()
+       rootErr := root.ExecuteTemplate(&b, "e", map[string]any{})
+       cloneErr := clone.ExecuteTemplate(&b, "e", map[string]any{})
+       if cloneErr == nil {
+               t.Errorf("expected error from missing key in cloned template")
+       } else if got, want := cloneErr.Error(), rootErr.Error(); got != want {
+               t.Errorf("got %q, wan t %q", got, want)
+       }
 }
 
 func TestAddParseTree(t *testing.T) {
index 78067af2add4ae529c660ddcdc5cc4c559d3b085..9ae5a6ca5bfa21939d2298fd93a82a5dccb8a994 100644 (file)
@@ -90,6 +90,7 @@ func (t *Template) Clone() (*Template, error) {
        if t.common == nil {
                return nt, nil
        }
+       nt.option = t.option
        t.muTmpl.RLock()
        defer t.muTmpl.RUnlock()
        for k, v := range t.tmpl {