2018-10-03 08:58:09 -04:00
|
|
|
// Copyright 2018 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// Package errors contains common Hugo errors and error related utilities.
|
|
|
|
package herrors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2018-10-27 11:19:36 -04:00
|
|
|
func TestCreateFileLogFormatter(t *testing.T) {
|
|
|
|
assert := require.New(t)
|
|
|
|
|
|
|
|
ctx := ErrorContext{Filename: "/my/file.txt", LineNumber: 12, ColumnNumber: 13}
|
|
|
|
|
|
|
|
assert.Equal("/my/file.txt|13|12", createFileLogFormatter(":file|:col|:line")(ctx))
|
|
|
|
assert.Equal("13|/my/file.txt|12", createFileLogFormatter(":col|:file|:line")(ctx))
|
|
|
|
assert.Equal("好:13", createFileLogFormatter("好::col")(ctx))
|
|
|
|
assert.Equal("\"/my/file.txt:12:13\"", createFileLogFormatter("")(ctx))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-03 08:58:09 -04:00
|
|
|
func TestErrorLocator(t *testing.T) {
|
|
|
|
assert := require.New(t)
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
lineMatcher := func(m LineMatcher) bool {
|
|
|
|
return strings.Contains(m.Line, "THEONE")
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
lines := `LINE 1
|
|
|
|
LINE 2
|
|
|
|
LINE 3
|
|
|
|
LINE 4
|
|
|
|
This is THEONE
|
|
|
|
LINE 6
|
|
|
|
LINE 7
|
|
|
|
LINE 8
|
|
|
|
`
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
location := locateErrorInString(lines, lineMatcher)
|
2018-10-03 08:58:09 -04:00
|
|
|
assert.Equal([]string{"LINE 3", "LINE 4", "This is THEONE", "LINE 6", "LINE 7"}, location.Lines)
|
|
|
|
|
2018-10-21 06:20:21 -04:00
|
|
|
assert.Equal(5, location.LineNumber)
|
2018-10-03 08:58:09 -04:00
|
|
|
assert.Equal(2, location.Pos)
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
assert.Equal([]string{"This is THEONE"}, locateErrorInString(`This is THEONE`, lineMatcher).Lines)
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
location = locateErrorInString(`L1
|
2018-10-03 08:58:09 -04:00
|
|
|
This is THEONE
|
|
|
|
L2
|
|
|
|
`, lineMatcher)
|
2018-10-23 02:54:10 -04:00
|
|
|
assert.Equal(2, location.LineNumber)
|
2018-10-03 08:58:09 -04:00
|
|
|
assert.Equal(1, location.Pos)
|
2018-10-23 02:54:10 -04:00
|
|
|
assert.Equal([]string{"L1", "This is THEONE", "L2", ""}, location.Lines)
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
location = locateErrorInString(`This is THEONE
|
2018-10-03 08:58:09 -04:00
|
|
|
L2
|
|
|
|
`, lineMatcher)
|
|
|
|
assert.Equal(0, location.Pos)
|
2018-10-23 02:54:10 -04:00
|
|
|
assert.Equal([]string{"This is THEONE", "L2", ""}, location.Lines)
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
location = locateErrorInString(`L1
|
2018-10-03 08:58:09 -04:00
|
|
|
This THEONE
|
|
|
|
`, lineMatcher)
|
2018-10-23 02:54:10 -04:00
|
|
|
assert.Equal([]string{"L1", "This THEONE", ""}, location.Lines)
|
2018-10-03 08:58:09 -04:00
|
|
|
assert.Equal(1, location.Pos)
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
location = locateErrorInString(`L1
|
2018-10-03 08:58:09 -04:00
|
|
|
L2
|
|
|
|
This THEONE
|
|
|
|
`, lineMatcher)
|
2018-10-23 02:54:10 -04:00
|
|
|
assert.Equal([]string{"L1", "L2", "This THEONE", ""}, location.Lines)
|
2018-10-03 08:58:09 -04:00
|
|
|
assert.Equal(2, location.Pos)
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
location = locateErrorInString("NO MATCH", lineMatcher)
|
2018-10-03 08:58:09 -04:00
|
|
|
assert.Equal(-1, location.LineNumber)
|
|
|
|
assert.Equal(-1, location.Pos)
|
|
|
|
assert.Equal(0, len(location.Lines))
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
lineMatcher = func(m LineMatcher) bool {
|
|
|
|
return m.LineNumber == 6
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
2018-10-23 02:54:10 -04:00
|
|
|
|
|
|
|
location = locateErrorInString(`A
|
2018-10-03 08:58:09 -04:00
|
|
|
B
|
|
|
|
C
|
|
|
|
D
|
|
|
|
E
|
|
|
|
F
|
|
|
|
G
|
|
|
|
H
|
|
|
|
I
|
|
|
|
J`, lineMatcher)
|
|
|
|
|
|
|
|
assert.Equal([]string{"D", "E", "F", "G", "H"}, location.Lines)
|
2018-10-21 06:20:21 -04:00
|
|
|
assert.Equal(6, location.LineNumber)
|
2018-10-03 08:58:09 -04:00
|
|
|
assert.Equal(2, location.Pos)
|
|
|
|
|
|
|
|
// Test match EOF
|
2018-10-23 02:54:10 -04:00
|
|
|
lineMatcher = func(m LineMatcher) bool {
|
|
|
|
return m.LineNumber == 4
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
location = locateErrorInString(`A
|
2018-10-03 08:58:09 -04:00
|
|
|
B
|
|
|
|
C
|
|
|
|
`, lineMatcher)
|
|
|
|
|
|
|
|
assert.Equal([]string{"B", "C", ""}, location.Lines)
|
2018-10-21 06:20:21 -04:00
|
|
|
assert.Equal(4, location.LineNumber)
|
2018-10-03 08:58:09 -04:00
|
|
|
assert.Equal(2, location.Pos)
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
offsetMatcher := func(m LineMatcher) bool {
|
|
|
|
return m.Offset == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
location = locateErrorInString(`A
|
|
|
|
B
|
|
|
|
C
|
|
|
|
D
|
|
|
|
E`, offsetMatcher)
|
|
|
|
|
|
|
|
assert.Equal([]string{"A", "B", "C", "D"}, location.Lines)
|
|
|
|
assert.Equal(2, location.LineNumber)
|
|
|
|
assert.Equal(1, location.Pos)
|
|
|
|
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|