Skip to content

Commit

Permalink
Merge pull request #4172 from phanimarupaka/CopyReferenceNodesBeforeC…
Browse files Browse the repository at this point in the history
…omparing

Copy reference nodes before copying comments and syncing order
  • Loading branch information
k8s-ci-robot committed Sep 8, 2021
2 parents 99e404c + 17f1860 commit 2b8a393
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
16 changes: 9 additions & 7 deletions kyaml/comments/comments.go
Expand Up @@ -11,10 +11,12 @@ import (

// CopyComments recursively copies the comments on fields in from to fields in to
func CopyComments(from, to *yaml.RNode) error {
copy(from, to)
// from node should not be modified, it should be just used as a reference
fromCopy := from.Copy()
copyFieldComments(fromCopy, to)
// walk the fields copying comments
_, err := walk.Walker{
Sources: []*yaml.RNode{from, to},
Sources: []*yaml.RNode{fromCopy, to},
Visitor: &copier{},
VisitKeysAsScalars: true}.Walk()
return err
Expand All @@ -25,7 +27,7 @@ func CopyComments(from, to *yaml.RNode) error {
type copier struct{}

func (c *copier) VisitMap(s walk.Sources, _ *openapi.ResourceSchema) (*yaml.RNode, error) {
copy(s.Dest(), s.Origin())
copyFieldComments(s.Dest(), s.Origin())
return s.Dest(), nil
}

Expand All @@ -39,13 +41,13 @@ func (c *copier) VisitScalar(s walk.Sources, _ *openapi.ResourceSchema) (*yaml.R
to.Document().Style = yaml.DoubleQuotedStyle
}

copy(s.Dest(), to)
copyFieldComments(s.Dest(), to)
return s.Dest(), nil
}

func (c *copier) VisitList(s walk.Sources, _ *openapi.ResourceSchema, _ walk.ListKind) (
*yaml.RNode, error) {
copy(s.Dest(), s.Origin())
copyFieldComments(s.Dest(), s.Origin())
destItems := s.Dest().Content()
originItems := s.Origin().Content()

Expand All @@ -64,8 +66,8 @@ func (c *copier) VisitList(s walk.Sources, _ *openapi.ResourceSchema, _ walk.Lis
return s.Dest(), nil
}

// copy copies the comment from one field to another
func copy(from, to *yaml.RNode) {
// copyFieldComments copies the comment from one field to another
func copyFieldComments(from, to *yaml.RNode) {
if from == nil || to == nil {
return
}
Expand Down
33 changes: 33 additions & 0 deletions kyaml/comments/comments_test.go
Expand Up @@ -351,6 +351,30 @@ apiVersion: v1
kind: ConfigMap
data:
somekey: "012345678901234567890123456789012345678901234567890123456789012345678901234" # x
`,
},
{
name: "sort fields with null value",
from: `apiVersion: v1
kind: ConfigMap
metadata:
name: workspaces.app.terraform.io
creationTimestamp: null # this field is null
namespace: staging
`,
to: `apiVersion: v1
kind: ConfigMap
metadata:
name: workspaces.app.terraform.io
creationTimestamp: null
namespace: staging
`,
expected: `apiVersion: v1
kind: ConfigMap
metadata:
name: workspaces.app.terraform.io
creationTimestamp: null # this field is null
namespace: staging
`,
},
}
Expand Down Expand Up @@ -378,9 +402,18 @@ data:
t.FailNow()
}

actualFrom, err := from.String()
if !assert.NoError(t, err) {
t.FailNow()
}

if !assert.Equal(t, strings.TrimSpace(tc.expected), strings.TrimSpace(actual)) {
t.FailNow()
}

if !assert.Equal(t, strings.TrimSpace(tc.from), strings.TrimSpace(actualFrom)) {
t.FailNow()
}
})
}
}
4 changes: 3 additions & 1 deletion kyaml/order/syncorder.go
Expand Up @@ -13,7 +13,9 @@ import (
// Field order might be altered due to round-tripping in arbitrary functions.
// This functionality helps to retain the original order of fields to avoid unnecessary diffs.
func SyncOrder(from, to *yaml.RNode) error {
if err := syncOrder(from, to); err != nil {
// from node should not be modified, it should be just used as a reference
fromCopy := from.Copy()
if err := syncOrder(fromCopy, to); err != nil {
return errors.Errorf("failed to sync field order: %q", err.Error())
}
rearrangeHeadCommentOfSeqNode(to.YNode())
Expand Down
10 changes: 10 additions & 0 deletions kyaml/order/syncorder_test.go
Expand Up @@ -5,6 +5,7 @@ package order

import (
"bytes"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -413,6 +414,15 @@ status:
if !assert.Equal(t, tc.expected, out.String()) {
t.FailNow()
}

actualFrom, err := from.String()
if !assert.NoError(t, err) {
t.FailNow()
}

if !assert.Equal(t, strings.TrimSpace(tc.from), strings.TrimSpace(actualFrom)) {
t.FailNow()
}
})
}
}

0 comments on commit 2b8a393

Please sign in to comment.