| 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 hugolib |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"os" |
| 19 |
"testing" |
| 20 |
|
| 21 |
qt "github.com/frankban/quicktest" |
| 22 |
) |
| 23 |
|
| 24 |
func TestDraftAndFutureRender(t *testing.T) { |
| 25 |
t.Parallel() |
| 26 |
|
| 27 |
basefiles := ` |
| 28 |
-- content/sect/doc1.md -- |
| 29 |
--- |
| 30 |
title: doc1 |
| 31 |
draft: true |
| 32 |
publishdate: "2414-05-29" |
| 33 |
--- |
| 34 |
# doc1 |
| 35 |
*some content* |
| 36 |
-- content/sect/doc2.md -- |
| 37 |
--- |
| 38 |
title: doc2 |
| 39 |
draft: true |
| 40 |
publishdate: "2012-05-29" |
| 41 |
--- |
| 42 |
# doc2 |
| 43 |
*some content* |
| 44 |
-- content/sect/doc3.md -- |
| 45 |
--- |
| 46 |
title: doc3 |
| 47 |
draft: false |
| 48 |
publishdate: "2414-05-29" |
| 49 |
--- |
| 50 |
# doc3 |
| 51 |
*some content* |
| 52 |
-- content/sect/doc4.md -- |
| 53 |
--- |
| 54 |
title: doc4 |
| 55 |
draft: false |
| 56 |
publishdate: "2012-05-29" |
| 57 |
--- |
| 58 |
# doc4 |
| 59 |
*some content* |
| 60 |
` |
| 61 |
|
| 62 |
t.Run("defaults", func(t *testing.T) { |
| 63 |
t.Parallel() |
| 64 |
files := ` |
| 65 |
-- hugo.toml -- |
| 66 |
baseURL = "http://auth/bub" |
| 67 |
` + basefiles |
| 68 |
|
| 69 |
b := Test(t, files) |
| 70 |
b.Assert(len(b.H.Sites[0].RegularPages()), qt.Equals, 1) |
| 71 |
}) |
| 72 |
|
| 73 |
t.Run("buildDrafts", func(t *testing.T) { |
| 74 |
t.Parallel() |
| 75 |
files := ` |
| 76 |
-- hugo.toml -- |
| 77 |
baseURL = "http://auth/bub" |
| 78 |
buildDrafts = true |
| 79 |
` + basefiles |
| 80 |
b := Test(t, files) |
| 81 |
b.Assert(len(b.H.Sites[0].RegularPages()), qt.Equals, 2) |
| 82 |
}) |
| 83 |
|
| 84 |
t.Run("buildFuture", func(t *testing.T) { |
| 85 |
t.Parallel() |
| 86 |
files := ` |
| 87 |
-- hugo.toml -- |
| 88 |
baseURL = "http://auth/bub" |
| 89 |
buildFuture = true |
| 90 |
` + basefiles |
| 91 |
b := Test(t, files) |
| 92 |
b.Assert(len(b.H.Sites[0].RegularPages()), qt.Equals, 2) |
| 93 |
}) |
| 94 |
|
| 95 |
t.Run("buildDrafts and buildFuture", func(t *testing.T) { |
| 96 |
t.Parallel() |
| 97 |
files := ` |
| 98 |
-- hugo.toml -- |
| 99 |
baseURL = "http://auth/bub" |
| 100 |
buildDrafts = true |
| 101 |
buildFuture = true |
| 102 |
` + basefiles |
| 103 |
b := Test(t, files) |
| 104 |
b.Assert(len(b.H.Sites[0].RegularPages()), qt.Equals, 4) |
| 105 |
}) |
| 106 |
} |
| 107 |
|
| 108 |
func TestFutureExpirationRender(t *testing.T) { |
| 109 |
t.Parallel() |
| 110 |
|
| 111 |
files := ` |
| 112 |
-- hugo.toml -- |
| 113 |
baseURL = "http://auth/bub" |
| 114 |
-- content/sect/doc3.md -- |
| 115 |
--- |
| 116 |
title: doc1 |
| 117 |
expirydate: "2400-05-29" |
| 118 |
--- |
| 119 |
# doc1 |
| 120 |
*some content* |
| 121 |
-- content/sect/doc4.md -- |
| 122 |
--- |
| 123 |
title: doc2 |
| 124 |
expirydate: "2000-05-29" |
| 125 |
--- |
| 126 |
# doc2 |
| 127 |
*some content* |
| 128 |
` |
| 129 |
b := Test(t, files) |
| 130 |
|
| 131 |
b.Assert(len(b.H.Sites[0].RegularPages()), qt.Equals, 1) |
| 132 |
b.Assert(b.H.Sites[0].RegularPages()[0].Title(), qt.Equals, "doc1") |
| 133 |
} |
| 134 |
|
| 135 |
func TestLastChange(t *testing.T) { |
| 136 |
t.Parallel() |
| 137 |
|
| 138 |
files := ` |
| 139 |
-- hugo.toml -- |
| 140 |
baseURL = "http://example.com/" |
| 141 |
-- content/sect/doc1.md -- |
| 142 |
--- |
| 143 |
title: doc1 |
| 144 |
weight: 1 |
| 145 |
date: 2014-05-29 |
| 146 |
--- |
| 147 |
# doc1 |
| 148 |
*some content* |
| 149 |
-- content/sect/doc2.md -- |
| 150 |
--- |
| 151 |
title: doc2 |
| 152 |
weight: 2 |
| 153 |
date: 2015-05-29 |
| 154 |
--- |
| 155 |
# doc2 |
| 156 |
*some content* |
| 157 |
-- content/sect/doc3.md -- |
| 158 |
--- |
| 159 |
title: doc3 |
| 160 |
weight: 3 |
| 161 |
date: 2017-05-29 |
| 162 |
--- |
| 163 |
# doc3 |
| 164 |
*some content* |
| 165 |
-- content/sect/doc4.md -- |
| 166 |
--- |
| 167 |
title: doc4 |
| 168 |
weight: 4 |
| 169 |
date: 2016-05-29 |
| 170 |
--- |
| 171 |
# doc4 |
| 172 |
*some content* |
| 173 |
-- content/sect/doc5.md -- |
| 174 |
--- |
| 175 |
title: doc5 |
| 176 |
weight: 3 |
| 177 |
--- |
| 178 |
# doc5 |
| 179 |
*some content* |
| 180 |
` |
| 181 |
b := Test(t, files, TestOptSkipRender()) |
| 182 |
|
| 183 |
b.Assert(b.H.Sites[0].Lastmod().IsZero(), qt.Equals, false) |
| 184 |
b.Assert(b.H.Sites[0].Lastmod().Year(), qt.Equals, 2017) |
| 185 |
} |
| 186 |
|
| 187 |
// Issue #_index |
| 188 |
func TestPageWithUnderScoreIndexInFilename(t *testing.T) { |
| 189 |
t.Parallel() |
| 190 |
|
| 191 |
files := ` |
| 192 |
-- hugo.toml -- |
| 193 |
baseURL = "http://example.com/" |
| 194 |
-- content/sect/my_index_file.md -- |
| 195 |
--- |
| 196 |
title: doc1 |
| 197 |
weight: 1 |
| 198 |
date: 2014-05-29 |
| 199 |
--- |
| 200 |
# doc1 |
| 201 |
*some content* |
| 202 |
` |
| 203 |
b := Test(t, files, TestOptSkipRender()) |
| 204 |
|
| 205 |
b.Assert(len(b.H.Sites[0].RegularPages()), qt.Equals, 1) |
| 206 |
} |
| 207 |
|
| 208 |
// Issue #939 |
| 209 |
// Issue #1923 |
| 210 |
func TestShouldAlwaysHaveUglyURLs(t *testing.T) { |
| 211 |
t.Parallel() |
| 212 |
|
| 213 |
basefiles := ` |
| 214 |
-- layouts/home.html -- |
| 215 |
Home Sweet {{ if.IsHome }}Home{{ end }}. |
| 216 |
-- layouts/single.html -- |
| 217 |
{{.Content}}{{ if.IsHome }}This is not home!{{ end }} |
| 218 |
-- layouts/404.html -- |
| 219 |
Page Not Found.{{ if.IsHome }}This is not home!{{ end }} |
| 220 |
-- layouts/rss.xml -- |
| 221 |
<root>RSS</root> |
| 222 |
-- layouts/sitemap.xml -- |
| 223 |
<root>SITEMAP</root> |
| 224 |
-- content/sect/doc1.md -- |
| 225 |
--- |
| 226 |
markup: markdown |
| 227 |
--- |
| 228 |
# title |
| 229 |
some *content* |
| 230 |
-- content/sect/doc2.md -- |
| 231 |
--- |
| 232 |
url: /ugly.html |
| 233 |
markup: markdown |
| 234 |
--- |
| 235 |
# title |
| 236 |
doc2 *content* |
| 237 |
` |
| 238 |
|
| 239 |
t.Run("uglyURLs=true", func(t *testing.T) { |
| 240 |
t.Parallel() |
| 241 |
files := ` |
| 242 |
-- hugo.toml -- |
| 243 |
baseURL = "http://auth/bub" |
| 244 |
uglyURLs = true |
| 245 |
` + basefiles |
| 246 |
b := Test(t, files) |
| 247 |
|
| 248 |
b.AssertFileContent("public/index.html", "Home Sweet Home.") |
| 249 |
b.AssertFileContent("public/sect/doc1.html", "<h1 id=\"title\">title</h1>\n<p>some <em>content</em></p>\n") |
| 250 |
b.AssertFileContent("public/404.html", "Page Not Found.") |
| 251 |
b.AssertFileContent("public/index.xml", "<root>RSS</root>") |
| 252 |
b.AssertFileContent("public/sitemap.xml", "<root>SITEMAP</root>") |
| 253 |
b.AssertFileContent("public/ugly.html", "<h1 id=\"title\">title</h1>\n<p>doc2 <em>content</em></p>\n") |
| 254 |
|
| 255 |
for _, p := range b.H.Sites[0].RegularPages() { |
| 256 |
b.Assert(p.IsHome(), qt.Equals, false) |
| 257 |
} |
| 258 |
}) |
| 259 |
|
| 260 |
t.Run("uglyURLs=false", func(t *testing.T) { |
| 261 |
t.Parallel() |
| 262 |
files := ` |
| 263 |
-- hugo.toml -- |
| 264 |
baseURL = "http://auth/bub" |
| 265 |
uglyURLs = false |
| 266 |
` + basefiles |
| 267 |
b := Test(t, files) |
| 268 |
|
| 269 |
b.AssertFileContent("public/index.html", "Home Sweet Home.") |
| 270 |
b.AssertFileContent("public/sect/doc1/index.html", "<h1 id=\"title\">title</h1>\n<p>some <em>content</em></p>\n") |
| 271 |
b.AssertFileContent("public/404.html", "Page Not Found.") |
| 272 |
b.AssertFileContent("public/index.xml", "<root>RSS</root>") |
| 273 |
b.AssertFileContent("public/sitemap.xml", "<root>SITEMAP</root>") |
| 274 |
b.AssertFileContent("public/ugly.html", "<h1 id=\"title\">title</h1>\n<p>doc2 <em>content</em></p>\n") |
| 275 |
|
| 276 |
for _, p := range b.H.Sites[0].RegularPages() { |
| 277 |
b.Assert(p.IsHome(), qt.Equals, false) |
| 278 |
} |
| 279 |
}) |
| 280 |
} |
| 281 |
|
| 282 |
func TestMainSectionsMoveToSite(t *testing.T) { |
| 283 |
t.Run("defined in params", func(t *testing.T) { |
| 284 |
t.Parallel() |
| 285 |
|
| 286 |
files := ` |
| 287 |
-- hugo.toml -- |
| 288 |
disableKinds = ['RSS','sitemap','taxonomy','term'] |
| 289 |
[params] |
| 290 |
mainSections=["a", "b"] |
| 291 |
-- content/mysect/page1.md -- |
| 292 |
-- layouts/home.html -- |
| 293 |
{{/* Behaviour before Hugo 0.112.0. */}} |
| 294 |
MainSections Params: {{ site.Params.mainSections }}| |
| 295 |
MainSections Site method: {{ site.MainSections }}| |
| 296 |
|
| 297 |
|
| 298 |
` |
| 299 |
|
| 300 |
b := Test(t, files) |
| 301 |
|
| 302 |
b.AssertFileContent("public/index.html", ` |
| 303 |
MainSections Params: [a b]| |
| 304 |
MainSections Site method: [a b]| |
| 305 |
`) |
| 306 |
}) |
| 307 |
|
| 308 |
t.Run("defined in top level config", func(t *testing.T) { |
| 309 |
t.Parallel() |
| 310 |
|
| 311 |
files := ` |
| 312 |
-- hugo.toml -- |
| 313 |
disableKinds = ['RSS','sitemap','taxonomy','term'] |
| 314 |
mainSections=["a", "b"] |
| 315 |
[params] |
| 316 |
[params.sub] |
| 317 |
mainSections=["c", "d"] |
| 318 |
-- content/mysect/page1.md -- |
| 319 |
-- layouts/home.html -- |
| 320 |
{{/* Behaviour before Hugo 0.112.0. */}} |
| 321 |
MainSections Params: {{ site.Params.mainSections }}| |
| 322 |
MainSections Param sub: {{ site.Params.sub.mainSections }}| |
| 323 |
MainSections Site method: {{ site.MainSections }}| |
| 324 |
|
| 325 |
|
| 326 |
` |
| 327 |
|
| 328 |
b := Test(t, files) |
| 329 |
|
| 330 |
b.AssertFileContent("public/index.html", ` |
| 331 |
MainSections Params: [a b]| |
| 332 |
MainSections Param sub: [c d]| |
| 333 |
MainSections Site method: [a b]| |
| 334 |
`) |
| 335 |
}) |
| 336 |
|
| 337 |
t.Run("guessed from pages", func(t *testing.T) { |
| 338 |
t.Parallel() |
| 339 |
|
| 340 |
files := ` |
| 341 |
-- hugo.toml -- |
| 342 |
disableKinds = ['RSS','sitemap','taxonomy','term'] |
| 343 |
-- content/mysect/page1.md -- |
| 344 |
-- layouts/home.html -- |
| 345 |
MainSections Params: {{ site.Params.mainSections }}| |
| 346 |
MainSections Site method: {{ site.MainSections }}| |
| 347 |
|
| 348 |
|
| 349 |
` |
| 350 |
|
| 351 |
b := Test(t, files) |
| 352 |
|
| 353 |
b.AssertFileContent("public/index.html", ` |
| 354 |
MainSections Params: [mysect]| |
| 355 |
MainSections Site method: [mysect]| |
| 356 |
`) |
| 357 |
}) |
| 358 |
} |
| 359 |
|
| 360 |
func TestRelRefWithTrailingSlash(t *testing.T) { |
| 361 |
files := ` |
| 362 |
-- hugo.toml -- |
| 363 |
-- content/docs/5.3/examples/_index.md -- |
| 364 |
--- |
| 365 |
title: "Examples" |
| 366 |
--- |
| 367 |
-- content/_index.md -- |
| 368 |
--- |
| 369 |
title: "Home" |
| 370 |
--- |
| 371 |
|
| 372 |
Examples: {{< relref "/docs/5.3/examples/" >}} |
| 373 |
-- layouts/home.html -- |
| 374 |
Content: {{ .Content }}| |
| 375 |
` |
| 376 |
|
| 377 |
b := Test(t, files) |
| 378 |
|
| 379 |
b.AssertFileContent("public/index.html", "Examples: /docs/5.3/examples/") |
| 380 |
} |
| 381 |
|
| 382 |
// https://github.com/gohugoio/hugo/issues/6952 |
| 383 |
func TestRefIssues(t *testing.T) { |
| 384 |
t.Parallel() |
| 385 |
|
| 386 |
files := ` |
| 387 |
-- hugo.toml -- |
| 388 |
baseURL = "http://example.com" |
| 389 |
-- content/post/b1/index.md -- |
| 390 |
--- |
| 391 |
title: pb1 |
| 392 |
--- |
| 393 |
Ref: {{< ref "b2" >}} |
| 394 |
-- content/post/b2/index.md -- |
| 395 |
--- |
| 396 |
title: pb2 |
| 397 |
--- |
| 398 |
-- content/post/nested-a/content-a.md -- |
| 399 |
--- |
| 400 |
title: ca |
| 401 |
--- |
| 402 |
{{< ref "content-b" >}} |
| 403 |
-- content/post/nested-b/content-b.md -- |
| 404 |
--- |
| 405 |
title: ca |
| 406 |
--- |
| 407 |
-- layouts/home.html -- |
| 408 |
Home |
| 409 |
-- layouts/single.html -- |
| 410 |
Content: {{ .Content }} |
| 411 |
` |
| 412 |
|
| 413 |
b := Test(t, files) |
| 414 |
|
| 415 |
b.AssertFileContent("public/post/b1/index.html", `Content: <p>Ref: http://example.com/post/b2/</p>`) |
| 416 |
b.AssertFileContent("public/post/nested-a/content-a/index.html", `Content: http://example.com/post/nested-b/content-b/`) |
| 417 |
} |
| 418 |
|
| 419 |
// Issue 14344 |
| 420 |
func TestRefUnderscoreSection(t *testing.T) { |
| 421 |
t.Parallel() |
| 422 |
|
| 423 |
files := ` |
| 424 |
-- hugo.toml -- |
| 425 |
baseURL = 'https://example.org/' |
| 426 |
-- layouts/home.html -- |
| 427 |
{{ .Content }} |
| 428 |
-- content/_index.md -- |
| 429 |
--- |
| 430 |
title: home |
| 431 |
--- |
| 432 |
{{% ref "definitions/_" %}} |
| 433 |
-- content/definitions/_/_index.md -- |
| 434 |
--- |
| 435 |
title: underscore |
| 436 |
--- |
| 437 |
` |
| 438 |
|
| 439 |
b := Test(t, files) |
| 440 |
|
| 441 |
b.AssertFileContent("public/index.html", |
| 442 |
`https://example.org/definitions/_/`, |
| 443 |
) |
| 444 |
} |
| 445 |
|
| 446 |
func TestClassCollector(t *testing.T) { |
| 447 |
for _, minify := range []bool{false, true} { |
| 448 |
t.Run(fmt.Sprintf("minify-%t", minify), func(t *testing.T) { |
| 449 |
statsFilename := "hugo_stats.json" |
| 450 |
defer os.Remove(statsFilename) |
| 451 |
|
| 452 |
files := fmt.Sprintf(` |
| 453 |
-- hugo.toml -- |
| 454 |
[minify] |
| 455 |
minifyOutput = %t |
| 456 |
|
| 457 |
[build] |
| 458 |
writeStats = true |
| 459 |
-- layouts/home.html -- |
| 460 |
|
| 461 |
<div id="el1" class="a b c">Foo</div> |
| 462 |
|
| 463 |
Some text. |
| 464 |
|
| 465 |
<div class="c d e [&>p]:text-red-600" id="el2">Foo</div> |
| 466 |
|
| 467 |
<span class=z>FOO</span> |
| 468 |
|
| 469 |
<a class="text-base hover:text-gradient inline-block px-3 pb-1 rounded lowercase" href="{{ .RelPermalink }}">{{ .Title }}</a> |
| 470 |
|
| 471 |
-- content/p1.md -- |
| 472 |
`, minify) |
| 473 |
|
| 474 |
b := Test(t, files, TestOptOsFs()) |
| 475 |
|
| 476 |
b.AssertFileContent("hugo_stats.json", ` |
| 477 |
{ |
| 478 |
"htmlElements": { |
| 479 |
"tags": [ |
| 480 |
"a", |
| 481 |
"div", |
| 482 |
"span" |
| 483 |
], |
| 484 |
"classes": [ |
| 485 |
"a", |
| 486 |
"b", |
| 487 |
"c", |
| 488 |
"d", |
| 489 |
"e", |
| 490 |
"hover:text-gradient", |
| 491 |
"[&>p]:text-red-600", |
| 492 |
"inline-block", |
| 493 |
"lowercase", |
| 494 |
"pb-1", |
| 495 |
"px-3", |
| 496 |
"rounded", |
| 497 |
"text-base", |
| 498 |
"z" |
| 499 |
], |
| 500 |
"ids": [ |
| 501 |
"el1", |
| 502 |
"el2" |
| 503 |
] |
| 504 |
} |
| 505 |
} |
| 506 |
`) |
| 507 |
}) |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
func TestClassCollectorConfigWriteStats(t *testing.T) { |
| 512 |
r := func(writeStatsConfig string) *IntegrationTestBuilder { |
| 513 |
files := ` |
| 514 |
-- hugo.toml -- |
| 515 |
` + writeStatsConfig + ` |
| 516 |
-- layouts/list.html -- |
| 517 |
<div id="myid" class="myclass">Foo</div> |
| 518 |
|
| 519 |
` |
| 520 |
b := Test(t, files, TestOptOsFs()) |
| 521 |
return b |
| 522 |
} |
| 523 |
|
| 524 |
// Legacy config. |
| 525 |
var b *IntegrationTestBuilder // Declare 'b' once |
| 526 |
b = r(` |
| 527 |
[build] |
| 528 |
writeStats = true |
| 529 |
`) |
| 530 |
|
| 531 |
b.AssertFileContent("hugo_stats.json", "myclass", "div", "myid") |
| 532 |
|
| 533 |
b = r(` |
| 534 |
[build] |
| 535 |
writeStats = false |
| 536 |
`) |
| 537 |
|
| 538 |
b.AssertFileExists("public/hugo_stats.json", false) |
| 539 |
|
| 540 |
b = r(` |
| 541 |
[build.buildStats] |
| 542 |
enable = true |
| 543 |
`) |
| 544 |
|
| 545 |
b.AssertFileContent("hugo_stats.json", "myclass", "div", "myid") |
| 546 |
|
| 547 |
b = r(` |
| 548 |
[build.buildStats] |
| 549 |
enable = true |
| 550 |
disableids = true |
| 551 |
`) |
| 552 |
|
| 553 |
b.AssertFileContent("hugo_stats.json", "myclass", "div", "! myid") |
| 554 |
|
| 555 |
b = r(` |
| 556 |
[build.buildStats] |
| 557 |
enable = true |
| 558 |
disableclasses = true |
| 559 |
`) |
| 560 |
|
| 561 |
b.AssertFileContent("hugo_stats.json", "! myclass", "div", "myid") |
| 562 |
|
| 563 |
b = r(` |
| 564 |
[build.buildStats] |
| 565 |
enable = true |
| 566 |
disabletags = true |
| 567 |
`) |
| 568 |
|
| 569 |
b.AssertFileContent("hugo_stats.json", "myclass", "! div", "myid") |
| 570 |
|
| 571 |
b = r(` |
| 572 |
[build.buildStats] |
| 573 |
enable = true |
| 574 |
disabletags = true |
| 575 |
disableclasses = true |
| 576 |
`) |
| 577 |
|
| 578 |
b.AssertFileContent("hugo_stats.json", "! myclass", "! div", "myid") |
| 579 |
|
| 580 |
b = r(` |
| 581 |
[build.buildStats] |
| 582 |
enable = false |
| 583 |
`) |
| 584 |
b.AssertFileExists("public/hugo_stats.json", false) |
| 585 |
} |