Copy reference nodes before copying comments and syncing order

This commit is contained in:
Phani Teja Marupaka
2021-09-07 16:58:06 -07:00
parent 99e404cb61
commit 17f18604e4
4 changed files with 55 additions and 8 deletions

View File

@@ -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())

View File

@@ -5,6 +5,7 @@ package order
import (
"bytes"
"strings"
"testing"
"github.com/stretchr/testify/assert"
@@ -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()
}
})
}
}