2016-03-21 19:28:42 -04:00
|
|
|
// Copyright 2016 The Hugo Authors. All rights reserved.
|
2015-12-10 17:19:38 -05:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-02-17 07:30:50 -05:00
|
|
|
package tplimpl
|
2013-11-10 15:04:51 -05:00
|
|
|
|
2015-04-30 04:51:01 -04:00
|
|
|
import (
|
2017-04-02 08:20:34 -04:00
|
|
|
"bytes"
|
2015-04-30 04:51:01 -04:00
|
|
|
"errors"
|
2017-04-02 08:20:34 -04:00
|
|
|
"html/template"
|
2015-04-30 04:51:01 -04:00
|
|
|
"io/ioutil"
|
2017-04-02 08:20:34 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2015-04-30 04:51:01 -04:00
|
|
|
"testing"
|
2016-03-16 15:54:06 -04:00
|
|
|
|
2017-04-02 08:20:34 -04:00
|
|
|
"github.com/spf13/afero"
|
2017-01-10 04:55:03 -05:00
|
|
|
"github.com/spf13/hugo/deps"
|
2017-02-04 22:20:06 -05:00
|
|
|
|
2017-02-17 07:30:50 -05:00
|
|
|
"github.com/spf13/hugo/tpl"
|
2017-01-10 04:55:03 -05:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/stretchr/testify/require"
|
2015-04-30 04:51:01 -04:00
|
|
|
)
|
|
|
|
|
2017-04-02 08:20:34 -04:00
|
|
|
// Some tests for Issue #1178 -- Ace
|
|
|
|
func TestAceTemplates(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
for i, this := range []struct {
|
|
|
|
basePath string
|
|
|
|
innerPath string
|
|
|
|
baseContent string
|
|
|
|
innerContent string
|
|
|
|
expect string
|
|
|
|
expectErr int
|
|
|
|
}{
|
|
|
|
{"", filepath.FromSlash("_default/single.ace"), "", "{{ . }}", "DATA", 0},
|
|
|
|
{filepath.FromSlash("_default/baseof.ace"), filepath.FromSlash("_default/single.ace"),
|
|
|
|
`= content main
|
|
|
|
h2 This is a content named "main" of an inner template. {{ . }}`,
|
|
|
|
`= doctype html
|
|
|
|
html lang=en
|
|
|
|
head
|
|
|
|
meta charset=utf-8
|
|
|
|
title Base and Inner Template
|
|
|
|
body
|
|
|
|
h1 This is a base template {{ . }}
|
|
|
|
= yield main`, `<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Base and Inner Template</title></head><body><h1>This is a base template DATA</h1></body></html>`, 0},
|
|
|
|
} {
|
|
|
|
|
|
|
|
for _, root := range []string{"", os.TempDir()} {
|
|
|
|
|
|
|
|
basePath := this.basePath
|
|
|
|
innerPath := this.innerPath
|
|
|
|
|
|
|
|
if basePath != "" && root != "" {
|
|
|
|
basePath = filepath.Join(root, basePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
if innerPath != "" && root != "" {
|
|
|
|
innerPath = filepath.Join(root, innerPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
d := "DATA"
|
|
|
|
|
|
|
|
config := newDepsConfig(viper.New())
|
|
|
|
config.WithTemplate = func(templ tpl.Template) error {
|
|
|
|
return templ.AddAceTemplate("mytemplate.ace", basePath, innerPath,
|
|
|
|
[]byte(this.baseContent), []byte(this.innerContent))
|
|
|
|
}
|
|
|
|
|
|
|
|
a, err := deps.New(config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
if err := a.LoadResources(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
templ := a.Tmpl.(*GoHTMLTemplate)
|
|
|
|
|
|
|
|
if len(templ.errors) > 0 && this.expectErr == 0 {
|
|
|
|
t.Errorf("Test %d with root '%s' errored: %v", i, root, templ.errors)
|
|
|
|
} else if len(templ.errors) == 0 && this.expectErr == 1 {
|
|
|
|
t.Errorf("#1 Test %d with root '%s' should have errored", i, root)
|
|
|
|
}
|
|
|
|
|
|
|
|
var buff bytes.Buffer
|
|
|
|
err = a.Tmpl.ExecuteTemplate(&buff, "mytemplate.html", d)
|
|
|
|
|
|
|
|
if err != nil && this.expectErr == 0 {
|
|
|
|
t.Errorf("Test %d with root '%s' errored: %s", i, root, err)
|
|
|
|
} else if err == nil && this.expectErr == 2 {
|
|
|
|
t.Errorf("#2 Test with root '%s' %d should have errored", root, i)
|
|
|
|
} else {
|
|
|
|
result := buff.String()
|
|
|
|
if result != this.expect {
|
|
|
|
t.Errorf("Test %d with root '%s' got\n%s\nexpected\n%s", i, root, result, this.expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func isAtLeastGo16() bool {
|
|
|
|
version := runtime.Version()
|
|
|
|
return strings.Contains(version, "1.6") || strings.Contains(version, "1.7")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddTemplateFileWithMaster(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
if !isAtLeastGo16() {
|
|
|
|
t.Skip("This test only runs on Go >= 1.6")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, this := range []struct {
|
|
|
|
masterTplContent string
|
|
|
|
overlayTplContent string
|
|
|
|
writeSkipper int
|
|
|
|
expect interface{}
|
|
|
|
}{
|
|
|
|
{`A{{block "main" .}}C{{end}}C`, `{{define "main"}}B{{end}}`, 0, "ABC"},
|
|
|
|
{`A{{block "main" .}}C{{end}}C{{block "sub" .}}D{{end}}E`, `{{define "main"}}B{{end}}`, 0, "ABCDE"},
|
|
|
|
{`A{{block "main" .}}C{{end}}C{{block "sub" .}}D{{end}}E`, `{{define "main"}}B{{end}}{{define "sub"}}Z{{end}}`, 0, "ABCZE"},
|
|
|
|
{`tpl`, `tpl`, 1, false},
|
|
|
|
{`tpl`, `tpl`, 2, false},
|
|
|
|
{`{{.0.E}}`, `tpl`, 0, false},
|
|
|
|
{`tpl`, `{{.0.E}}`, 0, false},
|
|
|
|
} {
|
|
|
|
|
|
|
|
overlayTplName := "ot"
|
|
|
|
masterTplName := "mt"
|
|
|
|
finalTplName := "tp"
|
|
|
|
|
|
|
|
config := newDepsConfig(viper.New())
|
|
|
|
config.WithTemplate = func(templ tpl.Template) error {
|
|
|
|
|
|
|
|
err := templ.AddTemplateFileWithMaster(finalTplName, overlayTplName, masterTplName)
|
|
|
|
|
|
|
|
if b, ok := this.expect.(bool); ok && !b {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("[%d] AddTemplateFileWithMaster didn't return an expected error", i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] AddTemplateFileWithMaster failed: %s", i, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resultTpl := templ.Lookup(finalTplName)
|
|
|
|
|
|
|
|
if resultTpl == nil {
|
|
|
|
t.Errorf("[%d] AddTemplateFileWithMaster: Result template not found", i)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
err := resultTpl.Execute(&b, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] AddTemplateFileWithMaster execute failed: %s", i, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
resultContent := b.String()
|
|
|
|
|
|
|
|
if resultContent != this.expect {
|
|
|
|
t.Errorf("[%d] AddTemplateFileWithMaster got \n%s but expected \n%v", i, resultContent, this.expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if this.writeSkipper != 1 {
|
|
|
|
afero.WriteFile(config.Fs.Source, masterTplName, []byte(this.masterTplContent), 0644)
|
|
|
|
}
|
|
|
|
if this.writeSkipper != 2 {
|
|
|
|
afero.WriteFile(config.Fs.Source, overlayTplName, []byte(this.overlayTplContent), 0644)
|
|
|
|
}
|
|
|
|
|
|
|
|
deps.New(config)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// A Go stdlib test for linux/arm. Will remove later.
|
|
|
|
// See #1771
|
|
|
|
func TestBigIntegerFunc(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
var func1 = func(v int64) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var funcs = map[string]interface{}{
|
|
|
|
"A": func1,
|
|
|
|
}
|
|
|
|
|
|
|
|
tpl, err := template.New("foo").Funcs(funcs).Parse("{{ A 3e80 }}")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Parse failed:", err)
|
|
|
|
}
|
|
|
|
err = tpl.Execute(ioutil.Discard, "foo")
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Execute should have failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("Got expected error:", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// A Go stdlib test for linux/arm. Will remove later.
|
|
|
|
// See #1771
|
|
|
|
type BI struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b BI) A(v int64) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func TestBigIntegerMethod(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
data := &BI{}
|
|
|
|
|
|
|
|
tpl, err := template.New("foo2").Parse("{{ .A 3e80 }}")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Parse failed:", err)
|
|
|
|
}
|
|
|
|
err = tpl.ExecuteTemplate(ioutil.Discard, "foo2", data)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Execute should have failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("Got expected error:", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-30 04:51:01 -04:00
|
|
|
// Test for bugs discovered by https://github.com/dvyukov/go-fuzz
|
|
|
|
func TestTplGoFuzzReports(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
2015-04-30 07:25:45 -04:00
|
|
|
|
|
|
|
// The following test case(s) also fail
|
|
|
|
// See https://github.com/golang/go/issues/10634
|
|
|
|
//{"{{ seq 433937734937734969526500969526500 }}", 2}}
|
|
|
|
|
2015-04-30 04:51:01 -04:00
|
|
|
for i, this := range []struct {
|
|
|
|
data string
|
|
|
|
expectErr int
|
2015-04-30 05:26:34 -04:00
|
|
|
}{
|
|
|
|
// Issue #1089
|
2015-05-02 05:32:38 -04:00
|
|
|
//{"{{apply .C \"first\" }}", 2},
|
2015-04-30 05:26:34 -04:00
|
|
|
// Issue #1090
|
2015-04-30 05:41:25 -04:00
|
|
|
{"{{ slicestr \"000000\" 10}}", 2},
|
|
|
|
// Issue #1091
|
2015-05-02 05:32:38 -04:00
|
|
|
//{"{{apply .C \"first\" 0 0 0}}", 2},
|
2015-05-01 11:00:22 -04:00
|
|
|
{"{{seq 3e80}}", 2},
|
|
|
|
// Issue #1095
|
|
|
|
{"{{apply .C \"urlize\" " +
|
|
|
|
"\".\"}}", 2}} {
|
2015-04-30 04:51:01 -04:00
|
|
|
|
|
|
|
d := &Data{
|
|
|
|
A: 42,
|
|
|
|
B: "foo",
|
|
|
|
C: []int{1, 2, 3},
|
|
|
|
D: map[int]string{1: "foo", 2: "bar"},
|
|
|
|
E: Data1{42, "foo"},
|
2015-04-30 07:25:45 -04:00
|
|
|
F: []string{"a", "b", "c"},
|
|
|
|
G: []string{"a", "b", "c", "d", "e"},
|
|
|
|
H: "a,b,c,d,e,f",
|
2015-04-30 04:51:01 -04:00
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
config := newDepsConfig(viper.New())
|
|
|
|
|
2017-04-02 08:20:34 -04:00
|
|
|
config.WithTemplate = func(templ tpl.Template) error {
|
2017-01-09 19:36:59 -05:00
|
|
|
return templ.AddTemplate("fuzz", this.data)
|
2017-01-10 04:55:03 -05:00
|
|
|
}
|
|
|
|
|
2017-03-25 09:37:04 -04:00
|
|
|
de, err := deps.New(config)
|
|
|
|
require.NoError(t, err)
|
2017-02-04 22:20:06 -05:00
|
|
|
require.NoError(t, de.LoadResources())
|
2015-04-30 04:51:01 -04:00
|
|
|
|
2017-04-02 08:20:34 -04:00
|
|
|
templ := de.Tmpl.(*GoHTMLTemplate)
|
2015-04-30 04:51:01 -04:00
|
|
|
|
2017-01-09 19:36:59 -05:00
|
|
|
if len(templ.errors) > 0 && this.expectErr == 0 {
|
|
|
|
t.Errorf("Test %d errored: %v", i, templ.errors)
|
|
|
|
} else if len(templ.errors) == 0 && this.expectErr == 1 {
|
|
|
|
t.Errorf("#1 Test %d should have errored", i)
|
|
|
|
}
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2017-04-02 08:20:34 -04:00
|
|
|
err = de.Tmpl.ExecuteTemplate(ioutil.Discard, "fuzz", d)
|
2015-04-30 04:51:01 -04:00
|
|
|
|
|
|
|
if err != nil && this.expectErr == 0 {
|
|
|
|
t.Fatalf("Test %d errored: %s", i, err)
|
|
|
|
} else if err == nil && this.expectErr == 2 {
|
|
|
|
t.Fatalf("#2 Test %d should have errored", i)
|
|
|
|
}
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2015-04-30 04:51:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Data struct {
|
|
|
|
A int
|
|
|
|
B string
|
|
|
|
C []int
|
|
|
|
D map[int]string
|
|
|
|
E Data1
|
2015-04-30 07:25:45 -04:00
|
|
|
F []string
|
|
|
|
G []string
|
|
|
|
H string
|
2015-04-30 04:51:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Data1 struct {
|
|
|
|
A int
|
|
|
|
B string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Data1) Q() string {
|
|
|
|
return "foo"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Data1) W() (string, error) {
|
|
|
|
return "foo", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Data1) E() (string, error) {
|
|
|
|
return "foo", errors.New("Data.E error")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Data1) R(v int) (string, error) {
|
|
|
|
return "foo", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Data1) T(s string) (string, error) {
|
|
|
|
return s, nil
|
|
|
|
}
|