file_error_test.go (3077B)
1 // Copyright 2022 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 herrors
15
16 import (
17 "fmt"
18 "strings"
19 "testing"
20
21 "errors"
22
23 "github.com/gohugoio/hugo/common/text"
24
25 qt "github.com/frankban/quicktest"
26 )
27
28 func TestNewFileError(t *testing.T) {
29 t.Parallel()
30
31 c := qt.New(t)
32
33 fe := NewFileErrorFromName(errors.New("bar"), "foo.html")
34 c.Assert(fe.Error(), qt.Equals, `"foo.html:1:1": bar`)
35
36 lines := ""
37 for i := 1; i <= 100; i++ {
38 lines += fmt.Sprintf("line %d\n", i)
39 }
40
41 fe.UpdatePosition(text.Position{LineNumber: 32, ColumnNumber: 2})
42 c.Assert(fe.Error(), qt.Equals, `"foo.html:32:2": bar`)
43 fe.UpdatePosition(text.Position{LineNumber: 0, ColumnNumber: 0, Offset: 212})
44 fe.UpdateContent(strings.NewReader(lines), nil)
45 c.Assert(fe.Error(), qt.Equals, `"foo.html:32:0": bar`)
46 errorContext := fe.ErrorContext()
47 c.Assert(errorContext, qt.IsNotNil)
48 c.Assert(errorContext.Lines, qt.DeepEquals, []string{"line 30", "line 31", "line 32", "line 33", "line 34"})
49 c.Assert(errorContext.LinesPos, qt.Equals, 2)
50 c.Assert(errorContext.ChromaLexer, qt.Equals, "go-html-template")
51
52 }
53
54 func TestNewFileErrorExtractFromMessage(t *testing.T) {
55 t.Parallel()
56
57 c := qt.New(t)
58
59 for i, test := range []struct {
60 in error
61 offset int
62 lineNumber int
63 columnNumber int
64 }{
65 {errors.New("no line number for you"), 0, 1, 1},
66 {errors.New(`template: _default/single.html:4:15: executing "_default/single.html" at <.Titles>: can't evaluate field Titles in type *hugolib.PageOutput`), 0, 4, 15},
67 {errors.New("parse failed: template: _default/bundle-resource-meta.html:11: unexpected in operand"), 0, 11, 1},
68 {errors.New(`failed:: template: _default/bundle-resource-meta.html:2:7: executing "main" at <.Titles>`), 0, 2, 7},
69 {errors.New(`failed to load translations: (6, 7): was expecting token =, but got "g" instead`), 0, 6, 7},
70 {errors.New(`execute of template failed: template: index.html:2:5: executing "index.html" at <partial "foo.html" .>: error calling partial: "/layouts/partials/foo.html:3:6": execute of template failed: template: partials/foo.html:3:6: executing "partials/foo.html" at <.ThisDoesNotExist>: can't evaluate field ThisDoesNotExist in type *hugolib.pageStat`), 0, 2, 5},
71 } {
72
73 got := NewFileErrorFromName(test.in, "test.txt")
74
75 errMsg := qt.Commentf("[%d][%T]", i, got)
76
77 pos := got.Position()
78 c.Assert(pos.LineNumber, qt.Equals, test.lineNumber, errMsg)
79 c.Assert(pos.ColumnNumber, qt.Equals, test.columnNumber, errMsg)
80 c.Assert(errors.Unwrap(got), qt.Not(qt.IsNil))
81 }
82 }