mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-07-17 17:52:12 +00:00
fix copy comments to include document comments
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
|||||||
|
|
||||||
// CopyComments recursively copies the comments on fields in from to fields in to
|
// CopyComments recursively copies the comments on fields in from to fields in to
|
||||||
func CopyComments(from, to *yaml.RNode) error {
|
func CopyComments(from, to *yaml.RNode) error {
|
||||||
|
copy(from, to)
|
||||||
// walk the fields copying comments
|
// walk the fields copying comments
|
||||||
_, err := walk.Walker{
|
_, err := walk.Walker{
|
||||||
Sources: []*yaml.RNode{from, to},
|
Sources: []*yaml.RNode{from, to},
|
||||||
@@ -44,13 +45,13 @@ func copy(from, to *yaml.RNode) {
|
|||||||
if from == nil || to == nil {
|
if from == nil || to == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if from.YNode().LineComment != "" {
|
if from.Document().LineComment != "" {
|
||||||
to.YNode().LineComment = from.YNode().LineComment
|
to.Document().LineComment = from.Document().LineComment
|
||||||
}
|
}
|
||||||
if from.YNode().HeadComment != "" {
|
if from.Document().HeadComment != "" {
|
||||||
to.YNode().HeadComment = from.YNode().HeadComment
|
to.Document().HeadComment = from.Document().HeadComment
|
||||||
}
|
}
|
||||||
if from.YNode().FootComment != "" {
|
if from.Document().FootComment != "" {
|
||||||
to.YNode().FootComment = from.YNode().FootComment
|
to.Document().FootComment = from.Document().FootComment
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
65
kyaml/comments/comments_test.go
Normal file
65
kyaml/comments/comments_test.go
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
// Copyright 2019 The Kubernetes Authors.
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package comments
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCopyComments(t *testing.T) {
|
||||||
|
from, err := yaml.Parse(`# A
|
||||||
|
#
|
||||||
|
# B
|
||||||
|
|
||||||
|
# C
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
spec: # comment 1
|
||||||
|
# comment 2
|
||||||
|
replicas: 3 # comment 3
|
||||||
|
# comment 4
|
||||||
|
`)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
|
to, err := yaml.Parse(`apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
spec:
|
||||||
|
replicas: 4
|
||||||
|
`)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
|
err = CopyComments(from, to)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
|
actual, err := to.String()
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := `# A
|
||||||
|
#
|
||||||
|
# B
|
||||||
|
|
||||||
|
# C
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
spec: # comment 1
|
||||||
|
# comment 2
|
||||||
|
replicas: 4 # comment 3
|
||||||
|
# comment 4
|
||||||
|
`
|
||||||
|
|
||||||
|
if !assert.Equal(t, expected, actual) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user