Debian Codegraph @

tpl/templates/defer_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 templates_test
15
16 import (
17 "fmt"
18 "path/filepath"
19 "strings"
20 "testing"
21
22 qt "github.com/frankban/quicktest"
23
24 "github.com/gohugoio/hugo/hugolib"
25 )
26
27 const deferFilesCommon = `
28 -- hugo.toml --
29 disableLiveReload = true
30 disableKinds = ["taxonomy", "term", "rss", "sitemap", "robotsTXT", "404", "section"]
31 [languages]
32 [languages.en]
33 weight = 1
34 [languages.nn]
35 weight = 2
36 -- i18n/en.toml --
37 [hello]
38 other = "Hello"
39 -- i18n/nn.toml --
40 [hello]
41 other = "Hei"
42 -- content/_index.en.md --
43 ---
44 title: "Home"
45 outputs: ["html", "amp"]
46 ---
47 -- content/_index.nn.md --
48 ---
49 title: "Heim"
50 outputs: ["html", "amp"]
51 ---
52 -- assets/mytext.txt --
53 Hello.
54 -- layouts/baseof.html --
55 HTML|{{ block "main" . }}{{ end }}$
56 -- layouts/home.html --
57 {{ define "main" }}
58 EDIT_COUNTER_OUTSIDE_0
59 {{ .Store.Set "hello" "Hello" }}
60 {{ $data := dict "page" . }}
61 {{ with (templates.Defer (dict "data" $data) ) }}
62 {{ $mytext := resources.Get "mytext.txt" }}
63 REPLACE_ME|Title: {{ .page.Title }}|{{ .page.RelPermalink }}|Hello: {{ T "hello" }}|Hello Store: {{ .page.Store.Get "hello" }}|Mytext: {{ $mytext.Content }}|
64 EDIT_COUNTER_DEFER_0
65 {{ end }}$
66 {{ end }}
67 -- layouts/index.amp.html --
68 AMP.
69 {{ $data := dict "page" . }}
70 {{ with (templates.Defer (dict "data" $data) ) }}Title AMP: {{ .page.Title }}|{{ .page.RelPermalink }}|Hello: {{ T "hello" }}{{ end }}$
71
72 `
73
74 func TestDeferNoBaseof(t *testing.T) {
75 t.Parallel()
76
77 files := `
78 -- hugo.toml --
79 -- layouts/home.html --
80 Home.
81 {{ with (templates.Defer (dict "key" "foo")) }}
82 Defer
83 {{ end }}
84 -- content/_index.md --
85 ---
86 title: "Home"
87 ---
88
89 `
90
91 b := hugolib.Test(t, files)
92
93 b.AssertFileContent("public/index.html", "Home.\n\n Defer")
94 }
95
96 func TestDeferBaseof(t *testing.T) {
97 t.Parallel()
98
99 files := `
100 -- hugo.toml --
101 -- layouts/baseof.html --
102 {{ with (templates.Defer (dict "key" "foo")) }}
103 Defer
104 {{ end }}
105 Block:{{ block "main" . }}{{ end }}$
106 -- layouts/home.html --
107 {{ define "main" }}
108 Home.
109 {{ end }}
110 -- content/_index.md --
111 ---
112 title: "Home"
113 ---
114
115 `
116
117 b := hugolib.Test(t, files)
118
119 b.AssertFileContent("public/index.html", "Home.\n\n Defer")
120 }
121
122 func TestDeferMain(t *testing.T) {
123 t.Parallel()
124
125 files := `
126 -- hugo.toml --
127 -- layouts/baseof.html --
128
129 Block:{{ block "main" . }}{{ end }}$
130 -- layouts/home.html --
131 {{ define "main" }}
132 Home.
133 {{ with (templates.Defer (dict "key" "foo")) }}
134 Defer
135 {{ end }}
136 {{ end }}
137 -- content/_index.md --
138 ---
139 title: "Home"
140 ---
141
142 `
143
144 b := hugolib.Test(t, files)
145
146 b.AssertFileContent("public/index.html", "Home.\n\n Defer")
147 }
148
149 func TestDeferBasic(t *testing.T) {
150 t.Parallel()
151
152 b := hugolib.Test(t, deferFilesCommon)
153
154 b.AssertFileContent("public/index.html", "Title: Home|/|Hello: Hello|Hello Store: Hello|Mytext: Hello.|")
155 b.AssertFileContent("public/amp/index.html", "Title AMP: Home|/amp/|Hello: Hello")
156 b.AssertFileContent("public/nn/index.html", "Title: Heim|/nn/|Hello: Hei")
157 b.AssertFileContent("public/nn/amp/index.html", "Title AMP: Heim|/nn/amp/|Hello: Hei")
158 }
159
160 func TestDeferRepeatedBuildsEditOutside(t *testing.T) {
161 t.Parallel()
162
163 b := hugolib.TestRunning(t, deferFilesCommon)
164
165 for i := range 5 {
166 old := fmt.Sprintf("EDIT_COUNTER_OUTSIDE_%d", i)
167 new := fmt.Sprintf("EDIT_COUNTER_OUTSIDE_%d", i+1)
168 b.EditFileReplaceAll("layouts/home.html", old, new).Build()
169 b.AssertFileContent("public/index.html", new)
170 }
171 }
172
173 func TestDeferRepeatedBuildsEditDefer(t *testing.T) {
174 t.Parallel()
175
176 b := hugolib.TestRunning(t, deferFilesCommon)
177
178 for i := range 8 {
179 old := fmt.Sprintf("EDIT_COUNTER_DEFER_%d", i)
180 new := fmt.Sprintf("EDIT_COUNTER_DEFER_%d", i+1)
181 b.EditFileReplaceAll("layouts/home.html", old, new).Build()
182 b.AssertFileContent("public/index.html", new)
183 }
184 }
185
186 func TestDeferErrorParse(t *testing.T) {
187 t.Parallel()
188
189 b, err := hugolib.TestE(t, strings.ReplaceAll(deferFilesCommon, "Title AMP: {{ .page.Title }}", "{{ .page.Title }"))
190
191 b.Assert(err, qt.Not(qt.IsNil))
192 b.Assert(err.Error(), qt.Contains, `index.amp.html:3: unexpected "}" in operand`)
193 }
194
195 func TestDeferErrorRuntime(t *testing.T) {
196 t.Parallel()
197
198 b, err := hugolib.TestE(t, strings.ReplaceAll(deferFilesCommon, "Title AMP: {{ .page.Title }}", "{{ .page.Titles }}"))
199
200 b.Assert(err, qt.Not(qt.IsNil))
201 b.Assert(err.Error(), qt.Contains, filepath.FromSlash(`/layouts/index.amp.html:3:57`))
202 b.Assert(err.Error(), qt.Contains, `execute of template failed: template: index.amp.html:3:57: executing at <.page.Titles>: can't evaluate field Titles`)
203 }
204
205 func TestDeferEditDeferBlock(t *testing.T) {
206 t.Parallel()
207
208 b := hugolib.TestRunning(t, deferFilesCommon)
209 b.AssertRenderCountPage(4)
210 b.EditFileReplaceAll("layouts/home.html", "REPLACE_ME", "Edited.").Build()
211 b.AssertFileContent("public/index.html", "Edited.")
212 b.AssertRenderCountPage(2)
213 }
214
215 //
216
217 func TestDeferEditResourceUsedInDeferBlock(t *testing.T) {
218 t.Parallel()
219
220 b := hugolib.TestRunning(t, deferFilesCommon)
221 b.AssertRenderCountPage(4)
222 b.EditFiles("assets/mytext.txt", "Mytext Hello Edited.").Build()
223 b.AssertFileContent("public/index.html", "Mytext Hello Edited.")
224 b.AssertRenderCountPage(2)
225 }
226
227 func TestDeferMountPublic(t *testing.T) {
228 t.Parallel()
229
230 files := `
231 -- hugo.toml --
232 [module]
233 [[module.mounts]]
234 source = "content"
235 target = "content"
236 [[module.mounts]]
237 source = "layouts"
238 target = "layouts"
239 [[module.mounts]]
240 source = 'public'
241 target = 'assets/public'
242 disableWatch = true
243 -- layouts/home.html --
244 Home.
245 {{ $mydata := dict "v1" "v1value" }}
246 {{ $json := resources.FromString "mydata/data.json" ($mydata | jsonify ) }}
247 {{ $nop := $json.RelPermalink }}
248 {{ with (templates.Defer (dict "key" "foo")) }}
249 {{ $jsonFilePublic := resources.Get "public/mydata/data.json" }}
250 {{ with $jsonFilePublic }}
251 {{ $m := $jsonFilePublic | transform.Unmarshal }}
252 v1: {{ $m.v1 }}
253 {{ end }}
254 {{ end }}
255 `
256
257 b := hugolib.Test(t, files)
258
259 b.AssertFileContent("public/index.html", "v1: v1value")
260 }
261
262 func TestDeferFromContentAdapterShouldFail(t *testing.T) {
263 t.Parallel()
264
265 files := `
266 -- hugo.toml --
267 -- content/_content.gotmpl --
268 {{ with (templates.Defer (dict "key" "foo")) }}
269 Foo.
270 {{ end }}
271 `
272
273 b, err := hugolib.TestE(t, files)
274
275 b.Assert(err, qt.Not(qt.IsNil))
276 b.Assert(err.Error(), qt.Contains, "error calling Defer: this method cannot be called before the site is fully initialized")
277 }
278
279 func TestDeferPostProcessShouldThrowAnError(t *testing.T) {
280 t.Parallel()
281
282 files := `
283 -- hugo.toml --
284 -- assets/mytext.txt --
285 ABCD.
286 -- layouts/home.html --
287 Home
288 {{ with (templates.Defer (dict "key" "foo")) }}
289 {{ $mytext := resources.Get "mytext.txt" | minify | resources.PostProcess }}
290 {{ end }}
291
292 `
293 b, err := hugolib.TestE(t, files)
294
295 b.Assert(err, qt.Not(qt.IsNil))
296 b.Assert(err.Error(), qt.Contains, "resources.PostProcess cannot be used in a deferred template")
297 }
298
299 // Issue #13236.
300 func TestDeferMultipleInSameTemplate(t *testing.T) {
301 t.Parallel()
302
303 files := `
304 -- hugo.toml --
305 -- layouts/home.html --
306 Home.
307 ...
308 {{ with (templates.Defer (dict "data" (dict "a" "b") )) }}
309 Defer 1
310 {{ end }}
311 ...
312 {{ with (templates.Defer (dict "data" (dict "a" "c") )) }}
313 Defer 2
314 {{ end }}
315 {{ with (templates.Defer (dict "data" (dict "a" "d") )) }}
316 Defer 3
317 {{ end }}{{ with (templates.Defer (dict "data" (dict "a" "d") )) }}{{ end }}
318 End.
319 `
320
321 b := hugolib.Test(t, files)
322
323 b.AssertFileContent("public/index.html", "Home.", "Defer 1", "Defer 2", "Defer 3", "End.")
324 }
325
326 // See issue 15234.
327 func TestDeferDifferentOutputFormats(t *testing.T) {
328 t.Parallel()
329
330 files := `
331 -- hugo.toml --
332 [outputFormats.probe]
333 mediaType = "text/plain"
334 baseName = "index"
335 isPlainText = true
336 [outputs]
337 home = ["html", "probe"]
338 -- layouts/home.html --
339 {{ $v := "<b>&" }}OUT[{{ $v }}]{{ with (templates.Defer (dict "key" "K" "data" (dict "v" $v))) }}IN[{{ .v }}]{{ end }}
340 -- layouts/home.probe.txt --
341 {{ $v := "<b>&" }}OUT[{{ $v }}]{{ with (templates.Defer (dict "key" "K" "data" (dict "v" $v))) }}IN[{{ .v }}]{{ end }}
342 `
343
344 b := hugolib.Test(t, files)
345
346 b.AssertFileContent("public/index.html", "OUT[&lt;b&gt;&amp;]IN[&lt;b&gt;&amp;]")
347 b.AssertFileContent("public/index.txt", "OUT[<b>&]IN[<b>&]")
348 }
349
350 // See issue 13492.
351 func TestDeferInsidePartialCachedShouldFail(t *testing.T) {
352 t.Parallel()
353
354 files := `
355 -- hugo.toml --
356 -- layouts/home.html --
357 Home.
358 {{ partialCached "css.html" . }}
359 -- layouts/partials/css.html --
360 {{ with (templates.Defer (dict "key" "global")) }}
361 Defer
362 {{ end }}
363 `
364
365 b, err := hugolib.TestE(t, files)
366
367 b.Assert(err, qt.Not(qt.IsNil))
368 b.Assert(err.Error(), qt.Contains, "templates.Defer cannot be used inside a partialCached partial")
369 }
370
371 // Verifies that the partialCached ctx flag propagates through a regular partial call.
372 // See issue 13492.
373 func TestDeferTransitivelyInsidePartialCachedShouldFail(t *testing.T) {
374 t.Parallel()
375
376 files := `
377 -- hugo.toml --
378 -- layouts/home.html --
379 Home.
380 {{ partialCached "outer.html" . }}
381 -- layouts/partials/outer.html --
382 {{ partial "inner.html" . }}
383 -- layouts/partials/inner.html --
384 {{ with (templates.Defer (dict "key" "global")) }}
385 Defer
386 {{ end }}
387 `
388
389 b, err := hugolib.TestE(t, files)
390
391 b.Assert(err, qt.Not(qt.IsNil))
392 b.Assert(err.Error(), qt.Contains, "templates.Defer cannot be used inside a partialCached partial")
393 }