test: add testutil for mutation tracker

This change provides a common test util for a mutation tracker stub.
This is intended to reduce the duplicated boilerplate as additional
filters implement the TrackableFilter interface.
This commit is contained in:
Sam Dowell
2022-01-28 19:33:03 +00:00
parent a5b61016bb
commit e3160373f0
4 changed files with 56 additions and 77 deletions

View File

@@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/assert"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
func run(input string, f kio.Filter) (string, error) {
@@ -46,3 +47,32 @@ func RunFilterE(t *testing.T, input string, f kio.Filter) (string, error) {
}
return output, nil
}
type SetValueArg struct {
Key string
Value string
Tag string
NodePath []string
}
// MutationTrackerStub to help stub a mutation tracker for kio.TrackableFilter
type MutationTrackerStub struct {
setValueArgs []SetValueArg
}
func (mts *MutationTrackerStub) MutationTracker(key, value, tag string, node *yaml.RNode) {
mts.setValueArgs = append(mts.setValueArgs, SetValueArg{
Key: key,
Value: value,
Tag: tag,
NodePath: node.FieldPath(),
})
}
func (mts *MutationTrackerStub) SetValueArgs() []SetValueArg {
return mts.setValueArgs
}
func (mts *MutationTrackerStub) Reset() {
mts.setValueArgs = nil
}