Debian Codegraph @

hugolib/hugo_sites_build_errors_test.go

raw
1 package hugolib
2
3 import (
4 "fmt"
5 "path/filepath"
6 "strings"
7 "testing"
8
9 qt "github.com/frankban/quicktest"
10 "github.com/gohugoio/hugo/common/herrors"
11 )
12
13 type testSiteBuildErrorAsserter struct {
14 name string
15 c *qt.C
16 }
17
18 func (t testSiteBuildErrorAsserter) getFileError(err error) herrors.FileError {
19 t.c.Assert(err, qt.Not(qt.IsNil), qt.Commentf(t.name))
20 fe := herrors.UnwrapFileError(err)
21 t.c.Assert(fe, qt.Not(qt.IsNil))
22 return fe
23 }
24
25 func (t testSiteBuildErrorAsserter) assertLineNumber(lineNumber int, err error) {
26 t.c.Helper()
27 fe := t.getFileError(err)
28 t.c.Assert(fe.Position().LineNumber, qt.Equals, lineNumber, qt.Commentf(err.Error()))
29 }
30
31 func (t testSiteBuildErrorAsserter) assertErrorMessage(e1, e2 string) {
32 // The error message will contain filenames with OS slashes. Normalize before compare.
33 e1, e2 = filepath.ToSlash(e1), filepath.ToSlash(e2)
34 t.c.Assert(e2, qt.Contains, e1)
35 }
36
37 func TestSiteBuildErrors(t *testing.T) {
38 const (
39 yamlcontent = "yamlcontent"
40 tomlcontent = "tomlcontent"
41 jsoncontent = "jsoncontent"
42 shortcode = "shortcode"
43 base = "base"
44 single = "single"
45 )
46
47 type testCase struct {
48 name string
49 fileType string
50 fileFixer func(content string) string
51 assertErr func(a testSiteBuildErrorAsserter, err error)
52 }
53
54 createTestFiles := func(tc testCase) string {
55 f := func(ftype, content string) string {
56 if ftype != tc.fileType {
57 return content
58 }
59 return tc.fileFixer(content)
60 }
61
62 return `
63 -- hugo.toml --
64 baseURL = "https://example.com"
65 -- layouts/_shortcodes/sc.html --
66 ` + f(shortcode, `SHORTCODE L1
67 SHORTCODE L2
68 SHORTCODE L3:
69 SHORTCODE L4: {{ .Page.Title }}
70 `) + `
71 -- layouts/baseof.html --
72 ` + f(base, `BASEOF L1
73 BASEOF L2
74 BASEOF L3
75 BASEOF L4{{ if .Title }}{{ end }}
76 {{block "main" .}}This is the main content.{{end}}
77 BASEOF L6
78 `) + `
79 -- layouts/single.html --
80 ` + f(single, `{{ define "main" }}
81 SINGLE L2:
82 SINGLE L3:
83 SINGLE L4:
84 SINGLE L5: {{ .Title }} {{ .Content }}
85 {{ end }}
86 `) + `
87 -- layouts/foo/single.html --
88 ` + f(single, `
89 SINGLE L2:
90 SINGLE L3:
91 SINGLE L4:
92 SINGLE L5: {{ .Title }} {{ .Content }}
93 `) + `
94 -- content/myyaml.md --
95 ` + f(yamlcontent, `---
96 title: "The YAML"
97 ---
98
99 Some content.
100
101 {{< sc >}}
102
103 Some more text.
104
105 The end.
106 `) + `
107 -- content/mytoml.md --
108 ` + f(tomlcontent, `+++
109 title = "The TOML"
110 p1 = "v"
111 p2 = "v"
112 p3 = "v"
113 description = "Descriptioon"
114 +++
115
116 Some content.
117 `) + `
118 -- content/myjson.md --
119 ` + f(jsoncontent, `{
120 "title": "This is a title",
121 "description": "This is a description."
122 }
123
124 Some content.
125 `)
126 }
127
128 tests := []testCase{
129 {
130 name: "Base template parse failed",
131 fileType: base,
132 fileFixer: func(content string) string {
133 return strings.Replace(content, ".Title }}", ".Title }", 1)
134 },
135 assertErr: func(a testSiteBuildErrorAsserter, err error) {
136 a.assertLineNumber(4, err)
137 },
138 },
139 {
140 name: "Base template execute failed",
141 fileType: base,
142 fileFixer: func(content string) string {
143 return strings.Replace(content, ".Title", ".Titles", 1)
144 },
145 assertErr: func(a testSiteBuildErrorAsserter, err error) {
146 a.assertLineNumber(4, err)
147 },
148 },
149 {
150 name: "Single template parse failed",
151 fileType: single,
152 fileFixer: func(content string) string {
153 return strings.Replace(content, ".Title }}", ".Title }", 1)
154 },
155 assertErr: func(a testSiteBuildErrorAsserter, err error) {
156 fe := a.getFileError(err)
157 a.c.Assert(fe.Position().LineNumber, qt.Equals, 5)
158 a.c.Assert(fe.Position().ColumnNumber, qt.Equals, 1)
159 a.assertErrorMessage("\"/layouts/foo/single.html:5:1\": parse of template failed: template: foo/single.html:5: unexpected \"}\" in operand", fe.Error())
160 },
161 },
162 {
163 name: "Single template execute failed",
164 fileType: single,
165 fileFixer: func(content string) string {
166 return strings.Replace(content, ".Title", ".Titles", 1)
167 },
168 assertErr: func(a testSiteBuildErrorAsserter, err error) {
169 fe := a.getFileError(err)
170 a.c.Assert(fe.Position().LineNumber, qt.Equals, 5)
171 a.c.Assert(fe.Position().ColumnNumber, qt.Equals, 14)
172 a.assertErrorMessage("\"layouts/single.html:5:14\": execute of template failed", fe.Error())
173 },
174 },
175 {
176 name: "Single template execute failed, long keyword",
177 fileType: single,
178 fileFixer: func(content string) string {
179 return strings.Replace(content, ".Title", ".ThisIsAVeryLongTitle", 1)
180 },
181 assertErr: func(a testSiteBuildErrorAsserter, err error) {
182 fe := a.getFileError(err)
183 a.c.Assert(fe.Position().LineNumber, qt.Equals, 5)
184 a.c.Assert(fe.Position().ColumnNumber, qt.Equals, 14)
185 a.assertErrorMessage("\"layouts/single.html:5:14\": execute of template failed", fe.Error())
186 },
187 },
188 {
189 name: "Shortcode parse failed",
190 fileType: shortcode,
191 fileFixer: func(content string) string {
192 return strings.Replace(content, ".Title }}", ".Title }", 1)
193 },
194 assertErr: func(a testSiteBuildErrorAsserter, err error) {
195 a.assertLineNumber(4, err)
196 },
197 },
198 {
199 name: "Shortcode execute failed",
200 fileType: shortcode,
201 fileFixer: func(content string) string {
202 return strings.Replace(content, ".Title", ".Titles", 1)
203 },
204 assertErr: func(a testSiteBuildErrorAsserter, err error) {
205 fe := a.getFileError(err)
206 // Make sure that it contains both the content file and template
207 a.assertErrorMessage(`"content/myyaml.md:7:10": failed to render shortcode "sc": failed to process shortcode: "layouts/_shortcodes/sc.html:4:22": execute of template failed: template: shortcodes/sc.html:4:22: executing "shortcodes/sc.html" at <.Page.Titles>: can't evaluate field Titles in type page.Page`, fe.Error())
208 a.c.Assert(fe.Position().LineNumber, qt.Equals, 7)
209 },
210 },
211 {
212 name: "Shortode does not exist",
213 fileType: yamlcontent,
214 fileFixer: func(content string) string {
215 return strings.Replace(content, "{{< sc >}}", "{{< nono >}}", 1)
216 },
217 assertErr: func(a testSiteBuildErrorAsserter, err error) {
218 fe := a.getFileError(err)
219 a.c.Assert(fe.Position().LineNumber, qt.Equals, 7)
220 a.c.Assert(fe.Position().ColumnNumber, qt.Equals, 10)
221 a.assertErrorMessage(`"content/myyaml.md:7:10": failed to extract shortcode: template for shortcode "nono" not found`, fe.Error())
222 },
223 },
224 {
225 name: "Invalid YAML front matter",
226 fileType: yamlcontent,
227 fileFixer: func(content string) string {
228 return `---
229 title: "My YAML Content"
230 foo bar
231 ---
232 `
233 },
234 assertErr: func(a testSiteBuildErrorAsserter, err error) {
235 a.assertLineNumber(3, err)
236 },
237 },
238 {
239 name: "Invalid TOML front matter",
240 fileType: tomlcontent,
241 fileFixer: func(content string) string {
242 return strings.Replace(content, "description = ", "description &", 1)
243 },
244 assertErr: func(a testSiteBuildErrorAsserter, err error) {
245 fe := a.getFileError(err)
246 a.c.Assert(fe.Position().LineNumber, qt.Equals, 6)
247 },
248 },
249 {
250 name: "Invalid JSON front matter",
251 fileType: jsoncontent,
252 fileFixer: func(content string) string {
253 return strings.Replace(content, "\"description\":", "\"description\"", 1)
254 },
255 assertErr: func(a testSiteBuildErrorAsserter, err error) {
256 fe := a.getFileError(err)
257 a.c.Assert(fe.Position().LineNumber, qt.Equals, 3)
258 },
259 },
260 {
261 // See https://github.com/gohugoio/hugo/issues/5327
262 name: "Panic in template Execute",
263 fileType: single,
264 fileFixer: func(content string) string {
265 return strings.Replace(content, ".Title", ".Parent.Parent.Parent", 1)
266 },
267
268 assertErr: func(a testSiteBuildErrorAsserter, err error) {
269 a.c.Assert(err, qt.Not(qt.IsNil))
270 fe := a.getFileError(err)
271 a.c.Assert(fe.Position().LineNumber, qt.Equals, 5)
272 a.c.Assert(fe.Position().ColumnNumber, qt.Equals, 21)
273 },
274 },
275 }
276
277 for _, test := range tests {
278 if test.name != "Base template parse failed" {
279 continue
280 }
281 t.Run(test.name, func(t *testing.T) {
282 t.Parallel()
283 c := qt.New(t)
284 errorAsserter := testSiteBuildErrorAsserter{
285 c: c,
286 name: test.name,
287 }
288
289 files := createTestFiles(test)
290
291 _, err := TestE(t, files)
292
293 if test.assertErr != nil {
294 test.assertErr(errorAsserter, err)
295 } else {
296 c.Assert(err, qt.IsNil)
297 }
298 })
299 }
300 }
301
302 // Issue 9852
303 func TestErrorMinify(t *testing.T) {
304 t.Parallel()
305
306 files := `
307 -- hugo.toml --
308 [minify]
309 minifyOutput = true
310
311 -- layouts/home.html --
312 <body>
313 <script>=;</script>
314 </body>
315
316 `
317
318 b, err := TestE(t, files)
319
320 b.Assert(err, qt.IsNotNil)
321 fes := herrors.UnwrapFileErrors(err)
322 b.Assert(len(fes), qt.Equals, 1)
323 fe := fes[0]
324 b.Assert(fe, qt.IsNotNil)
325 b.Assert(fe.Position().LineNumber, qt.Equals, 2)
326 b.Assert(fe.Position().ColumnNumber, qt.Equals, 9)
327 b.Assert(fe.Error(), qt.Contains, "unexpected = in expression on line 2 and column 9")
328 b.Assert(filepath.ToSlash(fe.Position().Filename), qt.Contains, "hugo-transform-error")
329 // os.Remove is not needed in txtar tests as the filesystem is ephemeral.
330 // b.Assert(os.Remove(fe.Position().Filename), qt.IsNil)
331 }
332
333 func TestErrorNestedRender(t *testing.T) {
334 t.Parallel()
335
336 files := `
337 -- hugo.toml --
338 -- content/_index.md --
339 ---
340 title: "Home"
341 ---
342 -- layouts/home.html --
343 line 1
344 line 2
345 1{{ .Render "myview" }}
346 -- layouts/myview.html --
347 line 1
348 12{{ partial "foo.html" . }}
349 line 4
350 line 5
351 -- layouts/_partials/foo.html --
352 line 1
353 line 2
354 123{{ .ThisDoesNotExist }}
355 line 4
356 `
357
358 b, err := TestE(t, files)
359
360 b.Assert(err, qt.IsNotNil)
361 errors := herrors.UnwrapFileErrorsWithErrorContext(err)
362 b.Assert(errors, qt.HasLen, 4)
363 b.Assert(errors[0].Position().LineNumber, qt.Equals, 3)
364 b.Assert(errors[0].Position().ColumnNumber, qt.Equals, 4)
365 b.Assert(errors[0].Error(), qt.Contains, filepath.FromSlash(`"/layouts/home.html:3:4": execute of template failed`))
366 b.Assert(errors[0].ErrorContext().Lines, qt.DeepEquals, []string{"line 1", "line 2", "1{{ .Render \"myview\" }}"})
367 b.Assert(errors[2].Position().LineNumber, qt.Equals, 2)
368 b.Assert(errors[2].Position().ColumnNumber, qt.Equals, 5)
369 b.Assert(errors[2].ErrorContext().Lines, qt.DeepEquals, []string{"line 1", "12{{ partial \"foo.html\" . }}", "line 4", "line 5"})
370
371 b.Assert(errors[3].Position().LineNumber, qt.Equals, 3)
372 b.Assert(errors[3].Position().ColumnNumber, qt.Equals, 6)
373 b.Assert(errors[3].ErrorContext().Lines, qt.DeepEquals, []string{"line 1", "line 2", "123{{ .ThisDoesNotExist }}", "line 4"})
374 }
375
376 func TestErrorNestedShortcode(t *testing.T) {
377 t.Parallel()
378
379 files := `
380 -- hugo.toml --
381 -- content/_index.md --
382 ---
383 title: "Home"
384 ---
385
386 ## Hello
387 {{< hello >}}
388
389 -- layouts/home.html --
390 line 1
391 line 2
392 {{ .Content }}
393 line 5
394 -- layouts/_shortcodes/hello.html --
395 line 1
396 12{{ partial "foo.html" . }}
397 line 4
398 line 5
399 -- layouts/_partials/foo.html --
400 line 1
401 line 2
402 123{{ .ThisDoesNotExist }}
403 line 4
404 `
405
406 b, err := TestE(t, files)
407
408 b.Assert(err, qt.IsNotNil)
409 errors := herrors.UnwrapFileErrorsWithErrorContext(err)
410
411 b.Assert(errors, qt.HasLen, 4)
412
413 b.Assert(errors[1].Position().LineNumber, qt.Equals, 6)
414 b.Assert(errors[1].Position().ColumnNumber, qt.Equals, 1)
415 b.Assert(errors[1].ErrorContext().ChromaLexer, qt.Equals, "md")
416 b.Assert(errors[1].Error(), qt.Contains, filepath.FromSlash(`"/content/_index.md:6:1": failed to render shortcode "hello": failed to process shortcode: "/layouts/_shortcodes/hello.html:2:5":`))
417 b.Assert(errors[1].ErrorContext().Lines, qt.DeepEquals, []string{"", "## Hello", "{{< hello >}}", ""})
418 b.Assert(errors[2].ErrorContext().Lines, qt.DeepEquals, []string{"line 1", "12{{ partial \"foo.html\" . }}", "line 4", "line 5"})
419 b.Assert(errors[3].Position().LineNumber, qt.Equals, 3)
420 b.Assert(errors[3].Position().ColumnNumber, qt.Equals, 6)
421 b.Assert(errors[3].ErrorContext().Lines, qt.DeepEquals, []string{"line 1", "line 2", "123{{ .ThisDoesNotExist }}", "line 4"})
422 }
423
424 func TestErrorRenderHookHeading(t *testing.T) {
425 t.Parallel()
426
427 files := `
428 -- hugo.toml --
429 -- content/_index.md --
430 ---
431 title: "Home"
432 ---
433
434 ## Hello
435
436 -- layouts/home.html --
437 line 1
438 line 2
439 {{ .Content }}
440 line 5
441 -- layouts/_markup/render-heading.html --
442 line 1
443 12{{ .Levels }}
444 line 4
445 line 5
446 `
447
448 b, err := TestE(t, files)
449
450 b.Assert(err, qt.IsNotNil)
451 errors := herrors.UnwrapFileErrorsWithErrorContext(err)
452
453 b.Assert(errors, qt.HasLen, 3)
454 b.Assert(errors[0].Error(), qt.Contains, filepath.FromSlash(`"/content/_index.md:2:5": "/layouts/_markup/render-heading.html:2:5": execute of template failed`))
455 }
456
457 func TestErrorRenderHookCodeblock(t *testing.T) {
458 t.Parallel()
459
460 files := `
461 -- hugo.toml --
462 -- content/_index.md --
463 ---
464 title: "Home"
465 ---
466
467 ## Hello
468
469 §§§ foo
470 bar
471 §§§
472
473
474 -- layouts/home.html --
475 line 1
476 line 2
477 {{ .Content }}
478 line 5
479 -- layouts/_markup/render-codeblock-foo.html --
480 line 1
481 12{{ .Foo }}
482 line 4
483 line 5
484 `
485
486 b, err := TestE(t, files)
487
488 b.Assert(err, qt.IsNotNil)
489 errors := herrors.UnwrapFileErrorsWithErrorContext(err)
490
491 b.Assert(errors, qt.HasLen, 3)
492 first := errors[0]
493 b.Assert(first.Error(), qt.Contains, filepath.FromSlash(`"/content/_index.md:7:1": "/layouts/_markup/render-codeblock-foo.html:2:5": execute of template failed`))
494 }
495
496 func TestErrorInBaseTemplate(t *testing.T) {
497 t.Parallel()
498
499 filesTemplate := `
500 -- hugo.toml --
501 -- content/_index.md --
502 ---
503 title: "Home"
504 ---
505 -- layouts/baseof.html --
506 line 1 base
507 line 2 base
508 {{ block "main" . }}empty{{ end }}
509 line 4 base
510 {{ block "toc" . }}empty{{ end }}
511 -- layouts/home.html --
512 {{ define "main" }}
513 line 2 index
514 line 3 index
515 line 4 index
516 {{ end }}
517 {{ define "toc" }}
518 TOC: {{ partial "toc.html" . }}
519 {{ end }}
520 -- layouts/_partials/toc.html --
521 toc line 1
522 toc line 2
523 toc line 3
524 toc line 4
525
526
527 `
528
529 t.Run("base template", func(t *testing.T) {
530 files := strings.Replace(filesTemplate, "line 4 base", "123{{ .ThisDoesNotExist \"abc\" }}", 1)
531
532 b, err := TestE(t, files)
533
534 b.Assert(err, qt.IsNotNil)
535 b.Assert(err.Error(), qt.Contains, `baseof.html:4:6`)
536 })
537
538 t.Run("home template", func(t *testing.T) {
539 files := strings.Replace(filesTemplate, "line 3 index", "1234{{ .ThisDoesNotExist \"abc\" }}", 1)
540
541 b, err := TestE(t, files)
542
543 b.Assert(err, qt.IsNotNil)
544 b.Assert(err.Error(), qt.Contains, `home.html:3:7"`)
545 })
546
547 t.Run("partial from define", func(t *testing.T) {
548 files := strings.Replace(filesTemplate, "toc line 2", "12345{{ .ThisDoesNotExist \"abc\" }}", 1)
549
550 b, err := TestE(t, files)
551
552 b.Assert(err, qt.IsNotNil)
553 b.Assert(err.Error(), qt.Contains, `toc.html:2:8"`)
554 })
555 }
556
557 // https://github.com/gohugoio/hugo/issues/5375
558 func TestSiteBuildTimeout(t *testing.T) {
559 t.Parallel()
560
561 var filesBuilder strings.Builder
562 filesBuilder.WriteString(`
563 -- hugo.toml --
564 timeout = '100ms'
565 -- layouts/single.html --
566 {{ .WordCount }}
567 -- layouts/_shortcodes/c.html --
568 {{ range .Page.Site.RegularPages }}
569 {{ .WordCount }}
570 {{ end }}
571 `)
572
573 for i := range 20 {
574 filesBuilder.WriteString(fmt.Sprintf(`
575 -- content/page%d.md --
576 ---
577 title: "A page"
578 ---
579
580 {{< c >}}
581 `, i))
582 }
583
584 _, err := TestE(t, filesBuilder.String())
585
586 qt.Assert(t, err, qt.Not(qt.IsNil))
587 qt.Assert(t, err.Error(), qt.Contains, "timed out rendering the page content")
588 }
589
590 func TestErrorTemplateRuntime(t *testing.T) {
591 t.Parallel()
592
593 files := `
594 -- hugo.toml --
595 -- layouts/home.html --
596 Home.
597 {{ .ThisDoesNotExist }}
598 `
599
600 b, err := TestE(t, files)
601
602 b.Assert(err, qt.Not(qt.IsNil))
603 b.Assert(err.Error(), qt.Contains, filepath.FromSlash(`/layouts/home.html:2:3`))
604 b.Assert(err.Error(), qt.Contains, `can't evaluate field ThisDoesNotExist`)
605 }
606
607 func TestErrorFrontmatterYAMLSyntax(t *testing.T) {
608 t.Parallel()
609
610 files := `
611 -- hugo.toml --
612 -- content/_index.md --
613
614
615
616
617
618 ---
619 line1: 'value1'
620 x
621 line2: 'value2'
622 line3: 'value3'
623 ---
624 `
625
626 b, err := TestE(t, files)
627
628 b.Assert(err, qt.Not(qt.IsNil))
629 b.Assert(err.Error(), qt.Contains, "[2:1] non-map value is specified")
630 fes := herrors.UnwrapFileErrors(err)
631 b.Assert(len(fes), qt.Equals, 1)
632 fe := fes[0]
633 b.Assert(fe, qt.Not(qt.IsNil))
634 pos := fe.Position()
635 b.Assert(pos.Filename, qt.Contains, filepath.FromSlash("content/_index.md"))
636 b.Assert(fe.ErrorContext(), qt.Not(qt.IsNil))
637 b.Assert(pos.LineNumber, qt.Equals, 8)
638 b.Assert(pos.ColumnNumber, qt.Equals, 1)
639 }