From 9ea6250feeb4f82a3443f71b4269688ca6299920 Mon Sep 17 00:00:00 2001 From: Christian Winther Date: Sun, 4 Feb 2024 22:06:18 +0100 Subject: [PATCH] rework formatting and add tests --- go.mod | 2 + go.sum | 9 ++++ pkg/render/handler_formatter.go | 2 +- pkg/render/other.go | 6 +-- pkg/render/output_colorized.go | 4 -- pkg/render/output_plain.go | 4 -- pkg/render/render_test.go | 46 +++++++++++++++++++ .../formatter/comment-spacing.golden.env | 5 ++ .../formatter/comment-spacing.input.env | 4 ++ .../formatter/compressed.golden.env | 17 +++++++ .../formatter/compressed.input.env | 12 +++++ .../test-fixtures/formatter/empty.golden.env | 0 .../test-fixtures/formatter/empty.input.env | 0 .../formatter/just-a-comment.golden.env | 1 + .../formatter/just-a-comment.input.env | 1 + .../formatter/single-pair.golden.env | 1 + .../formatter/single-pair.input.env | 1 + .../formatter/two-pairs-newlines.golden.env | 2 + .../formatter/two-pairs-newlines.input.env | 5 ++ .../formatter/two-pairs.golden.env | 2 + .../formatter/two-pairs.input.env | 2 + 21 files changed, 113 insertions(+), 13 deletions(-) create mode 100644 pkg/render/render_test.go create mode 100644 pkg/render/test-fixtures/formatter/comment-spacing.golden.env create mode 100644 pkg/render/test-fixtures/formatter/comment-spacing.input.env create mode 100644 pkg/render/test-fixtures/formatter/compressed.golden.env create mode 100644 pkg/render/test-fixtures/formatter/compressed.input.env create mode 100644 pkg/render/test-fixtures/formatter/empty.golden.env create mode 100644 pkg/render/test-fixtures/formatter/empty.input.env create mode 100644 pkg/render/test-fixtures/formatter/just-a-comment.golden.env create mode 100644 pkg/render/test-fixtures/formatter/just-a-comment.input.env create mode 100644 pkg/render/test-fixtures/formatter/single-pair.golden.env create mode 100644 pkg/render/test-fixtures/formatter/single-pair.input.env create mode 100644 pkg/render/test-fixtures/formatter/two-pairs-newlines.golden.env create mode 100644 pkg/render/test-fixtures/formatter/two-pairs-newlines.input.env create mode 100644 pkg/render/test-fixtures/formatter/two-pairs.golden.env create mode 100644 pkg/render/test-fixtures/formatter/two-pairs.input.env diff --git a/go.mod b/go.mod index 27f41b1..c52bbc2 100644 --- a/go.mod +++ b/go.mod @@ -61,6 +61,8 @@ require ( github.com/muesli/reflow v0.3.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rivo/uniseg v0.4.4 // indirect + github.com/sebdah/goldie/v2 v2.5.3 // indirect + github.com/sergi/go-diff v1.3.1 // indirect github.com/sirupsen/logrus v1.9.0 // indirect github.com/ulikunitz/xz v0.5.10 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect diff --git a/go.sum b/go.sum index 395ccb7..62abf04 100644 --- a/go.sum +++ b/go.sum @@ -412,6 +412,7 @@ github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -422,12 +423,18 @@ github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/sebdah/goldie/v2 v2.5.3 h1:9ES/mNN+HNUbNWpVAlrzuZ7jE+Nrczbj8uFRjM7624Y= +github.com/sebdah/goldie/v2 v2.5.3/go.mod h1:oZ9fp0+se1eapSRjfYbsV/0Hqhbuu3bJVvKI/NNtssI= +github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= +github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8= +github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I= github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= @@ -972,12 +979,14 @@ google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/cheggaaa/pb.v1 v1.0.27/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/render/handler_formatter.go b/pkg/render/handler_formatter.go index cc9f732..f0e6ca3 100644 --- a/pkg/render/handler_formatter.go +++ b/pkg/render/handler_formatter.go @@ -47,7 +47,7 @@ func Format(in *HandlerInput) Signal { // Looks like current and previous Statement is both "Assignment" // which mean they might be too close in the document, so we will // attempt to inject some new-lines to give them some space - if in.Settings.ShowPretty && val.Is(in.Previous) { + if in.Settings.WithBlankLines() && val.Is(in.Previous) { // only allow cuddling of assignments if they both have no comments if val.HasComments() || assignmentHasComments(in.Previous) { buff.Newline() diff --git a/pkg/render/other.go b/pkg/render/other.go index 2e22fec..7b5347a 100644 --- a/pkg/render/other.go +++ b/pkg/render/other.go @@ -12,8 +12,7 @@ func NewFormatter(doc *ast.Document) string { ShowGroups: true, } - return NewRenderer(settings, Format). - Document(doc, settings) + return NewRenderer(settings, Format).Document(doc, settings) } func NewDirect(doc *ast.Document) string { @@ -26,8 +25,7 @@ func NewDirect(doc *ast.Document) string { ShowGroups: true, } - return NewRenderer(settings). - Document(doc, settings) + return NewRenderer(settings).Document(doc, settings) } func assignmentHasComments(stmt ast.Statement) bool { diff --git a/pkg/render/output_colorized.go b/pkg/render/output_colorized.go index 55df80c..0ee03e0 100644 --- a/pkg/render/output_colorized.go +++ b/pkg/render/output_colorized.go @@ -79,10 +79,6 @@ func (r Colorized) Comment(comment *ast.Comment, settings Settings, isAssignment } func (r Colorized) Newline(newline *ast.Newline, settings Settings) string { - if settings.ShowPretty { - return "" - } - if newline.Blank && !settings.WithBlankLines() { return "" } diff --git a/pkg/render/output_plain.go b/pkg/render/output_plain.go index 51a2f95..6f38879 100644 --- a/pkg/render/output_plain.go +++ b/pkg/render/output_plain.go @@ -44,10 +44,6 @@ func (r Plain) Comment(comment *ast.Comment, settings Settings, isAssignmentComm } func (r Plain) Newline(newline *ast.Newline, settings Settings) string { - if settings.ShowPretty { - return "" - } - if newline.Blank && !settings.WithBlankLines() { return "" } diff --git a/pkg/render/render_test.go b/pkg/render/render_test.go new file mode 100644 index 0000000..ccfd6c2 --- /dev/null +++ b/pkg/render/render_test.go @@ -0,0 +1,46 @@ +package render_test + +import ( + "log" + "os" + "strings" + "testing" + + "github.com/jippi/dottie/pkg" + "github.com/jippi/dottie/pkg/render" + "github.com/sebdah/goldie/v2" + "github.com/stretchr/testify/require" +) + +func TestFormatter(t *testing.T) { + t.Parallel() + + g := goldie.New( + t, + goldie.WithFixtureDir("test-fixtures/formatter"), + goldie.WithNameSuffix(".golden.env"), + goldie.WithDiffEngine(goldie.ColoredDiff), + goldie.WithTestNameForDir(false), + ) + + files, err := os.ReadDir("test-fixtures/formatter") + if err != nil { + log.Fatal(err) + } + + for _, file := range files { + switch { + case strings.HasSuffix(file.Name(), ".input.env"): + env, err := pkg.Load("test-fixtures/formatter/" + file.Name()) + require.NoError(t, err) + + testName := strings.TrimSuffix(file.Name(), ".input.env") + + g.Assert(t, testName, []byte(render.NewFormatter(env))) + + case strings.HasSuffix(file.Name(), ".golden.env"): + default: + panic("unexpected file") + } + } +} diff --git a/pkg/render/test-fixtures/formatter/comment-spacing.golden.env b/pkg/render/test-fixtures/formatter/comment-spacing.golden.env new file mode 100644 index 0000000..d221241 --- /dev/null +++ b/pkg/render/test-fixtures/formatter/comment-spacing.golden.env @@ -0,0 +1,5 @@ +# My first key +KEY=VALUE + +# My Second Key +FOO=BAZ \ No newline at end of file diff --git a/pkg/render/test-fixtures/formatter/comment-spacing.input.env b/pkg/render/test-fixtures/formatter/comment-spacing.input.env new file mode 100644 index 0000000..0197ebc --- /dev/null +++ b/pkg/render/test-fixtures/formatter/comment-spacing.input.env @@ -0,0 +1,4 @@ +# My first key +KEY=VALUE +# My Second Key +FOO=BAZ diff --git a/pkg/render/test-fixtures/formatter/compressed.golden.env b/pkg/render/test-fixtures/formatter/compressed.golden.env new file mode 100644 index 0000000..2397fe8 --- /dev/null +++ b/pkg/render/test-fixtures/formatter/compressed.golden.env @@ -0,0 +1,17 @@ +my_key="ok" + +# This is my favorite int +# @dottie/validate number +PORT="test" + +my_lol="ok" +my_lol2="ok" + +################################################################################ +# database +################################################################################ + +# the hostname to the database +DB_HOST="db" + +DB_PORT="${PORT}" diff --git a/pkg/render/test-fixtures/formatter/compressed.input.env b/pkg/render/test-fixtures/formatter/compressed.input.env new file mode 100644 index 0000000..74fc60a --- /dev/null +++ b/pkg/render/test-fixtures/formatter/compressed.input.env @@ -0,0 +1,12 @@ +my_key="ok" +# This is my favorite int +# @dottie/validate number +PORT="test" +my_lol="ok" +my_lol2="ok" +################################################################################ +# database +################################################################################ +# the hostname to the database +DB_HOST="db" +DB_PORT="${PORT}" diff --git a/pkg/render/test-fixtures/formatter/empty.golden.env b/pkg/render/test-fixtures/formatter/empty.golden.env new file mode 100644 index 0000000..e69de29 diff --git a/pkg/render/test-fixtures/formatter/empty.input.env b/pkg/render/test-fixtures/formatter/empty.input.env new file mode 100644 index 0000000..e69de29 diff --git a/pkg/render/test-fixtures/formatter/just-a-comment.golden.env b/pkg/render/test-fixtures/formatter/just-a-comment.golden.env new file mode 100644 index 0000000..c7c17d9 --- /dev/null +++ b/pkg/render/test-fixtures/formatter/just-a-comment.golden.env @@ -0,0 +1 @@ +# Hello world \ No newline at end of file diff --git a/pkg/render/test-fixtures/formatter/just-a-comment.input.env b/pkg/render/test-fixtures/formatter/just-a-comment.input.env new file mode 100644 index 0000000..716ed14 --- /dev/null +++ b/pkg/render/test-fixtures/formatter/just-a-comment.input.env @@ -0,0 +1 @@ +# Hello world diff --git a/pkg/render/test-fixtures/formatter/single-pair.golden.env b/pkg/render/test-fixtures/formatter/single-pair.golden.env new file mode 100644 index 0000000..01da589 --- /dev/null +++ b/pkg/render/test-fixtures/formatter/single-pair.golden.env @@ -0,0 +1 @@ +KEY=VALUE \ No newline at end of file diff --git a/pkg/render/test-fixtures/formatter/single-pair.input.env b/pkg/render/test-fixtures/formatter/single-pair.input.env new file mode 100644 index 0000000..acd56aa --- /dev/null +++ b/pkg/render/test-fixtures/formatter/single-pair.input.env @@ -0,0 +1 @@ +KEY=VALUE diff --git a/pkg/render/test-fixtures/formatter/two-pairs-newlines.golden.env b/pkg/render/test-fixtures/formatter/two-pairs-newlines.golden.env new file mode 100644 index 0000000..928553b --- /dev/null +++ b/pkg/render/test-fixtures/formatter/two-pairs-newlines.golden.env @@ -0,0 +1,2 @@ +KEY=VALUE +FOO=baz \ No newline at end of file diff --git a/pkg/render/test-fixtures/formatter/two-pairs-newlines.input.env b/pkg/render/test-fixtures/formatter/two-pairs-newlines.input.env new file mode 100644 index 0000000..18f57bc --- /dev/null +++ b/pkg/render/test-fixtures/formatter/two-pairs-newlines.input.env @@ -0,0 +1,5 @@ +KEY=VALUE + + + +FOO=baz diff --git a/pkg/render/test-fixtures/formatter/two-pairs.golden.env b/pkg/render/test-fixtures/formatter/two-pairs.golden.env new file mode 100644 index 0000000..928553b --- /dev/null +++ b/pkg/render/test-fixtures/formatter/two-pairs.golden.env @@ -0,0 +1,2 @@ +KEY=VALUE +FOO=baz \ No newline at end of file diff --git a/pkg/render/test-fixtures/formatter/two-pairs.input.env b/pkg/render/test-fixtures/formatter/two-pairs.input.env new file mode 100644 index 0000000..0ee7e13 --- /dev/null +++ b/pkg/render/test-fixtures/formatter/two-pairs.input.env @@ -0,0 +1,2 @@ +KEY=VALUE +FOO=baz