Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions cli/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

c "github.com/gookit/color"
"github.com/katbyte/terrafmt/lib/common"
"github.com/katbyte/terrafmt/lib/diff"
"github.com/katbyte/terrafmt/lib/fmtverbs"
"github.com/kylelemons/godebug/diff"
"github.com/spf13/afero"
)

Expand Down Expand Up @@ -384,7 +384,7 @@ func TestCmdBlocksDefault(t *testing.T) {
}

if actualStdOut != expected {
t.Errorf("Output does not match expected: ('-' actual, '+' expected)\n%s", diff.Diff(actualStdOut, expected))
t.Errorf("Output does not match expected: ('-' actual, '+' expected)\n%s", diff.LineDiff(actualStdOut, expected))
}

if actualStdErr != "" {
Expand Down Expand Up @@ -449,7 +449,7 @@ func TestCmdBlocksZeroTerminated(t *testing.T) {
}

if actualStdOut != expected {
t.Errorf("Output does not match expected: ('-' actual, '+' expected)\n%s", diff.Diff(actualStdOut, expected))
t.Errorf("Output does not match expected: ('-' actual, '+' expected)\n%s", diff.LineDiff(actualStdOut, expected))
}

if actualStdErr != "" {
Expand Down Expand Up @@ -496,7 +496,7 @@ func TestCmdBlocksJson(t *testing.T) {
}

if !equivalentJSON([]byte(actualStdOut), expected) {
t.Errorf("Output does not match expected: ('-' actual, '+' expected)\n%s", diff.Diff(actualStdOut, string(expected)))
t.Errorf("Output does not match expected: ('-' actual, '+' expected)\n%s", diff.LineDiff(actualStdOut, string(expected)))
}

if actualStdErr != "" {
Expand Down Expand Up @@ -543,7 +543,7 @@ func TestCmdBlocksFmtVerbsJson(t *testing.T) {
}

if !equivalentJSON([]byte(actualStdOut), expected) {
t.Errorf("Output does not match expected: ('-' actual, '+' expected)\n%s", diff.Diff(actualStdOut, string(expected)))
t.Errorf("Output does not match expected: ('-' actual, '+' expected)\n%s", diff.LineDiff(actualStdOut, string(expected)))
}

if actualStdErr != "" {
Expand Down
2 changes: 1 addition & 1 deletion cli/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (

c "github.com/gookit/color"
"github.com/hashicorp/go-multierror"
diff "github.com/katbyte/andreyvit-diff"
"github.com/katbyte/terrafmt/lib/blocks"
"github.com/katbyte/terrafmt/lib/common"
"github.com/katbyte/terrafmt/lib/diff"
verbs "github.com/katbyte/terrafmt/lib/fmtverbs"
"github.com/katbyte/terrafmt/lib/format"
"github.com/katbyte/terrafmt/lib/version"
Expand Down
4 changes: 2 additions & 2 deletions cli/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

c "github.com/gookit/color"
"github.com/katbyte/terrafmt/lib/common"
"github.com/kylelemons/godebug/diff"
"github.com/katbyte/terrafmt/lib/diff"
"github.com/spf13/afero"
)

Expand Down Expand Up @@ -184,7 +184,7 @@ func TestCmdDiffDefault(t *testing.T) {
}

if actualStdOut != expected {
t.Errorf("Output does not match expected: ('-' actual, '+' expected)\n%s", diff.Diff(actualStdOut, expected))
t.Errorf("Output does not match expected: ('-' actual, '+' expected)\n%s", diff.LineDiff(actualStdOut, expected))
}

errMsg := []string{}
Expand Down
8 changes: 4 additions & 4 deletions cli/fmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

c "github.com/gookit/color"
"github.com/katbyte/terrafmt/lib/common"
"github.com/kylelemons/godebug/diff"
"github.com/katbyte/terrafmt/lib/diff"
"github.com/spf13/afero"
)

Expand Down Expand Up @@ -212,7 +212,7 @@ func TestCmdFmtStdinDefault(t *testing.T) {
}

if actualStdOut != expected {
t.Errorf("Case %q: Output does not match expected: ('-' actual, '+' expected)\n%s", testcase.name, diff.Diff(actualStdOut, expected))
t.Errorf("Case %q: Output does not match expected: ('-' actual, '+' expected)\n%s", testcase.name, diff.LineDiff(actualStdOut, expected))
}

errMsg := []string{}
Expand Down Expand Up @@ -323,7 +323,7 @@ func TestCmdFmtFileDefault(t *testing.T) {
}
actualContent := c.String(string(data))
if actualContent != expected {
t.Errorf("Case %q: File does not match expected: ('-' actual, '+' expected)\n%s", testcase.name, diff.Diff(actualContent, expected))
t.Errorf("Case %q: File does not match expected: ('-' actual, '+' expected)\n%s", testcase.name, diff.LineDiff(actualContent, expected))
}

if len(testcase.errMsg) != br.ErrorBlocks {
Expand Down Expand Up @@ -430,7 +430,7 @@ func TestCmdFmtIdempotency(t *testing.T) {
}

if !bytes.Equal(before, after) {
t.Errorf("fmt is not idempotent for %q: ('-' before, '+' after)\n%s", testcase.sourcefile, diff.Diff(string(before), string(after)))
t.Errorf("fmt is not idempotent for %q: ('-' before, '+' after)\n%s", testcase.sourcefile, diff.LineDiff(string(before), string(after)))
}
})
}
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ require (
github.com/gookit/color v1.6.1
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/hcl/v2 v2.24.0
github.com/katbyte/andreyvit-diff v0.0.3
github.com/kylelemons/godebug v1.1.0
github.com/sirupsen/logrus v1.9.4
github.com/spf13/afero v1.15.0
github.com/spf13/cobra v1.10.2
Expand All @@ -24,7 +22,6 @@ require (
github.com/google/go-cmp v0.7.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/katbyte/sergi-go-diff v1.2.3 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
github.com/sagikazarmark/locafero v0.11.0 // indirect
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,10 @@ github.com/hashicorp/hcl/v2 v2.24.0 h1:2QJdZ454DSsYGoaE6QheQZjtKZSUs9Nh2izTWiwQx
github.com/hashicorp/hcl/v2 v2.24.0/go.mod h1:oGoO1FIQYfn/AgyOhlg9qLC6/nOJPX3qGbkZpYAcqfM=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/katbyte/andreyvit-diff v0.0.3 h1:gMfE0xQUKAZb3B2cnnrkLYCCZxnuGCxG4nVczq/dWfE=
github.com/katbyte/andreyvit-diff v0.0.3/go.mod h1:S//wdxNytA9hp598USfO3jHUDuLJug2TqUyvEErbowM=
github.com/katbyte/sergi-go-diff v1.2.3 h1:z08AHpmtBTYIOCQBInFTHu9KULlvaWP1Iz2RZ9po3pQ=
github.com/katbyte/sergi-go-diff v1.2.3/go.mod h1:BxkLLDDB1iVQsnURErqoQMjkyXIlR0DefDKzZCUHNEw=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
Expand Down
4 changes: 2 additions & 2 deletions lib/blocks/blockreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/katbyte/terrafmt/lib/common"
"github.com/kylelemons/godebug/diff"
"github.com/katbyte/terrafmt/lib/diff"
"github.com/spf13/afero"
)

Expand Down Expand Up @@ -301,7 +301,7 @@ func TestBlockDetection(t *testing.T) {
for i, actual := range actualBlocks {
expected := testcase.expectedBlocks[i]
if actual.text != expected.text {
t.Errorf("Case %q, block %d text: ('-' actual, '+' expected)\n%s", testcase.sourcefile, i+1, diff.Diff(actual.text, expected.text))
t.Errorf("Case %q, block %d text: ('-' actual, '+' expected)\n%s", testcase.sourcefile, i+1, diff.LineDiff(actual.text, expected.text))
}
if actual.leadingPadding != expected.leadingPadding {
t.Errorf("Case %q, block %d leading padding: expected %q, got %q", testcase.sourcefile, i+1, expected.leadingPadding, actual.leadingPadding)
Expand Down
File renamed without changes.
41 changes: 12 additions & 29 deletions ...github.com/katbyte/andreyvit-diff/diff.go → lib/diff/diff.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// Package diff produces linewise diffs between two strings.
//
// Absorbed from github.com/andreyvit/diff (via github.com/katbyte/andreyvit-diff,
// MIT licensed, see LICENSE), trimmed to the LineDiff path used by terrafmt.
package diff

import (
"bytes"
"strings"

"github.com/katbyte/sergi-go-diff/diffmatchpatch"
"github.com/katbyte/terrafmt/lib/diff/diffmatchpatch"
)

func diff(a, b string) []diffmatchpatch.Diff {
Expand All @@ -17,31 +21,6 @@ func diff(a, b string) []diffmatchpatch.Diff {
return diffs
}

// CharacterDiff returns an inline diff between the two strings, using (++added++) and (~~deleted~~) markup.
func CharacterDiff(a, b string) string {
return diffsToString(diff(a, b))
}

func diffsToString(diffs []diffmatchpatch.Diff) string {
var buff bytes.Buffer
for _, diff := range diffs {
text := diff.Text
switch diff.Type {
case diffmatchpatch.DiffInsert:
buff.WriteString("(++")
buff.WriteString(text)
buff.WriteString("++)")
case diffmatchpatch.DiffDelete:
buff.WriteString("(~~")
buff.WriteString(text)
buff.WriteString("~~)")
case diffmatchpatch.DiffEqual:
buff.WriteString(text)
}
}
return buff.String()
}

// LineDiff returns a normal linewise diff between the two given strings.
func LineDiff(a, b string) string {
return strings.Join(LineDiffAsLines(a, b), "\n")
Expand All @@ -68,6 +47,7 @@ func (b *patchBuilder) AddCharacters(text string, op diffmatchpatch.Operation) {
b.oldLineBuffer.WriteString(text)
}
}

func (b *patchBuilder) AddNewline(op diffmatchpatch.Operation) {
oldLine := b.oldLineBuffer.String()
newLine := b.newLineBuffer.String()
Expand All @@ -88,6 +68,7 @@ func (b *patchBuilder) AddNewline(op diffmatchpatch.Operation) {
}
}
}

func (b *patchBuilder) FlushChunk() {
if b.oldLines != nil {
b.output = append(b.output, b.oldLines...)
Expand All @@ -98,12 +79,14 @@ func (b *patchBuilder) FlushChunk() {
b.newLines = nil
}
}

func (b *patchBuilder) Flush() {
if b.oldLineBuffer.Len() > 0 && b.newLineBuffer.Len() > 0 {
switch {
case b.oldLineBuffer.Len() > 0 && b.newLineBuffer.Len() > 0:
b.AddNewline(diffmatchpatch.DiffEqual)
} else if b.oldLineBuffer.Len() > 0 {
case b.oldLineBuffer.Len() > 0:
b.AddNewline(diffmatchpatch.DiffDelete)
} else if b.newLineBuffer.Len() > 0 {
case b.newLineBuffer.Len() > 0:
b.AddNewline(diffmatchpatch.DiffInsert)
}
b.FlushChunk()
Expand Down
80 changes: 80 additions & 0 deletions lib/diff/diff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package diff_test

import (
"testing"

"github.com/katbyte/terrafmt/lib/diff"
)

// The expected outputs are pinned from github.com/katbyte/andreyvit-diff v0.0.3,
// which this package absorbed; byte-identical output was verified against that
// library by differential fuzzing and a corpus replay before it was removed.
func TestLineDiff(t *testing.T) {
t.Parallel()

tests := []struct {
name string
a string
b string
want string
}{
{
name: "changed line",
a: "a\nb\nc\n",
b: "a\nB\nc\n",
want: " a\n-b\n+B\n c",
},
{
name: "removed line",
a: "a\nb\nc\n",
b: "a\nc\n",
want: " a\n-b\n c",
},
{
name: "added line",
a: "a\nc\n",
b: "a\nb\nc\n",
want: " a\n+b\n c",
},
{
name: "terraform block formatting",
a: "resource \"aws_thing\" \"test\" {\nname=\"foo\"\ncount=1\n}\n",
b: "resource \"aws_thing\" \"test\" {\n name = \"foo\"\n count = 1\n}\n",
want: " resource \"aws_thing\" \"test\" {\n-name=\"foo\"\n-count=1\n+ name = \"foo\"\n+ count = 1\n }",
},
{
name: "empty to content",
a: "",
b: "a\n",
want: "+a",
},
{
name: "no trailing newline",
a: "a",
b: "b",
want: "-a\n+b",
},
{
name: "multibyte runes",
a: "héllo wörld\n",
b: "héllo world\n",
want: "-héllo wörld\n+héllo world",
},
{
name: "identical input",
a: "a\nb\n",
b: "a\nb\n",
want: " a\n b",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

if got := diff.LineDiff(tt.a, tt.b); got != tt.want {
t.Errorf("LineDiff(%q, %q) = %q, want %q", tt.a, tt.b, got, tt.want)
}
})
}
}
Loading
Loading