2016-12-14 14:12:03 -05:00
|
|
|
// Copyright 2016 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 hugolib
|
|
|
|
|
|
|
|
import (
|
2017-03-02 04:04:20 -05:00
|
|
|
"fmt"
|
2016-12-14 14:12:03 -05:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
"github.com/spf13/hugo/deps"
|
|
|
|
"github.com/spf13/hugo/hugofs"
|
|
|
|
|
2016-12-14 14:12:03 -05:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2017-03-02 04:04:20 -05:00
|
|
|
func TestTemplateLookupOrder(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
|
|
|
var (
|
|
|
|
fs *hugofs.Fs
|
|
|
|
cfg *viper.Viper
|
|
|
|
th testHelper
|
|
|
|
)
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2017-03-02 04:04:20 -05:00
|
|
|
// Variants base templates:
|
2016-12-14 14:12:03 -05:00
|
|
|
// 1. <current-path>/<template-name>-baseof.<suffix>, e.g. list-baseof.<suffix>.
|
|
|
|
// 2. <current-path>/baseof.<suffix>
|
|
|
|
// 3. _default/<template-name>-baseof.<suffix>, e.g. list-baseof.<suffix>.
|
|
|
|
// 4. _default/baseof.<suffix>
|
2017-03-02 04:04:20 -05:00
|
|
|
for i, this := range []struct {
|
2016-12-14 14:12:03 -05:00
|
|
|
setup func(t *testing.T)
|
|
|
|
assert func(t *testing.T)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// Variant 1
|
|
|
|
func(t *testing.T) {
|
2017-03-02 04:04:20 -05:00
|
|
|
writeSource(t, fs, filepath.Join("layouts", "section", "sect1-baseof.html"), `Base: {{block "main" .}}block{{end}}`)
|
|
|
|
writeSource(t, fs, filepath.Join("layouts", "section", "sect1.html"), `{{define "main"}}sect{{ end }}`)
|
2016-12-14 14:12:03 -05:00
|
|
|
|
|
|
|
},
|
|
|
|
func(t *testing.T) {
|
2017-03-02 04:04:20 -05:00
|
|
|
th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "Base: sect")
|
2016-12-14 14:12:03 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Variant 2
|
|
|
|
func(t *testing.T) {
|
2017-01-10 04:55:03 -05:00
|
|
|
writeSource(t, fs, filepath.Join("layouts", "baseof.html"), `Base: {{block "main" .}}block{{end}}`)
|
|
|
|
writeSource(t, fs, filepath.Join("layouts", "index.html"), `{{define "main"}}index{{ end }}`)
|
2017-02-06 14:04:12 -05:00
|
|
|
|
2016-12-14 14:12:03 -05:00
|
|
|
},
|
|
|
|
func(t *testing.T) {
|
2017-02-17 15:14:52 -05:00
|
|
|
th.assertFileContent(filepath.Join("public", "index.html"), "Base: index")
|
2016-12-14 14:12:03 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Variant 3
|
|
|
|
func(t *testing.T) {
|
2017-01-10 04:55:03 -05:00
|
|
|
writeSource(t, fs, filepath.Join("layouts", "_default", "list-baseof.html"), `Base: {{block "main" .}}block{{end}}`)
|
|
|
|
writeSource(t, fs, filepath.Join("layouts", "_default", "list.html"), `{{define "main"}}list{{ end }}`)
|
2016-12-14 14:12:03 -05:00
|
|
|
|
|
|
|
},
|
|
|
|
func(t *testing.T) {
|
2017-03-02 04:04:20 -05:00
|
|
|
th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "Base: list")
|
2016-12-14 14:12:03 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Variant 4
|
|
|
|
func(t *testing.T) {
|
2017-01-10 04:55:03 -05:00
|
|
|
writeSource(t, fs, filepath.Join("layouts", "_default", "baseof.html"), `Base: {{block "main" .}}block{{end}}`)
|
|
|
|
writeSource(t, fs, filepath.Join("layouts", "_default", "list.html"), `{{define "main"}}list{{ end }}`)
|
2016-12-14 14:12:03 -05:00
|
|
|
|
|
|
|
},
|
|
|
|
func(t *testing.T) {
|
2017-03-02 04:04:20 -05:00
|
|
|
th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "Base: list")
|
2016-12-14 14:12:03 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Variant 1, theme, use project's base
|
|
|
|
func(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
cfg.Set("theme", "mytheme")
|
2017-03-02 04:04:20 -05:00
|
|
|
writeSource(t, fs, filepath.Join("layouts", "section", "sect1-baseof.html"), `Base: {{block "main" .}}block{{end}}`)
|
2017-01-10 04:55:03 -05:00
|
|
|
writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "section", "sect-baseof.html"), `Base Theme: {{block "main" .}}block{{end}}`)
|
2017-03-02 04:04:20 -05:00
|
|
|
writeSource(t, fs, filepath.Join("layouts", "section", "sect1.html"), `{{define "main"}}sect{{ end }}`)
|
2016-12-14 14:12:03 -05:00
|
|
|
|
|
|
|
},
|
|
|
|
func(t *testing.T) {
|
2017-03-02 04:04:20 -05:00
|
|
|
th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "Base: sect")
|
2016-12-14 14:12:03 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Variant 1, theme, use theme's base
|
|
|
|
func(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
cfg.Set("theme", "mytheme")
|
2017-03-02 04:04:20 -05:00
|
|
|
writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "section", "sect1-baseof.html"), `Base Theme: {{block "main" .}}block{{end}}`)
|
|
|
|
writeSource(t, fs, filepath.Join("layouts", "section", "sect1.html"), `{{define "main"}}sect{{ end }}`)
|
2016-12-14 14:12:03 -05:00
|
|
|
|
|
|
|
},
|
|
|
|
func(t *testing.T) {
|
2017-03-02 04:04:20 -05:00
|
|
|
th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "Base Theme: sect")
|
2016-12-14 14:12:03 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Variant 4, theme, use project's base
|
|
|
|
func(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
cfg.Set("theme", "mytheme")
|
2017-01-10 04:55:03 -05:00
|
|
|
writeSource(t, fs, filepath.Join("layouts", "_default", "baseof.html"), `Base: {{block "main" .}}block{{end}}`)
|
|
|
|
writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "_default", "baseof.html"), `Base Theme: {{block "main" .}}block{{end}}`)
|
|
|
|
writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "_default", "list.html"), `{{define "main"}}list{{ end }}`)
|
2016-12-14 14:12:03 -05:00
|
|
|
|
|
|
|
},
|
|
|
|
func(t *testing.T) {
|
2017-03-02 04:04:20 -05:00
|
|
|
th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "Base: list")
|
2016-12-14 14:12:03 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Variant 4, theme, use themes's base
|
|
|
|
func(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
cfg.Set("theme", "mytheme")
|
2017-01-10 04:55:03 -05:00
|
|
|
writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "_default", "baseof.html"), `Base Theme: {{block "main" .}}block{{end}}`)
|
|
|
|
writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "_default", "list.html"), `{{define "main"}}list{{ end }}`)
|
2016-12-14 14:12:03 -05:00
|
|
|
|
|
|
|
},
|
|
|
|
func(t *testing.T) {
|
2017-03-02 04:04:20 -05:00
|
|
|
th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "Base Theme: list")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Test section list and single template selection.
|
|
|
|
// Issue #3116
|
|
|
|
func(t *testing.T) {
|
|
|
|
cfg.Set("theme", "mytheme")
|
|
|
|
|
2017-03-02 07:52:08 -05:00
|
|
|
writeSource(t, fs, filepath.Join("layouts", "_default", "baseof.html"), `Base: {{block "main" .}}block{{end}}`)
|
|
|
|
|
2017-03-02 04:04:20 -05:00
|
|
|
// Both single and list template in /SECTION/
|
|
|
|
writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "sect1", "list.html"), `sect list`)
|
|
|
|
writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "_default", "list.html"), `default list`)
|
|
|
|
writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "sect1", "single.html"), `sect single`)
|
|
|
|
writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "_default", "single.html"), `default single`)
|
|
|
|
|
|
|
|
// sect2 with list template in /section
|
|
|
|
writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "section", "sect2.html"), `sect2 list`)
|
|
|
|
|
|
|
|
},
|
|
|
|
func(t *testing.T) {
|
|
|
|
th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "sect list")
|
|
|
|
th.assertFileContent(filepath.Join("public", "sect1", "page1", "index.html"), "sect single")
|
|
|
|
th.assertFileContent(filepath.Join("public", "sect2", "index.html"), "sect2 list")
|
2016-12-14 14:12:03 -05:00
|
|
|
},
|
|
|
|
},
|
2017-03-02 07:52:08 -05:00
|
|
|
{
|
|
|
|
// Test section list and single template selection with base template.
|
|
|
|
// Issue #2995
|
|
|
|
func(t *testing.T) {
|
|
|
|
|
|
|
|
writeSource(t, fs, filepath.Join("layouts", "_default", "baseof.html"), `Base Default: {{block "main" .}}block{{end}}`)
|
|
|
|
writeSource(t, fs, filepath.Join("layouts", "sect1", "baseof.html"), `Base Sect1: {{block "main" .}}block{{end}}`)
|
|
|
|
writeSource(t, fs, filepath.Join("layouts", "section", "sect2-baseof.html"), `Base Sect2: {{block "main" .}}block{{end}}`)
|
|
|
|
|
|
|
|
// Both single and list + base template in /SECTION/
|
|
|
|
writeSource(t, fs, filepath.Join("layouts", "sect1", "list.html"), `{{define "main"}}sect1 list{{ end }}`)
|
|
|
|
writeSource(t, fs, filepath.Join("layouts", "_default", "list.html"), `{{define "main"}}default list{{ end }}`)
|
|
|
|
writeSource(t, fs, filepath.Join("layouts", "sect1", "single.html"), `{{define "main"}}sect single{{ end }}`)
|
|
|
|
writeSource(t, fs, filepath.Join("layouts", "_default", "single.html"), `{{define "main"}}default single{{ end }}`)
|
|
|
|
|
|
|
|
// sect2 with list template in /section
|
|
|
|
writeSource(t, fs, filepath.Join("layouts", "section", "sect2.html"), `{{define "main"}}sect2 list{{ end }}`)
|
|
|
|
|
|
|
|
},
|
|
|
|
func(t *testing.T) {
|
|
|
|
th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "Base Sect1", "sect1 list")
|
|
|
|
th.assertFileContent(filepath.Join("public", "sect1", "page1", "index.html"), "Base Sect1", "sect single")
|
|
|
|
th.assertFileContent(filepath.Join("public", "sect2", "index.html"), "Base Sect2", "sect2 list")
|
|
|
|
|
|
|
|
// Note that this will get the default base template and not the one in /sect2 -- because there are no
|
|
|
|
// single template defined in /sect2.
|
|
|
|
th.assertFileContent(filepath.Join("public", "sect2", "page2", "index.html"), "Base Default", "default single")
|
|
|
|
},
|
|
|
|
},
|
2016-12-14 14:12:03 -05:00
|
|
|
} {
|
|
|
|
|
2017-03-02 07:52:08 -05:00
|
|
|
if i != 9 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
cfg, fs = newTestCfg()
|
2017-02-17 14:52:50 -05:00
|
|
|
th = testHelper{cfg, fs, t}
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2017-03-02 04:04:20 -05:00
|
|
|
for i := 1; i <= 3; i++ {
|
|
|
|
writeSource(t, fs, filepath.Join("content", fmt.Sprintf("sect%d", i), fmt.Sprintf("page%d.md", i)), `---
|
2016-12-14 14:12:03 -05:00
|
|
|
title: Template test
|
|
|
|
---
|
|
|
|
Some content
|
|
|
|
`)
|
2017-03-02 04:04:20 -05:00
|
|
|
}
|
|
|
|
|
2016-12-14 14:12:03 -05:00
|
|
|
this.setup(t)
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
|
2017-03-02 04:04:20 -05:00
|
|
|
t.Log("Template Lookup test", i)
|
2016-12-14 14:12:03 -05:00
|
|
|
this.assert(t)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|