summaryrefslogtreecommitdiffstats
path: root/tpl
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2017-02-05 10:20:06 +0700
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2017-02-17 17:15:26 +0100
commit93ca7c9e958e34469a337e4efcc7c75774ec50fd (patch)
tree5dfa296cfe74fd5ef8f0d41ea4078704f453aa04 /tpl
parente34af6ee30f70f5780a281e2fd8f4ed9b487ee61 (diff)
all: Refactor to nonglobal Viper, i18n etc.
This is a final rewrite that removes all the global state in Hugo, which also enables the use if `t.Parallel` in tests. Updates #2701 Fixes #3016
Diffstat (limited to 'tpl')
-rw-r--r--tpl/template.go15
-rw-r--r--tpl/template_ast_transformers_test.go3
-rw-r--r--tpl/template_func_truncate_test.go1
-rw-r--r--tpl/template_funcs.go25
-rw-r--r--tpl/template_funcs_test.go167
-rw-r--r--tpl/template_i18n.go100
-rw-r--r--tpl/template_i18n_test.go149
-rw-r--r--tpl/template_resources.go40
-rw-r--r--tpl/template_resources_test.go54
-rw-r--r--tpl/template_test.go37
10 files changed, 198 insertions, 393 deletions
diff --git a/tpl/template.go b/tpl/template.go
index 844608014..9a6364d5a 100644
--- a/tpl/template.go
+++ b/tpl/template.go
@@ -21,6 +21,8 @@ import (
"path/filepath"
"strings"
+ "sync"
+
"github.com/eknkc/amber"
"github.com/spf13/afero"
bp "github.com/spf13/hugo/bufferpool"
@@ -31,6 +33,9 @@ import (
// TODO(bep) globals get rid of the rest of the jww.ERR etc.
+// Protecting global map access (Amber)
+var amberMu sync.Mutex
+
type templateErr struct {
name string
err error
@@ -132,6 +137,7 @@ func (t *GoHTMLTemplate) initFuncs(d *deps.Deps) {
t.amberFuncMap = template.FuncMap{}
+ amberMu.Lock()
for k, v := range amber.FuncMap {
t.amberFuncMap[k] = v
}
@@ -143,6 +149,7 @@ func (t *GoHTMLTemplate) initFuncs(d *deps.Deps) {
panic("should never be invoked")
}
}
+ amberMu.Unlock()
}
@@ -362,7 +369,9 @@ func (t *GoHTMLTemplate) AddTemplateFile(name, baseTemplatePath, path string) er
return err
}
+ amberMu.Lock()
templ, err := t.CompileAmberWithTemplate(b, path, t.New(templateName))
+ amberMu.Unlock()
if err != nil {
return err
}
@@ -482,11 +491,11 @@ func (t *GoHTMLTemplate) loadTemplates(absPath string, prefix string) {
}
if needsBase {
- layoutDir := helpers.GetLayoutDirPath()
+ layoutDir := t.PathSpec.GetLayoutDirPath()
currBaseFilename := fmt.Sprintf("%s-%s", helpers.Filename(path), baseFileName)
templateDir := filepath.Dir(path)
- themeDir := filepath.Join(helpers.GetThemeDir())
- relativeThemeLayoutsDir := filepath.Join(helpers.GetRelativeThemeDir(), "layouts")
+ themeDir := filepath.Join(t.PathSpec.GetThemeDir())
+ relativeThemeLayoutsDir := filepath.Join(t.PathSpec.GetRelativeThemeDir(), "layouts")
var baseTemplatedDir string
diff --git a/tpl/template_ast_transformers_test.go b/tpl/template_ast_transformers_test.go
index 8ffb1cab1..43d78284c 100644
--- a/tpl/template_ast_transformers_test.go
+++ b/tpl/template_ast_transformers_test.go
@@ -113,6 +113,7 @@ F3: {{ Echo (printf "themes/%s-theme" .Site.Params.LOWER) }}
)
func TestParamsKeysToLower(t *testing.T) {
+ t.Parallel()
require.Error(t, applyTemplateTransformers(nil))
@@ -190,6 +191,7 @@ func BenchmarkTemplateParamsKeysToLower(b *testing.B) {
}
func TestParamsKeysToLowerVars(t *testing.T) {
+ t.Parallel()
var (
ctx = map[string]interface{}{
"Params": map[string]interface{}{
@@ -227,6 +229,7 @@ Blue: {{ $__amber_1.Blue}}
}
func TestParamsKeysToLowerInBlockTemplate(t *testing.T) {
+ t.Parallel()
var (
ctx = map[string]interface{}{
diff --git a/tpl/template_func_truncate_test.go b/tpl/template_func_truncate_test.go
index 95368f83e..9213c6faa 100644
--- a/tpl/template_func_truncate_test.go
+++ b/tpl/template_func_truncate_test.go
@@ -21,6 +21,7 @@ import (
)
func TestTruncate(t *testing.T) {
+ t.Parallel()
var err error
cases := []struct {
v1 interface{}
diff --git a/tpl/template_funcs.go b/tpl/template_funcs.go
index 01b975665..9777bf619 100644
--- a/tpl/template_funcs.go
+++ b/tpl/template_funcs.go
@@ -46,7 +46,6 @@ import (
"github.com/spf13/hugo/deps"
"github.com/spf13/hugo/helpers"
jww "github.com/spf13/jwalterweatherman"
- "github.com/spf13/viper"
// Importing image codecs for image.DecodeConfig
_ "image/gif"
@@ -58,7 +57,6 @@ import (
type templateFuncster struct {
funcMap template.FuncMap
cachedPartials partialCache
-
*deps.Deps
}
@@ -398,6 +396,7 @@ func intersect(l1, l2 interface{}) (interface{}, error) {
}
// ResetCaches resets all caches that might be used during build.
+// TODO(bep) globals move image config cache to funcster
func ResetCaches() {
resetImageConfigCache()
}
@@ -1357,31 +1356,29 @@ func returnWhenSet(a, k interface{}) interface{} {
}
// highlight returns an HTML string with syntax highlighting applied.
-func highlight(in interface{}, lang, opts string) (template.HTML, error) {
+func (t *templateFuncster) highlight(in interface{}, lang, opts string) (template.HTML, error) {
str, err := cast.ToStringE(in)
if err != nil {
return "", err
}
- return template.HTML(helpers.Highlight(html.UnescapeString(str), lang, opts)), nil
+ return template.HTML(helpers.Highlight(t.Cfg, html.UnescapeString(str), lang, opts)), nil
}
var markdownTrimPrefix = []byte("<p>")
var markdownTrimSuffix = []byte("</p>\n")
// markdownify renders a given string from Markdown to HTML.
-func markdownify(in interface{}) (template.HTML, error) {
+func (t *templateFuncster) markdownify(in interface{}) (template.HTML, error) {
text, err := cast.ToStringE(in)
if err != nil {
return "", err
}
- language := viper.Get("currentContentLanguage").(*helpers.Language)
-
- m := helpers.RenderBytes(&helpers.RenderingContext{
- ConfigProvider: language,
- Content: []byte(text), PageFmt: "markdown"})
+ m := t.ContentSpec.RenderBytes(&helpers.RenderingContext{
+ Cfg: t.Cfg,
+ Content: []byte(text), PageFmt: "markdown"})
m = bytes.TrimPrefix(m, markdownTrimPrefix)
m = bytes.TrimSuffix(m, markdownTrimSuffix)
return template.HTML(m), nil
@@ -2143,7 +2140,7 @@ func (t *templateFuncster) initFuncMap() {
"getenv": getenv,
"gt": gt,
"hasPrefix": hasPrefix,
- "highlight": highlight,
+ "highlight": t.highlight,
"htmlEscape": htmlEscape,
"htmlUnescape": htmlUnescape,
"humanize": humanize,
@@ -2159,7 +2156,7 @@ func (t *templateFuncster) initFuncMap() {
"le": le,
"lower": lower,
"lt": lt,
- "markdownify": markdownify,
+ "markdownify": t.markdownify,
"md5": md5,
"mod": mod,
"modBool": modBool,
@@ -2211,8 +2208,8 @@ func (t *templateFuncster) initFuncMap() {
"upper": upper,
"urlize": t.PathSpec.URLize,
"where": where,
- "i18n": i18nTranslate,
- "T": i18nTranslate,
+ "i18n": t.Translate,
+ "T": t.Translate,
}
t.funcMap = funcMap
diff --git a/tpl/template_funcs_test.go b/tpl/template_funcs_test.go
index e4af75b30..35214b649 100644
--- a/tpl/template_funcs_test.go
+++ b/tpl/template_funcs_test.go
@@ -42,7 +42,9 @@ import (
"github.com/spf13/afero"
"github.com/spf13/cast"
+ "github.com/spf13/hugo/config"
"github.com/spf13/hugo/hugofs"
+ "github.com/spf13/hugo/i18n"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
@@ -53,12 +55,16 @@ var (
logger = jww.NewNotepad(jww.LevelFatal, jww.LevelFatal, os.Stdout, ioutil.Discard, "", log.Ldate|log.Ltime)
)
-func newDefaultDepsCfg() deps.DepsCfg {
+func newDepsConfig(cfg config.Provider) deps.DepsCfg {
+ l := helpers.NewLanguage("en", cfg)
+ l.Set("i18nDir", "i18n")
return deps.DepsCfg{
- Language: helpers.NewLanguage("en"),
- Fs: hugofs.NewMem(),
- Logger: logger,
- TemplateProvider: DefaultTemplateProvider,
+ Language: l,
+ Cfg: cfg,
+ Fs: hugofs.NewMem(l),
+ Logger: logger,
+ TemplateProvider: DefaultTemplateProvider,
+ TranslationProvider: i18n.NewTranslationProvider(),
}
}
@@ -88,22 +94,17 @@ func tstIsLt(tp tstCompareType) bool {
return tp == tstLt || tp == tstLe
}
-func tstInitTemplates() {
- viper.Set("CurrentContentLanguage", helpers.NewLanguage("en"))
- helpers.ResetConfigProvider()
-}
-
func TestFuncsInTemplate(t *testing.T) {
-
- testReset()
+ t.Parallel()
workingDir := "/home/hugo"
- viper.Set("workingDir", workingDir)
- viper.Set("currentContentLanguage", helpers.NewDefaultLanguage())
- viper.Set("multilingual", true)
+ v := viper.New()
- fs := hugofs.NewMem()
+ v.Set("workingDir", workingDir)
+ v.Set("multilingual", true)
+
+ fs := hugofs.NewMem(v)
afero.WriteFile(fs.Source, filepath.Join(workingDir, "README.txt"), []byte("Hugo Rocks!"), 0755)
@@ -268,11 +269,10 @@ urlize: bat-man
data.Section = "blog"
data.Params = map[string]interface{}{"langCode": "en"}
- viper.Set("baseURL", "http://mysite.com/hugo/")
-
- tstInitTemplates()
+ v.Set("baseURL", "http://mysite.com/hugo/")
+ v.Set("CurrentContentLanguage", helpers.NewLanguage("en", v))
- config := newDefaultDepsCfg()
+ config := newDepsConfig(v)
config.WithTemplate = func(templ tplapi.Template) error {
if _, err := templ.New("test").Parse(in); err != nil {
t.Fatal("Got error on parse", err)
@@ -282,7 +282,7 @@ urlize: bat-man
config.Fs = fs
d := deps.New(config)
- if err := d.LoadTemplates(); err != nil {
+ if err := d.LoadResources(); err != nil {
t.Fatal(err)
}
@@ -300,6 +300,7 @@ urlize: bat-man
}
func TestCompare(t *testing.T) {
+ t.Parallel()
for _, this := range []struct {
tstCompareType
funcUnderTest func(a, b interface{}) bool
@@ -370,6 +371,7 @@ func doTestCompare(t *testing.T, tp tstCompareType, funcUnderTest func(a, b inte
}
func TestMod(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
a interface{}
b interface{}
@@ -405,6 +407,7 @@ func TestMod(t *testing.T) {
}
func TestModBool(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
a interface{}
b interface{}
@@ -445,6 +448,7 @@ func TestModBool(t *testing.T) {
}
func TestFirst(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
count interface{}
sequence interface{}
@@ -480,6 +484,7 @@ func TestFirst(t *testing.T) {
}
func TestLast(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
count interface{}
sequence interface{}
@@ -515,6 +520,7 @@ func TestLast(t *testing.T) {
}
func TestAfter(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
count interface{}
sequence interface{}
@@ -550,6 +556,7 @@ func TestAfter(t *testing.T) {
}
func TestShuffleInputAndOutputFormat(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
sequence interface{}
success bool
@@ -588,6 +595,7 @@ func TestShuffleInputAndOutputFormat(t *testing.T) {
}
func TestShuffleRandomising(t *testing.T) {
+ t.Parallel()
// Note that this test can fail with false negative result if the shuffle
// of the sequence happens to be the same as the original sequence. However
// the propability of the event is 10^-158 which is negligible.
@@ -615,6 +623,7 @@ func TestShuffleRandomising(t *testing.T) {
}
func TestDictionary(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
v1 []interface{}
expecterr bool
@@ -647,13 +656,15 @@ func blankImage(width, height int) []byte {
}
func TestImageConfig(t *testing.T) {
- testReset()
+ t.Parallel()
workingDir := "/home/hugo"
- viper.Set("workingDir", workingDir)
+ v := viper.New()
- f := newTestFuncster()
+ v.Set("workingDir", workingDir)
+
+ f := newTestFuncsterWithViper(v)
for i, this := range []struct {
resetCache bool
@@ -754,6 +765,7 @@ func TestImageConfig(t *testing.T) {
}
func TestIn(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
v1 interface{}
v2 interface{}
@@ -783,6 +795,7 @@ func TestIn(t *testing.T) {
}
func TestSlicestr(t *testing.T) {
+ t.Parallel()
var err error
for i, this := range []struct {
v1 interface{}
@@ -848,6 +861,7 @@ func TestSlicestr(t *testing.T) {
}
func TestHasPrefix(t *testing.T) {
+ t.Parallel()
cases := []struct {
s interface{}
prefix interface{}
@@ -875,6 +889,7 @@ func TestHasPrefix(t *testing.T) {
}
func TestSubstr(t *testing.T) {
+ t.Parallel()
var err error
var n int
for i, this := range []struct {
@@ -952,6 +967,7 @@ func TestSubstr(t *testing.T) {
}
func TestSplit(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
v1 interface{}
v2 string
@@ -982,6 +998,7 @@ func TestSplit(t *testing.T) {
}
func TestIntersect(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
sequence1 interface{}
sequence2 interface{}
@@ -1025,6 +1042,7 @@ func TestIntersect(t *testing.T) {
}
func TestIsSet(t *testing.T) {
+ t.Parallel()
aSlice := []interface{}{1, 2, 3, 5}
aMap := map[string]interface{}{"a": 1, "b": 2}
@@ -1074,6 +1092,7 @@ type TstX struct {
}
func TestTimeUnix(t *testing.T) {
+ t.Parallel()
var sec int64 = 1234567890
tv := reflect.ValueOf(time.Unix(sec, 0))
i := 1
@@ -1096,6 +1115,7 @@ func TestTimeUnix(t *testing.T) {
}
func TestEvaluateSubElem(t *testing.T) {
+ t.Parallel()
tstx := TstX{A: "foo", B: "bar"}
var inner struct {
S fmt.Stringer
@@ -1146,6 +1166,7 @@ func TestEvaluateSubElem(t *testing.T) {
}
func TestCheckCondition(t *testing.T) {
+ t.Parallel()
type expect struct {
result bool
isError bool
@@ -1266,6 +1287,7 @@ func TestCheckCondition(t *testing.T) {
}
func TestWhere(t *testing.T) {
+ t.Parallel()
type Mid struct {
Tst TstX
@@ -1671,6 +1693,7 @@ func TestWhere(t *testing.T) {
}
func TestDelimit(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
sequence interface{}
delimiter interface{}
@@ -1720,6 +1743,7 @@ func TestDelimit(t *testing.T) {
}
func TestSort(t *testing.T) {
+ t.Parallel()
type ts struct {
MyInt int
MyFloat float64
@@ -1932,6 +1956,7 @@ func TestSort(t *testing.T) {
}
func TestReturnWhenSet(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
data interface{}
key interface{}
@@ -1957,7 +1982,10 @@ func TestReturnWhenSet(t *testing.T) {
}
func TestMarkdownify(t *testing.T) {
- viper.Set("currentContentLanguage", helpers.NewDefaultLanguage())
+ t.Parallel()
+ v := viper.New()
+
+ f := newTestFuncsterWithViper(v)
for i, this := range []struct {
in interface{}
@@ -1966,7 +1994,7 @@ func TestMarkdownify(t *testing.T) {
{"Hello **World!**", template.HTML("Hello <strong>World!</strong>")},
{[]byte("Hello Bytes **World!**"), template.HTML("Hello Bytes <strong>World!</strong>")},
} {
- result, err := markdownify(this.in)
+ result, err := f.markdownify(this.in)
if err != nil {
t.Fatalf("[%d] unexpected error in markdownify: %s", i, err)
}
@@ -1975,12 +2003,13 @@ func TestMarkdownify(t *testing.T) {
}
}
- if _, err := markdownify(t); err == nil {
+ if _, err := f.markdownify(t); err == nil {
t.Fatalf("markdownify should have errored")
}
}
func TestApply(t *testing.T) {
+ t.Parallel()
f := newTestFuncster()
@@ -2024,6 +2053,7 @@ func TestApply(t *testing.T) {
}
func TestChomp(t *testing.T) {
+ t.Parallel()
base := "\n This is\na story "
for i, item := range []string{
"\n", "\n\n",
@@ -2046,6 +2076,7 @@ func TestChomp(t *testing.T) {
}
func TestLower(t *testing.T) {
+ t.Parallel()
cases := []struct {
s interface{}
want string
@@ -2069,6 +2100,7 @@ func TestLower(t *testing.T) {
}
func TestTitle(t *testing.T) {
+ t.Parallel()
cases := []struct {
s interface{}
want string
@@ -2092,6 +2124,7 @@ func TestTitle(t *testing.T) {
}
func TestUpper(t *testing.T) {
+ t.Parallel()
cases := []struct {
s interface{}
want string
@@ -2115,8 +2148,12 @@ func TestUpper(t *testing.T) {
}
func TestHighlight(t *testing.T) {
+ t.Parallel()
code := "func boo() {}"
- highlighted, err := highlight(code, "go", "")
+
+ f := newTestFuncster()
+
+ highlighted, err := f.highlight(code, "go", "")
if err != nil {
t.Fatal("Highlight returned error:", err)
@@ -2127,7 +2164,7 @@ func TestHighlight(t *testing.T) {
t.Errorf("Highlight mismatch, got %v", highlighted)
}
- _, err = highlight(t, "go", "")
+ _, err = f.highlight(t, "go", "")
if err == nil {
t.Error("Expected highlight error")
@@ -2135,6 +2172,7 @@ func TestHighlight(t *testing.T) {
}
func TestInflect(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
inflectFunc func(i interface{}) (string, error)
in interface{}
@@ -2169,6 +2207,7 @@ func TestInflect(t *testing.T) {
}
func TestCounterFuncs(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
countFunc func(i interface{}) (int, error)
in string
@@ -2195,6 +2234,7 @@ func TestCounterFuncs(t *testing.T) {
}
func TestReplace(t *testing.T) {
+ t.Parallel()
v, _ := replace("aab", "a", "b")
assert.Equal(t, "bbb", v)
v, _ = replace("11a11", 1, 2)
@@ -2210,6 +2250,7 @@ func TestReplace(t *testing.T) {
}
func TestReplaceRE(t *testing.T) {
+ t.Parallel()
for i, val := range []struct {
pattern interface{}
repl interface{}
@@ -2234,6 +2275,7 @@ func TestReplaceRE(t *testing.T) {
}
func TestFindRE(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
expr string
content interface{}
@@ -2264,6 +2306,7 @@ func TestFindRE(t *testing.T) {
}
func TestTrim(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
v1 interface{}
@@ -2294,6 +2337,7 @@ func TestTrim(t *testing.T) {
}
func TestDateFormat(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
layout string
value interface{}
@@ -2328,6 +2372,7 @@ func TestDateFormat(t *testing.T) {
}
func TestDefaultFunc(t *testing.T) {
+ t.Parallel()
then := time.Now()
now := time.Now()
@@ -2385,6 +2430,7 @@ func TestDefaultFunc(t *testing.T) {
}
func TestDefault(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
input interface{}
tpl string
@@ -2414,6 +2460,7 @@ func TestDefault(t *testing.T) {
}
func TestSafeHTML(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
str string
tmplStr string
@@ -2454,6 +2501,7 @@ func TestSafeHTML(t *testing.T) {
}
func TestSafeHTMLAttr(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
str string
tmplStr string
@@ -2494,6 +2542,7 @@ func TestSafeHTMLAttr(t *testing.T) {
}
func TestSafeCSS(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
str string
tmplStr string
@@ -2535,6 +2584,7 @@ func TestSafeCSS(t *testing.T) {
// TODO(bep) what is this? Also look above.
func TestSafeJS(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
str string
tmplStr string
@@ -2576,6 +2626,7 @@ func TestSafeJS(t *testing.T) {
// TODO(bep) what is this?
func TestSafeURL(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
str string
tmplStr string
@@ -2616,6 +2667,7 @@ func TestSafeURL(t *testing.T) {
}
func TestBase64Decode(t *testing.T) {
+ t.Parallel()
testStr := "abc123!?$*&()'-=@~"
enc := base64.StdEncoding.EncodeToString([]byte(testStr))
result, err := base64Decode(enc)
@@ -2635,6 +2687,7 @@ func TestBase64Decode(t *testing.T) {
}
func TestBase64Encode(t *testing.T) {
+ t.Parallel()
testStr := "YWJjMTIzIT8kKiYoKSctPUB+"
dec, err := base64.StdEncoding.DecodeString(testStr)
@@ -2659,6 +2712,7 @@ func TestBase64Encode(t *testing.T) {
}
func TestMD5(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
input string
expectedHash string
@@ -2683,6 +2737,7 @@ func TestMD5(t *testing.T) {
}
func TestSHA1(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
input string
expectedHash string
@@ -2707,6 +2762,7 @@ func TestSHA1(t *testing.T) {
}
func TestSHA256(t *testing.T) {
+ t.Parallel()
for i, this := range []struct {
input string
expectedHash string
@@ -2731,13 +2787,15 @@ func TestSHA256(t *testing.T) {
}
func TestReadFile(t *testing.T) {
- testReset()
+ t.Parallel()
workingDir := "/home/hugo"
- viper.Set("workingDir", workingDir)
+ v := viper.New()
- f := newTestFuncster()
+ v.Set("workingDir", workingDir)
+
+ f := newTestFuncsterWithViper(v)
afero.WriteFile(f.Fs.Source, filepath.Join(workingDir, "/f/f1.txt"), []byte("f1-content"), 0755)
afero.WriteFile(f.Fs.Source, filepath.Join("/home", "f2.txt"), []byte("f2-content"), 0755)
@@ -2770,6 +2828,7 @@ func TestReadFile(t *testing.T) {
}
func TestPartialCached(t *testing.T) {
+ t.Parallel()
testCases := []struct {
name string
partial string
@@ -2793,7 +2852,6 @@ func TestPartialCached(t *testing.T) {
data.Section = "blog"
data.Params = map[string]interface{}{"langCode": "en"}
- tstInitTemplates()
for i, tc := range testCases {
var tmp string
if tc.variant != "" {
@@ -2802,9 +2860,9 @@ func TestPartialCached(t *testing.T) {
tmp = tc.tmpl
}
- cfg := newDefaultDepsCfg()
+ config := newDepsConfig(viper.New())
- cfg.WithTemplate = func(templ tplapi.Template) error {
+ config.WithTemplate = func(templ tplapi.Template) error {
err := templ.AddTemplate("testroot", tmp)
if err != nil {
return err
@@ -2817,8 +2875,8 @@ func TestPartialCached(t *testing.T) {
return nil
}
- de := deps.New(cfg)
- require.NoError(t, de.LoadTemplates())
+ de := deps.New(config)
+ require.NoError(t, de.LoadResources())
buf := new(bytes.Buffer)
templ := de.Tmpl.Lookup("testroot")
@@ -2842,8 +2900,8 @@ func TestPartialCached(t *testing.T) {
}
func BenchmarkPartial(b *testing.B) {
- cfg := newDefaultDepsCfg()
- cfg.WithTemplate = func(templ tplapi.Template) error {
+ config := newDepsConfig(viper.New())
+ config.WithTemplate = func(templ tplapi.Template) error {
err := templ.AddTemplate("testroot", `{{ partial "bench1" . }}`)
if err != nil {
return err
@@ -2856,8 +2914,8 @@ func BenchmarkPartial(b *testing.B) {
return nil
}
- de := deps.New(cfg)
- require.NoError(b, de.LoadTemplates())
+ de := deps.New(config)
+ require.NoError(b, de.LoadResources())
buf := new(bytes.Buffer)
tmpl := de.Tmpl.Lookup("testroot")
@@ -2873,8 +2931,8 @@ func BenchmarkPartial(b *testing.B) {
}
func BenchmarkPartialCached(b *testing.B) {
- cfg := newDefaultDepsCfg()
- cfg.WithTemplate = func(templ tplapi.Template) error {
+ config := newDepsConfig(viper.New())
+ config.WithTemplate = func(templ tplapi.Template) error {
err := templ.AddTemplate("testroot", `{{ partialCached "bench1" . }}`)
if err != nil {
return err
@@ -2887,8 +2945,8 @@ func BenchmarkPartialCached(b *testing.B) {
return nil
}
- de := deps.New(cfg)
- require.NoError(b, de.LoadTemplates())
+ de := deps.New(config)
+ require.NoError(b, de.LoadResources())
buf := new(bytes.Buffer)
tmpl := de.Tmpl.Lookup("testroot")
@@ -2904,9 +2962,14 @@ func BenchmarkPartialCached(b *testing.B) {
}
func newTestFuncster() *templateFuncster {
- cfg := newDefaultDepsCfg()
- d := deps.New(cfg)
- if err := d.LoadTemplates(); err != nil {
+ return newTestFuncsterWithViper(viper.New())
+}
+
+func newTestFuncsterWithViper(v *viper.Viper) *templateFuncster {
+ config := newDepsConfig(v)
+ d := deps.New(config)
+
+ if err := d.LoadResources(); err != nil {
panic(err)
}
@@ -2914,8 +2977,8 @@ func newTestFuncster() *templateFuncster {
}
func newTestTemplate(t *testing.T, name, template string) *template.Template {
- cfg := newDefaultDepsCfg()
- cfg.WithTemplate = func(templ tplapi.Template) error {
+ config := newDepsConfig(viper.New())
+ config.WithTemplate = func(templ tplapi.Template) error {
err := templ.AddTemplate(name, template)
if err != nil {
return err
@@ -2923,8 +2986,8 @@ func newTestTemplate(t *testing.T, name, template string) *template.Template {
return nil
}
- de := deps.New(cfg)
- require.NoError(t, de.LoadTemplates())
+ de := deps.New(config)
+ require.NoError(t, de.LoadResources())
return de.Tmpl.Lookup(name)
}
diff --git a/tpl/template_i18n.go b/tpl/template_i18n.go
deleted file mode 100644
index a7ec0df98..000000000
--- a/tpl/template_i18n.go
+++ /dev/null
@@ -1,100 +0,0 @@
-// Copyright 2015 The Hugo Authors. All rights reserved.
-//
-// 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.
-
-package tpl
-
-import (
- "github.com/nicksnyder/