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