mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
add more tests for ElementSetter
This commit is contained in:
@@ -41,10 +41,9 @@ func (a ElementAppender) Filter(rn *RNode) (*RNode, error) {
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ElementSetter sets the value for an Element in an associative list. ElementSetter
|
// ElementSetter sets the value for an Element in an associative list.
|
||||||
// will remove any elements which are empty.
|
|
||||||
// ElementSetter will append, replace or delete an element in an associative list.
|
// ElementSetter will append, replace or delete an element in an associative list.
|
||||||
// To append, user a key-value pair that doesn't exist in the sequence. Note: this
|
// To append, user a key-value pair that doesn't exist in the sequence. this
|
||||||
// behavior is intended to handle the case that not matching element found. It's
|
// behavior is intended to handle the case that not matching element found. It's
|
||||||
// not designed for this purpose. To append an element, please use ElementAppender.
|
// not designed for this purpose. To append an element, please use ElementAppender.
|
||||||
// To replace, set the key-value pair and a non-nil Element.
|
// To replace, set the key-value pair and a non-nil Element.
|
||||||
|
|||||||
@@ -116,6 +116,55 @@ func TestElementSetter(t *testing.T) {
|
|||||||
- c: d
|
- c: d
|
||||||
`, assertNoErrorString(t)(node.String()))
|
`, assertNoErrorString(t)(node.String()))
|
||||||
|
|
||||||
|
node = orig.Copy()
|
||||||
|
// Nothing happens because no element is matched
|
||||||
|
rn, err = node.Pipe(ElementSetter{Key: "a", Value: "zebra"})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Nil(t, rn)
|
||||||
|
assert.Equal(t, `- a: b
|
||||||
|
- scalarValue
|
||||||
|
- c: d
|
||||||
|
`, assertNoErrorString(t)(node.String()))
|
||||||
|
|
||||||
|
node = orig.Copy()
|
||||||
|
// Return error because ElementSetter doesn't support a single key
|
||||||
|
// when there is a scalar value in the list
|
||||||
|
rn, err = node.Pipe(ElementSetter{Key: "a"})
|
||||||
|
assert.EqualError(t, err, "wrong Node Kind for expected: MappingNode was ScalarNode: value: {scalarValue}")
|
||||||
|
|
||||||
|
node = MustParse(`
|
||||||
|
- a: b
|
||||||
|
- c: d
|
||||||
|
`)
|
||||||
|
// {a: b} is removed since the value is omitted and only key is used
|
||||||
|
// to match and no Element specified.
|
||||||
|
rn, err = node.Pipe(ElementSetter{Key: "a"})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Nil(t, rn)
|
||||||
|
assert.Equal(t, `- c: d
|
||||||
|
`, assertNoErrorString(t)(node.String()))
|
||||||
|
|
||||||
|
node = MustParse(`
|
||||||
|
- a: b
|
||||||
|
- c: d
|
||||||
|
`)
|
||||||
|
// Return error because ElementSetter will assume all elements are scalar when
|
||||||
|
// there is only value provided.
|
||||||
|
rn, err = node.Pipe(ElementSetter{Value: "b"})
|
||||||
|
assert.EqualError(t, err, "wrong Node Kind for expected: ScalarNode was MappingNode: value: {a: b}")
|
||||||
|
|
||||||
|
node = MustParse(`
|
||||||
|
- a
|
||||||
|
- b
|
||||||
|
`)
|
||||||
|
// b is removed since ElementSetter use the value "b" to match the
|
||||||
|
// scalar values.
|
||||||
|
rn, err = node.Pipe(ElementSetter{Value: "b"})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Nil(t, rn)
|
||||||
|
assert.Equal(t, `- a
|
||||||
|
`, assertNoErrorString(t)(node.String()))
|
||||||
|
|
||||||
node = orig.Copy()
|
node = orig.Copy()
|
||||||
// Set an element, replacing 'a: b' with 'e: f'
|
// Set an element, replacing 'a: b' with 'e: f'
|
||||||
newElement := NewMapRNode(&map[string]string{
|
newElement := NewMapRNode(&map[string]string{
|
||||||
|
|||||||
Reference in New Issue
Block a user