Skip to content

Commit be2179d

Browse files
committed
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.
1 parent 67b1614 commit be2179d

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

pkg/test/golden/validator.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"os/exec"
2626
"path/filepath"
2727
"strings"
28-
"testing"
2928

3029
"k8s.io/apimachinery/pkg/api/meta"
3130
"k8s.io/apimachinery/pkg/runtime"
@@ -44,7 +43,25 @@ import (
4443
"sigs.k8s.io/kustomize/kyaml/filesys"
4544
)
4645

47-
func NewValidator(t *testing.T, b *scheme.Builder) *validator {
46+
// T re-exposes the parts of testing.T we're using as an interface,
47+
// in order to play more nicely with third-party libraries (e.g. gomega).
48+
// This helps making these testing utils compatible with the testing strategy
49+
// generated by kubebuilder.
50+
//
51+
// Note that testing.T has a bunch of other things as well; these are just the
52+
// ones currently in use in this project.
53+
//
54+
// See e.g. https://github.com/onsi/ginkgo/blob/master/ginkgo_t_dsl.go#L7-L17
55+
type T interface {
56+
Cleanup(func())
57+
Errorf(format string, args ...interface{})
58+
Fatalf(format string, args ...interface{})
59+
Helper()
60+
Logf(format string, args ...interface{})
61+
TempDir() string
62+
}
63+
64+
func NewValidator(t T, b *scheme.Builder) *validator {
4865
v := &validator{T: t, scheme: runtime.NewScheme()}
4966
if err := b.AddToScheme(v.scheme); err != nil {
5067
t.Fatalf("error from AddToScheme: %v", err)
@@ -87,7 +104,7 @@ func NewValidator(t *testing.T, b *scheme.Builder) *validator {
87104
}
88105

89106
type validator struct {
90-
T *testing.T
107+
T T
91108
scheme *runtime.Scheme
92109
TestDir string
93110

@@ -351,7 +368,7 @@ func (v *validator) Validate(r declarative.Reconciler) {
351368
}
352369
}
353370

354-
func diffFiles(t *testing.T, expectedPath, actual string) error {
371+
func diffFiles(t T, expectedPath, actual string) error {
355372
t.Helper()
356373
writeTmp := func(content string) (string, error) {
357374
tmp, err := os.CreateTemp("", "*.yaml")

0 commit comments

Comments
 (0)