Debian Codegraph @

hugolib/cascade_test.go

raw
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 "strings"
19 "testing"
20
21 "github.com/gohugoio/hugo/hugolib/sitesmatrix"
22
23 qt "github.com/frankban/quicktest"
24 )
25
26 func BenchmarkCascadeTarget(b *testing.B) {
27 var files strings.Builder
28 files.WriteString(`
29 -- content/_index.md --
30 background = 'yosemite.jpg'
31 [cascade.target]
32 kind = '{section,term}'
33 -- content/posts/_index.md --
34 -- content/posts/funny/_index.md --
35 `)
36
37 for i := 1; i < 100; i++ {
38 files.WriteString(fmt.Sprintf("\n-- content/posts/p%d.md --\n", i+1))
39 }
40
41 for i := 1; i < 100; i++ {
42 files.WriteString(fmt.Sprintf("\n-- content/posts/funny/pf%d.md --\n", i+1))
43 }
44
45 b.Run("Kind", func(b *testing.B) {
46 cfg := IntegrationTestConfig{
47 T: b,
48 TxtarString: files.String(),
49 }
50 b.ResetTimer()
51
52 for b.Loop() {
53 b.StopTimer()
54 builder := NewIntegrationTestBuilder(cfg)
55 b.StartTimer()
56 builder.Build()
57 }
58 })
59 }
60
61 func TestCascadeBuildOptionsTaxonomies(t *testing.T) {
62 t.Parallel()
63
64 files := `
65 -- hugo.toml --
66 baseURL="https://example.org"
67 [taxonomies]
68 tag = "tags"
69
70 [[cascade]]
71
72 [cascade.build]
73 render = "never"
74 list = "never"
75 publishResources = false
76
77 [cascade.target]
78 path = '/hidden/**'
79 -- content/p1.md --
80 ---
81 title: P1
82 ---
83 -- content/hidden/p2.md --
84 ---
85 title: P2
86 tags: [t1, t2]
87 ---
88 -- layouts/list.html --
89 List: {{ len .Pages }}|
90 -- layouts/single.html --
91 Single: Tags: {{ site.Taxonomies.tags }}|
92 `
93
94 b := Test(t, files)
95
96 b.AssertFileContent("public/p1/index.html", "Single: Tags: map[]|")
97 b.AssertFileContent("public/tags/index.html", "List: 0|")
98 b.AssertFileExists("public/hidden/p2/index.html", false)
99 b.AssertFileExists("public/tags/t2/index.html", false)
100 }
101
102 func TestCascadeEditIssue12449(t *testing.T) {
103 t.Parallel()
104
105 files := `
106 -- hugo.toml --
107 baseURL = "https://example.com"
108 disableKinds = ['sitemap','rss', 'home', 'taxonomy','term']
109 disableLiveReload = true
110 -- layouts/list.html --
111 Title: {{ .Title }}|{{ .Content }}|cascadeparam: {{ .Params.cascadeparam }}|
112 -- layouts/single.html --
113 Title: {{ .Title }}|{{ .Content }}|cascadeparam: {{ .Params.cascadeparam }}|
114 -- content/mysect/_index.md --
115 ---
116 title: mysect
117 cascade:
118 description: descriptionvalue
119 params:
120 cascadeparam: cascadeparamvalue
121 ---
122 mysect-content|
123 -- content/mysect/p1/index.md --
124 ---
125 slug: p1
126 ---
127 p1-content|
128 -- content/mysect/subsect/_index.md --
129 ---
130 slug: subsect
131 ---
132 subsect-content|
133 `
134
135 b := TestRunning(t, files)
136
137 // Make the cascade set the title.
138 b.EditFileReplaceAll("content/mysect/_index.md", "description: descriptionvalue", "title: cascadetitle").Build()
139 b.AssertFileContent("public/mysect/subsect/index.html", "Title: cascadetitle|")
140
141 // Edit cascade title.
142 b.EditFileReplaceAll("content/mysect/_index.md", "title: cascadetitle", "title: cascadetitle-edit").Build()
143 b.AssertFileContent("public/mysect/subsect/index.html", "Title: cascadetitle-edit|")
144
145 // Revert title change.
146 // The step below failed in #12449.
147 b.EditFileReplaceAll("content/mysect/_index.md", "title: cascadetitle-edit", "description: descriptionvalue").Build()
148 b.AssertFileContent("public/mysect/subsect/index.html", "Title: |")
149 }
150
151 func TestCascadeIssue12172(t *testing.T) {
152 t.Parallel()
153
154 files := `
155 -- hugo.toml --
156 disableKinds = ['rss','sitemap','taxonomy','term']
157 [[cascade]]
158 headless = true
159 [cascade.target]
160 path = '/s1**'
161 -- content/s1/p1.md --
162 ---
163 title: p1
164 ---
165 -- layouts/single.html --
166 {{ .Title }}|
167 -- layouts/list.html --
168 {{ .Title }}|
169 `
170 b := Test(t, files)
171
172 b.AssertFileExists("public/index.html", true)
173 b.AssertFileExists("public/s1/index.html", false)
174 b.AssertFileExists("public/s1/p1/index.html", false)
175 }
176
177 // Issue 12594.
178 func TestCascadeOrder(t *testing.T) {
179 t.Parallel()
180
181 files := `
182 -- hugo.toml --
183 disableKinds = ['rss','sitemap','taxonomy','term', 'home']
184 -- content/_index.md --
185 ---
186 title: Home
187 cascade:
188 - target:
189 path: "**"
190 params:
191 background: yosemite.jpg
192 - target:
193 params:
194 background: goldenbridge.jpg
195 ---
196 -- content/p1.md --
197 ---
198 title: p1
199 ---
200 -- layouts/single.html --
201 Background: {{ .Params.background }}|
202 -- layouts/list.html --
203 {{ .Title }}|
204 `
205
206 for range 10 {
207 b := Test(t, files)
208 b.AssertFileContent("public/p1/index.html", "Background: yosemite.jpg")
209 }
210 }
211
212 // Issue #12465.
213 func TestCascadeOverlap(t *testing.T) {
214 t.Parallel()
215
216 files := `
217 -- hugo.toml --
218 disableKinds = ['home','rss','sitemap','taxonomy','term']
219 -- layouts/list.html --
220 {{ .Title }}
221 -- layouts/single.html --
222 {{ .Title }}
223 -- content/s/_index.md --
224 ---
225 title: s
226 cascade:
227 build:
228 render: never
229 ---
230 -- content/s/p1.md --
231 ---
232 title: p1
233 ---
234 -- content/sx/_index.md --
235 ---
236 title: sx
237 ---
238 -- content/sx/p2.md --
239 ---
240 title: p2
241 ---
242 `
243
244 b := Test(t, files)
245
246 b.AssertFileExists("public/s/index.html", false)
247 b.AssertFileExists("public/s/p1/index.html", false)
248
249 b.AssertFileExists("public/sx/index.html", true) // failing
250 b.AssertFileExists("public/sx/p2/index.html", true) // failing
251 }
252
253 func TestCascadeGotmplIssue13743(t *testing.T) {
254 t.Parallel()
255
256 files := `
257 -- hugo.toml --
258 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
259 [cascade.params]
260 foo = 'bar'
261 [cascade.target]
262 path = '/p1'
263 -- content/_content.gotmpl --
264 {{ .AddPage (dict "title" "p1" "path" "p1") }}
265 -- layouts/all.html --
266 {{ .Title }}|{{ .Params.foo }}
267 `
268
269 b := Test(t, files)
270
271 b.AssertFileContent("public/p1/index.html", "p1|bar") // actual content is "p1|"
272 }
273
274 func TestSitesMatrixCascadeConfig(t *testing.T) {
275 files := `
276 -- hugo.toml --
277 disableKinds = ["taxonomy", "term", "rss", "sitemap"]
278 [languages]
279 [languages.en]
280 weight = 1
281 [languages.nn]
282 weight = 2
283 [languages.sv]
284 weight = 3
285
286 [versions]
287 [versions."v1.0.0"]
288 [versions."v2.0.0"]
289 [versions."v2.1.0"]
290
291 [roles]
292 [roles.guest]
293 [roles.member]
294 [cascade]
295 [cascade.sites.matrix]
296 languages = ["en"]
297 versions = ["v2**"]
298 roles = ["member"]
299 [cascade.sites.complements]
300 languages = ["nn"]
301 versions = ["v1.0.*"]
302 roles = ["guest"]
303 -- content/_index.md --
304 ---
305 title: "Home"
306 sites:
307 matrix:
308 roles: ["guest"]
309 ---
310 -- layouts/all.html --
311 All.
312
313 `
314
315 b := Test(t, files)
316
317 s0 := b.H.sitesVersionsRolesMap[sitesmatrix.Vector{0, 0, 0}] // en, v2.1.0, guest
318 b.Assert(s0.home, qt.IsNotNil)
319 b.Assert(s0.home.File(), qt.IsNotNil)
320 b.Assert(s0.language.Name(), qt.Equals, "en")
321 b.Assert(s0.version.Name(), qt.Equals, "v2.1.0")
322 b.Assert(s0.role.Name(), qt.Equals, "guest")
323 s0Pconfig := s0.Home().(*pageState).m.pageConfigSource
324 b.Assert(s0Pconfig.SitesMatrix.Vectors(), qt.DeepEquals, []sitesmatrix.Vector{{0, 0, 0}, {0, 1, 0}}) // en, v2.1.0, guest + en, v2.0.0, guest
325
326 s1 := b.H.sitesVersionsRolesMap[sitesmatrix.Vector{1, 2, 0}]
327 b.Assert(s1.home, qt.IsNotNil)
328 b.Assert(s1.home.File(), qt.IsNil)
329 b.Assert(s1.language.Name(), qt.Equals, "nn")
330 b.Assert(s1.version.Name(), qt.Equals, "v1.0.0")
331 b.Assert(s1.role.Name(), qt.Equals, "guest")
332 s1Pconfig := s1.Home().(*pageState).m.pageConfigSource
333 b.Assert(s1Pconfig.SitesMatrix.HasVector(sitesmatrix.Vector{1, 2, 0}), qt.IsTrue) // nn, v1.0.0, guest
334 // Every site needs a home page. This matrix adds the missing ones, (3 * 3 * 2) - 2 = 16
335 b.Assert(s1Pconfig.SitesMatrix.LenVectors(), qt.Equals, 16)
336 }
337
338 func TestCascadeBundledPage(t *testing.T) {
339 t.Parallel()
340
341 files := `
342 -- hugo.toml --
343 baseURL = "https://example.org"
344 -- content/_index.md --
345 ---
346 title: Home
347 cascade:
348 params:
349 p1: v1
350 ---
351 -- content/b1/index.md --
352 ---
353 title: b1
354 ---
355 -- content/b1/p2.md --
356 ---
357 title: p2
358 ---
359 -- layouts/all.html --
360 Title: {{ .Title }}|p1: {{ .Params.p1 }}|
361 {{ range .Resources }}
362 Resource: {{ .Name }}|p1: {{ .Params.p1 }}|
363 {{ end }}
364 `
365
366 b := Test(t, files)
367
368 b.AssertFileContent("public/b1/index.html", "Title: b1|p1: v1|", "Resource: p2.md|p1: v1|")
369 }
370
371 // Issue 14310
372 // Issue 14321
373 func TestCascadeIssue14310(t *testing.T) {
374 t.Parallel()
375
376 files := `
377 -- hugo.toml --
378 disableKinds = ['home', 'rss', 'sitemap', 'taxonomy', 'term']
379 defaultContentLanguage = 'en'
380 defaultContentLanguageInSubdir = true
381 [languages.en]
382 weight = 1
383 [languages.de]
384 weight = 2
385 [[cascade]]
386 [cascade.params]
387 size = 'medium'
388 [cascade.target]
389 kind = 'page'
390 -- layouts/all.html --
391 |color: {{ .Params.color }}|size: {{ .Params.size }}|
392 -- content/s1/_index.de.md --
393 ---
394 title: s1 (de)
395 cascade:
396 params:
397 color: red (de)
398 ---
399 -- content/s1/_index.en.md --
400 ---
401 title: s1 (en)
402 cascade:
403 params:
404 color: red (en)
405 ---
406 -- content/s1/p1.de.md --
407 ---
408 title: p1 (de)
409 ---
410 -- content/s1/p1.en.md --
411 ---
412 title: p1 (en)
413 ---
414 `
415
416 b := Test(t, files)
417
418 b.AssertFileContent("public/en/s1/p1/index.html", "|color: red (en)|size: medium|") // fails: file contains "|color: red (en)|size: |"
419 b.AssertFileContent("public/de/s1/p1/index.html", "|color: red (de)|size: medium|")
420 }
421
422 // Issue 14409
423 func TestCascadeDraftTrue14409(t *testing.T) {
424 t.Parallel()
425
426 files := `
427 -- hugo.toml --
428 disableKinds = ['rss','taxonomy','term']
429 defaultContentLanguage = 'en'
430 defaultContentLanguageInSubdir = true
431
432 [languages.en]
433 weight = 1
434 [languages.de]
435 weight = 2
436
437 [[cascade]]
438 draft = true
439 [cascade.target.sites.matrix]
440 languages = ['en']
441 -- content/_index.en.md --
442 ---
443 title: home en
444 draft: false
445 ---
446 -- content/_index.de.md --
447 ---
448 title: home de
449 draft: false
450 ---
451 -- content/s1/_index.en.md --
452 ---
453 title: s1 en
454 draft: false
455 ---
456 -- content/s1/_index.de.md --
457 ---
458 title: s1 de
459 draft: false
460 ---
461 -- content/s1/p1.en.md --
462 ---
463 title: p1 en
464 ---
465 -- content/s1/p1.de.md --
466 ---
467 title: p1 de
468 ---
469 -- layouts/all.html --
470 {{ .Title }}
471 `
472
473 b := Test(t, files)
474
475 b.AssertFileExists("public/de/index.html", true)
476 b.AssertFileExists("public/en/index.html", true)
477
478 b.AssertFileExists("public/de/s1/index.html", true)
479 b.AssertFileExists("public/en/s1/index.html", true)
480
481 b.AssertFileExists("public/de/s1/p1/index.html", true)
482 b.AssertFileExists("public/en/s1/p1/index.html", false)
483 }
484
485 // Issue 14627
486 func TestRegularPageCascadeToSelf(t *testing.T) {
487 t.Parallel()
488
489 files := `
490 -- hugo.toml --
491 disableKinds = ['home','rss','sitemap','taxonomy','term']
492 environment = 'pubweb'
493 -- content/s1/_index.md --
494 ---
495 title: s1
496 cascade:
497 target:
498 environment: pubweb
499 build:
500 list: never
501 render: never
502 publishResources: false
503 ---
504 -- content/s1/p1.md --
505 ---
506 title: p1
507 ---
508 -- content/s2/_index.md --
509 ---
510 title: s2
511 ---
512 -- content/s2/p2.md --
513 ---
514 title: p2
515 cascade:
516 target:
517 environment: pubweb
518 build:
519 list: never
520 render: never
521 publishResources: false
522 ---
523 -- content/s2/p3/index.md --
524 ---
525 title: p3
526 cascade:
527 target:
528 environment: pubweb
529 build:
530 list: never
531 render: never
532 publishResources: false
533 ---
534 -- layouts/all.html --
535 {{ .Title }}|
536 `
537
538 b := Test(t, files)
539
540 b.AssertFileExists("public/s1/index.html", false)
541 b.AssertFileExists("public/s1/p1/index.html", false)
542 b.AssertFileExists("public/s2/index.html", true)
543 b.AssertFileExists("public/s2/p2/index.html", false)
544 b.AssertFileExists("public/s2/p3/index.html", false)
545 }
546
547 // Issue 13869
548 func TestCascadeSliceFromModule13869(t *testing.T) {
549 t.Parallel()
550
551 files := `
552 -- hugo.toml --
553 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
554 theme = 'foo'
555 [cascade]
556 _merge = 'deep'
557 -- content/_index.md --
558 ---
559 title: home
560 ---
561 -- layouts/home.html --
562 color: {{ .Params.color }}
563 -- themes/foo/hugo.toml --
564 [[cascade]]
565 [cascade.params]
566 color = 'red'
567 `
568
569 b := Test(t, files)
570
571 b.AssertFileContent("public/index.html", "color: red")
572 }
573
574 // Issue 14848
575 func TestCascadeParamsLangIssue14848(t *testing.T) {
576 t.Parallel()
577
578 files := `
579 -- hugo.toml --
580 baseURL = "https://example.org"
581 disableKinds = ['rss','sitemap','taxonomy','term']
582 -- content/_index.md --
583 ---
584 title: Home
585 cascade:
586 params:
587 lang: en
588 ---
589 -- content/p1.md --
590 ---
591 title: p1
592 ---
593 -- layouts/all.html --
594 {{ .Title }}|lang: {{ .Params.lang }}|
595 `
596
597 b := Test(t, files)
598
599 b.AssertFileContent("public/p1/index.html", "p1|lang: en|")
600 }