Skip to content

Commit

Permalink
tpl/internal: Synch Go templates fork with Go 1.16dev
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Feb 18, 2021
1 parent 66beac9 commit cf3e077
Show file tree
Hide file tree
Showing 25 changed files with 2,521 additions and 138 deletions.
2 changes: 1 addition & 1 deletion scripts/fork_go_templates/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

func main() {
// TODO(bep) git checkout tag
// The current is built with Go version b68fa57c599720d33a2d735782969ce95eabf794 / go1.15dev
// The current is built with Go version da54dfb6a1f3bef827b9ec3780c98fde77a97d11 / go1.16dev
fmt.Println("Forking ...")
defer fmt.Println("Done ...")

Expand Down
1 change: 1 addition & 0 deletions tpl/internal/go_templates/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const KnownEnv = `
GOSUMDB
GOTMPDIR
GOTOOLDIR
GOVCS
GOWASM
GO_EXTLINK_ENABLED
PKG_CONFIG
Expand Down
2 changes: 1 addition & 1 deletion tpl/internal/go_templates/fmtsort/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func compare(aVal, bVal reflect.Value) int {
default:
return -1
}
case reflect.Ptr:
case reflect.Ptr, reflect.UnsafePointer:
a, b := aVal.Pointer(), bVal.Pointer()
switch {
case a < b:
Expand Down
22 changes: 22 additions & 0 deletions tpl/internal/go_templates/fmtsort/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"reflect"
"strings"
"testing"
"unsafe"
)

var compareTests = [][]reflect.Value{
Expand All @@ -32,6 +33,7 @@ var compareTests = [][]reflect.Value{
ct(reflect.TypeOf(complex128(0+1i)), -1-1i, -1+0i, -1+1i, 0-1i, 0+0i, 0+1i, 1-1i, 1+0i, 1+1i),
ct(reflect.TypeOf(false), false, true),
ct(reflect.TypeOf(&ints[0]), &ints[0], &ints[1], &ints[2]),
ct(reflect.TypeOf(unsafe.Pointer(&ints[0])), unsafe.Pointer(&ints[0]), unsafe.Pointer(&ints[1]), unsafe.Pointer(&ints[2])),
ct(reflect.TypeOf(chans[0]), chans[0], chans[1], chans[2]),
ct(reflect.TypeOf(toy{}), toy{0, 1}, toy{0, 2}, toy{1, -1}, toy{1, 1}),
ct(reflect.TypeOf([2]int{}), [2]int{1, 1}, [2]int{1, 2}, [2]int{2, 0}),
Expand Down Expand Up @@ -118,6 +120,10 @@ var sortTests = []sortTest{
pointerMap(),
"PTR0:0 PTR1:1 PTR2:2",
},
{
unsafePointerMap(),
"UNSAFEPTR0:0 UNSAFEPTR1:1 UNSAFEPTR2:2",
},
{
map[toy]string{{7, 2}: "72", {7, 1}: "71", {3, 4}: "34"},
"{3 4}:34 {7 1}:71 {7 2}:72",
Expand Down Expand Up @@ -159,6 +165,14 @@ func sprintKey(key reflect.Value) string {
}
}
return "PTR???"
case "unsafe.Pointer":
ptr := key.Interface().(unsafe.Pointer)
for i := range ints {
if ptr == unsafe.Pointer(&ints[i]) {
return fmt.Sprintf("UNSAFEPTR%d", i)
}
}
return "UNSAFEPTR???"
case "chan int":
c := key.Interface().(chan int)
for i := range chans {
Expand All @@ -185,6 +199,14 @@ func pointerMap() map[*int]string {
return m
}

func unsafePointerMap() map[unsafe.Pointer]string {
m := make(map[unsafe.Pointer]string)
for i := 2; i >= 0; i-- {
m[unsafe.Pointer(&ints[i])] = fmt.Sprint(i)
}
return m
}

func chanMap() map[chan int]string {
m := make(map[chan int]string)
for i := 2; i >= 0; i-- {
Expand Down
16 changes: 8 additions & 8 deletions tpl/internal/go_templates/htmltemplate/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"io"
"strings"
"sync"
"testing"

"github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse"
)

func TestAddParseTree(t *testing.T) {
func TestAddParseTreeHTML(t *testing.T) {
root := Must(New("root").Parse(`{{define "a"}} {{.}} {{template "b"}} {{.}} "></a>{{end}}`))
tree, err := parse.Parse("t", `{{define "b"}}<a href="{{end}}`, "", "", nil, nil)
if err != nil {
Expand Down Expand Up @@ -174,7 +174,7 @@ func TestCloneThenParse(t *testing.T) {
t.Error("adding a template to a clone added it to the original")
}
// double check that the embedded template isn't available in the original
err := t0.ExecuteTemplate(ioutil.Discard, "a", nil)
err := t0.ExecuteTemplate(io.Discard, "a", nil)
if err == nil {
t.Error("expected 'no such template' error")
}
Expand All @@ -188,13 +188,13 @@ func TestFuncMapWorksAfterClone(t *testing.T) {

// get the expected error output (no clone)
uncloned := Must(New("").Funcs(funcs).Parse("{{customFunc}}"))
wantErr := uncloned.Execute(ioutil.Discard, nil)
wantErr := uncloned.Execute(io.Discard, nil)

// toClone must be the same as uncloned. It has to be recreated from scratch,
// since cloning cannot occur after execution.
toClone := Must(New("").Funcs(funcs).Parse("{{customFunc}}"))
cloned := Must(toClone.Clone())
gotErr := cloned.Execute(ioutil.Discard, nil)
gotErr := cloned.Execute(io.Discard, nil)

if wantErr.Error() != gotErr.Error() {
t.Errorf("clone error message mismatch want %q got %q", wantErr, gotErr)
Expand All @@ -216,7 +216,7 @@ func TestTemplateCloneExecuteRace(t *testing.T) {
go func() {
defer wg.Done()
for i := 0; i < 100; i++ {
if err := tmpl.Execute(ioutil.Discard, "data"); err != nil {
if err := tmpl.Execute(io.Discard, "data"); err != nil {
panic(err)
}
}
Expand All @@ -240,7 +240,7 @@ func TestCloneGrowth(t *testing.T) {
tmpl = Must(tmpl.Clone())
Must(tmpl.Parse(`{{define "B"}}Text{{end}}`))
for i := 0; i < 10; i++ {
tmpl.Execute(ioutil.Discard, nil)
tmpl.Execute(io.Discard, nil)
}
if len(tmpl.DefinedTemplates()) > 200 {
t.Fatalf("too many templates: %v", len(tmpl.DefinedTemplates()))
Expand All @@ -260,7 +260,7 @@ func TestCloneRedefinedName(t *testing.T) {
for i := 0; i < 2; i++ {
t2 := Must(t1.Clone())
t2 = Must(t2.New(fmt.Sprintf("%d", i)).Parse(page))
err := t2.Execute(ioutil.Discard, nil)
err := t2.Execute(io.Discard, nil)
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions tpl/internal/go_templates/htmltemplate/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,11 +404,11 @@ func TestTypedContent(t *testing.T) {
}

// Test that we print using the String method. Was issue 3073.
type stringer struct {
type myStringer struct {
v int
}

func (s *stringer) String() string {
func (s *myStringer) String() string {
return fmt.Sprintf("string=%d", s.v)
}

Expand All @@ -421,7 +421,7 @@ func (s *errorer) Error() string {
}

func TestStringer(t *testing.T) {
s := &stringer{3}
s := &myStringer{3}
b := new(bytes.Buffer)
tmpl := Must(New("x").Parse("{{.}}"))
if err := tmpl.Execute(b, s); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions tpl/internal/go_templates/htmltemplate/escape.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ func (e *escaper) escape(c context, n parse.Node) context {
switch n := n.(type) {
case *parse.ActionNode:
return e.escapeAction(c, n)
case *parse.CommentNode:
return c
case *parse.IfNode:
return e.escapeBranch(c, &n.BranchNode, "if")
case *parse.ListNode:
Expand Down
2 changes: 1 addition & 1 deletion tpl/internal/go_templates/htmltemplate/escape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,7 @@ func TestIndirectPrint(t *testing.T) {
}

// This is a test for issue 3272.
func TestEmptyTemplate(t *testing.T) {
func TestEmptyTemplateHTML(t *testing.T) {
page := Must(New("page").ParseFiles(os.DevNull))
if err := page.ExecuteTemplate(os.Stdout, "page", "nothing"); err == nil {
t.Fatal("expected error")
Expand Down
Loading

0 comments on commit cf3e077

Please sign in to comment.