Skip to content

Commit

Permalink
Merge pull request #353 from tomasaschan/play-nicely-with-third-party…
Browse files Browse the repository at this point in the history
…-testing

Play nicely with third-party testing libraries
  • Loading branch information
k8s-ci-robot authored Oct 13, 2023
2 parents 2bcbaa8 + be2179d commit e772071
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions pkg/test/golden/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"os/exec"
"path/filepath"
"strings"
"testing"

"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit e772071

Please sign in to comment.