Skip to content

Commit

Permalink
fix sorting inconsistency on diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
semihbkgr committed Nov 4, 2024
1 parent 541dcb9 commit 4e622e7
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 13 deletions.
5 changes: 5 additions & 0 deletions compare/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ func (a DocDiffs) Less(i, j int) bool {
nodeB = diffB.rightNode
}

//todo: it is still partial ordered, which can cause the inconsistent order of items after sorting
if nodeA.GetToken().Position.Line == nodeB.GetToken().Position.Line {
return diffA.leftNode != nil
}

return nodeA.GetToken().Position.Line < nodeB.GetToken().Position.Line
}

Expand Down
76 changes: 63 additions & 13 deletions compare/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package compare
import (
"fmt"
"os"
"strings"
"testing"

"github.com/goccy/go-yaml"
Expand All @@ -15,18 +16,54 @@ const (
fileRight = "testdata/file-right.yaml"
)

var diffStringLines = []string{
"~ people.name: John -> Bob",
"~ people.surname: Doe -> Rose",
"~ city.name: New York -> San Francisco",
"~ item.id: 124 -> 123",
"~ item.price: 10.9 -> 10.3",
}

var diffValues = [][2]string{
[2]string{
"John", "Bob",
},
[2]string{
"Doe", "Rose",
},
[2]string{
"New York", "San Francisco",
},
[2]string{
"124", "123",
},
[2]string{
"10.9", "10.3",
},
}

func TestCompareFile(t *testing.T) {
diffs, err := CompareFile(fileLeft, fileRight, false, DefaultDiffOptions)
assert.NoError(t, err)
assert.Len(t, diffs, 1)
assert.Len(t, diffs[0], 5)

for i, diff := range diffs[0] {
assert.Equal(t, diff.leftNode.GetToken().Value, diffValues[i][0])
assert.Equal(t, diff.rightNode.GetToken().Value, diffValues[i][1])
}
}

func TestCompare(t *testing.T) {
diffs, err := Compare(readFile(t, fileLeft), readFile(t, fileRight), false, DefaultDiffOptions)
assert.NoError(t, err)
assert.Len(t, diffs, 1)
assert.Len(t, diffs[0], 5)

for i, diff := range diffs[0] {
assert.Equal(t, diff.leftNode.GetToken().Value, diffValues[i][0])
assert.Equal(t, diff.rightNode.GetToken().Value, diffValues[i][1])
}
}

func TestFileDiffsHasDiff(t *testing.T) {
Expand Down Expand Up @@ -141,20 +178,17 @@ func TestDiffsArray(t *testing.T) {
})
}

func toYaml(t *testing.T, a any) []byte {
b, err := yaml.Marshal(a)
if err != nil {
t.Error(err)
}
return b
}
func TestFormat(t *testing.T) {
diffs, err := CompareFile(fileLeft, fileRight, false, DefaultDiffOptions)
assert.NoError(t, err)

func readFile(t *testing.T, path string) []byte {
data, err := os.ReadFile(path)
if err != nil {
t.Error(err)
}
return data
output := diffs.Format(FormatOptions{
Plain: true,
Silent: false,
Metadata: false,
})

assert.Equal(t, output, strings.Join(diffStringLines, "\n"))
}

func ExampleCompare() {
Expand Down Expand Up @@ -192,3 +226,19 @@ items:
// + value: 990
// ~ items[1]: two -> three
}

func toYaml(t *testing.T, a any) []byte {
b, err := yaml.Marshal(a)
if err != nil {
t.Error(err)
}
return b
}

func readFile(t *testing.T, path string) []byte {
data, err := os.ReadFile(path)
if err != nil {
t.Error(err)
}
return data
}

0 comments on commit 4e622e7

Please sign in to comment.