github.com/fighterlyt/hugo@v0.47.1/output/layout_base_test.go (about) 1 // Copyright 2017-present 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 output 15 16 import ( 17 "path/filepath" 18 "strings" 19 "testing" 20 21 "github.com/stretchr/testify/require" 22 ) 23 24 func TestLayoutBase(t *testing.T) { 25 26 var ( 27 workingDir = "/sites/mysite/" 28 layoutPath1 = "_default/single.html" 29 layoutPathAmp = "_default/single.amp.html" 30 layoutPathJSON = "_default/single.json" 31 ) 32 33 for _, this := range []struct { 34 name string 35 d TemplateLookupDescriptor 36 needsBase bool 37 basePathMatchStrings string 38 expect TemplateNames 39 }{ 40 {"No base", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: layoutPath1}, false, "", 41 TemplateNames{ 42 Name: "_default/single.html", 43 OverlayFilename: "_default/single.html", 44 }}, 45 {"Base", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: layoutPath1}, true, "", 46 TemplateNames{ 47 Name: "_default/single.html", 48 OverlayFilename: "_default/single.html", 49 MasterFilename: "_default/single-baseof.html", 50 }}, 51 // Issue #3893 52 {"Base Lang, Default Base", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: "_default/list.en.html"}, true, "_default/baseof.html", 53 TemplateNames{ 54 Name: "_default/list.en.html", 55 OverlayFilename: "_default/list.en.html", 56 MasterFilename: "_default/baseof.html", 57 }}, 58 {"Base Lang, Lang Base", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: "_default/list.en.html"}, true, "_default/baseof.html|_default/baseof.en.html", 59 TemplateNames{ 60 Name: "_default/list.en.html", 61 OverlayFilename: "_default/list.en.html", 62 MasterFilename: "_default/baseof.en.html", 63 }}, 64 // Issue #3856 65 {"Base Taxonomy Term", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: "taxonomy/tag.terms.html"}, true, "_default/baseof.html", 66 TemplateNames{ 67 Name: "taxonomy/tag.terms.html", 68 OverlayFilename: "taxonomy/tag.terms.html", 69 MasterFilename: "_default/baseof.html", 70 }}, 71 72 {"Partial", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: "partials/menu.html"}, true, 73 "mytheme/layouts/_default/baseof.html", 74 TemplateNames{ 75 Name: "partials/menu.html", 76 OverlayFilename: "partials/menu.html", 77 }}, 78 {"Partial in subfolder", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: "/partials/sub/menu.html"}, true, 79 "_default/baseof.html", 80 TemplateNames{ 81 Name: "partials/sub/menu.html", 82 OverlayFilename: "/partials/sub/menu.html", 83 }}, 84 {"Shortcode in subfolder", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: "shortcodes/sub/menu.html"}, true, 85 "_default/baseof.html", 86 TemplateNames{ 87 Name: "shortcodes/sub/menu.html", 88 OverlayFilename: "shortcodes/sub/menu.html", 89 }}, 90 {"AMP, no base", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: layoutPathAmp}, false, "", 91 TemplateNames{ 92 Name: "_default/single.amp.html", 93 OverlayFilename: "_default/single.amp.html", 94 }}, 95 {"JSON, no base", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: layoutPathJSON}, false, "", 96 TemplateNames{ 97 Name: "_default/single.json", 98 OverlayFilename: "_default/single.json", 99 }}, 100 {"AMP with base", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: layoutPathAmp}, true, "single-baseof.html|single-baseof.amp.html", 101 TemplateNames{ 102 Name: "_default/single.amp.html", 103 OverlayFilename: "_default/single.amp.html", 104 MasterFilename: "_default/single-baseof.amp.html", 105 }}, 106 {"AMP with no AMP base", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: layoutPathAmp}, true, "single-baseof.html", 107 TemplateNames{ 108 Name: "_default/single.amp.html", 109 OverlayFilename: "_default/single.amp.html", 110 MasterFilename: "_default/single-baseof.html", 111 }}, 112 113 {"JSON with base", TemplateLookupDescriptor{WorkingDir: workingDir, RelPath: layoutPathJSON}, true, "single-baseof.json", 114 TemplateNames{ 115 Name: "_default/single.json", 116 OverlayFilename: "_default/single.json", 117 MasterFilename: "_default/single-baseof.json", 118 }}, 119 } { 120 t.Run(this.name, func(t *testing.T) { 121 122 this.basePathMatchStrings = filepath.FromSlash(this.basePathMatchStrings) 123 124 fileExists := func(filename string) (bool, error) { 125 stringsToMatch := strings.Split(this.basePathMatchStrings, "|") 126 for _, s := range stringsToMatch { 127 if strings.Contains(filename, s) { 128 return true, nil 129 } 130 131 } 132 return false, nil 133 } 134 135 needsBase := func(filename string, subslices [][]byte) (bool, error) { 136 return this.needsBase, nil 137 } 138 139 this.d.OutputFormats = Formats{AMPFormat, HTMLFormat, RSSFormat, JSONFormat} 140 this.d.WorkingDir = filepath.FromSlash(this.d.WorkingDir) 141 this.d.RelPath = filepath.FromSlash(this.d.RelPath) 142 this.d.ContainsAny = needsBase 143 this.d.FileExists = fileExists 144 145 this.expect.MasterFilename = filepath.FromSlash(this.expect.MasterFilename) 146 this.expect.OverlayFilename = filepath.FromSlash(this.expect.OverlayFilename) 147 148 if strings.Contains(this.d.RelPath, "json") { 149 // currently the only plain text templates in this test. 150 this.expect.Name = "_text/" + this.expect.Name 151 } 152 153 id, err := CreateTemplateNames(this.d) 154 155 require.NoError(t, err) 156 require.Equal(t, this.expect, id, this.name) 157 158 }) 159 } 160 161 }