mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 18:10:59 +00:00
fix(kyaml/yaml): minor nil safety fix for RNode.Content etc (#5985)
* Fix kyaml/yaml field access deref nil value for methods that look "nil-safe" This change is addressing observed panics within kustomize that obscure the actual failure. The primary observed problem case involves RNode.Content. * Fix test case * Fixes from review
This commit is contained in:
@@ -2356,3 +2356,70 @@ metadata:
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "hello-world-foo", fooAppName) // no field named 'foo.appname'
|
||||
}
|
||||
|
||||
func TestRNode_nilSafety(t *testing.T) {
|
||||
// Both of these scenarios should cause rn.YNode() to return nil.
|
||||
nodesToTest := [...]struct {
|
||||
name string
|
||||
rn *RNode
|
||||
}{
|
||||
{"nil *RNode receiver", nil},
|
||||
{"RNode with nil internal node", &RNode{value: nil}},
|
||||
}
|
||||
|
||||
t.Run("Content", func(t *testing.T) {
|
||||
for _, tc := range nodesToTest {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert.NotPanics(t, func() {
|
||||
assert.Nil(t, tc.rn.Content())
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Field", func(t *testing.T) {
|
||||
for _, tc := range nodesToTest {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert.NotPanics(t, func() {
|
||||
assert.Nil(t, tc.rn.Field("any-field"))
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Element", func(t *testing.T) {
|
||||
for _, tc := range nodesToTest {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert.NotPanics(t, func() {
|
||||
assert.Nil(t, tc.rn.Element("any-key", "any-value"))
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ElementList", func(t *testing.T) {
|
||||
for _, tc := range nodesToTest {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert.NotPanics(t, func() {
|
||||
assert.Nil(t, tc.rn.ElementList([]string{"any-key"}, []string{"any-value"}))
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("MarshalJSON", func(t *testing.T) {
|
||||
for _, tc := range nodesToTest {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var (
|
||||
jsonData []byte
|
||||
err error
|
||||
)
|
||||
assert.NotPanics(t, func() {
|
||||
jsonData, err = tc.rn.MarshalJSON()
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, []byte("null"), jsonData)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user