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 (
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBaseGoTemplate(t *testing.T) {
|
2017-01-10 04:55:03 -05:00
|
|
|
|
|
|
|
var fs *hugofs.Fs
|
|
|
|
|
2016-12-14 14:12:03 -05:00
|
|
|
// Variants:
|
|
|
|
// 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-01-10 04:55:03 -05:00
|
|
|
for _, 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-01-10 04:55:03 -05:00
|
|
|
writeSource(t, fs, filepath.Join("layouts", "section", "sect-baseof.html"), `Base: {{block "main" .}}block{{end}}`)
|
|
|
|
writeSource(t, fs, filepath.Join("layouts", "section", "sect.html"), `{{define "main"}}sect{{ end }}`)
|
2016-12-14 14:12:03 -05:00
|
|
|
|
2017-02-06 11:07:48 -05:00
|
|
|
// See #2995
|
|
|
|
writeSource(t, fs, filepath.Join("layouts", "sect", "single.html"), `{{define "main"}}single sect{{ end }}`)
|
|
|
|
|
|
|
|
// Should not be picked.
|
|
|
|
writeSource(t, fs, filepath.Join("layouts", "_default", "baseof.html"), `Base: {{block "main" .}}NO{{end}}`)
|
|
|
|
|
2016-12-14 14:12:03 -05:00
|
|
|
},
|
|
|
|
func(t *testing.T) {
|
2017-01-10 04:55:03 -05:00
|
|
|
assertFileContent(t, fs, filepath.Join("public", "sect", "index.html"), false, "Base: sect")
|
2017-02-06 11:07:48 -05:00
|
|
|
assertFileContent(t, fs, filepath.Join("public", "sect", "page", "index.html"), false, "Base: single 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 }}`)
|
2016-12-14 14:12:03 -05:00
|
|
|
},
|
|
|
|
func(t *testing.T) {
|
2017-01-10 04:55:03 -05:00
|
|
|
assertFileContent(t, fs, filepath.Join("public", "index.html"), false, "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-01-10 04:55:03 -05:00
|
|
|
assertFileContent(t, fs, filepath.Join("public", "sect", "index.html"), false, "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-01-10 04:55:03 -05:00
|
|
|
assertFileContent(t, fs, filepath.Join("public", "sect", "index.html"), false, "Base: list")
|
2016-12-14 14:12:03 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Variant 1, theme, use project's base
|
|
|
|
func(t *testing.T) {
|
|
|
|
viper.Set("theme", "mytheme")
|
2017-01-10 04:55:03 -05:00
|
|
|
writeSource(t, fs, filepath.Join("layouts", "section", "sect-baseof.html"), `Base: {{block "main" .}}block{{end}}`)
|
|
|
|
writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "section", "sect-baseof.html"), `Base Theme: {{block "main" .}}block{{end}}`)
|
|
|
|
writeSource(t, fs, filepath.Join("layouts", "section", "sect.html"), `{{define "main"}}sect{{ end }}`)
|
2016-12-14 14:12:03 -05:00
|
|
|
|
2017-02-06 11:07:48 -05:00
|
|
|
// See #2995
|
|
|
|
writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "sect", "single.html"), `{{define "main"}}single sect{{ end }}`)
|
|
|
|
|
|
|
|
// Should not be picked.
|
|
|
|
writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "_default", "baseof.html"), `Base: {{block "main" .}}NO{{end}}`)
|
|
|
|
|
2016-12-14 14:12:03 -05:00
|
|
|
},
|
|
|
|
func(t *testing.T) {
|
2017-01-10 04:55:03 -05:00
|
|
|
assertFileContent(t, fs, filepath.Join("public", "sect", "index.html"), false, "Base: sect")
|
2017-02-06 11:07:48 -05:00
|
|
|
assertFileContent(t, fs, filepath.Join("public", "sect", "page", "index.html"), false, "Base: single sect")
|
2016-12-14 14:12:03 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Variant 1, theme, use theme's base
|
|
|
|
func(t *testing.T) {
|
|
|
|
viper.Set("theme", "mytheme")
|
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}}`)
|
|
|
|
writeSource(t, fs, filepath.Join("layouts", "section", "sect.html"), `{{define "main"}}sect{{ end }}`)
|
2016-12-14 14:12:03 -05:00
|
|
|
|
2017-02-06 11:07:48 -05:00
|
|
|
// See #2995
|
|
|
|
writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "sect", "single.html"), `{{define "main"}}single sect{{ end }}`)
|
|
|
|
|
|
|
|
// Should not be picked.
|
|
|
|
writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "_default", "baseof.html"), `Base: {{block "main" .}}NO{{end}}`)
|
|
|
|
|
2016-12-14 14:12:03 -05:00
|
|
|
},
|
|
|
|
func(t *testing.T) {
|
2017-01-10 04:55:03 -05:00
|
|
|
assertFileContent(t, fs, filepath.Join("public", "sect", "index.html"), false, "Base Theme: sect")
|
2017-02-06 11:07:48 -05:00
|
|
|
assertFileContent(t, fs, filepath.Join("public", "sect", "page", "index.html"), false, "Base: single sect")
|
2016-12-14 14:12:03 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Variant 4, theme, use project's base
|
|
|
|
func(t *testing.T) {
|
|
|
|
viper.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-01-10 04:55:03 -05:00
|
|
|
assertFileContent(t, fs, filepath.Join("public", "sect", "index.html"), false, "Base: list")
|
2016-12-14 14:12:03 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Variant 4, theme, use themes's base
|
|
|
|
func(t *testing.T) {
|
|
|
|
viper.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-01-10 04:55:03 -05:00
|
|
|
assertFileContent(t, fs, filepath.Join("public", "sect", "index.html"), false, "Base Theme: list")
|
2016-12-14 14:12:03 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
|
|
|
|
testCommonResetState()
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
fs = hugofs.NewMem()
|
|
|
|
|
|
|
|
writeSource(t, fs, filepath.Join("content", "sect", "page.md"), `---
|
2016-12-14 14:12:03 -05:00
|
|
|
title: Template test
|
|
|
|
---
|
|
|
|
Some content
|
|
|
|
`)
|
|
|
|
this.setup(t)
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
buildSingleSite(t, deps.DepsCfg{Fs: fs}, BuildCfg{})
|
2016-12-14 14:12:03 -05:00
|
|
|
|
|
|
|
this.assert(t)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|