| 1 |
// Copyright 2025 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 tplimpl_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"testing" |
| 18 |
|
| 19 |
"github.com/gohugoio/hugo/hugolib" |
| 20 |
) |
| 21 |
|
| 22 |
// Old as in before Hugo v0.146.0. |
| 23 |
func TestLayoutsOldSetup(t *testing.T) { |
| 24 |
t.Parallel() |
| 25 |
|
| 26 |
files := ` |
| 27 |
-- hugo.toml -- |
| 28 |
defaultContentLanguage = "en" |
| 29 |
defaultContentLanguageInSubdir = true |
| 30 |
[languages] |
| 31 |
[languages.en] |
| 32 |
title = "Title in English" |
| 33 |
weight = 1 |
| 34 |
[languages.nn] |
| 35 |
title = "Tittel på nynorsk" |
| 36 |
weight = 2 |
| 37 |
-- layouts/index.html -- |
| 38 |
Home. |
| 39 |
{{ template "_internal/twitter_cards.html" . }} |
| 40 |
-- layouts/_default/single.html -- |
| 41 |
Single. |
| 42 |
-- layouts/_default/single.nn.html -- |
| 43 |
Single NN. |
| 44 |
-- layouts/_default/list.html -- |
| 45 |
List HTML. |
| 46 |
-- layouts/docs/list-baseof.html -- |
| 47 |
Docs Baseof List HTML. |
| 48 |
{{ block "main" . }}Docs Baseof List HTML main block.{{ end }} |
| 49 |
-- layouts/docs/list.section.html -- |
| 50 |
{{ define "main" }} |
| 51 |
Docs List HTML. |
| 52 |
{{ end }} |
| 53 |
-- layouts/_default/list.json -- |
| 54 |
List JSON. |
| 55 |
-- layouts/_default/list.rss.xml -- |
| 56 |
List RSS. |
| 57 |
-- layouts/_default/list.nn.rss.xml -- |
| 58 |
List NN RSS. |
| 59 |
-- layouts/_default/baseof.html -- |
| 60 |
Base. |
| 61 |
-- layouts/partials/mypartial.html -- |
| 62 |
Partial. |
| 63 |
-- layouts/shortcodes/myshortcode.html -- |
| 64 |
Shortcode. |
| 65 |
-- content/docs/p1.md -- |
| 66 |
--- |
| 67 |
title: "P1" |
| 68 |
--- |
| 69 |
|
| 70 |
` |
| 71 |
|
| 72 |
b := hugolib.Test(t, files) |
| 73 |
|
| 74 |
// b.DebugPrint("", tplimpl.CategoryBaseof) |
| 75 |
|
| 76 |
b.AssertFileContent("public/en/docs/index.html", "Docs Baseof List HTML.\n\nDocs List HTML.") |
| 77 |
} |
| 78 |
|
| 79 |
func TestLayoutsOldSetupBaseofPrefix(t *testing.T) { |
| 80 |
t.Parallel() |
| 81 |
|
| 82 |
files := ` |
| 83 |
-- hugo.toml -- |
| 84 |
-- layouts/_default/layout1-baseof.html -- |
| 85 |
Baseof layout1. {{ block "main" . }}{{ end }} |
| 86 |
-- layouts/_default/layout2-baseof.html -- |
| 87 |
Baseof layout2. {{ block "main" . }}{{ end }} |
| 88 |
-- layouts/_default/layout1.html -- |
| 89 |
{{ define "main" }}Layout1. {{ .Title }}{{ end }} |
| 90 |
-- layouts/_default/layout2.html -- |
| 91 |
{{ define "main" }}Layout2. {{ .Title }}{{ end }} |
| 92 |
-- content/p1.md -- |
| 93 |
--- |
| 94 |
title: "P1" |
| 95 |
layout: "layout1" |
| 96 |
--- |
| 97 |
-- content/p2.md -- |
| 98 |
--- |
| 99 |
title: "P2" |
| 100 |
layout: "layout2" |
| 101 |
--- |
| 102 |
` |
| 103 |
|
| 104 |
b := hugolib.Test(t, files) |
| 105 |
|
| 106 |
b.AssertFileContent("public/p1/index.html", "Baseof layout1. Layout1. P1") |
| 107 |
b.AssertFileContent("public/p2/index.html", "Baseof layout2. Layout2. P2") |
| 108 |
} |
| 109 |
|
| 110 |
func TestLayoutsOldSetupTaxonomyAndTerm(t *testing.T) { |
| 111 |
t.Parallel() |
| 112 |
|
| 113 |
files := ` |
| 114 |
-- hugo.toml -- |
| 115 |
[taxonomies] |
| 116 |
cat = 'cats' |
| 117 |
dog = 'dogs' |
| 118 |
# Templates for term taxonomy, old setup. |
| 119 |
-- layouts/dogs/terms.html -- |
| 120 |
Dogs Terms. Most specific taxonomy template. |
| 121 |
-- layouts/taxonomy/terms.html -- |
| 122 |
Taxonomy Terms. Down the list. |
| 123 |
# Templates for term term, old setup. |
| 124 |
-- layouts/dogs/term.html -- |
| 125 |
Dogs Term. Most specific term template. |
| 126 |
-- layouts/term/term.html -- |
| 127 |
Term Term. Down the list. |
| 128 |
-- layouts/dogs/max/list.html -- |
| 129 |
max: {{ .Title }} |
| 130 |
-- layouts/_default/list.html -- |
| 131 |
Default list. |
| 132 |
-- layouts/_default/single.html -- |
| 133 |
Default single. |
| 134 |
-- content/p1.md -- |
| 135 |
--- |
| 136 |
title: "P1" |
| 137 |
dogs: ["luna", "daisy", "max"] |
| 138 |
--- |
| 139 |
|
| 140 |
` |
| 141 |
b := hugolib.Test(t, files, hugolib.TestOptWarn()) |
| 142 |
|
| 143 |
b.AssertLogContains("! WARN") |
| 144 |
|
| 145 |
b.AssertFileContent("public/dogs/index.html", "Dogs Terms. Most specific taxonomy template.") |
| 146 |
b.AssertFileContent("public/dogs/luna/index.html", "Dogs Term. Most specific term template.") |
| 147 |
b.AssertFileContent("public/dogs/max/index.html", "max: Max") // layouts/dogs/max/list.html wins over layouts/term/term.html because of distance. |
| 148 |
} |
| 149 |
|
| 150 |
func TestLayoutsOldSetupCustomRSS(t *testing.T) { |
| 151 |
t.Parallel() |
| 152 |
|
| 153 |
files := ` |
| 154 |
-- hugo.toml -- |
| 155 |
disableKinds = ["taxonomy", "term", "page"] |
| 156 |
[outputs] |
| 157 |
home = ["rss"] |
| 158 |
-- layouts/_default/list.rss.xml -- |
| 159 |
List RSS. |
| 160 |
` |
| 161 |
b := hugolib.Test(t, files) |
| 162 |
b.AssertFileContent("public/index.xml", "List RSS.") |
| 163 |
} |
| 164 |
|
| 165 |
func TestLayoutWithLanguagesLegacy(t *testing.T) { |
| 166 |
t.Parallel() |
| 167 |
files := ` |
| 168 |
-- hugo.toml -- |
| 169 |
disableKinds = ["taxonomy", "term", "sitemap", "rss"] |
| 170 |
defaultContentLanguage = "en" |
| 171 |
defaultContentLanguageInSubdir = true |
| 172 |
[languages] |
| 173 |
[languages.en] |
| 174 |
weight = 1 |
| 175 |
[languages.nn] |
| 176 |
weight = 2 |
| 177 |
[languages.sv] |
| 178 |
weight = 3 |
| 179 |
-- layouts/_default/single.en.html -- |
| 180 |
layouts/_default/single.html |
| 181 |
-- layouts/_default/single.nn.html -- |
| 182 |
layouts/_default/single.nn.html |
| 183 |
-- layouts/_default/single.sv.html -- |
| 184 |
layouts/_default/single.sv.html |
| 185 |
-- content/p1.md -- |
| 186 |
--- |
| 187 |
title: "P1" |
| 188 |
--- |
| 189 |
-- content/p1.nn.md -- |
| 190 |
--- |
| 191 |
title: "P1 NN" |
| 192 |
--- |
| 193 |
-- content/p1.sv.md -- |
| 194 |
--- |
| 195 |
title: "P1 SV" |
| 196 |
--- |
| 197 |
` |
| 198 |
b := hugolib.Test(t, files) |
| 199 |
|
| 200 |
b.AssertFileContent("public/en/p1/index.html", "layouts/_default/single.html") |
| 201 |
b.AssertFileContent("public/nn/p1/index.html", "layouts/_default/single.nn.html") |
| 202 |
b.AssertFileContent("public/sv/p1/index.html", "layouts/_default/single.sv.html") |
| 203 |
} |
| 204 |
|
| 205 |
func TestLayoutWithLanguagesLegacyMounts(t *testing.T) { |
| 206 |
t.Parallel() |
| 207 |
files := ` |
| 208 |
-- hugo.toml -- |
| 209 |
disableKinds = ["taxonomy", "term", "sitemap", "rss"] |
| 210 |
defaultContentLanguage = "en" |
| 211 |
defaultContentLanguageInSubdir = true |
| 212 |
[languages] |
| 213 |
[languages.en] |
| 214 |
weight = 1 |
| 215 |
[languages.nn] |
| 216 |
weight = 2 |
| 217 |
[languages.sv] |
| 218 |
weight = 3 |
| 219 |
|
| 220 |
|
| 221 |
[[module.mounts]] |
| 222 |
source = 'layouts/en' |
| 223 |
target = 'layouts' |
| 224 |
lang = 'en' |
| 225 |
|
| 226 |
[[module.mounts]] |
| 227 |
source = 'layouts/nn' |
| 228 |
target = 'layouts' |
| 229 |
lang = 'nn' |
| 230 |
[[module.mounts]] |
| 231 |
source = 'layouts/sv' |
| 232 |
target = 'layouts' |
| 233 |
lang = 'sv' |
| 234 |
|
| 235 |
-- layouts/en/_default/single.html -- |
| 236 |
layouts/en/_default/single.html |
| 237 |
-- layouts/nn/_default/single.html -- |
| 238 |
layouts/nn/_default/single.html |
| 239 |
-- layouts/sv/_default/single.html -- |
| 240 |
layouts/sv/_default/single.html |
| 241 |
-- content/p1.md -- |
| 242 |
--- |
| 243 |
title: "P1" |
| 244 |
--- |
| 245 |
-- content/p1.nn.md -- |
| 246 |
--- |
| 247 |
title: "P1 NN" |
| 248 |
--- |
| 249 |
-- content/p1.sv.md -- |
| 250 |
--- |
| 251 |
title: "P1 SV" |
| 252 |
--- |
| 253 |
` |
| 254 |
b := hugolib.Test(t, files) |
| 255 |
|
| 256 |
b.AssertFileContent("public/en/p1/index.html", "layouts/en/_default/single.html") |
| 257 |
b.AssertFileContent("public/nn/p1/index.html", "layouts/nn/_default/single.html") |
| 258 |
b.AssertFileContent("public/sv/p1/index.html", "layouts/sv/_default/single.html") |
| 259 |
} |
| 260 |
|
| 261 |
func TestLegacyPartialIssue13599(t *testing.T) { |
| 262 |
t.Parallel() |
| 263 |
|
| 264 |
files := ` |
| 265 |
-- hugo.toml -- |
| 266 |
-- layouts/partials/mypartial.html -- |
| 267 |
Mypartial. |
| 268 |
-- layouts/index.html -- |
| 269 |
mypartial: {{ template "partials/mypartial.html" . }} |
| 270 |
|
| 271 |
` |
| 272 |
b := hugolib.Test(t, files) |
| 273 |
|
| 274 |
b.AssertFileContent("public/index.html", "Mypartial.") |
| 275 |
} |