| 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 |
"context" |
| 18 |
"io" |
| 19 |
"strings" |
| 20 |
"testing" |
| 21 |
|
| 22 |
qt "github.com/frankban/quicktest" |
| 23 |
"github.com/gohugoio/hugo/htesting" |
| 24 |
"github.com/gohugoio/hugo/hugolib" |
| 25 |
"github.com/gohugoio/hugo/resources/kinds" |
| 26 |
"github.com/gohugoio/hugo/resources/page" |
| 27 |
"github.com/gohugoio/hugo/tpl/tplimpl" |
| 28 |
) |
| 29 |
|
| 30 |
var newSetupTestSites = ` |
| 31 |
-- hugo.toml -- |
| 32 |
defaultContentLanguage = "en" |
| 33 |
defaultContentLanguageInSubdir = true |
| 34 |
[languages] |
| 35 |
[languages.en] |
| 36 |
title = "Title in English" |
| 37 |
weight = 1 |
| 38 |
[languages.nn] |
| 39 |
title = "Tittel på nynorsk" |
| 40 |
weight = 2 |
| 41 |
[languages.fr] |
| 42 |
title = "Titre en français" |
| 43 |
weight = 3 |
| 44 |
|
| 45 |
[outputs] |
| 46 |
home = ["html", "rss", "redir"] |
| 47 |
|
| 48 |
[outputFormats] |
| 49 |
[outputFormats.redir] |
| 50 |
mediatype = "text/plain" |
| 51 |
baseName = "_redirects" |
| 52 |
isPlainText = true |
| 53 |
-- layouts/404.html -- |
| 54 |
{{ define "main" }} |
| 55 |
404. |
| 56 |
{{ end }} |
| 57 |
-- layouts/home.html -- |
| 58 |
{{ define "main" }} |
| 59 |
Home: {{ .Title }}|{{ .Content }}| |
| 60 |
Inline Partial: {{ partial "my-inline-partial.html" . }} |
| 61 |
{{ end }} |
| 62 |
{{ define "hero" }} |
| 63 |
Home hero. |
| 64 |
{{ end }} |
| 65 |
{{ define "partials/my-inline-partial.html" }} |
| 66 |
{{ $value := 32 }} |
| 67 |
{{ return $value }} |
| 68 |
{{ end }} |
| 69 |
-- layouts/index.redir -- |
| 70 |
Redir. |
| 71 |
-- layouts/single.html -- |
| 72 |
{{ define "main" }} |
| 73 |
Single needs base. |
| 74 |
{{ end }} |
| 75 |
-- layouts/foo/bar/single.html -- |
| 76 |
{{ define "main" }} |
| 77 |
Single sub path. |
| 78 |
{{ end }} |
| 79 |
-- layouts/_markup/render-codeblock.html -- |
| 80 |
Render codeblock. |
| 81 |
-- layouts/_markup/render-blockquote.html -- |
| 82 |
Render blockquote. |
| 83 |
-- layouts/_markup/render-codeblock-go.html -- |
| 84 |
Render codeblock go. |
| 85 |
-- layouts/_markup/render-link.html -- |
| 86 |
Link: {{ .Destination | safeURL }} |
| 87 |
-- layouts/foo/baseof.html -- |
| 88 |
Base sub path.{{ block "main" . }}{{ end }} |
| 89 |
-- layouts/foo/bar/baseof.page.html -- |
| 90 |
Base sub path.{{ block "main" . }}{{ end }} |
| 91 |
-- layouts/list.html -- |
| 92 |
{{ define "main" }} |
| 93 |
List needs base. |
| 94 |
{{ end }} |
| 95 |
-- layouts/section.html -- |
| 96 |
Section. |
| 97 |
-- layouts/mysectionlayout.section.fr.amp.html -- |
| 98 |
Section with layout. |
| 99 |
-- layouts/baseof.html -- |
| 100 |
Base.{{ block "main" . }}{{ end }} |
| 101 |
Hero:{{ block "hero" . }}{{ end }}: |
| 102 |
{{ with (templates.Defer (dict "key" "global")) }} |
| 103 |
Defer Block. |
| 104 |
{{ end }} |
| 105 |
-- layouts/baseof.fr.html -- |
| 106 |
Base fr.{{ block "main" . }}{{ end }} |
| 107 |
-- layouts/baseof.term.html -- |
| 108 |
Base term. |
| 109 |
-- layouts/baseof.section.fr.amp.html -- |
| 110 |
Base with identifiers.{{ block "main" . }}{{ end }} |
| 111 |
-- layouts/_partials/mypartial.html -- |
| 112 |
Partial. {{ partial "_inline/my-inline-partial-in-partial-with-no-ext" . }} |
| 113 |
{{ define "partials/_inline/my-inline-partial-in-partial-with-no-ext" }} |
| 114 |
Partial in partial. |
| 115 |
{{ end }} |
| 116 |
-- layouts/_partials/returnfoo.html -- |
| 117 |
{{ $v := "foo" }} |
| 118 |
{{ return $v }} |
| 119 |
-- layouts/_shortcodes/myshortcode.html -- |
| 120 |
Shortcode. {{ partial "mypartial.html" . }}|return:{{ partial "returnfoo.html" . }}| |
| 121 |
-- content/_index.md -- |
| 122 |
--- |
| 123 |
title: Home sweet home! |
| 124 |
--- |
| 125 |
|
| 126 |
{{< myshortcode >}} |
| 127 |
|
| 128 |
> My blockquote. |
| 129 |
|
| 130 |
|
| 131 |
Markdown link: [Foo](/foo) |
| 132 |
-- content/p1.md -- |
| 133 |
--- |
| 134 |
title: "P1" |
| 135 |
--- |
| 136 |
-- content/foo/bar/index.md -- |
| 137 |
--- |
| 138 |
title: "Foo Bar" |
| 139 |
--- |
| 140 |
|
| 141 |
{{< myshortcode >}} |
| 142 |
|
| 143 |
-- content/single-list.md -- |
| 144 |
--- |
| 145 |
title: "Single List" |
| 146 |
layout: "list" |
| 147 |
--- |
| 148 |
|
| 149 |
` |
| 150 |
|
| 151 |
func TestLayoutsType(t *testing.T) { |
| 152 |
files := ` |
| 153 |
-- hugo.toml -- |
| 154 |
disableKinds = ["taxonomy", "term"] |
| 155 |
-- layouts/list.html -- |
| 156 |
List. |
| 157 |
-- layouts/mysection/single.html -- |
| 158 |
mysection/single|{{ .Title }} |
| 159 |
-- layouts/mytype/single.html -- |
| 160 |
mytype/single|{{ .Title }} |
| 161 |
-- content/mysection/_index.md -- |
| 162 |
-- content/mysection/mysubsection/_index.md -- |
| 163 |
-- content/mysection/mysubsection/p1.md -- |
| 164 |
--- |
| 165 |
title: "P1" |
| 166 |
--- |
| 167 |
-- content/mysection/mysubsection/p2.md -- |
| 168 |
--- |
| 169 |
title: "P2" |
| 170 |
type: "mytype" |
| 171 |
--- |
| 172 |
|
| 173 |
` |
| 174 |
|
| 175 |
b := hugolib.Test(t, files, hugolib.TestOptWarn()) |
| 176 |
|
| 177 |
b.AssertLogContains("! WARN") |
| 178 |
|
| 179 |
b.AssertFileContent("public/mysection/mysubsection/p1/index.html", "mysection/single|P1") |
| 180 |
b.AssertFileContent("public/mysection/mysubsection/p2/index.html", "mytype/single|P2") |
| 181 |
} |
| 182 |
|
| 183 |
// New, as in from Hugo v0.146.0. |
| 184 |
func TestLayoutsNewSetup(t *testing.T) { |
| 185 |
const numIterations = 1 |
| 186 |
for range numIterations { |
| 187 |
|
| 188 |
b := hugolib.Test(t, newSetupTestSites, hugolib.TestOptWarn()) |
| 189 |
|
| 190 |
b.AssertLogContains("! WARN") |
| 191 |
|
| 192 |
b.AssertFileContent("public/en/index.html", |
| 193 |
"Base.\nHome: Home sweet home!|", |
| 194 |
"|Shortcode.\n|", |
| 195 |
"<p>Markdown link: Link: /foo</p>", |
| 196 |
"|return:foo|", |
| 197 |
"Defer Block.", |
| 198 |
"Home hero.", |
| 199 |
"Render blockquote.", |
| 200 |
) |
| 201 |
|
| 202 |
b.AssertFileContent("public/en/p1/index.html", "Base.\nSingle needs base.\n\nHero::\n\nDefer Block.") |
| 203 |
b.AssertFileContent("public/en/404.html", "404.") |
| 204 |
b.AssertFileContent("public/nn/404.html", "404.") |
| 205 |
b.AssertFileContent("public/fr/404.html", "404.") |
| 206 |
|
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
func TestHomeRSSAndHTMLWithHTMLOnlyShortcode(t *testing.T) { |
| 211 |
t.Parallel() |
| 212 |
|
| 213 |
files := ` |
| 214 |
-- hugo.toml -- |
| 215 |
disableKinds = ["taxonomy", "term"] |
| 216 |
[outputs] |
| 217 |
home = ["html", "rss"] |
| 218 |
-- layouts/home.html -- |
| 219 |
Home: {{ .Title }}|{{ .Content }}| |
| 220 |
-- layouts/single.html -- |
| 221 |
Single: {{ .Title }}|{{ .Content }}| |
| 222 |
-- layouts/_shortcodes/myshortcode.html -- |
| 223 |
Myshortcode: Count: {{ math.Counter }}| |
| 224 |
-- content/p1.md -- |
| 225 |
--- |
| 226 |
title: "P1" |
| 227 |
--- |
| 228 |
|
| 229 |
{{< myshortcode >}} |
| 230 |
` |
| 231 |
|
| 232 |
b := hugolib.Test(t, files) |
| 233 |
|
| 234 |
b.AssertFileContent("public/p1/index.html", "Single: P1|Myshortcode: Count: 1|") |
| 235 |
b.AssertFileContent("public/index.xml", "Myshortcode: Count: 1") |
| 236 |
} |
| 237 |
|
| 238 |
func TestHomeRSSAndHTMLWithHTMLOnlyRenderHook(t *testing.T) { |
| 239 |
t.Parallel() |
| 240 |
|
| 241 |
files := ` |
| 242 |
-- hugo.toml -- |
| 243 |
disableKinds = ["taxonomy", "term"] |
| 244 |
[outputs] |
| 245 |
home = ["html", "rss"] |
| 246 |
-- layouts/home.html -- |
| 247 |
Home: {{ .Title }}|{{ .Content }}| |
| 248 |
-- layouts/single.html -- |
| 249 |
Single: {{ .Title }}|{{ .Content }}| |
| 250 |
-- layouts/_markup/render-link.html -- |
| 251 |
Render Link: {{ math.Counter }}| |
| 252 |
-- content/p1.md -- |
| 253 |
--- |
| 254 |
title: "P1" |
| 255 |
--- |
| 256 |
|
| 257 |
Link: [Foo](/foo) |
| 258 |
` |
| 259 |
|
| 260 |
for range 2 { |
| 261 |
b := hugolib.Test(t, files) |
| 262 |
b.AssertFileContent("public/index.xml", "Link: Render Link: 1|") |
| 263 |
b.AssertFileContent("public/p1/index.html", "Single: P1|<p>Link: Render Link: 1|<") |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
func TestCodeblockIssue13864(t *testing.T) { |
| 268 |
t.Parallel() |
| 269 |
|
| 270 |
files := ` |
| 271 |
-- hugo.toml -- |
| 272 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 273 |
-- content/_index.md -- |
| 274 |
--- |
| 275 |
title: home |
| 276 |
--- |
| 277 |
|
| 278 |
~~~ |
| 279 |
LANG: none |
| 280 |
~~~ |
| 281 |
|
| 282 |
~~~go |
| 283 |
LANG: go |
| 284 |
~~~ |
| 285 |
|
| 286 |
~~~go-html-template |
| 287 |
LANG: go-html-template |
| 288 |
~~~ |
| 289 |
|
| 290 |
~~~xy |
| 291 |
LANG: xy |
| 292 |
~~~ |
| 293 |
|
| 294 |
~~~x-y |
| 295 |
LANG: x-y |
| 296 |
~~~ |
| 297 |
-- layouts/home.html -- |
| 298 |
{{ .Content }} |
| 299 |
-- layouts/_markup/render-codeblock.html -- |
| 300 |
{{ .Inner }} LAYOUT: render-codeblock.html| |
| 301 |
-- layouts/_markup/render-codeblock-go.html -- |
| 302 |
{{ .Inner }} LAYOUT: render-codeblock-go.html| |
| 303 |
-- layouts/_markup/render-codeblock-go-html-template.html -- |
| 304 |
{{ .Inner }} LAYOUT: render-codeblock-go-html-template.html| |
| 305 |
-- layouts/_markup/render-codeblock-xy.html -- |
| 306 |
{{ .Inner }} LAYOUT: render-codeblock-xy.html| |
| 307 |
-- layouts/_markup/render-codeblock-x-y.html.html -- |
| 308 |
{{ .Inner }} LAYOUT: render-codeblock-x-y.html| |
| 309 |
` |
| 310 |
|
| 311 |
b := hugolib.Test(t, files) |
| 312 |
|
| 313 |
b.AssertFileContent("public/index.html", |
| 314 |
"LANG: none LAYOUT: render-codeblock.html|", // pass |
| 315 |
"LANG: go LAYOUT: render-codeblock-go.html|", // fail: uses render-codeblock-go-html-template.html |
| 316 |
"LANG: go-html-template LAYOUT: render-codeblock-go-html-template.html|", // fail: uses render-codeblock.html |
| 317 |
"LANG: xy LAYOUT: render-codeblock-xy.html|", // pass |
| 318 |
"LANG: x-y LAYOUT: render-codeblock-x-y.html|", // fail: uses render-codeblock.html |
| 319 |
) |
| 320 |
} |
| 321 |
|
| 322 |
func TestRenderCodeblockSpecificity(t *testing.T) { |
| 323 |
files := ` |
| 324 |
-- hugo.toml -- |
| 325 |
-- layouts/_markup/render-codeblock.html -- |
| 326 |
Render codeblock.|{{ .Inner }}| |
| 327 |
-- layouts/_markup/render-codeblock-go.html -- |
| 328 |
Render codeblock go.|{{ .Inner }}| |
| 329 |
-- layouts/single.html -- |
| 330 |
{{ .Title }}|{{ .Content }}| |
| 331 |
-- content/p1.md -- |
| 332 |
--- |
| 333 |
title: "P1" |
| 334 |
--- |
| 335 |
|
| 336 |
§§§ |
| 337 |
Basic |
| 338 |
§§§ |
| 339 |
|
| 340 |
§§§ go |
| 341 |
Go |
| 342 |
§§§ |
| 343 |
|
| 344 |
` |
| 345 |
|
| 346 |
b := hugolib.Test(t, files) |
| 347 |
|
| 348 |
b.AssertFileContent("public/p1/index.html", "P1|Render codeblock.|Basic|Render codeblock go.|Go|") |
| 349 |
} |
| 350 |
|
| 351 |
func TestPrintUnusedTemplates(t *testing.T) { |
| 352 |
t.Parallel() |
| 353 |
|
| 354 |
files := ` |
| 355 |
-- hugo.toml -- |
| 356 |
baseURL = 'http://example.com/' |
| 357 |
printUnusedTemplates=true |
| 358 |
-- content/p1.md -- |
| 359 |
--- |
| 360 |
title: "P1" |
| 361 |
--- |
| 362 |
{{< usedshortcode >}} |
| 363 |
-- layouts/baseof.html -- |
| 364 |
{{ block "main" . }}{{ end }} |
| 365 |
-- layouts/baseof.json -- |
| 366 |
{{ block "main" . }}{{ end }} |
| 367 |
-- layouts/home.html -- |
| 368 |
{{ define "main" }}FOO{{ end }} |
| 369 |
-- layouts/single.json -- |
| 370 |
-- layouts/single.html -- |
| 371 |
{{ define "main" }}MAIN /_default/single.html{{ end }} |
| 372 |
-- layouts/post/single.html -- |
| 373 |
{{ define "main" }}MAIN{{ end }} |
| 374 |
-- layouts/_partials/usedpartial.html -- |
| 375 |
-- layouts/_partials/unusedpartial.html -- |
| 376 |
-- layouts/_shortcodes/usedshortcode.html -- |
| 377 |
{{ partial "usedpartial.html" }} |
| 378 |
-- layouts/_shortcodes/unusedshortcode.html -- |
| 379 |
|
| 380 |
` |
| 381 |
|
| 382 |
b := hugolib.Test(t, files, hugolib.TestOptOsFs()) |
| 383 |
|
| 384 |
b.AssertFileContent("public/p1/index.html", "MAIN /_default/single.html") |
| 385 |
|
| 386 |
unused := b.H.GetTemplateStore().UnusedTemplates() |
| 387 |
var names []string |
| 388 |
for _, tmpl := range unused { |
| 389 |
if fi := tmpl.Fi; fi != nil { |
| 390 |
names = append(names, fi.Meta().PathInfo.PathNoLeadingSlash()) |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
b.Assert(names, qt.DeepEquals, []string{"_partials/unusedpartial.html", "_shortcodes/unusedshortcode.html", "baseof.json", "post/single.html", "single.json"}) |
| 395 |
b.Assert(len(unused), qt.Equals, 5, qt.Commentf("%#v", names)) |
| 396 |
} |
| 397 |
|
| 398 |
func TestCreateManyTemplateStores(t *testing.T) { |
| 399 |
t.Parallel() |
| 400 |
htesting.SkipSlowTestUnlessCI(t) |
| 401 |
b := hugolib.Test(t, newSetupTestSites) |
| 402 |
store := b.H.TemplateStore |
| 403 |
|
| 404 |
for range 70 { |
| 405 |
newStore, err := store.NewFromOpts() |
| 406 |
b.Assert(err, qt.IsNil) |
| 407 |
b.Assert(newStore, qt.Not(qt.IsNil)) |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
func TestLayoutWithLanguagesVersionsAndRoles(t *testing.T) { |
| 412 |
t.Parallel() |
| 413 |
files := ` |
| 414 |
-- hugo.toml -- |
| 415 |
disableKinds = ["taxonomy", "term", "sitemap", "rss"] |
| 416 |
defaultContentLanguage = "en" |
| 417 |
defaultContentLanguageInSubdir = true |
| 418 |
defaultContentVersion = "v2.0.0" |
| 419 |
defaultContentVersionInSubdir = true |
| 420 |
defaultContentRole = "admin" |
| 421 |
defaultContentRoleInSubdir = true |
| 422 |
|
| 423 |
[languages] |
| 424 |
[languages.en] |
| 425 |
weight = 1 |
| 426 |
[languages.nn] |
| 427 |
weight = 2 |
| 428 |
|
| 429 |
[versions."v2.0.0"] |
| 430 |
[versions."v1.0.0"] |
| 431 |
|
| 432 |
[roles.admin] |
| 433 |
weight = 1 |
| 434 |
[roles.user] |
| 435 |
weight = 2 |
| 436 |
|
| 437 |
|
| 438 |
[[module.mounts]] |
| 439 |
source = 'content/en' |
| 440 |
target = 'content' |
| 441 |
[module.mounts.sites.matrix] |
| 442 |
languages = ['en'] |
| 443 |
versions = ['**'] |
| 444 |
[[module.mounts]] |
| 445 |
source = 'content/nn' |
| 446 |
target = 'content' |
| 447 |
[module.mounts.sites.matrix] |
| 448 |
languages = ['nn'] |
| 449 |
versions = ['**'] |
| 450 |
[[module.mounts]] |
| 451 |
source = 'layouts/v1' |
| 452 |
target = 'layouts' |
| 453 |
[module.mounts.sites.matrix] |
| 454 |
weight = 10 |
| 455 |
versions = ['v1**'] |
| 456 |
languages = ['**'] |
| 457 |
[[module.mounts]] |
| 458 |
source = 'layouts/en' |
| 459 |
target = 'layouts' |
| 460 |
[module.mounts.sites.matrix] |
| 461 |
languages = ['en'] |
| 462 |
versions = ['**'] |
| 463 |
[[module.mounts]] |
| 464 |
source = 'layouts/nn' |
| 465 |
target = 'layouts' |
| 466 |
[module.mounts.sites.matrix] |
| 467 |
languages = ['nn'] |
| 468 |
versions = ['**'] |
| 469 |
-- layouts/v1/all.html -- |
| 470 |
layouts/v1/all.html |
| 471 |
-- layouts/en/all.html -- |
| 472 |
layouts/en/all.html |
| 473 |
-- layouts/nn/all.html -- |
| 474 |
layouts/nn/all.html |
| 475 |
|
| 476 |
|
| 477 |
` |
| 478 |
|
| 479 |
b := hugolib.Test(t, files) |
| 480 |
// b.AssertPublishDir("asdf") |
| 481 |
b.AssertFileContent("public/admin/v2.0.0/en/index.html", "layouts/en/all.html") |
| 482 |
b.AssertFileContent("public/admin/v2.0.0/nn/index.html", "layouts/nn/all.html") |
| 483 |
b.AssertFileContent("public/admin/v1.0.0/nn/index.html", "layouts/v1/all.html") |
| 484 |
b.AssertFileContent("public/admin/v1.0.0/en/index.html", "layouts/v1/all.html") |
| 485 |
} |
| 486 |
|
| 487 |
func BenchmarkLookupPagesLayout(b *testing.B) { |
| 488 |
files := ` |
| 489 |
-- hugo.toml -- |
| 490 |
-- layouts/single.html -- |
| 491 |
{{ define "main" }} |
| 492 |
Main. |
| 493 |
{{ end }} |
| 494 |
-- layouts/baseof.html -- |
| 495 |
baseof: {{ block "main" . }}{{ end }} |
| 496 |
-- layouts/foo/bar/single.html -- |
| 497 |
{{ define "main" }} |
| 498 |
Main. |
| 499 |
{{ end }} |
| 500 |
|
| 501 |
` |
| 502 |
bb := hugolib.Test(b, files) |
| 503 |
store := bb.H.TemplateStore |
| 504 |
|
| 505 |
b.ResetTimer() |
| 506 |
b.Run("Single root", func(b *testing.B) { |
| 507 |
q := tplimpl.TemplateQuery{ |
| 508 |
Path: "/baz", |
| 509 |
Category: tplimpl.CategoryLayout, |
| 510 |
Desc: tplimpl.TemplateDescriptor{Kind: kinds.KindPage, LayoutFromTemplate: "single", OutputFormat: "html"}, |
| 511 |
} |
| 512 |
for b.Loop() { |
| 513 |
store.LookupPagesLayout(q) |
| 514 |
} |
| 515 |
}) |
| 516 |
|
| 517 |
b.Run("Single sub folder", func(b *testing.B) { |
| 518 |
q := tplimpl.TemplateQuery{ |
| 519 |
Path: "/foo/bar", |
| 520 |
Category: tplimpl.CategoryLayout, |
| 521 |
Desc: tplimpl.TemplateDescriptor{Kind: kinds.KindPage, LayoutFromTemplate: "single", OutputFormat: "html"}, |
| 522 |
} |
| 523 |
for b.Loop() { |
| 524 |
store.LookupPagesLayout(q) |
| 525 |
} |
| 526 |
}) |
| 527 |
} |
| 528 |
|
| 529 |
func BenchmarkNewTemplateStore(b *testing.B) { |
| 530 |
bb := hugolib.Test(b, newSetupTestSites) |
| 531 |
store := bb.H.TemplateStore |
| 532 |
|
| 533 |
for b.Loop() { |
| 534 |
newStore, err := store.NewFromOpts() |
| 535 |
if err != nil { |
| 536 |
b.Fatal(err) |
| 537 |
} |
| 538 |
if newStore == nil { |
| 539 |
b.Fatal("newStore is nil") |
| 540 |
} |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
func TestLayoutsLookupVariants(t *testing.T) { |
| 545 |
t.Parallel() |
| 546 |
|
| 547 |
files := ` |
| 548 |
-- hugo.toml -- |
| 549 |
defaultContentLanguage = "en" |
| 550 |
defaultContentLanguageInSubdir = true |
| 551 |
[outputs] |
| 552 |
home = ["html", "rss"] |
| 553 |
page = ["html", "rss", "amp"] |
| 554 |
section = ["html", "rss"] |
| 555 |
|
| 556 |
[languages] |
| 557 |
[languages.en] |
| 558 |
title = "Title in English" |
| 559 |
weight = 1 |
| 560 |
[languages.nn] |
| 561 |
title = "Tittel på nynorsk" |
| 562 |
weight = 2 |
| 563 |
-- layouts/list.xml -- |
| 564 |
layouts/list.xml |
| 565 |
-- layouts/_shortcodes/myshortcode.html -- |
| 566 |
layouts/_shortcodes/myshortcode.html |
| 567 |
-- layouts/foo/bar/_shortcodes/myshortcode.html -- |
| 568 |
layouts/foo/bar/_shortcodes/myshortcode.html |
| 569 |
-- layouts/_markup/render-codeblock.html -- |
| 570 |
layouts/_markup/render-codeblock.html|{{ .Type }}| |
| 571 |
-- layouts/_markup/render-codeblock-go.html -- |
| 572 |
layouts/_markup/render-codeblock-go.html|{{ .Type }}| |
| 573 |
-- layouts/single.xml -- |
| 574 |
layouts/single.xml |
| 575 |
-- layouts/single.rss.xml -- |
| 576 |
layouts/single.rss.xml |
| 577 |
-- layouts/single.nn.rss.xml -- |
| 578 |
layouts/single.nn.rss.xml |
| 579 |
-- layouts/list.html -- |
| 580 |
layouts/list.html |
| 581 |
-- layouts/single.html -- |
| 582 |
layouts/single.html |
| 583 |
{{ .Content }} |
| 584 |
-- layouts/mylayout.html -- |
| 585 |
layouts/mylayout.html |
| 586 |
-- layouts/mylayout.nn.html -- |
| 587 |
layouts/mylayout.nn.html |
| 588 |
-- layouts/foo/single.rss.xml -- |
| 589 |
layouts/foo/single.rss.xml |
| 590 |
-- layouts/foo/single.amp.html -- |
| 591 |
layouts/foo/single.amp.html |
| 592 |
-- layouts/foo/bar/page.html -- |
| 593 |
layouts/foo/bar/page.html |
| 594 |
-- layouts/foo/bar/baz/single.html -- |
| 595 |
layouts/foo/bar/baz/single.html |
| 596 |
{{ .Content }} |
| 597 |
-- layouts/qux/mylayout.html -- |
| 598 |
layouts/qux/mylayout.html |
| 599 |
-- layouts/qux/single.xml -- |
| 600 |
layouts/qux/single.xml |
| 601 |
-- layouts/qux/mylayout.section.html -- |
| 602 |
layouts/qux/mylayout.section.html |
| 603 |
-- content/p.md -- |
| 604 |
--- |
| 605 |
--- |
| 606 |
§§§ |
| 607 |
code |
| 608 |
§§§ |
| 609 |
|
| 610 |
§§§ go |
| 611 |
code |
| 612 |
§§§ |
| 613 |
|
| 614 |
{{< myshortcode >}} |
| 615 |
-- content/foo/p.md -- |
| 616 |
-- content/foo/p.nn.md -- |
| 617 |
-- content/foo/bar/p.md -- |
| 618 |
-- content/foo/bar/withmylayout.md -- |
| 619 |
--- |
| 620 |
layout: mylayout |
| 621 |
--- |
| 622 |
-- content/foo/bar/_index.md -- |
| 623 |
-- content/foo/bar/baz/p.md -- |
| 624 |
--- |
| 625 |
--- |
| 626 |
{{< myshortcode >}} |
| 627 |
-- content/qux/p.md -- |
| 628 |
-- content/qux/_index.md -- |
| 629 |
--- |
| 630 |
layout: mylayout |
| 631 |
--- |
| 632 |
-- content/qux/quux/p.md -- |
| 633 |
-- content/qux/quux/withmylayout.md -- |
| 634 |
--- |
| 635 |
layout: mylayout |
| 636 |
--- |
| 637 |
-- content/qux/quux/withmylayout.nn.md -- |
| 638 |
--- |
| 639 |
layout: mylayout |
| 640 |
--- |
| 641 |
|
| 642 |
|
| 643 |
` |
| 644 |
|
| 645 |
for range 2 { |
| 646 |
|
| 647 |
b := hugolib.Test(t, files, hugolib.TestOptWarn()) |
| 648 |
|
| 649 |
b.AssertLogContains("! WARN") |
| 650 |
|
| 651 |
// Single pages. |
| 652 |
// output format: html. |
| 653 |
b.AssertFileContent("public/en/p/index.html", "layouts/single.html", |
| 654 |
"layouts/_markup/render-codeblock.html|", |
| 655 |
"layouts/_markup/render-codeblock-go.html|go|", |
| 656 |
"layouts/_shortcodes/myshortcode.html", |
| 657 |
) |
| 658 |
b.AssertFileContent("public/en/foo/p/index.html", "layouts/single.html") |
| 659 |
b.AssertFileContent("public/en/foo/bar/p/index.html", "layouts/foo/bar/page.html") |
| 660 |
b.AssertFileContent("public/en/foo/bar/withmylayout/index.html", "layouts/mylayout.html") |
| 661 |
b.AssertFileContent("public/en/foo/bar/baz/p/index.html", "layouts/foo/bar/baz/single.html", "layouts/foo/bar/_shortcodes/myshortcode.html") |
| 662 |
b.AssertFileContent("public/en/qux/quux/withmylayout/index.html", "layouts/qux/mylayout.html") |
| 663 |
// output format: amp. |
| 664 |
b.AssertFileContent("public/en/amp/p/index.html", "layouts/single.html") |
| 665 |
b.AssertFileContent("public/en/amp/foo/p/index.html", "layouts/foo/single.amp.html") |
| 666 |
// output format: rss. |
| 667 |
b.AssertFileContent("public/en/p/index.xml", "layouts/single.rss.xml") |
| 668 |
b.AssertFileContent("public/en/foo/p/index.xml", "layouts/foo/single.rss.xml") |
| 669 |
// Note: THere is layouts/foo/single.rss.xml, but we use the root because of a better language match. |
| 670 |
b.AssertFileContent("public/nn/foo/p/index.xml", "layouts/single.nn.rss.xml") |
| 671 |
|
| 672 |
// Note: There is qux/single.xml that's closer, but the one in the root is used because of the output format match. |
| 673 |
b.AssertFileContent("public/en/qux/p/index.xml", "layouts/single.rss.xml") |
| 674 |
|
| 675 |
// Note. |
| 676 |
b.AssertFileContent("public/nn/qux/quux/withmylayout/index.html", "layouts/mylayout.nn.html") |
| 677 |
|
| 678 |
// Section pages. |
| 679 |
// output format: html. |
| 680 |
b.AssertFileContent("public/en/foo/index.html", "layouts/list.html") |
| 681 |
b.AssertFileContent("public/en/qux/index.html", "layouts/qux/mylayout.section.html") |
| 682 |
// output format: rss. |
| 683 |
b.AssertFileContent("public/en/foo/index.xml", "layouts/list.xml") |
| 684 |
|
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
func TestLookupOrderIssue13636(t *testing.T) { |
| 689 |
t.Parallel() |
| 690 |
|
| 691 |
filesTemplate := ` |
| 692 |
-- hugo.toml -- |
| 693 |
defaultContentLanguage = "en" |
| 694 |
defaultContentLanguageInSubdir = true |
| 695 |
[languages] |
| 696 |
[languages.en] |
| 697 |
weight = 1 |
| 698 |
[languages.nn] |
| 699 |
weight = 2 |
| 700 |
-- content/s1/p1.en.md -- |
| 701 |
--- |
| 702 |
outputs: ["html", "amp", "json"] |
| 703 |
--- |
| 704 |
-- content/s1/p1.nn.md -- |
| 705 |
--- |
| 706 |
outputs: ["html", "amp", "json"] |
| 707 |
--- |
| 708 |
-- layouts/L1 -- |
| 709 |
L1 |
| 710 |
-- layouts/L2 -- |
| 711 |
L2 |
| 712 |
-- layouts/L3 -- |
| 713 |
L3 |
| 714 |
|
| 715 |
` |
| 716 |
|
| 717 |
tests := []struct { |
| 718 |
Lang string |
| 719 |
L1 string |
| 720 |
L2 string |
| 721 |
L3 string |
| 722 |
ExpectHTML string |
| 723 |
ExpectAmp string |
| 724 |
ExpectJSON string |
| 725 |
}{ |
| 726 |
{"en", "all.en.html", "all.html", "single.html", "single.html", "single.html", ""}, |
| 727 |
{"en", "all.amp.html", "all.html", "page.html", "page.html", "all.amp.html", ""}, |
| 728 |
{"en", "all.amp.html", "all.html", "list.html", "all.html", "all.amp.html", ""}, |
| 729 |
{"en", "all.en.html", "all.json", "single.html", "single.html", "single.html", "all.json"}, |
| 730 |
{"en", "all.en.html", "single.json", "single.html", "single.html", "single.html", "single.json"}, |
| 731 |
{"en", "all.en.html", "all.html", "list.html", "all.en.html", "all.en.html", ""}, |
| 732 |
{"en", "list.en.html", "list.html", "list.en.html", "", "", ""}, |
| 733 |
{"nn", "all.en.html", "all.html", "single.html", "single.html", "single.html", ""}, |
| 734 |
{"nn", "all.en.html", "all.nn.html", "single.html", "single.html", "single.html", ""}, |
| 735 |
{"nn", "all.en.html", "all.nn.html", "single.nn.html", "single.nn.html", "single.nn.html", ""}, |
| 736 |
{"nn", "single.json", "single.nn.json", "all.json", "", "", "single.nn.json"}, |
| 737 |
{"nn", "single.json", "single.en.json", "all.nn.json", "", "", "single.json"}, |
| 738 |
} |
| 739 |
|
| 740 |
for i, test := range tests { |
| 741 |
if i != 10 { |
| 742 |
// continue |
| 743 |
} |
| 744 |
files := strings.ReplaceAll(filesTemplate, "L1", test.L1) |
| 745 |
files = strings.ReplaceAll(files, "L2", test.L2) |
| 746 |
files = strings.ReplaceAll(files, "L3", test.L3) |
| 747 |
t.Logf("Test %d: %s %s %s %s", i, test.Lang, test.L1, test.L2, test.L3) |
| 748 |
|
| 749 |
for range 1 { |
| 750 |
b := hugolib.Test(t, files) |
| 751 |
b.Assert(len(b.H.Sites), qt.Equals, 2) |
| 752 |
|
| 753 |
var ( |
| 754 |
pubhHTML = "public/LANG/s1/p1/index.html" |
| 755 |
pubhAmp = "public/LANG/amp/s1/p1/index.html" |
| 756 |
pubhJSON = "public/LANG/s1/p1/index.json" |
| 757 |
) |
| 758 |
|
| 759 |
pubhHTML = strings.ReplaceAll(pubhHTML, "LANG", test.Lang) |
| 760 |
pubhAmp = strings.ReplaceAll(pubhAmp, "LANG", test.Lang) |
| 761 |
pubhJSON = strings.ReplaceAll(pubhJSON, "LANG", test.Lang) |
| 762 |
|
| 763 |
if test.ExpectHTML != "" { |
| 764 |
b.AssertFileContent(pubhHTML, test.ExpectHTML) |
| 765 |
} else { |
| 766 |
b.AssertFileExists(pubhHTML, false) |
| 767 |
} |
| 768 |
|
| 769 |
if test.ExpectAmp != "" { |
| 770 |
b.AssertFileContent(pubhAmp, test.ExpectAmp) |
| 771 |
} else { |
| 772 |
b.AssertFileExists(pubhAmp, false) |
| 773 |
} |
| 774 |
|
| 775 |
if test.ExpectJSON != "" { |
| 776 |
b.AssertFileContent(pubhJSON, test.ExpectJSON) |
| 777 |
} else { |
| 778 |
b.AssertFileExists(pubhJSON, false) |
| 779 |
} |
| 780 |
} |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
func TestLookupShortcodeDepth(t *testing.T) { |
| 785 |
t.Parallel() |
| 786 |
|
| 787 |
files := ` |
| 788 |
-- hugo.toml -- |
| 789 |
-- layouts/_shortcodes/myshortcode.html -- |
| 790 |
layouts/_shortcodes/myshortcode.html |
| 791 |
-- layouts/foo/_shortcodes/myshortcode.html -- |
| 792 |
layouts/foo/_shortcodes/myshortcode.html |
| 793 |
-- layouts/single.html -- |
| 794 |
{{ .Content }}| |
| 795 |
-- content/p.md -- |
| 796 |
--- |
| 797 |
--- |
| 798 |
{{< myshortcode >}} |
| 799 |
-- content/foo/p.md -- |
| 800 |
--- |
| 801 |
--- |
| 802 |
{{< myshortcode >}} |
| 803 |
-- content/foo/bar/p.md -- |
| 804 |
--- |
| 805 |
--- |
| 806 |
{{< myshortcode >}} |
| 807 |
` |
| 808 |
|
| 809 |
b := hugolib.Test(t, files) |
| 810 |
|
| 811 |
b.AssertFileContent("public/p/index.html", "layouts/_shortcodes/myshortcode.html") |
| 812 |
b.AssertFileContent("public/foo/p/index.html", "layouts/foo/_shortcodes/myshortcode.html") |
| 813 |
b.AssertFileContent("public/foo/bar/p/index.html", "layouts/foo/_shortcodes/myshortcode.html") |
| 814 |
} |
| 815 |
|
| 816 |
func TestLookupShortcodeLayout(t *testing.T) { |
| 817 |
t.Parallel() |
| 818 |
|
| 819 |
files := ` |
| 820 |
-- hugo.toml -- |
| 821 |
-- layouts/_shortcodes/myshortcode.single.html -- |
| 822 |
layouts/_shortcodes/myshortcode.single.html |
| 823 |
-- layouts/_shortcodes/myshortcode.list.html -- |
| 824 |
layouts/_shortcodes/myshortcode.list.html |
| 825 |
-- layouts/single.html -- |
| 826 |
{{ .Content }}| |
| 827 |
-- layouts/list.html -- |
| 828 |
{{ .Content }}| |
| 829 |
-- content/_index.md -- |
| 830 |
--- |
| 831 |
--- |
| 832 |
{{< myshortcode >}} |
| 833 |
-- content/p.md -- |
| 834 |
--- |
| 835 |
--- |
| 836 |
{{< myshortcode >}} |
| 837 |
-- content/foo/p.md -- |
| 838 |
--- |
| 839 |
--- |
| 840 |
{{< myshortcode >}} |
| 841 |
-- content/foo/bar/p.md -- |
| 842 |
--- |
| 843 |
--- |
| 844 |
{{< myshortcode >}} |
| 845 |
` |
| 846 |
|
| 847 |
b := hugolib.Test(t, files) |
| 848 |
|
| 849 |
b.AssertFileContent("public/p/index.html", "layouts/_shortcodes/myshortcode.single.html") |
| 850 |
b.AssertFileContent("public/index.html", "layouts/_shortcodes/myshortcode.list.html") |
| 851 |
} |
| 852 |
|
| 853 |
func TestLayoutAll(t *testing.T) { |
| 854 |
t.Parallel() |
| 855 |
|
| 856 |
files := ` |
| 857 |
-- hugo.toml -- |
| 858 |
-- layouts/single.html -- |
| 859 |
Single. |
| 860 |
-- layouts/all.html -- |
| 861 |
All. |
| 862 |
-- content/p1.md -- |
| 863 |
|
| 864 |
` |
| 865 |
|
| 866 |
b := hugolib.Test(t, files) |
| 867 |
|
| 868 |
b.AssertFileContent("public/p1/index.html", "Single.") |
| 869 |
b.AssertFileContent("public/index.html", "All.") |
| 870 |
} |
| 871 |
|
| 872 |
func TestLayoutAllNested(t *testing.T) { |
| 873 |
t.Parallel() |
| 874 |
|
| 875 |
files := ` |
| 876 |
-- hugo.toml -- |
| 877 |
disableKinds = ['rss','sitemap','taxonomy','term'] |
| 878 |
-- content/s1/p1.md -- |
| 879 |
--- |
| 880 |
title: p1 |
| 881 |
--- |
| 882 |
-- content/s2/p2.md -- |
| 883 |
--- |
| 884 |
title: p2 |
| 885 |
--- |
| 886 |
-- layouts/single.html -- |
| 887 |
layouts/single.html |
| 888 |
-- layouts/list.html -- |
| 889 |
layouts/list.html |
| 890 |
-- layouts/s1/all.html -- |
| 891 |
layouts/s1/all.html |
| 892 |
` |
| 893 |
|
| 894 |
b := hugolib.Test(t, files) |
| 895 |
|
| 896 |
b.AssertFileContent("public/index.html", "layouts/list.html") |
| 897 |
b.AssertFileContent("public/s1/index.html", "layouts/s1/all.html") |
| 898 |
b.AssertFileContent("public/s1/p1/index.html", "layouts/s1/all.html") |
| 899 |
b.AssertFileContent("public/s2/index.html", "layouts/list.html") |
| 900 |
b.AssertFileContent("public/s2/p2/index.html", "layouts/single.html") |
| 901 |
} |
| 902 |
|
| 903 |
func TestPartialHTML(t *testing.T) { |
| 904 |
t.Parallel() |
| 905 |
|
| 906 |
files := ` |
| 907 |
-- hugo.toml -- |
| 908 |
-- layouts/all.html -- |
| 909 |
<html> |
| 910 |
<head> |
| 911 |
{{ partial "css.html" .}} |
| 912 |
</head> |
| 913 |
</html> |
| 914 |
-- layouts/_partials/css.html -- |
| 915 |
<link rel="stylesheet" href="/css/style.css"> |
| 916 |
` |
| 917 |
|
| 918 |
b := hugolib.Test(t, files) |
| 919 |
|
| 920 |
b.AssertFileContent("public/index.html", "<link rel=\"stylesheet\" href=\"/css/style.css\">") |
| 921 |
} |
| 922 |
|
| 923 |
func TestPartialPlainTextInHTML(t *testing.T) { |
| 924 |
t.Parallel() |
| 925 |
|
| 926 |
files := ` |
| 927 |
-- hugo.toml -- |
| 928 |
-- layouts/all.html -- |
| 929 |
<html> |
| 930 |
<head> |
| 931 |
{{ partial "mypartial.txt" . }} |
| 932 |
</head> |
| 933 |
</html> |
| 934 |
-- layouts/_partials/mypartial.txt -- |
| 935 |
My <div>partial</div>. |
| 936 |
` |
| 937 |
|
| 938 |
b := hugolib.Test(t, files) |
| 939 |
|
| 940 |
b.AssertFileContent("public/index.html", "My <div>partial</div>.") |
| 941 |
} |
| 942 |
|
| 943 |
// Issue #13593. |
| 944 |
func TestGoatAndNoGoat(t *testing.T) { |
| 945 |
t.Parallel() |
| 946 |
|
| 947 |
files := ` |
| 948 |
-- hugo.toml -- |
| 949 |
-- content/_index.md -- |
| 950 |
--- |
| 951 |
title: "Home" |
| 952 |
--- |
| 953 |
|
| 954 |
|
| 955 |
§§§ |
| 956 |
printf "Hello, world!" |
| 957 |
§§§ |
| 958 |
|
| 959 |
|
| 960 |
§§§ goat |
| 961 |
.---. .-. .-. .-. .---. |
| 962 |
| A +--->| 1 |<--->| 2 |<--->| 3 |<---+ B | |
| 963 |
'---' '-' '+' '+' '---' |
| 964 |
§§§ |
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
-- layouts/all.html -- |
| 969 |
{{ .Content }} |
| 970 |
|
| 971 |
` |
| 972 |
|
| 973 |
b := hugolib.Test(t, files) |
| 974 |
|
| 975 |
// Basic code block. |
| 976 |
b.AssertFileContent("public/index.html", "<code>printf "Hello, world!"\n</code>") |
| 977 |
|
| 978 |
// Goat code block. |
| 979 |
b.AssertFileContent("public/index.html", "Menlo,Lucida") |
| 980 |
} |
| 981 |
|
| 982 |
// Issue #13595. |
| 983 |
func TestGoatAndNoGoatCustomTemplate(t *testing.T) { |
| 984 |
t.Parallel() |
| 985 |
|
| 986 |
files := ` |
| 987 |
-- hugo.toml -- |
| 988 |
-- content/_index.md -- |
| 989 |
--- |
| 990 |
title: "Home" |
| 991 |
--- |
| 992 |
|
| 993 |
§§§ |
| 994 |
printf "Hello, world!" |
| 995 |
§§§ |
| 996 |
|
| 997 |
§§§ goat |
| 998 |
.---. .-. .-. .-. .---. |
| 999 |
| A +--->| 1 |<--->| 2 |<--->| 3 |<---+ B | |
| 1000 |
'---' '-' '+' '+' '---' |
| 1001 |
§§§ |
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
-- layouts/_markup/render-codeblock.html -- |
| 1006 |
_markup/render-codeblock.html |
| 1007 |
-- layouts/all.html -- |
| 1008 |
{{ .Content }} |
| 1009 |
|
| 1010 |
` |
| 1011 |
|
| 1012 |
b := hugolib.Test(t, files) |
| 1013 |
|
| 1014 |
// Basic code block. |
| 1015 |
b.AssertFileContent("public/index.html", "_markup/render-codeblock.html") |
| 1016 |
|
| 1017 |
// Goat code block. |
| 1018 |
b.AssertFileContent("public/index.html", "Menlo,Lucida") |
| 1019 |
} |
| 1020 |
|
| 1021 |
func TestGoatcustom(t *testing.T) { |
| 1022 |
t.Parallel() |
| 1023 |
|
| 1024 |
files := ` |
| 1025 |
-- hugo.toml -- |
| 1026 |
-- content/_index.md -- |
| 1027 |
--- |
| 1028 |
title: "Home" |
| 1029 |
--- |
| 1030 |
|
| 1031 |
§§§ |
| 1032 |
printf "Hello, world!" |
| 1033 |
§§§ |
| 1034 |
|
| 1035 |
§§§ goat |
| 1036 |
.---. .-. .-. .-. .---. |
| 1037 |
| A +--->| 1 |<--->| 2 |<--->| 3 |<---+ B | |
| 1038 |
'---' '-' '+' '+' '---' |
| 1039 |
§§§ |
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
-- layouts/_markup/render-codeblock.html -- |
| 1044 |
_markup/render-codeblock.html |
| 1045 |
-- layouts/_markup/render-codeblock-goat.html -- |
| 1046 |
_markup/render-codeblock-goat.html |
| 1047 |
-- layouts/all.html -- |
| 1048 |
{{ .Content }} |
| 1049 |
|
| 1050 |
` |
| 1051 |
|
| 1052 |
b := hugolib.Test(t, files) |
| 1053 |
|
| 1054 |
// Basic code block. |
| 1055 |
b.AssertFileContent("public/index.html", "_markup/render-codeblock.html") |
| 1056 |
|
| 1057 |
// Custom Goat code block. |
| 1058 |
b.AssertFileContent("public/index.html", "_markup/render-codeblock.html_markup/render-codeblock-goat.html") |
| 1059 |
} |
| 1060 |
|
| 1061 |
func TestLookupCodeblockIssue13651(t *testing.T) { |
| 1062 |
t.Parallel() |
| 1063 |
|
| 1064 |
files := ` |
| 1065 |
-- hugo.toml -- |
| 1066 |
-- layouts/all.html -- |
| 1067 |
{{ .Content }}| |
| 1068 |
-- layouts/_markup/render-codeblock-foo.html -- |
| 1069 |
render-codeblock-foo.html |
| 1070 |
-- content/_index.md -- |
| 1071 |
--- |
| 1072 |
--- |
| 1073 |
|
| 1074 |
§§§ |
| 1075 |
printf "Hello, world!" |
| 1076 |
§§§ |
| 1077 |
|
| 1078 |
§§§ foo |
| 1079 |
printf "Hello, world again!" |
| 1080 |
§§§ |
| 1081 |
` |
| 1082 |
|
| 1083 |
b := hugolib.Test(t, files) |
| 1084 |
|
| 1085 |
content := b.FileContent("public/index.html") |
| 1086 |
fooCount := strings.Count(content, "render-codeblock-foo.html") |
| 1087 |
b.Assert(fooCount, qt.Equals, 1) |
| 1088 |
} |
| 1089 |
|
| 1090 |
// Issue #13515 |
| 1091 |
func TestPrintPathWarningOnDotRemoval(t *testing.T) { |
| 1092 |
t.Parallel() |
| 1093 |
|
| 1094 |
files := ` |
| 1095 |
-- hugo.toml -- |
| 1096 |
baseURL = "https://example.com" |
| 1097 |
printPathWarnings = true |
| 1098 |
-- content/v0.124.0.md -- |
| 1099 |
-- content/v0.123.0.md -- |
| 1100 |
-- layouts/all.html -- |
| 1101 |
All. |
| 1102 |
-- layouts/single.html -- |
| 1103 |
{{ .Title }}| |
| 1104 |
` |
| 1105 |
|
| 1106 |
b := hugolib.Test(t, files, hugolib.TestOptWarn()) |
| 1107 |
|
| 1108 |
b.AssertLogContains("! Duplicate content path") |
| 1109 |
} |
| 1110 |
|
| 1111 |
// Issue #13577. |
| 1112 |
func TestPrintPathWarningOnInvalidMarkupFilename(t *testing.T) { |
| 1113 |
t.Parallel() |
| 1114 |
|
| 1115 |
files := ` |
| 1116 |
-- hugo.toml -- |
| 1117 |
-- layouts/all.html -- |
| 1118 |
All. |
| 1119 |
-- layouts/_markup/sitemap.xml -- |
| 1120 |
` |
| 1121 |
b := hugolib.Test(t, files, hugolib.TestOptWarn()) |
| 1122 |
|
| 1123 |
b.AssertLogContains("unrecognized render hook") |
| 1124 |
} |
| 1125 |
|
| 1126 |
func TestLayoutNotFound(t *testing.T) { |
| 1127 |
t.Parallel() |
| 1128 |
|
| 1129 |
files := ` |
| 1130 |
-- hugo.toml -- |
| 1131 |
-- layouts/single.html -- |
| 1132 |
Single. |
| 1133 |
` |
| 1134 |
b := hugolib.Test(t, files, hugolib.TestOptWarn()) |
| 1135 |
b.AssertLogContains("WARN found no layout file for \"html\" for kind \"home\"") |
| 1136 |
} |
| 1137 |
|
| 1138 |
func TestLayoutOverrideThemeWhenThemeOnOldFormatIssue13715(t *testing.T) { |
| 1139 |
t.Parallel() |
| 1140 |
|
| 1141 |
files := ` |
| 1142 |
-- hugo.toml -- |
| 1143 |
theme = "mytheme" |
| 1144 |
-- layouts/list.html -- |
| 1145 |
layouts/list.html |
| 1146 |
-- themes/mytheme/layouts/_default/list.html -- |
| 1147 |
mytheme/layouts/_default/list.html |
| 1148 |
|
| 1149 |
` |
| 1150 |
|
| 1151 |
b := hugolib.Test(t, files) |
| 1152 |
b.AssertFileContent("public/index.html", "layouts/list.html") |
| 1153 |
} |
| 1154 |
|
| 1155 |
func BenchmarkExecuteWithContext(b *testing.B) { |
| 1156 |
files := ` |
| 1157 |
-- hugo.toml -- |
| 1158 |
disableKinds = ["taxonomy", "term", "home"] |
| 1159 |
-- layouts/all.html -- |
| 1160 |
{{ .Title }}| |
| 1161 |
{{ partial "p1.html" . }} |
| 1162 |
-- layouts/_partials/p1.html -- |
| 1163 |
p1. |
| 1164 |
{{ partial "p2.html" . }} |
| 1165 |
{{ partial "p2.html" . }} |
| 1166 |
{{ partial "p3.html" . }} |
| 1167 |
{{ partial "p2.html" . }} |
| 1168 |
{{ partial "p2.html" . }} |
| 1169 |
{{ partial "p2.html" . }} |
| 1170 |
{{ partial "p3.html" . }} |
| 1171 |
-- layouts/_partials/p2.html -- |
| 1172 |
{{ partial "p3.html" . }} |
| 1173 |
-- layouts/_partials/p3.html -- |
| 1174 |
p3 |
| 1175 |
-- content/p1.md -- |
| 1176 |
` |
| 1177 |
|
| 1178 |
bb := hugolib.Test(b, files) |
| 1179 |
|
| 1180 |
store := bb.H.TemplateStore |
| 1181 |
|
| 1182 |
ti := store.LookupByPath("/all.html") |
| 1183 |
bb.Assert(ti, qt.Not(qt.IsNil)) |
| 1184 |
p := bb.H.Sites[0].RegularPages()[0] |
| 1185 |
bb.Assert(p, qt.Not(qt.IsNil)) |
| 1186 |
|
| 1187 |
for b.Loop() { |
| 1188 |
err := store.ExecuteWithContext(context.Background(), ti, io.Discard, p) |
| 1189 |
if err != nil { |
| 1190 |
b.Fatal(err) |
| 1191 |
} |
| 1192 |
} |
| 1193 |
} |
| 1194 |
|
| 1195 |
func BenchmarkLookupPartial(b *testing.B) { |
| 1196 |
files := ` |
| 1197 |
-- hugo.toml -- |
| 1198 |
disableKinds = ["taxonomy", "term", "home"] |
| 1199 |
-- layouts/all.html -- |
| 1200 |
{{ .Title }}| |
| 1201 |
-- layouts/_partials/p1.html -- |
| 1202 |
-- layouts/_partials/p2.html -- |
| 1203 |
-- layouts/_partials/p2.json -- |
| 1204 |
-- layouts/_partials/p3.html -- |
| 1205 |
` |
| 1206 |
bb := hugolib.Test(b, files) |
| 1207 |
|
| 1208 |
store := bb.H.TemplateStore |
| 1209 |
|
| 1210 |
for b.Loop() { |
| 1211 |
fi := store.LookupPartial("p3.html") |
| 1212 |
if fi == nil { |
| 1213 |
b.Fatal("not found") |
| 1214 |
} |
| 1215 |
} |
| 1216 |
} |
| 1217 |
|
| 1218 |
// Implemented by pageOutput. |
| 1219 |
type getDescriptorProvider interface { |
| 1220 |
GetInternalTemplateBasePathAndDescriptor() (string, tplimpl.TemplateDescriptor) |
| 1221 |
} |
| 1222 |
|
| 1223 |
func BenchmarkLookupShortcode(b *testing.B) { |
| 1224 |
files := ` |
| 1225 |
-- hugo.toml -- |
| 1226 |
disableKinds = ["taxonomy", "term", "home"] |
| 1227 |
-- content/toplevelpage.md -- |
| 1228 |
-- content/a/b/c/nested.md -- |
| 1229 |
-- layouts/all.html -- |
| 1230 |
{{ .Title }}| |
| 1231 |
-- layouts/_shortcodes/s.html -- |
| 1232 |
s1. |
| 1233 |
-- layouts/_shortcodes/a/b/s.html -- |
| 1234 |
s2. |
| 1235 |
|
| 1236 |
` |
| 1237 |
bb := hugolib.Test(b, files) |
| 1238 |
store := bb.H.TemplateStore |
| 1239 |
|
| 1240 |
runOne := func(p page.Page) { |
| 1241 |
pth, desc := p.(getDescriptorProvider).GetInternalTemplateBasePathAndDescriptor() |
| 1242 |
q := tplimpl.TemplateQuery{ |
| 1243 |
Path: pth, |
| 1244 |
Name: "s", |
| 1245 |
Category: tplimpl.CategoryShortcode, |
| 1246 |
Desc: desc, |
| 1247 |
} |
| 1248 |
v, err := store.LookupShortcode(q) |
| 1249 |
if v == nil || err != nil { |
| 1250 |
b.Fatal("not found") |
| 1251 |
} |
| 1252 |
} |
| 1253 |
|
| 1254 |
b.Run("toplevelpage", func(b *testing.B) { |
| 1255 |
toplevelpage, _ := bb.H.Sites[0].GetPage("/toplevelpage") |
| 1256 |
for b.Loop() { |
| 1257 |
runOne(toplevelpage) |
| 1258 |
} |
| 1259 |
}) |
| 1260 |
|
| 1261 |
b.Run("nestedpage", func(b *testing.B) { |
| 1262 |
toplevelpage, _ := bb.H.Sites[0].GetPage("/a/b/c/nested") |
| 1263 |
for b.Loop() { |
| 1264 |
runOne(toplevelpage) |
| 1265 |
} |
| 1266 |
}) |
| 1267 |
} |
| 1268 |
|
| 1269 |
func TestStandardLayoutInFrontMatter13588(t *testing.T) { |
| 1270 |
t.Parallel() |
| 1271 |
|
| 1272 |
files := ` |
| 1273 |
-- hugo.toml -- |
| 1274 |
disableKinds = ['home','page','rss','sitemap','taxonomy','term'] |
| 1275 |
-- content/s1/_index.md -- |
| 1276 |
--- |
| 1277 |
title: s1 |
| 1278 |
--- |
| 1279 |
-- content/s2/_index.md -- |
| 1280 |
--- |
| 1281 |
title: s2 |
| 1282 |
layout: list |
| 1283 |
--- |
| 1284 |
-- content/s3/_index.md -- |
| 1285 |
--- |
| 1286 |
title: s3 |
| 1287 |
layout: single |
| 1288 |
--- |
| 1289 |
-- layouts/list.html -- |
| 1290 |
list.html |
| 1291 |
-- layouts/section.html -- |
| 1292 |
section.html |
| 1293 |
-- layouts/single.html -- |
| 1294 |
single.html |
| 1295 |
` |
| 1296 |
|
| 1297 |
b := hugolib.Test(t, files) |
| 1298 |
|
| 1299 |
b.AssertFileContent("public/s1/index.html", "section.html") |
| 1300 |
b.AssertFileContent("public/s2/index.html", "list.html") // fail |
| 1301 |
b.AssertFileContent("public/s3/index.html", "single.html") // fail |
| 1302 |
} |
| 1303 |
|
| 1304 |
func TestIssue13605(t *testing.T) { |
| 1305 |
t.Parallel() |
| 1306 |
|
| 1307 |
files := ` |
| 1308 |
-- hugo.toml -- |
| 1309 |
disableKinds = ['home','rss','section','sitemap','taxonomy','term'] |
| 1310 |
-- content/s1/p1.md -- |
| 1311 |
--- |
| 1312 |
title: p1 |
| 1313 |
--- |
| 1314 |
{{< sc >}} |
| 1315 |
-- layouts/s1/_shortcodes/sc.html -- |
| 1316 |
layouts/s1/_shortcodes/sc.html |
| 1317 |
-- layouts/single.html -- |
| 1318 |
{{ .Content }} |
| 1319 |
` |
| 1320 |
|
| 1321 |
b := hugolib.Test(t, files) |
| 1322 |
|
| 1323 |
b.AssertFileContent("public/s1/p1/index.html", "layouts/s1/_shortcodes/sc.html") |
| 1324 |
} |
| 1325 |
|
| 1326 |
func TestSkipDotFiles(t *testing.T) { |
| 1327 |
t.Parallel() |
| 1328 |
|
| 1329 |
files := ` |
| 1330 |
-- hugo.toml -- |
| 1331 |
-- layouts/all.html -- |
| 1332 |
All. |
| 1333 |
-- layouts/.DS_Store -- |
| 1334 |
{{ foo }} |
| 1335 |
` |
| 1336 |
|
| 1337 |
// Just make sure it doesn't fail. |
| 1338 |
hugolib.Test(t, files) |
| 1339 |
} |
| 1340 |
|
| 1341 |
func TestPartialsLangIssue13612(t *testing.T) { |
| 1342 |
t.Parallel() |
| 1343 |
|
| 1344 |
files := ` |
| 1345 |
-- hugo.toml -- |
| 1346 |
disableKinds = ['page','section','sitemap','taxonomy','term'] |
| 1347 |
|
| 1348 |
defaultContentLanguage = 'ru' |
| 1349 |
defaultContentLanguageInSubdir = true |
| 1350 |
|
| 1351 |
[languages.ru] |
| 1352 |
weight = 1 |
| 1353 |
|
| 1354 |
[languages.en] |
| 1355 |
weight = 2 |
| 1356 |
|
| 1357 |
[outputs] |
| 1358 |
home = ['html','rss'] |
| 1359 |
|
| 1360 |
-- layouts/_partials/comment.en.html -- |
| 1361 |
layouts/_partials/comment.en.html |
| 1362 |
-- layouts/_partials/comment.en.xml -- |
| 1363 |
layouts/_partials/comment.en.xml |
| 1364 |
-- layouts/_partials/comment.ru.html -- |
| 1365 |
layouts/_partials/comment.ru.html |
| 1366 |
-- layouts/_partials/comment.ru.xml -- |
| 1367 |
layouts/_partials/comment.ru.xml |
| 1368 |
-- layouts/home.html -- |
| 1369 |
{{ partial (print "comment." (default "ru" .Lang) ".html") . }} |
| 1370 |
-- layouts/home.rss.xml -- |
| 1371 |
{{ partial (print "comment." (default "ru" .Lang) ".xml") . }} |
| 1372 |
` |
| 1373 |
|
| 1374 |
b := hugolib.Test(t, files) |
| 1375 |
|
| 1376 |
b.AssertFileContent("public/en/index.html", "layouts/_partials/comment.en.html") |
| 1377 |
b.AssertFileContent("public/en/index.xml", "layouts/_partials/comment.en.xml") // fail |
| 1378 |
b.AssertFileContent("public/ru/index.html", "layouts/_partials/comment.ru.html") // fail |
| 1379 |
b.AssertFileContent("public/ru/index.xml", "layouts/_partials/comment.ru.xml") // fail |
| 1380 |
} |
| 1381 |
|
| 1382 |
func TestLayoutIssue13628(t *testing.T) { |
| 1383 |
t.Parallel() |
| 1384 |
|
| 1385 |
files := ` |
| 1386 |
-- hugo.toml -- |
| 1387 |
disableKinds = ['home','rss','sitemap','taxonomy','term'] |
| 1388 |
-- content/p1.md -- |
| 1389 |
--- |
| 1390 |
title: p1 |
| 1391 |
layout: foo |
| 1392 |
--- |
| 1393 |
-- layouts/single.html -- |
| 1394 |
layouts/single.html |
| 1395 |
-- layouts/list.html -- |
| 1396 |
layouts/list.html |
| 1397 |
` |
| 1398 |
|
| 1399 |
for range 5 { |
| 1400 |
|
| 1401 |
b := hugolib.Test(t, files) |
| 1402 |
|
| 1403 |
b.AssertFileContent("public/p1/index.html", "layouts/single.html") |
| 1404 |
} |
| 1405 |
} |
| 1406 |
|
| 1407 |
func TestTemplateLoop(t *testing.T) { |
| 1408 |
t.Parallel() |
| 1409 |
htesting.SkipSlowTestUnlessCI(t) |
| 1410 |
|
| 1411 |
files := ` |
| 1412 |
-- hugo.toml -- |
| 1413 |
-- layouts/_partials/p.html -- |
| 1414 |
p: {{ partial "p.html" . }} |
| 1415 |
-- layouts/all.html -- |
| 1416 |
{{ partial "p.html" . }} |
| 1417 |
|
| 1418 |
` |
| 1419 |
b, err := hugolib.TestE(t, files) |
| 1420 |
b.Assert(err, qt.IsNotNil) |
| 1421 |
b.Assert(err.Error(), qt.Contains, "error calling partial: maximum template call stack size exceeded") |
| 1422 |
} |
| 1423 |
|
| 1424 |
func TestIssue13630(t *testing.T) { |
| 1425 |
t.Parallel() |
| 1426 |
|
| 1427 |
files := ` |
| 1428 |
-- hugo.toml -- |
| 1429 |
disableKinds = ['rss','sitemap'] |
| 1430 |
-- content/p1.md -- |
| 1431 |
--- |
| 1432 |
title: p1 |
| 1433 |
layout: foo |
| 1434 |
--- |
| 1435 |
-- layouts/list.html -- |
| 1436 |
layouts/list.html |
| 1437 |
-- layouts/taxononmy.html.html -- |
| 1438 |
layouts/taxononmy.html.html |
| 1439 |
` |
| 1440 |
|
| 1441 |
var b *hugolib.IntegrationTestBuilder |
| 1442 |
|
| 1443 |
for range 3 { |
| 1444 |
b = hugolib.Test(t, files) |
| 1445 |
b.AssertFileExists("public/p1/index.html", false) |
| 1446 |
} |
| 1447 |
} |
| 1448 |
|
| 1449 |
func TestTemplateLoopBlogVsBlogrollIssue13672(t *testing.T) { |
| 1450 |
t.Parallel() |
| 1451 |
files := ` |
| 1452 |
-- hugo.toml -- |
| 1453 |
-- layouts/blog/_shortcodes/myshortcode.html -- |
| 1454 |
layouts/blog/_shortcodes/myshortcode.html |
| 1455 |
-- layouts/blog/baseof.html -- |
| 1456 |
blog/baseof.html {{ block "main" . }}{{ end }} |
| 1457 |
-- layouts/blog/all.html -- |
| 1458 |
{{ define "main" }}blog/all.html|{{ .Content}}{{ end }} |
| 1459 |
-- layouts/blogroll/_shortcodes/myshortcode.html -- |
| 1460 |
layouts/blogroll/myshortcode.html |
| 1461 |
-- layouts/blogroll/baseof.html -- |
| 1462 |
{{ block "main" . }}blogroll/baseof.html{{ end }} |
| 1463 |
-- layouts/blogroll/all.html -- |
| 1464 |
{{ define "main" }}blogroll/all.html|{{ .Content}}{{ end }} |
| 1465 |
-- content/blog/p1.md -- |
| 1466 |
--- |
| 1467 |
title: p1 |
| 1468 |
--- |
| 1469 |
{{< myshortcode >}} |
| 1470 |
-- content/blogroll/p1.md -- |
| 1471 |
--- |
| 1472 |
title: p1 |
| 1473 |
--- |
| 1474 |
{{< myshortcode >}} |
| 1475 |
` |
| 1476 |
|
| 1477 |
b := hugolib.Test(t, files) |
| 1478 |
|
| 1479 |
b.AssertFileContent("public/blog/p1/index.html", "blog/baseof.html blog/all.html|layouts/blog/_shortcodes/myshortcode.html") |
| 1480 |
b.AssertFileContent("public/blogroll/p1/index.html", "blogroll/all.html|layouts/blogroll/myshortcode.html") |
| 1481 |
} |
| 1482 |
|
| 1483 |
// See issue #13668. |
| 1484 |
func TestPartialPlainTextVsHTML(t *testing.T) { |
| 1485 |
t.Parallel() |
| 1486 |
|
| 1487 |
/* |
| 1488 |
Note that in the below, there's no output format named txt, |
| 1489 |
so the isPlainText is fetched from the only output format with that extension. |
| 1490 |
*/ |
| 1491 |
files := ` |
| 1492 |
-- hugo.toml -- |
| 1493 |
-- layouts/_partials/myhtml.html -- |
| 1494 |
<div>myhtml</div> |
| 1495 |
-- layouts/_partials/mytext.txt -- |
| 1496 |
<div>mytext</div> |
| 1497 |
-- layouts/all.html -- |
| 1498 |
myhtml: {{ partial "myhtml.html" . }} |
| 1499 |
mytext: {{ partial "mytext.txt" . }} |
| 1500 |
mytexts|safeHTML: {{ partial "mytext.txt" . | safeHTML }} |
| 1501 |
` |
| 1502 |
|
| 1503 |
b := hugolib.Test(t, files) |
| 1504 |
|
| 1505 |
b.AssertFileContent("public/index.html", |
| 1506 |
"myhtml: <div>myhtml</div>", |
| 1507 |
"mytext: <div>mytext</div>", |
| 1508 |
"mytexts|safeHTML: <div>mytext</div>", |
| 1509 |
) |
| 1510 |
} |
| 1511 |
|
| 1512 |
func TestIssue13351(t *testing.T) { |
| 1513 |
t.Parallel() |
| 1514 |
|
| 1515 |
files := ` |
| 1516 |
-- hugo.toml -- |
| 1517 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 1518 |
[outputs] |
| 1519 |
home = ['html','json'] |
| 1520 |
[outputFormats.html] |
| 1521 |
weight = 1 |
| 1522 |
[outputFormats.json] |
| 1523 |
weight = 2 |
| 1524 |
-- content/_index.md -- |
| 1525 |
--- |
| 1526 |
title: home |
| 1527 |
--- |
| 1528 |
a|b |
| 1529 |
:--|:-- |
| 1530 |
1|2 |
| 1531 |
-- layouts/home.html -- |
| 1532 |
{{ .Content }} |
| 1533 |
-- layouts/index.json -- |
| 1534 |
{{ .Content }} |
| 1535 |
` |
| 1536 |
|
| 1537 |
b := hugolib.Test(t, files) |
| 1538 |
b.AssertFileContent("public/index.html", "<table>") |
| 1539 |
b.AssertFileContent("public/index.json", "<table>") |
| 1540 |
|
| 1541 |
f := strings.ReplaceAll(files, "weight = 1", "weight = 0") |
| 1542 |
b = hugolib.Test(t, f) |
| 1543 |
b.AssertFileContent("public/index.html", "<table>") |
| 1544 |
b.AssertFileContent("public/index.json", "<table>") |
| 1545 |
|
| 1546 |
f = strings.ReplaceAll(files, "weight = 1", "") |
| 1547 |
b = hugolib.Test(t, f) |
| 1548 |
b.AssertFileContent("public/index.html", "<table>") |
| 1549 |
b.AssertFileContent("public/index.json", "<table>") |
| 1550 |
} |
| 1551 |
|
| 1552 |
func TestPageKindIssue13868(t *testing.T) { |
| 1553 |
t.Parallel() |
| 1554 |
|
| 1555 |
files := ` |
| 1556 |
-- hugo.toml -- |
| 1557 |
disableKinds = ['home','rss','section','sitemap','taxonomy','term'] |
| 1558 |
-- content/p1.md -- |
| 1559 |
--- |
| 1560 |
title: p1 |
| 1561 |
--- |
| 1562 |
-- layouts/page.html -- |
| 1563 |
layouts/page.html |
| 1564 |
-- layouts/p1/page.html -- |
| 1565 |
layouts/p1/page.html |
| 1566 |
` |
| 1567 |
|
| 1568 |
b := hugolib.Test(t, files) |
| 1569 |
|
| 1570 |
b.AssertFileContent("public/p1/index.html", "layouts/p1/page.html") |
| 1571 |
} |