From be2179de3289babcb03f1a96b3d08381406a046a Mon Sep 17 00:00:00 2001 From: Tomas Aschan Date: Thu, 21 Sep 2023 14:06:25 +0200 Subject: [PATCH] Play nicely with third-party testing libraries See e.g. https://onsi.github.io/ginkgo/#using-third-party-libraries for a use case of this setup. --- pkg/test/golden/validator.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/pkg/test/golden/validator.go b/pkg/test/golden/validator.go index 1f171d4c..66160b8d 100644 --- a/pkg/test/golden/validator.go +++ b/pkg/test/golden/validator.go @@ -25,7 +25,6 @@ import ( "os/exec" "path/filepath" "strings" - "testing" "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/runtime" @@ -44,7 +43,25 @@ import ( "sigs.k8s.io/kustomize/kyaml/filesys" ) -func NewValidator(t *testing.T, b *scheme.Builder) *validator { +// T re-exposes the parts of testing.T we're using as an interface, +// in order to play more nicely with third-party libraries (e.g. gomega). +// This helps making these testing utils compatible with the testing strategy +// generated by kubebuilder. +// +// Note that testing.T has a bunch of other things as well; these are just the +// ones currently in use in this project. +// +// See e.g. https://github.com/onsi/ginkgo/blob/master/ginkgo_t_dsl.go#L7-L17 +type T interface { + Cleanup(func()) + Errorf(format string, args ...interface{}) + Fatalf(format string, args ...interface{}) + Helper() + Logf(format string, args ...interface{}) + TempDir() string +} + +func NewValidator(t T, b *scheme.Builder) *validator { v := &validator{T: t, scheme: runtime.NewScheme()} if err := b.AddToScheme(v.scheme); err != nil { t.Fatalf("error from AddToScheme: %v", err) @@ -87,7 +104,7 @@ func NewValidator(t *testing.T, b *scheme.Builder) *validator { } type validator struct { - T *testing.T + T T scheme *runtime.Scheme TestDir string @@ -351,7 +368,7 @@ func (v *validator) Validate(r declarative.Reconciler) { } } -func diffFiles(t *testing.T, expectedPath, actual string) error { +func diffFiles(t T, expectedPath, actual string) error { t.Helper() writeTmp := func(content string) (string, error) { tmp, err := os.CreateTemp("", "*.yaml")