Skip to content

Commit

Permalink
Fix contract test skips
Browse files Browse the repository at this point in the history
  • Loading branch information
josvazg committed Jan 15, 2025
1 parent a04982b commit 7ed2616
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 10 deletions.
23 changes: 13 additions & 10 deletions test/helper/contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -48,16 +49,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 skipReason :=skipCheck(name, os.Getenv("AKO_CONTRACT_TEST_FOCUS"), control.Enabled("AKO_CONTRACT_TEST")); skipReason != "" {
t.Skip(skipReason)
}
ct := newContractTest(ctx)
defer func() {
Expand All @@ -68,6 +61,16 @@ func RunGoContractTest(ctx context.Context, t *testing.T, name string, contractT
})
}

func skipCheck(name, focus string, enabled bool) string {
if !enabled {
return "Skipping contract test as AKO_CONTRACT_TEST is unset"
}
if focus != "" && !strings.Contains(name, focus) {
return fmt.Sprintf("Skipping contract test %q as it does not contain focus string %q", name, focus)
}
return ""
}

func newContractTest(ctx context.Context) *contractTest {
return &contractTest{
k8sClient: mustCreateK8sClient(),
Expand Down
66 changes: 66 additions & 0 deletions test/helper/contract/skip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package contract

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSkip(t *testing.T) {
for _, tc := range []struct {
title string
name string
focus string
enabled bool
expected string
}{
{
title: "empty name, focus and disabled skips all",
expected: "Skipping contract test as 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: "Skipping contract test as 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: "Skipping contract test \"something else\" as it does not contain focus string \"target\"",
},
{
title: "enabled with matching focus does not skip",
enabled: true,
name: "target",
focus: "target",
},
{
title: "enabled matching a sugtarget focus does not skip",
enabled: true,
name: "some target phrase",
focus: "target",
},
} {
t.Run(tc.title, func(t *testing.T) {
skipReason := skipCheck(tc.name, tc.focus, tc.enabled)
assert.Equal(t, tc.expected, skipReason)
})
}
}

0 comments on commit 7ed2616

Please sign in to comment.