Copy comments from function inputs to function outputs.

This commit is contained in:
Phillip Wittrock
2020-05-12 15:02:57 -07:00
parent 77fa5c2921
commit 5907c9e141
5 changed files with 468 additions and 74 deletions

View File

@@ -37,6 +37,18 @@ func (c *copier) VisitScalar(s walk.Sources, _ *openapi.ResourceSchema) (*yaml.R
func (c *copier) VisitList(s walk.Sources, _ *openapi.ResourceSchema, _ walk.ListKind) (
*yaml.RNode, error) {
copy(s.Dest(), s.Origin())
destItems := s.Dest().Content()
originItems := s.Origin().Content()
for i := 0; i < len(destItems) && i < len(originItems); i++ {
dest := destItems[i]
origin := originItems[i]
if dest.Value == origin.Value {
copy(yaml.NewRNode(dest), yaml.NewRNode(origin))
}
}
return s.Dest(), nil
}
@@ -45,13 +57,13 @@ func copy(from, to *yaml.RNode) {
if from == nil || to == nil {
return
}
if from.Document().LineComment != "" {
if to.Document().LineComment == "" {
to.Document().LineComment = from.Document().LineComment
}
if from.Document().HeadComment != "" {
if to.Document().HeadComment == "" {
to.Document().HeadComment = from.Document().HeadComment
}
if from.Document().FootComment != "" {
if to.Document().FootComment == "" {
to.Document().FootComment = from.Document().FootComment
}
}

View File

@@ -4,6 +4,7 @@
package comments
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
@@ -11,7 +12,15 @@ import (
)
func TestCopyComments(t *testing.T) {
from, err := yaml.Parse(`# A
testCases := []struct {
name string
from string
to string
expected string
}{
{
name: "copy_comments",
from: `# A
#
# B
@@ -22,31 +31,13 @@ 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
`,
to: `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
`,
expected: `# A
#
# B
@@ -57,9 +48,274 @@ spec: # comment 1
# comment 2
replicas: 4 # comment 3
# comment 4
`
`,
},
if !assert.Equal(t, expected, actual) {
t.FailNow()
{
name: "associative_list",
from: `
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
containers:
- name: foo
image: bar # comment 1
`,
to: `
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
containers:
- name: foo
image: bar
`,
expected: `
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
containers:
- name: foo
image: bar # comment 1
`,
},
{
name: "keep_comments",
from: `# A
#
# B
# C
apiVersion: apps/v1
kind: Deployment
spec: # comment 1
# comment 2
replicas: 3 # comment 3
# comment 4
`,
to: `apiVersion: apps/v1
kind: Deployment
spec:
replicas: 4 # comment 5
`,
expected: `# A
#
# B
# C
apiVersion: apps/v1
kind: Deployment
spec: # comment 1
# comment 2
replicas: 4 # comment 5
# comment 4
`,
},
{
name: "copy_item_comments",
from: `
apiVersion: apps/v1
kind: Deployment
items:
- a # comment
`,
to: `
apiVersion: apps/v1
kind: Deployment
items:
- a
`,
expected: `
apiVersion: apps/v1
kind: Deployment
items:
- a # comment
`,
},
{
name: "copy_item_comments_2",
from: `
apiVersion: apps/v1
kind: Deployment
items:
# comment
- a
`,
to: `
apiVersion: apps/v1
kind: Deployment
items:
- a
`,
expected: `
apiVersion: apps/v1
kind: Deployment
items:
# comment
- a
`,
},
{
name: "copy_item_comments_middle",
from: `
apiVersion: apps/v1
kind: Deployment
items:
- a
- b # comment
- c
`,
to: `
apiVersion: apps/v1
kind: Deployment
items:
- d
- b
- e
`,
expected: `
apiVersion: apps/v1
kind: Deployment
items:
- d
- b # comment
- e
`,
},
{
name: "copy_item_comments_moved",
from: `
apiVersion: apps/v1
kind: Deployment
items:
- a
- b # comment
- c
`,
to: `
apiVersion: apps/v1
kind: Deployment
items:
- a
- c
- b
`,
expected: `
apiVersion: apps/v1
kind: Deployment
items:
- a
- c
- b
`,
},
{
name: "copy_item_comments_no_match",
from: `
apiVersion: apps/v1
kind: Deployment
items:
- a # comment
`,
to: `
apiVersion: apps/v1
kind: Deployment
items:
- b
`,
expected: `
apiVersion: apps/v1
kind: Deployment
items:
- b
`,
},
{
name: "copy_item_comments_add",
from: `
apiVersion: apps/v1
kind: Deployment
items:
- a # comment
`,
to: `
apiVersion: apps/v1
kind: Deployment
items:
- a
- b
`,
expected: `
apiVersion: apps/v1
kind: Deployment
items:
- a # comment
- b
`,
},
{
name: "copy_item_comments_remove",
from: `
apiVersion: apps/v1
kind: Deployment
items:
- a # comment
- b
`,
to: `
apiVersion: apps/v1
kind: Deployment
items:
- a
`,
expected: `
apiVersion: apps/v1
kind: Deployment
items:
- a # comment
`,
},
}
for i := range testCases {
tc := testCases[i]
t.Run(tc.name, func(t *testing.T) {
from, err := yaml.Parse(tc.from)
if !assert.NoError(t, err) {
t.FailNow()
}
to, err := yaml.Parse(tc.to)
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()
}
if !assert.Equal(t, strings.TrimSpace(tc.expected), strings.TrimSpace(actual)) {
t.FailNow()
}
})
}
}