github.com/fighterlyt/hugo@v0.47.1/hugolib/template_test.go (about)

     1  // Copyright 2016 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package hugolib
    15  
    16  import (
    17  	"fmt"
    18  	"path/filepath"
    19  	"testing"
    20  
    21  	"github.com/gohugoio/hugo/deps"
    22  	"github.com/gohugoio/hugo/hugofs"
    23  
    24  	"github.com/spf13/viper"
    25  )
    26  
    27  func TestTemplateLookupOrder(t *testing.T) {
    28  	t.Parallel()
    29  	var (
    30  		fs  *hugofs.Fs
    31  		cfg *viper.Viper
    32  		th  testHelper
    33  	)
    34  
    35  	// Variants base templates:
    36  	//   1. <current-path>/<template-name>-baseof.<suffix>, e.g. list-baseof.<suffix>.
    37  	//   2. <current-path>/baseof.<suffix>
    38  	//   3. _default/<template-name>-baseof.<suffix>, e.g. list-baseof.<suffix>.
    39  	//   4. _default/baseof.<suffix>
    40  	for _, this := range []struct {
    41  		name   string
    42  		setup  func(t *testing.T)
    43  		assert func(t *testing.T)
    44  	}{
    45  		{
    46  			"Variant 1",
    47  			func(t *testing.T) {
    48  				writeSource(t, fs, filepath.Join("layouts", "section", "sect1-baseof.html"), `Base: {{block "main" .}}block{{end}}`)
    49  				writeSource(t, fs, filepath.Join("layouts", "section", "sect1.html"), `{{define "main"}}sect{{ end }}`)
    50  
    51  			},
    52  			func(t *testing.T) {
    53  				th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "Base: sect")
    54  			},
    55  		},
    56  		{
    57  			"Variant 2",
    58  			func(t *testing.T) {
    59  				writeSource(t, fs, filepath.Join("layouts", "baseof.html"), `Base: {{block "main" .}}block{{end}}`)
    60  				writeSource(t, fs, filepath.Join("layouts", "index.html"), `{{define "main"}}index{{ end }}`)
    61  
    62  			},
    63  			func(t *testing.T) {
    64  				th.assertFileContent(filepath.Join("public", "index.html"), "Base: index")
    65  			},
    66  		},
    67  		{
    68  			"Variant 3",
    69  			func(t *testing.T) {
    70  				writeSource(t, fs, filepath.Join("layouts", "_default", "list-baseof.html"), `Base: {{block "main" .}}block{{end}}`)
    71  				writeSource(t, fs, filepath.Join("layouts", "_default", "list.html"), `{{define "main"}}list{{ end }}`)
    72  
    73  			},
    74  			func(t *testing.T) {
    75  				th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "Base: list")
    76  			},
    77  		},
    78  		{
    79  			"Variant 4",
    80  			func(t *testing.T) {
    81  				writeSource(t, fs, filepath.Join("layouts", "_default", "baseof.html"), `Base: {{block "main" .}}block{{end}}`)
    82  				writeSource(t, fs, filepath.Join("layouts", "_default", "list.html"), `{{define "main"}}list{{ end }}`)
    83  
    84  			},
    85  			func(t *testing.T) {
    86  				th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "Base: list")
    87  			},
    88  		},
    89  		{
    90  			"Variant 1, theme, use site base",
    91  			func(t *testing.T) {
    92  				cfg.Set("theme", "mytheme")
    93  				writeSource(t, fs, filepath.Join("layouts", "section", "sect1-baseof.html"), `Base: {{block "main" .}}block{{end}}`)
    94  				writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "section", "sect-baseof.html"), `Base Theme: {{block "main" .}}block{{end}}`)
    95  				writeSource(t, fs, filepath.Join("layouts", "section", "sect1.html"), `{{define "main"}}sect{{ end }}`)
    96  
    97  			},
    98  			func(t *testing.T) {
    99  				th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "Base: sect")
   100  			},
   101  		},
   102  		{
   103  			"Variant 1, theme, use theme base",
   104  			func(t *testing.T) {
   105  				cfg.Set("theme", "mytheme")
   106  				writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "section", "sect1-baseof.html"), `Base Theme: {{block "main" .}}block{{end}}`)
   107  				writeSource(t, fs, filepath.Join("layouts", "section", "sect1.html"), `{{define "main"}}sect{{ end }}`)
   108  
   109  			},
   110  			func(t *testing.T) {
   111  				th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "Base Theme: sect")
   112  			},
   113  		},
   114  		{
   115  			"Variant 4, theme, use site base",
   116  			func(t *testing.T) {
   117  				cfg.Set("theme", "mytheme")
   118  				writeSource(t, fs, filepath.Join("layouts", "_default", "baseof.html"), `Base: {{block "main" .}}block{{end}}`)
   119  				writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "_default", "baseof.html"), `Base Theme: {{block "main" .}}block{{end}}`)
   120  				writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "_default", "list.html"), `{{define "main"}}list{{ end }}`)
   121  				writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "index.html"), `{{define "main"}}index{{ end }}`)
   122  
   123  			},
   124  			func(t *testing.T) {
   125  				th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "Base: list")
   126  				th.assertFileContent(filepath.Join("public", "index.html"), "Base: index") // Issue #3505
   127  			},
   128  		},
   129  		{
   130  			"Variant 4, theme, use themes base",
   131  			func(t *testing.T) {
   132  				cfg.Set("theme", "mytheme")
   133  				writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "_default", "baseof.html"), `Base Theme: {{block "main" .}}block{{end}}`)
   134  				writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "_default", "list.html"), `{{define "main"}}list{{ end }}`)
   135  
   136  			},
   137  			func(t *testing.T) {
   138  				th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "Base Theme: list")
   139  			},
   140  		},
   141  		{
   142  			// Issue #3116
   143  			"Test section list and single template selection",
   144  			func(t *testing.T) {
   145  				cfg.Set("theme", "mytheme")
   146  
   147  				writeSource(t, fs, filepath.Join("layouts", "_default", "baseof.html"), `Base: {{block "main" .}}block{{end}}`)
   148  
   149  				// Both single and list template in /SECTION/
   150  				writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "sect1", "list.html"), `sect list`)
   151  				writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "_default", "list.html"), `default list`)
   152  				writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "sect1", "single.html"), `sect single`)
   153  				writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "_default", "single.html"), `default single`)
   154  
   155  				// sect2 with list template in /section
   156  				writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "section", "sect2.html"), `sect2 list`)
   157  
   158  			},
   159  			func(t *testing.T) {
   160  				th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "sect list")
   161  				th.assertFileContent(filepath.Join("public", "sect1", "page1", "index.html"), "sect single")
   162  				th.assertFileContent(filepath.Join("public", "sect2", "index.html"), "sect2 list")
   163  			},
   164  		},
   165  		{
   166  			// Issue #2995
   167  			"Test section list and single template selection with base template",
   168  			func(t *testing.T) {
   169  
   170  				writeSource(t, fs, filepath.Join("layouts", "_default", "baseof.html"), `Base Default: {{block "main" .}}block{{end}}`)
   171  				writeSource(t, fs, filepath.Join("layouts", "sect1", "baseof.html"), `Base Sect1: {{block "main" .}}block{{end}}`)
   172  				writeSource(t, fs, filepath.Join("layouts", "section", "sect2-baseof.html"), `Base Sect2: {{block "main" .}}block{{end}}`)
   173  
   174  				// Both single and list + base template in /SECTION/
   175  				writeSource(t, fs, filepath.Join("layouts", "sect1", "list.html"), `{{define "main"}}sect1 list{{ end }}`)
   176  				writeSource(t, fs, filepath.Join("layouts", "_default", "list.html"), `{{define "main"}}default list{{ end }}`)
   177  				writeSource(t, fs, filepath.Join("layouts", "sect1", "single.html"), `{{define "main"}}sect single{{ end }}`)
   178  				writeSource(t, fs, filepath.Join("layouts", "_default", "single.html"), `{{define "main"}}default single{{ end }}`)
   179  
   180  				// sect2 with list template in /section
   181  				writeSource(t, fs, filepath.Join("layouts", "section", "sect2.html"), `{{define "main"}}sect2 list{{ end }}`)
   182  
   183  			},
   184  			func(t *testing.T) {
   185  				th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "Base Sect1", "sect1 list")
   186  				th.assertFileContent(filepath.Join("public", "sect1", "page1", "index.html"), "Base Sect1", "sect single")
   187  				th.assertFileContent(filepath.Join("public", "sect2", "index.html"), "Base Sect2", "sect2 list")
   188  
   189  				// Note that this will get the default base template and not the one in /sect2 -- because there are no
   190  				// single template defined in /sect2.
   191  				th.assertFileContent(filepath.Join("public", "sect2", "page2", "index.html"), "Base Default", "default single")
   192  			},
   193  		},
   194  	} {
   195  
   196  		cfg, fs = newTestCfg()
   197  		th = testHelper{cfg, fs, t}
   198  
   199  		for i := 1; i <= 3; i++ {
   200  			writeSource(t, fs, filepath.Join("content", fmt.Sprintf("sect%d", i), fmt.Sprintf("page%d.md", i)), `---
   201  title: Template test
   202  ---
   203  Some content
   204  `)
   205  		}
   206  
   207  		this.setup(t)
   208  
   209  		buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
   210  		t.Log(this.name)
   211  		this.assert(t)
   212  
   213  	}
   214  }