diff now does sort first

This commit is contained in:
2026-10-19 20:12:33 +02:00
parent 67ce736fd0
commit 5137646157
5 changed files with 105 additions and 3 deletions
+11 -2
View File
@@ -74,6 +74,15 @@ func subtract(yaml2 yaml.MapSlice, yaml1 yaml.MapSlice) yaml.MapSlice {
return res
}
func compare(yaml1 yaml.MapSlice, yaml2 yaml.MapSlice) (yaml.MapSlice, yaml.MapSlice) {
((MapSlice)(yaml1)).Sort()
((MapSlice)(yaml2)).Sort()
diff1 := subtract(yaml2, yaml1)
diff2 := subtract(yaml1, yaml2)
return diff1, diff2
}
func diff(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return fmt.Errorf("Expected 2 files")
@@ -102,10 +111,10 @@ func diff(cmd *cobra.Command, args []string) error {
return fmt.Errorf("%s: %w", file2, err)
}
diff1 := subtract(yaml2, yaml1)
diff1, backwardDiff := compare(yaml1, yaml2)
diff2 := make(yaml.MapSlice, 0)
if SYMMETRIC_DIFF {
diff2 = subtract(yaml1, yaml2)
diff2 = backwardDiff
}
diff := diff1
+76
View File
@@ -0,0 +1,76 @@
package main
import (
"testing"
"github.com/goccy/go-yaml"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type DiffSuite struct {
suite.Suite
}
func TestDiffSuite(t *testing.T) {
suite.Run(t, new(DiffSuite))
}
func (s *DiffSuite) TestCompareIgnoresTopLevelMapKeyOrder() {
diff1, diff2 := s.compareDocuments(`
first: 1
second: 2
`, `
second: 2
first: 1
`)
require.Empty(s.T(), diff1)
require.Empty(s.T(), diff2)
}
func (s *DiffSuite) TestCompareIgnoresMapKeyOrderInArray() {
diff1, diff2 := s.compareDocuments(`
items:
- name: alpha
value: 1
- name: beta
value: 2
`, `
items:
- value: 1
name: alpha
- value: 2
name: beta
`)
require.Empty(s.T(), diff1)
require.Empty(s.T(), diff2)
}
func (s *DiffSuite) TestCompareStillDetectsChangedMapValue() {
diff1, diff2 := s.compareDocuments(`
items:
- name: alpha
value: 1
`, `
items:
- value: 2
name: alpha
`)
require.NotEmpty(s.T(), diff1)
require.NotEmpty(s.T(), diff2)
}
func (s *DiffSuite) compareDocuments(yaml1Data, yaml2Data string) (yaml.MapSlice, yaml.MapSlice) {
s.T().Helper()
yaml1, err := parse([]byte(yaml1Data))
require.NoError(s.T(), err)
yaml2, err := parse([]byte(yaml2Data))
require.NoError(s.T(), err)
diff1, diff2 := compare(yaml1, yaml2)
return diff1, diff2
}
+6 -1
View File
@@ -3,6 +3,7 @@ package main
import (
"bytes"
"fmt"
"os"
"github.com/goccy/go-yaml"
"github.com/spf13/cobra"
@@ -25,10 +26,14 @@ func parseFiles(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
_, err = parse(data)
decoded, err := parse(data)
if err != nil {
fmt.Printf("%s: %v\n", arg, err.Error())
}
if len(args) > 1 {
fmt.Printf("---\n")
}
encode(os.Stdout, decoded)
}
return nil
}
+4
View File
@@ -5,9 +5,13 @@ go 1.24.7
require (
github.com/goccy/go-yaml v1.15.13
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.10.0
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
+8
View File
@@ -1,12 +1,20 @@
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/goccy/go-yaml v1.15.13 h1:Xd87Yddmr2rC1SLLTm2MNDcTjeO/GYo0JGiww6gSTDg=
github.com/goccy/go-yaml v1.15.13/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=