Skip to content

Commit

Permalink
rework formatting and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jippi committed Feb 4, 2024
1 parent 025e5af commit 9ea6250
Show file tree
Hide file tree
Showing 21 changed files with 113 additions and 13 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion pkg/render/handler_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 2 additions & 4 deletions pkg/render/other.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
4 changes: 0 additions & 4 deletions pkg/render/output_colorized.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}
Expand Down
4 changes: 0 additions & 4 deletions pkg/render/output_plain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}
Expand Down
46 changes: 46 additions & 0 deletions pkg/render/render_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
}
5 changes: 5 additions & 0 deletions pkg/render/test-fixtures/formatter/comment-spacing.golden.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# My first key
KEY=VALUE

# My Second Key
FOO=BAZ
4 changes: 4 additions & 0 deletions pkg/render/test-fixtures/formatter/comment-spacing.input.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# My first key
KEY=VALUE
# My Second Key
FOO=BAZ
17 changes: 17 additions & 0 deletions pkg/render/test-fixtures/formatter/compressed.golden.env
Original file line number Diff line number Diff line change
@@ -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}"
12 changes: 12 additions & 0 deletions pkg/render/test-fixtures/formatter/compressed.input.env
Original file line number Diff line number Diff line change
@@ -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}"
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Hello world
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Hello world
1 change: 1 addition & 0 deletions pkg/render/test-fixtures/formatter/single-pair.golden.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
KEY=VALUE
1 change: 1 addition & 0 deletions pkg/render/test-fixtures/formatter/single-pair.input.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
KEY=VALUE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
KEY=VALUE
FOO=baz
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
KEY=VALUE



FOO=baz
2 changes: 2 additions & 0 deletions pkg/render/test-fixtures/formatter/two-pairs.golden.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
KEY=VALUE
FOO=baz
2 changes: 2 additions & 0 deletions pkg/render/test-fixtures/formatter/two-pairs.input.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
KEY=VALUE
FOO=baz

0 comments on commit 9ea6250

Please sign in to comment.