postcss: Fix line numbers in error messages

Fixes #9880
This commit is contained in:
Bjørn Erik Pedersen 2022-05-12 10:09:15 +02:00
parent 2fbdee7268
commit e8537e6dd0
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
2 changed files with 63 additions and 51 deletions

View file

@ -22,19 +22,15 @@ import (
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
qt "github.com/frankban/quicktest" qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/htesting" "github.com/gohugoio/hugo/htesting"
"github.com/gohugoio/hugo/hugolib" "github.com/gohugoio/hugo/hugolib"
) )
func TestTransformPostCSS(t *testing.T) { const postCSSIntegrationTestFiles = `
if !htesting.IsCI() {
t.Skip("Skip long running test when running locally")
}
c := qt.New(t)
files := `
-- assets/css/components/a.css -- -- assets/css/components/a.css --
/* A comment. */
/* Another comment. */
class-in-a { class-in-a {
color: blue; color: blue;
} }
@ -98,14 +94,20 @@ module.exports = {
` `
c.Run("Success", func(c *qt.C) { func TestTransformPostCSS(t *testing.T) {
if !htesting.IsCI() {
t.Skip("Skip long running test when running locally")
}
c := qt.New(t)
b := hugolib.NewIntegrationTestBuilder( b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{ hugolib.IntegrationTestConfig{
T: c, T: c,
NeedsOsFS: true, NeedsOsFS: true,
NeedsNpmInstall: true, NeedsNpmInstall: true,
LogLevel: jww.LevelInfo, LogLevel: jww.LevelInfo,
TxtarString: files, TxtarString: postCSSIntegrationTestFiles,
}).Build() }).Build()
b.AssertLogContains("Hugo Environment: production") b.AssertLogContains("Hugo Environment: production")
@ -116,33 +118,33 @@ module.exports = {
Styles RelPermalink: /css/styles.css Styles RelPermalink: /css/styles.css
Styles Content: Len: 770875| Styles Content: Len: 770875|
`) `)
})
c.Run("Error", func(c *qt.C) { }
// 9880
func TestTransformPostCSSError(t *testing.T) {
if !htesting.IsCI() {
t.Skip("Skip long running test when running locally")
}
c := qt.New(t)
s, err := hugolib.NewIntegrationTestBuilder( s, err := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{ hugolib.IntegrationTestConfig{
T: c, T: c,
NeedsOsFS: true, NeedsOsFS: true,
NeedsNpmInstall: true, NeedsNpmInstall: true,
TxtarString: strings.ReplaceAll(files, "color: blue;", "@apply foo;"), // Syntax error TxtarString: strings.ReplaceAll(postCSSIntegrationTestFiles, "color: blue;", "@apply foo;"), // Syntax error
}).BuildE() }).BuildE()
s.AssertIsFileError(err) s.AssertIsFileError(err)
}) fe := herrors.UnwrapFileError(err)
} pos := fe.Position()
c.Assert(strings.TrimPrefix(pos.Filename, s.H.WorkingDir), qt.Equals, filepath.FromSlash("/assets/css/components/a.css"))
// bookmark2 c.Assert(pos.LineNumber, qt.Equals, 4)
func TestIntegrationTestTemplate(t *testing.T) { errctx := fe.ErrorContext()
c := qt.New(t) c.Assert(errctx, qt.IsNotNil)
c.Assert(errctx.Lines, qt.DeepEquals, []string{"/* Another comment. */", "class-in-a {", "\t@apply foo;", "}", ""})
files := `` c.Assert(errctx.ChromaLexer, qt.Equals, "css")
b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: c,
NeedsOsFS: false,
NeedsNpmInstall: false,
TxtarString: files,
}).Build()
b.Assert(true, qt.IsTrue)
} }

View file

@ -28,6 +28,7 @@ import (
"github.com/gohugoio/hugo/common/collections" "github.com/gohugoio/hugo/common/collections"
"github.com/gohugoio/hugo/common/hexec" "github.com/gohugoio/hugo/common/hexec"
"github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/common/hugo" "github.com/gohugoio/hugo/common/hugo"
@ -39,8 +40,6 @@ import (
"errors" "errors"
"github.com/gohugoio/hugo/hugofs"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"github.com/gohugoio/hugo/common/herrors" "github.com/gohugoio/hugo/common/herrors"
@ -391,8 +390,19 @@ func (imp *importResolver) toFileError(output string) error {
return inErr return inErr
} }
realFilename := fi.(hugofs.FileMetaInfo).Meta().Filename meta := fi.(hugofs.FileMetaInfo).Meta()
realFilename := meta.Filename
f, err := meta.Open()
if err != nil {
return inErr
}
defer f.Close()
return herrors.NewFileErrorFromFile(inErr, file.Filename, realFilename, hugofs.Os, herrors.SimpleLineMatcher) ferr := herrors.NewFileError(realFilename, inErr)
pos := ferr.Position()
pos.LineNumber = file.Offset + 1
return ferr.UpdatePosition(pos).UpdateContent(f, nil)
//return herrors.NewFileErrorFromFile(inErr, file.Filename, realFilename, hugofs.Os, herrors.SimpleLineMatcher)
} }