From 51376461575af5bd3b0212b8755e3068739c94d6 Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Mon, 19 Oct 2026 20:12:33 +0200 Subject: [PATCH] diff now does sort first --- cmd/yamltool/diff.go | 13 +++++-- cmd/yamltool/diff_test.go | 76 +++++++++++++++++++++++++++++++++++++++ cmd/yamltool/parse.go | 7 +++- go.mod | 4 +++ go.sum | 8 +++++ 5 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 cmd/yamltool/diff_test.go diff --git a/cmd/yamltool/diff.go b/cmd/yamltool/diff.go index 849e636..56b85e0 100644 --- a/cmd/yamltool/diff.go +++ b/cmd/yamltool/diff.go @@ -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 diff --git a/cmd/yamltool/diff_test.go b/cmd/yamltool/diff_test.go new file mode 100644 index 0000000..51bce04 --- /dev/null +++ b/cmd/yamltool/diff_test.go @@ -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 +} diff --git a/cmd/yamltool/parse.go b/cmd/yamltool/parse.go index 1a958a4..31806d3 100644 --- a/cmd/yamltool/parse.go +++ b/cmd/yamltool/parse.go @@ -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 } diff --git a/go.mod b/go.mod index 5718c96..a4f7ea0 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 7f53a3d..c133d06 100644 --- a/go.sum +++ b/go.sum @@ -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=