From d48cd9f4e36773b0161eb2460fa23cd9c2d2b73d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Thu, 17 Mar 2022 12:52:05 +0100 Subject: [PATCH 1/2] Add support for log events (e.g. @debug) --- go.mod | 2 +- go.sum | 14 ++++++++++++++ options.go | 26 ++++++++++++++++++++++++++ transpiler.go | 24 +++++++++++++++++++++++- transpiler_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 102 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 13c1fee..217e12f 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,6 @@ go 1.15 require ( github.com/cli/safeexec v1.0.0 - github.com/frankban/quicktest v1.11.2 + github.com/frankban/quicktest v1.14.2 google.golang.org/protobuf v1.25.0 ) diff --git a/go.sum b/go.sum index 0749443..0f0a582 100644 --- a/go.sum +++ b/go.sum @@ -4,10 +4,13 @@ github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA github.com/cli/safeexec v1.0.0 h1:0VngyaIyqACHdcMNWfo6+KdUYnqEr2Sg+bSP1pdF+dI= github.com/cli/safeexec v1.0.0/go.mod h1:Z/D4tTN8Vs5gXYHDCbaM1S/anmEDnJb1iW0+EJ5zx3Q= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/frankban/quicktest v1.11.2 h1:mjwHjStlXWibxOohM7HYieIViKyh56mmt3+6viyhDDI= github.com/frankban/quicktest v1.11.2/go.mod h1:K+q6oSqb0W0Ininfk863uOk1lMy69l/P6txr3mVT54s= +github.com/frankban/quicktest v1.14.2 h1:SPb1KFFmM+ybpEjPUhCCkZOM5xlovT5UbrMvWnXyBns= +github.com/frankban/quicktest v1.14.2/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -25,12 +28,21 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= +github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k= +github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= @@ -70,5 +82,7 @@ google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2 google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/options.go b/options.go index d3ef2cf..8ecc46d 100644 --- a/options.go +++ b/options.go @@ -24,6 +24,32 @@ type Options struct { // as running this code against the beta5 binary would hang // on Execute. Timeout time.Duration + + // LogEventHandler will, if set, receive log events from Dart Sass, + // e.g. @debug and @warn log statements. + LogEventHandler func(LogEvent) +} + +// LogEvent is a type of log event from Dart Sass. +type LogEventType int + +const ( + // Usually triggered by the @warn directive. + LogEventTypeWarning LogEventType = iota + + // Events trigered for usage of deprecated Sass features. + LogEventTypeDeprecated + + // Triggered by the @debug directive. + LogEventTypeDebug +) + +type LogEvent struct { + // Type is the type of log event. + Type LogEventType + + // Message on the form url:line:col message. + Message string } func (opts *Options) init() error { diff --git a/transpiler.go b/transpiler.go index 8988de3..f85fac4 100644 --- a/transpiler.go +++ b/transpiler.go @@ -339,7 +339,29 @@ func (t *Transpiler) input() { }, ) case *embeddedsass.OutboundMessage_LogEvent_: - // Drop these for now. + if t.opts.LogEventHandler != nil { + var logEvent LogEvent + e := c.LogEvent + if e.Span != nil { + url := e.Span.Url + if url == "" { + url = "stdin" + } + logEvent = LogEvent{ + Type: LogEventType(e.Type), + Message: fmt.Sprintf("%s:%d:%d: %s", url, e.Span.Start.Line, e.Span.Start.Column, c.LogEvent.GetMessage()), + } + } else { + logEvent = LogEvent{ + Type: LogEventType(e.Type), + Message: e.GetMessage(), + } + } + + t.opts.LogEventHandler(logEvent) + + } + case *embeddedsass.OutboundMessage_Error: err = fmt.Errorf("SASS error: %s", c.Error.GetMessage()) default: diff --git a/transpiler_test.go b/transpiler_test.go index cbd8f31..f6f7374 100644 --- a/transpiler_test.go +++ b/transpiler_test.go @@ -99,6 +99,7 @@ body {"Error in ImportResolver.Load", Options{}, Args{Source: "@import \"colors\";", ImportResolver: testImportResolver{name: "colors", failOnLoad: true}}, false}, {"Invalid OutputStyle", Options{}, Args{Source: "a", OutputStyle: "asdf"}, false}, {"Invalid SourceSyntax", Options{}, Args{Source: "a", SourceSyntax: "asdf"}, false}, + {"Erro logging", Options{}, Args{Source: `@error "foo";`}, false}, } { test := test @@ -125,6 +126,43 @@ body } } +func TestDebugWarn(t *testing.T) { + c := qt.New(t) + + args := Args{ + Source: ` +$color: #333; +body { + color: $color; +} + + @debug "foo"; +@warn "bar"; + +`, + } + + var events []LogEvent + eventHandler := func(e LogEvent) { + events = append(events, e) + } + + opts := Options{ + LogEventHandler: eventHandler, + } + + transpiler, clean := newTestTranspiler(c, opts) + defer clean() + result, err := transpiler.Execute(args) + c.Assert(err, qt.IsNil) + + c.Assert(result.CSS, qt.Equals, "body {\n color: #333;\n}") + c.Assert(events, qt.DeepEquals, []LogEvent{ + {Type: 2, Message: "stdin:6:1: foo"}, + {Type: 0, Message: "bar"}, + }) +} + func TestIncludePaths(t *testing.T) { dir1, _ := ioutil.TempDir(os.TempDir(), "libsass-test-include-paths-dir1") defer os.RemoveAll(dir1) From 129d1d1bea31246c02a64d3c5aeeffc872f177f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Thu, 17 Mar 2022 14:45:46 +0100 Subject: [PATCH 2/2] github: Update test matrix to 1.17.x, 1.18.x --- .github/workflows/test.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 912e19a..ac78383 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,10 +1,13 @@ -on: [push, pull_request] +on: + push: + branches: [ main ] + pull_request: name: Test jobs: test: strategy: matrix: - go-version: [1.15.x] + go-version: [1.17.x, 1.18.x] os: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.os }} steps: