Debian Codegraph @

tpl/templates/templates_integration_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 templates_test
15
16 import (
17 "path/filepath"
18 "testing"
19
20 qt "github.com/frankban/quicktest"
21 "github.com/gohugoio/hugo/hugolib"
22 )
23
24 func TestExists(t *testing.T) {
25 t.Parallel()
26
27 files := `
28 -- hugo.toml --
29 baseURL = 'http://example.com/'
30 -- layouts/home.html --
31 home.html: {{ templates.Exists "home.html" }}
32 post/single.html: {{ templates.Exists "post/single.html" }}
33 _partials/foo.html: {{ templates.Exists "_partials/foo.html" }}
34 _partials/doesnotexist.html: {{ templates.Exists "_partials/doesnotexist.html" }}
35 -- layouts/post/single.html --
36 -- layouts/_partials/foo.html --
37 `
38
39 b := hugolib.Test(t, files)
40
41 b.AssertFileContent("public/index.html", `
42 home.html: true
43 post/single.html: true
44 _partials/foo.html: true
45 _partials/doesnotexist.html: false
46 `)
47 }
48
49 func TestExistsWithBaseOf(t *testing.T) {
50 t.Parallel()
51
52 files := `
53 -- hugo.toml --
54 baseURL = 'http://example.com/'
55 -- layouts/baseof.html --
56 {{ block "main" . }}{{ end }}
57 -- layouts/home.html --
58 {{ define "main" }}
59 index.html: {{ templates.Exists "home.html" }}
60 post/single.html: {{ templates.Exists "post/single.html" }}
61 post/doesnotexist.html: {{ templates.Exists "post/doesnotexist.html" }}
62 {{ end }}
63 -- layouts/post/single.html --
64 {{ define "main" }}MAIN{{ end }}
65
66
67 `
68
69 b := hugolib.Test(t, files)
70
71 b.AssertFileContent("public/index.html", `
72 index.html: true
73 post/single.html: true
74 post/doesnotexist.html: false
75
76 `)
77 }
78
79 // See #10774
80 func TestPageFunctionExists(t *testing.T) {
81 t.Parallel()
82
83 files := `
84 -- hugo.toml --
85 baseURL = 'http://example.com/'
86 -- layouts/home.html --
87 Home: {{ page.IsHome }}
88
89 `
90
91 b := hugolib.Test(t, files)
92
93 b.AssertFileContent("public/index.html", `
94 Home: true
95
96 `)
97 }
98
99 func TestTry(t *testing.T) {
100 t.Parallel()
101
102 files := `
103 -- hugo.toml --
104 baseURL = 'http://example.com/'
105 -- layouts/home.html --
106 Home.
107 {{ $g := try ("hello = \"Hello Hugo\"" | transform.Unmarshal) }}
108 {{ with $g.Err }}
109 Err1: {{ . }}
110 {{ else }}
111 Value1: {{ $g.Value.hello | safeHTML }}|
112 {{ end }}
113 {{ $g := try ("hello != \"Hello Hugo\"" | transform.Unmarshal) }}
114 {{ with $g.Err }}
115 Err2: {{ . | safeHTML }}
116 {{ else }}
117 Value2: {{ $g.Value.hello | safeHTML }}|
118 {{ end }}
119 Try upper: {{ (try ("hello" | upper)).Value }}
120 Try printf: {{ (try (printf "hello %s" "world")).Value }}
121 `
122
123 b := hugolib.Test(t, files)
124
125 b.AssertFileContent("public/index.html",
126 "Value1: Hello Hugo|",
127 "Err2: template: home.html:",
128 "Try upper: HELLO",
129 "Try printf: hello world",
130 )
131 }
132
133 func TestTemplatesCurrent(t *testing.T) {
134 t.Parallel()
135
136 files := `
137 -- hugo.toml --
138 -- layouts/baseof.html --
139 baseof: {{ block "main" . }}{{ end }}
140 -- layouts/all.html --
141 {{ define "main" }}
142 all.current: {{ templates.Current.Name }}
143 all.current.filename: {{ templates.Current.Filename }}
144 all.base: {{ with templates.Current.Base }}{{ .Name }}{{ end }}|
145 all.parent: {{ with .Parent }}Name: {{ .Name }}{{ end }}|
146 {{ partial "p1.html" . }}
147 {{ end }}
148 -- layouts/_partials/p1.html --
149 p1.current: {{ with templates.Current }}Name: {{ .Name }}|{{ with .Parent }}Parent.Name: {{ .Name }}{{ end }}{{ end }}|
150 p1.current.Ancestors: {{ with templates.Current }}{{ range .Ancestors }}{{ .Name }}|{{ end }}{{ end }}
151 {{ partial "p2.html" . }}
152 -- layouts/_partials/p2.html --
153 p2.current: {{ with templates.Current }}Name: {{ .Name }}|{{ with .Parent }}Parent.Name: {{ .Name }}{{ end }}{{ end }}|
154 p2.current.Ancestors: {{ with templates.Current }}{{ range .Ancestors }}{{ .Name }}|{{ end }}{{ end }}
155 p3.current.Ancestors.Reverse: {{ with templates.Current }}{{ range .Ancestors.Reverse }}{{ .Name }}|{{ end }}{{ end }}
156
157 `
158 b := hugolib.Test(t, files)
159
160 b.AssertFileContent("public/index.html",
161 "all.current: all.html",
162 filepath.FromSlash("all.current.filename: /layouts/all.html"),
163 "all.base: baseof.html",
164 "all.parent: |",
165 "p1.current: Name: _partials/p1.html|Parent.Name: all.html|",
166 "p1.current.Ancestors: all.html|",
167 "p2.current.Ancestors: _partials/p1.html|all.html",
168 )
169 }
170
171 func TestBaseOfIssue13583(t *testing.T) {
172 t.Parallel()
173
174 files := `
175 -- hugo.toml --
176 -- content/_index.md --
177 ---
178 title: "Home"
179 outputs: ["html", "amp"]
180 ---
181 title: "Home"
182 -- layouts/baseof.html --
183 layouts/baseof.html
184 {{ block "main" . }}{{ end }}
185 -- layouts/baseof.amp.html --
186 layouts/baseof.amp.html
187 {{ block "main" . }}{{ end }}
188 -- layouts/home.html --
189 {{ define "main" }}
190 Home.
191 {{ end }}
192
193 `
194 b := hugolib.Test(t, files)
195 b.AssertFileContent("public/index.html", "layouts/baseof.html")
196 b.AssertFileContent("public/amp/index.html", "layouts/baseof.amp.html")
197 }
198
199 func TestAllVsAmp(t *testing.T) {
200 t.Parallel()
201
202 files := `
203 -- hugo.toml --
204 -- content/_index.md --
205 ---
206 title: "Home"
207 outputs: ["html", "amp"]
208 ---
209 title: "Home"
210 -- layouts/all.html --
211 All.
212
213 `
214 b := hugolib.Test(t, files)
215 b.AssertFileContent("public/index.html", "All.")
216 b.AssertFileContent("public/amp/index.html", "All.")
217 }
218
219 // Issue #13584.
220 func TestLegacySectionSection(t *testing.T) {
221 t.Parallel()
222
223 files := `
224 -- hugo.toml --
225 -- content/mysection/_index.md --
226 -- layouts/section/section.html --
227 layouts/section/section.html
228
229 `
230 b := hugolib.Test(t, files)
231 b.AssertFileContent("public/mysection/index.html", "layouts/section/section.html")
232 }
233
234 func TestErrorMessageParseError(t *testing.T) {
235 t.Parallel()
236
237 files := `
238 -- hugo.toml --
239 -- layouts/home.html --
240 Line 1.
241 Line 2. {{ foo }} <- this func does not exist.
242 Line 3.
243 `
244
245 b, err := hugolib.TestE(t, files)
246 b.Assert(err, qt.IsNotNil)
247 b.Assert(err.Error(), qt.Contains, filepath.FromSlash(`"/layouts/home.html:2:1": parse of template failed: template: home.html:2: function "foo" not defined`))
248 }
249
250 func TestErrorMessageExecuteError(t *testing.T) {
251 t.Parallel()
252
253 files := `
254 -- hugo.toml --
255 -- layouts/home.html --
256 Line 1.
257 Line 2. {{ .Foo }} <- this method does not exist.
258 Line 3.
259 `
260
261 b, err := hugolib.TestE(t, files)
262 b.Assert(err, qt.IsNotNil)
263 b.Assert(err.Error(), qt.Contains, filepath.FromSlash(` "/layouts/home.html:2:11": execute of template failed`))
264 }
265
266 func TestPartialReturnPanicIssue13600(t *testing.T) {
267 t.Parallel()
268
269 files := `
270 -- hugo.toml --
271 -- layouts/home.html --
272 Partial: {{ partial "p1.html" . }}
273 -- layouts/_partials/p1.html --
274 P1.
275 {{ return ( delimit . ", " ) | string }}
276 `
277
278 b, err := hugolib.TestE(t, files)
279 b.Assert(err, qt.IsNotNil)
280 b.Assert(err.Error(), qt.Contains, "return must be the last command in a pipeline")
281 }
282
283 func TestPartialWithoutSuffixIssue13601(t *testing.T) {
284 t.Parallel()
285
286 files := `
287 -- hugo.toml --
288 -- layouts/home.html --
289 P1: {{ partial "p1" . }}
290 P2: {{ partial "p2" . }}
291 -- layouts/_partials/p1 --
292 P1.
293 -- layouts/_partials/p2 --
294 P2.
295 {{ return "foo bar" }}
296
297 `
298
299 b := hugolib.Test(t, files)
300 b.AssertFileContent("public/index.html", "P1: P1.\nP2: foo bar")
301 }
302
303 func TestTemplateExistsCaseIssue13684(t *testing.T) {
304 t.Parallel()
305
306 files := `
307 -- hugo.toml --
308 -- layouts/home.html --
309 P1: {{ templates.Exists "_partials/MyPartial.html" }}|P1: {{ templates.Exists "_partials/mypartial.html" }}|
310 -- layouts/_partials/MyPartial.html --
311 MyPartial.
312
313 `
314
315 b := hugolib.Test(t, files)
316 b.AssertFileContent("public/index.html", "P1: true|P1: true|")
317 }
318
319 func TestYAMLAddDateIssue14079(t *testing.T) {
320 t.Parallel()
321
322 files := `
323 -- hugo.toml --
324 disableKinds = ["page", "section", "taxonomy", "term", "sitemap", "RSS"]
325 -- assets/mydata.yaml --
326 myinteger: 2
327 -- layouts/all.html --
328 {{ $date := "2023-10-15T13:18:50-07:00" | time }}
329 {{ $mydata := resources.Get "mydata.yaml" | transform.Unmarshal }}
330 date: {{ $date | time.Format "2006-01-02" }}|
331 date+2y: {{ $date.AddDate $mydata.myinteger 0 0 | time.Format "2006-01-02" }}|
332 `
333
334 b := hugolib.Test(t, files)
335
336 b.AssertFileContent("public/index.html", "date: 2023-10-15", "date+2y: 2025-10-15")
337 }
338
339 func TestTOMLAddDateIssue14079(t *testing.T) {
340 t.Parallel()
341
342 files := `
343 -- hugo.toml --
344 disableKinds = ["page", "section", "taxonomy", "term", "sitemap", "RSS"]
345 -- assets/mydata.toml --
346 myinteger = 2
347 -- layouts/all.html --
348 {{ $date := "2023-10-15T13:18:50-07:00" | time }}
349 {{ $mydata := resources.Get "mydata.toml" | transform.Unmarshal }}
350 date: {{ $date | time.Format "2006-01-02" }}|
351 date+2y: {{ $date.AddDate $mydata.myinteger 0 0 | time.Format "2006-01-02" }}|
352 `
353
354 b := hugolib.Test(t, files)
355
356 b.AssertFileContent("public/index.html", "date: 2023-10-15", "date+2y: 2025-10-15")
357 }
358
359 func TestJSONAddDateIssue14079(t *testing.T) {
360 t.Parallel()
361
362 files := `
363 -- hugo.toml --
364 disableKinds = ["page", "section", "taxonomy", "term", "sitemap", "RSS"]
365 -- assets/mydata.json --
366 {
367 "myinteger": 2
368 }
369 -- layouts/all.html --
370 {{ $date := "2023-10-15T13:18:50-07:00" | time }}
371 {{ $mydata := resources.Get "mydata.json" | transform.Unmarshal }}
372 date: {{ $date | time.Format "2006-01-02" }}|
373 date+2y: {{ $date.AddDate $mydata.myinteger 0 0 | time.Format "2006-01-02" }}|
374 `
375
376 b := hugolib.Test(t, files)
377
378 b.AssertFileContent("public/index.html", "date: 2023-10-15", "date+2y: 2025-10-15")
379 }