diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-10-03 14:58:09 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-10-16 22:10:56 +0200 |
commit | 35fbfb19a173b01bc881f2bbc5d104136633a7ec (patch) | |
tree | 636d0d51fa262dc808eb3c5cc9cf92ad977a0c6a /common/herrors/error_locator_test.go | |
parent | 3a3089121b852332b5744d1f566959c8cf93cef4 (diff) |
commands: Show server error info in browser
The main item in this commit is showing of errors with a file context when running `hugo server`.
This can be turned off: `hugo server --disableBrowserError` (can also be set in `config.toml`).
But to get there, the error handling in Hugo needed a revision. There are some items left TODO for commits soon to follow, most notable errors in content and config files.
Fixes #5284
Fixes #5290
See #5325
See #5324
Diffstat (limited to 'common/herrors/error_locator_test.go')
-rw-r--r-- | common/herrors/error_locator_test.go | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/common/herrors/error_locator_test.go b/common/herrors/error_locator_test.go new file mode 100644 index 000000000..6c879727e --- /dev/null +++ b/common/herrors/error_locator_test.go @@ -0,0 +1,112 @@ +// 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" +) + +func TestErrorLocator(t *testing.T) { + assert := require.New(t) + + lineMatcher := func(le FileError, lineno int, line string) bool { + return strings.Contains(line, "THEONE") + } + + lines := `LINE 1 +LINE 2 +LINE 3 +LINE 4 +This is THEONE +LINE 6 +LINE 7 +LINE 8 +` + + location := locateErrorInString(nil, lines, lineMatcher) + assert.Equal([]string{"LINE 3", "LINE 4", "This is THEONE", "LINE 6", "LINE 7"}, location.Lines) + + assert.Equal(3, location.LineNumber) + assert.Equal(2, location.Pos) + + assert.Equal([]string{"This is THEONE"}, locateErrorInString(nil, `This is THEONE`, lineMatcher).Lines) + + location = locateErrorInString(nil, `L1 +This is THEONE +L2 +`, lineMatcher) + assert.Equal(1, location.Pos) + assert.Equal([]string{"L1", "This is THEONE", "L2"}, location.Lines) + + location = locateErrorInString(nil, `This is THEONE +L2 +`, lineMatcher) + assert.Equal(0, location.Pos) + assert.Equal([]string{"This is THEONE", "L2"}, location.Lines) + + location = locateErrorInString(nil, `L1 +This THEONE +`, lineMatcher) + assert.Equal([]string{"L1", "This THEONE"}, location.Lines) + assert.Equal(1, location.Pos) + + location = locateErrorInString(nil, `L1 +L2 +This THEONE +`, lineMatcher) + assert.Equal([]string{"L1", "L2", "This THEONE"}, location.Lines) + assert.Equal(2, location.Pos) + + location = locateErrorInString(nil, "NO MATCH", lineMatcher) + assert.Equal(-1, location.LineNumber) + assert.Equal(-1, location.Pos) + assert.Equal(0, len(location.Lines)) + + lineMatcher = func(le FileError, lineno int, line string) bool { + return lineno == 6 + } + location = locateErrorInString(nil, `A +B +C +D +E +F +G +H +I +J`, lineMatcher) + + assert.Equal([]string{"D", "E", "F", "G", "H"}, location.Lines) + assert.Equal(4, location.LineNumber) + assert.Equal(2, location.Pos) + + // Test match EOF + lineMatcher = func(le FileError, lineno int, line string) bool { + return lineno == 4 + } + + location = locateErrorInString(nil, `A +B +C +`, lineMatcher) + + assert.Equal([]string{"B", "C", ""}, location.Lines) + assert.Equal(3, location.LineNumber) + assert.Equal(2, location.Pos) + +} |