diff --git a/test/helper/contract/contract.go b/test/helper/contract/contract.go index ba0e58c359..f88024ad33 100644 --- a/test/helper/contract/contract.go +++ b/test/helper/contract/contract.go @@ -2,8 +2,10 @@ package contract import ( "context" + "errors" "fmt" "os" + "strings" "testing" "time" @@ -48,16 +50,8 @@ func (ct *contractTest) cleanup(ctx context.Context) error { } func RunGoContractTest(ctx context.Context, t *testing.T, name string, contractTest func(ch ContractHelper)) { - enabled := control.Enabled("AKO_CONTRACT_TEST") - focus := os.Getenv("AKO_CONTRACT_TEST_FOCUS") - focused := focus != "" && focus == name - if !enabled || !focused { - if !focused { - t.Skipf("Skipping contract test %q as focus is %q", name, focus) - } else { - t.Skip("Skipping contract test as AKO_CONTRACT_TEST is unset") - } - return + if err := skipCheck(name, os.Getenv("AKO_CONTRACT_TEST_FOCUS"), control.Enabled("AKO_CONTRACT_TEST")); err != nil { + t.Skipf("Skipping contract test: %v", err.Error()) } ct := newContractTest(ctx) defer func() { @@ -68,6 +62,16 @@ func RunGoContractTest(ctx context.Context, t *testing.T, name string, contractT }) } +func skipCheck(name, focus string, enabled bool) error { + if !enabled { + return errors.New("AKO_CONTRACT_TEST is unset") + } + if focus != "" && !strings.Contains(name, focus) { + return fmt.Errorf("test %q does not contain focus string %q", name, focus) + } + return nil +} + func newContractTest(ctx context.Context) *contractTest { return &contractTest{ k8sClient: mustCreateK8sClient(), diff --git a/test/helper/contract/skip_test.go b/test/helper/contract/skip_test.go new file mode 100644 index 0000000000..2bc1a55b94 --- /dev/null +++ b/test/helper/contract/skip_test.go @@ -0,0 +1,67 @@ +package contract + +import ( + "errors" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSkip(t *testing.T) { + for _, tc := range []struct { + title string + name string + focus string + enabled bool + expected error + }{ + { + title: "empty name, focus and disabled skips all", + expected: errors.New("AKO_CONTRACT_TEST is unset"), + }, + { + title: "empty name, focus and enabled does not skip", + enabled: true, + }, + { + title: "disabled skips regardles of focus matching", + name: "target", + focus: "target", + expected: errors.New("AKO_CONTRACT_TEST is unset"), + }, + { + title: "enabled with no focus does not skip", + enabled: true, + name: "target", + }, + { + title: "enabled with no focus does not skip", + enabled: true, + name: "target", + }, + { + title: "enabled with non matching focus skips", + enabled: true, + name: "something else", + focus: "target", + expected: errors.New("test \"something else\" does not contain focus string \"target\""), + }, + { + title: "enabled with matching focus does not skip", + enabled: true, + name: "target", + focus: "target", + }, + { + title: "enabled matching a sub-target focus does not skip", + enabled: true, + name: "some target phrase", + focus: "target", + }, + } { + t.Run(tc.title, func(t *testing.T) { + err := skipCheck(tc.name, tc.focus, tc.enabled) + assert.Equal(t, tc.expected, err) + }) + } +}