Debian Codegraph @

tpl/partials/partials_integration_test.go

raw
1 // Copyright 2024 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 partials_test
15
16 import (
17 "bytes"
18 "context"
19 "regexp"
20 "sort"
21 "strings"
22 "testing"
23
24 qt "github.com/frankban/quicktest"
25
26 "github.com/gohugoio/hugo/htesting"
27 "github.com/gohugoio/hugo/htesting/hqt"
28 "github.com/gohugoio/hugo/hugolib"
29 "github.com/gohugoio/hugo/tpl/partials"
30 )
31
32 func TestInclude(t *testing.T) {
33 t.Parallel()
34
35 files := `
36 -- hugo.toml --
37 baseURL = 'http://example.com/'
38 -- layouts/home.html --
39 partial: {{ partials.Include "foo.html" . }}
40 -- layouts/_partials/foo.html --
41 foo
42 `
43
44 b := hugolib.Test(t, files)
45
46 b.AssertFileContent("public/index.html", `
47 partial: foo
48 `)
49 }
50
51 func TestIncludeCached(t *testing.T) {
52 t.Parallel()
53
54 files := `
55 -- hugo.toml --
56 baseURL = 'http://example.com/'
57 -- layouts/home.html --
58 partialCached: {{ partials.IncludeCached "foo.html" . }}
59 partialCached: {{ partials.IncludeCached "foo.html" . }}
60 -- layouts/_partials/foo.html --
61 foo
62 `
63
64 b := hugolib.Test(t, files)
65
66 b.AssertFileContent("public/index.html", `
67 partialCached: foo
68 partialCached: foo
69 `)
70 }
71
72 // Issue 9519
73 func TestIncludeCachedRecursion(t *testing.T) {
74 t.Parallel()
75
76 files := `
77 -- hugo.toml --
78 baseURL = 'http://example.com/'
79 -- layouts/home.html --
80 {{ partials.IncludeCached "p1.html" . }}
81 -- layouts/_partials/p1.html --
82 {{ partials.IncludeCached "p2.html" . }}
83 -- layouts/_partials/p2.html --
84 P2
85
86 `
87
88 b := hugolib.Test(t, files)
89
90 b.AssertFileContent("public/index.html", `
91 P2
92 `)
93 }
94
95 // Issue #588
96 func TestIncludeCachedRecursionShortcode(t *testing.T) {
97 t.Parallel()
98
99 files := `
100 -- hugo.toml --
101 baseURL = 'http://example.com/'
102 -- content/_index.md --
103 ---
104 title: "Index"
105 ---
106 {{< short >}}
107 -- layouts/home.html --
108 {{ partials.IncludeCached "p1.html" . }}
109 -- layouts/_partials/p1.html --
110 {{ .Content }}
111 {{ partials.IncludeCached "p2.html" . }}
112 -- layouts/_partials/p2.html --
113 -- layouts/_shortcodes/short.html --
114 SHORT
115 {{ partials.IncludeCached "p2.html" . }}
116 P2
117
118 `
119
120 b := hugolib.Test(t, files)
121
122 b.AssertFileContent("public/index.html", `
123 SHORT
124 P2
125 `)
126 }
127
128 func TestIncludeCacheHints(t *testing.T) {
129 t.Parallel()
130
131 files := `
132 -- hugo.toml --
133 baseURL = 'http://example.com/'
134 templateMetrics=true
135 templateMetricsHints=true
136 disableKinds = ["page", "section", "taxonomy", "term", "sitemap"]
137 [outputs]
138 home = ["HTML"]
139 -- layouts/home.html --
140 {{ partials.IncludeCached "static1.html" . }}
141 {{ partials.IncludeCached "static1.html" . }}
142 {{ partials.Include "static2.html" . }}
143
144 D1I: {{ partials.Include "dynamic1.html" . }}
145 D1C: {{ partials.IncludeCached "dynamic1.html" . }}
146 D1C: {{ partials.IncludeCached "dynamic1.html" . }}
147 D1C: {{ partials.IncludeCached "dynamic1.html" . }}
148 H1I: {{ partials.Include "halfdynamic1.html" . }}
149 H1C: {{ partials.IncludeCached "halfdynamic1.html" . }}
150 H1C: {{ partials.IncludeCached "halfdynamic1.html" . }}
151
152 -- layouts/_partials/static1.html --
153 P1
154 -- layouts/_partials/static2.html --
155 P2
156 -- layouts/_partials/dynamic1.html --
157 {{ math.Counter }}
158 -- layouts/_partials/halfdynamic1.html --
159 D1
160 {{ math.Counter }}
161
162
163 `
164
165 b := hugolib.Test(t, files)
166
167 // fmt.Println(b.FileContent("public/index.html"))
168
169 var buf bytes.Buffer
170 b.H.Metrics.WriteMetrics(&buf)
171
172 got := buf.String()
173
174 // Get rid of all the durations, including the space and unit, they are never the same.
175 durationRe := regexp.MustCompile(`\b[\.\d]*\s*(ms|ns|µs|s)\b`)
176
177 normalize := func(s string) string {
178 s = durationRe.ReplaceAllString(s, "")
179 linesIn := strings.Split(s, "\n")[3:]
180 var lines []string
181 for _, l := range linesIn {
182 l = strings.TrimSpace(l)
183 if l == "" {
184 continue
185 }
186 lines = append(lines, l)
187 }
188
189 sort.Strings(lines)
190
191 return strings.Join(lines, "\n")
192 }
193
194 got = normalize(got)
195
196 expect := `
197 0 0 0 1 home.html
198 100 0 0 1 _partials/static2.html
199 100 50 1 2 _partials/static1.html
200 25 50 2 4 _partials/dynamic1.html
201 66 33 1 3 _partials/halfdynamic1.html
202 `
203
204 b.Assert(got, hqt.IsSameString, expect)
205 }
206
207 // gobench --package ./tpl/partials
208 func BenchmarkIncludeCached(b *testing.B) {
209 var files strings.Builder
210 files.WriteString(`
211 -- hugo.toml --
212 baseURL = 'http://example.com/'
213 -- layouts/home.html --
214 -- layouts/single.html --
215 {{ partialCached "heavy.html" "foo" }}
216 {{ partialCached "easy1.html" "bar" }}
217 {{ partialCached "easy1.html" "baz" }}
218 {{ partialCached "easy2.html" "baz" }}
219 -- layouts/_partials/abc.html --
220 ABCD
221 -- layouts/_partials/42.html --
222 {{ return 42 }}
223
224
225
226 `)
227
228 bb := hugolib.Test(b, files.String())
229 ns := bb.H.TemplateStore.GetTemplateFuncsNamespace("partials").(*partials.Namespace)
230
231 b.Run("abc", func(b *testing.B) {
232 for b.Loop() {
233 _, _ = ns.IncludeCached(context.Background(), "abc.html", "foo")
234 }
235 })
236
237 b.Run("variant", func(b *testing.B) {
238 for b.Loop() {
239 _, _ = ns.IncludeCached(context.Background(), "abc.html", "foo", "variant")
240 }
241 })
242
243 b.Run("return", func(b *testing.B) {
244 for b.Loop() {
245 _, _ = ns.IncludeCached(context.Background(), "42.html", "foo")
246 }
247 })
248 }
249
250 func TestIncludeTimeout(t *testing.T) {
251 htesting.SkipSlowTestUnlessCI(t)
252 t.Parallel()
253
254 files := `
255 -- hugo.toml --
256 baseURL = 'http://example.com/'
257 -- layouts/home.html --
258 {{ partials.Include "foo.html" . }}
259 -- layouts/_partials/foo.html --
260 {{ partial "foo.html" . }}
261 `
262
263 b, err := hugolib.TestE(t, files)
264
265 b.Assert(err, qt.Not(qt.IsNil))
266 b.Assert(err.Error(), qt.Contains, "maximum template call stack size exceeded")
267 }
268
269 func TestIncludeCachedTimeout(t *testing.T) {
270 t.Parallel()
271
272 files := `
273 -- hugo.toml --
274 baseURL = 'http://example.com/'
275 timeout = '200ms'
276 -- layouts/home.html --
277 {{ partials.IncludeCached "foo.html" . }}
278 -- layouts/_partials/foo.html --
279 {{ partialCached "bar.html" . }}
280 -- layouts/_partials/bar.html --
281 {{ partialCached "foo.html" . }}
282 `
283
284 b, err := hugolib.TestE(t, files)
285
286 b.Assert(err, qt.Not(qt.IsNil))
287 b.Assert(err.Error(), qt.Contains, `error calling partialCached: circular call stack detected in partial`)
288 }
289
290 // See Issue #13889
291 func TestIncludeCachedDifferentKey(t *testing.T) {
292 t.Parallel()
293
294 files := `
295 -- hugo.toml --
296 baseURL = 'http://example.com/'
297 timeout = '200ms'
298 -- layouts/home.html --
299 {{ partialCached "foo.html" "a" "a" }}
300 -- layouts/_partials/foo.html --
301 {{ if eq . "a" }}
302 {{ partialCached "bar.html" . }}
303 {{ else }}
304 DONE
305 {{ end }}
306 -- layouts/_partials/bar.html --
307 {{ partialCached "foo.html" "b" "b" }}
308 `
309 b := hugolib.Test(t, files)
310
311 b.AssertFileContent("public/index.html", `
312 DONE
313 `)
314 }
315
316 // See Issue #10789
317 func TestReturnExecuteFromTemplateInPartial(t *testing.T) {
318 t.Parallel()
319
320 files := `
321 -- hugo.toml --
322 baseURL = 'http://example.com/'
323 -- layouts/home.html --
324 {{ $r := partial "foo" }}
325 FOO:{{ $r.Content }}
326 -- layouts/_partials/foo.html --
327 {{ $r := §§{{ partial "bar" }}§§ | resources.FromString "bar.html" | resources.ExecuteAsTemplate "bar.html" . }}
328 {{ return $r }}
329 -- layouts/_partials/bar.html --
330 BAR
331 `
332
333 b := hugolib.Test(t, files)
334
335 b.AssertFileContent("public/index.html", "OO:BAR")
336 }
337
338 // See issue 15212.
339 func TestPartialReturnConditional(t *testing.T) {
340 t.Parallel()
341
342 files := `
343 -- hugo.toml --
344 baseURL = 'http://example.com/'
345 -- layouts/home.html --
346 1:{{ partial "parity.html" 1 }}|2:{{ partial "parity.html" 2 }}|
347 -- layouts/_partials/parity.html --
348 {{ if math.ModBool . 2 }}
349 {{ return "even" }}
350 {{ end }}
351 {{ return "odd" }}
352 `
353
354 b := hugolib.Test(t, files)
355
356 b.AssertFileContent("public/index.html", "1:odd|2:even|")
357 }
358
359 // See issue 15212.
360 func TestPartialReturnFromRange(t *testing.T) {
361 t.Parallel()
362
363 files := `
364 -- hugo.toml --
365 baseURL = 'http://example.com/'
366 -- layouts/home.html --
367 {{ partial "find.html" (slice 1 2 3) }}|
368 -- layouts/_partials/find.html --
369 {{ range . }}{{ if eq . 2 }}{{ return . }}{{ end }}{{ end }}{{ return "notfound" }}
370 `
371
372 b := hugolib.Test(t, files)
373
374 b.AssertFileContent("public/index.html", "2|")
375 }
376
377 // See issue 15212.
378 func TestPartialReturnBareStopsEarly(t *testing.T) {
379 t.Parallel()
380
381 files := `
382 -- hugo.toml --
383 baseURL = 'http://example.com/'
384 -- layouts/home.html --
385 {{ partial "p.html" . }}
386 -- layouts/_partials/p.html --
387 partial-start|{{ if true }}{{ return }}{{ end }}partial-end
388 `
389
390 b := hugolib.Test(t, files)
391
392 b.AssertFileContent("public/index.html", "partial-start|", "! partial-end")
393 }
394
395 // See issue 15212.
396 func TestPartialReturnNil(t *testing.T) {
397 t.Parallel()
398
399 files := `
400 -- hugo.toml --
401 baseURL = 'http://example.com/'
402 -- layouts/home.html --
403 {{ if eq (partial "p.html" .) nil }}NIL{{ end }}
404 -- layouts/_partials/p.html --
405 {{ return nil }}
406 `
407
408 b := hugolib.Test(t, files)
409
410 b.AssertFileContent("public/index.html", "NIL")
411 }
412
413 // See issue 15212.
414 func TestPartialReturnValueFromTemplateInclude(t *testing.T) {
415 t.Parallel()
416
417 files := `
418 -- hugo.toml --
419 baseURL = 'http://example.com/'
420 -- layouts/home.html --
421 {{ partial "p.html" . }}|
422 -- layouts/_partials/p.html --
423 {{ template "p-helper" . }}
424 {{ define "p-helper" }}{{ return 42 }}{{ end }}
425 `
426
427 b := hugolib.Test(t, files)
428
429 b.AssertFileContent("public/index.html", "42|")
430 }
431
432 // See issue 15373.
433 func TestIncludeRelativePath(t *testing.T) {
434 t.Parallel()
435
436 files := `
437 -- hugo.toml --
438 disableKinds = ["section", "taxonomy", "term", "sitemap", "RSS"]
439 -- layouts/home.html --
440 {{ partial "a/b/main.html" . }}
441 {{ partial "c/main.html" . }}
442 {{ partial "inline.html" . }}
443 {{ define "_partials/a/inline.html" }}inline:{{ partial "./helper.html" . }}{{ end }}
444 -- layouts/_partials/inline.html --
445 {{ partial "a/inline.html" . }}
446 -- layouts/_partials/a/b/main.html --
447 same:{{ partial "./helper.html" . }}
448 sub:{{ partial "./sub/helper.html" . }}
449 parent:{{ partial "../helper.html" . }}
450 root:{{ partial "../../root.html" . }}
451 cached:{{ partialCached "./cached.html" . }}
452 dynamic:{{ partial (printf "./%s.html" "helper") . }}
453 include:{{ partials.Include "./helper.html" . }}
454 -- layouts/_partials/a/b/helper.html --
455 a/b/helper
456 -- layouts/_partials/a/b/sub/helper.html --
457 a/b/sub/helper
458 -- layouts/_partials/a/b/cached.html --
459 a/b/cached
460 -- layouts/_partials/a/helper.html --
461 a/helper
462 -- layouts/_partials/root.html --
463 root
464 -- layouts/_partials/c/main.html --
465 cached:{{ partialCached "./cached.html" . }}
466 -- layouts/_partials/c/cached.html --
467 c/cached
468 `
469
470 b := hugolib.Test(t, files)
471
472 b.AssertFileContent("public/index.html",
473 "same:a/b/helper",
474 "sub:a/b/sub/helper",
475 "parent:a/helper",
476 "root:root",
477 "cached:a/b/cached",
478 "dynamic:a/b/helper",
479 "include:a/b/helper",
480 "cached:c/cached",
481 "inline:a/helper",
482 )
483 }
484
485 func TestIncludeRelativePathInDecoratorInner(t *testing.T) {
486 t.Parallel()
487
488 files := `
489 -- hugo.toml --
490 disableKinds = ["section", "taxonomy", "term", "sitemap", "RSS"]
491 -- layouts/home.html --
492 {{ partial "a/main.html" . }}
493 -- layouts/_partials/a/main.html --
494 {{ with partial "b/wrapper.html" . }}{{ partial "./helper.html" . }}{{ end }}
495 -- layouts/_partials/a/helper.html --
496 a/helper
497 -- layouts/_partials/b/wrapper.html --
498 wrapper:{{ inner . }}
499 -- layouts/_partials/b/helper.html --
500 b/helper
501 `
502
503 b := hugolib.Test(t, files)
504
505 b.AssertFileContent("public/index.html", "wrapper:a/helper")
506 }
507
508 func TestIncludeRelativePathNotFromPartial(t *testing.T) {
509 t.Parallel()
510
511 files := `
512 -- hugo.toml --
513 disableKinds = ["section", "taxonomy", "term", "sitemap", "RSS"]
514 -- layouts/home.html --
515 {{ partial "./helper.html" . }}
516 -- layouts/_partials/helper.html --
517 helper
518 `
519
520 b, err := hugolib.TestE(t, files)
521
522 b.Assert(err, qt.ErrorMatches, `(?s).*relative partial path "./helper.html" can only be used from within a partial.*`)
523 }
524
525 func TestIncludeRelativePathOutsidePartials(t *testing.T) {
526 t.Parallel()
527
528 files := `
529 -- hugo.toml --
530 disableKinds = ["section", "taxonomy", "term", "sitemap", "RSS"]
531 -- layouts/home.html --
532 {{ partial "a/main.html" . }}
533 -- layouts/_partials/a/main.html --
534 {{ partial "../../helper.html" . }}
535 -- layouts/_partials/helper.html --
536 helper
537 `
538
539 b, err := hugolib.TestE(t, files)
540
541 b.Assert(err, qt.ErrorMatches, `(?s).*relative partial path "../../helper.html" in "_partials/a/main.html" resolves outside the partials directory.*`)
542 }
543
544 func TestIncludeRelativePathInDeferredBlock(t *testing.T) {
545 t.Parallel()
546
547 files := `
548 -- hugo.toml --
549 disableKinds = ["section", "taxonomy", "term", "sitemap", "RSS"]
550 -- layouts/home.html --
551 {{ partial "a/main.html" . }}
552 -- layouts/_partials/a/main.html --
553 {{ with (templates.Defer (dict "key" "a")) }}deferred:{{ partial "./helper.html" . }}{{ end }}
554 -- layouts/_partials/a/helper.html --
555 a/helper
556 `
557
558 b := hugolib.Test(t, files)
559
560 b.AssertFileContent("public/index.html", "deferred:a/helper")
561 }