Skip to content

Commit

Permalink
fix(testing): fix all usecases
Browse files Browse the repository at this point in the history
  • Loading branch information
franklinkim committed Aug 2, 2023
1 parent 22ed5aa commit aa7f3b3
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 20 deletions.
47 changes: 27 additions & 20 deletions testing/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,46 @@ const envTestTags = "GO_TEST_TAGS"
// - `GO_TEST_TAGS=fast,-integration`
func Tags(t *testing.T, tags ...tag.Tag) {
t.Helper()
testing.Short()

// always skip if no tags are provided so it can be used as block tests
if len(tags) == 0 {
t.Skip("skipping untagged test")
return
}
ok := true
for _, v := range tags {
if !getTag(v) {
ok = false
}
}
if !ok {

if skipTags(tags) {
t.Skipf("skipping test with tag: %s", tags)
}
}

func getTag(v tag.Tag) bool {
tags := os2.GetenvStrings(envTestTags, nil)

// always return true if there are non tags defined
if tags == nil {
func skipTags(tags []tag.Tag) bool {
// always skip if no tags are provided so it can be used as block tests
if len(tags) == 0 {
return true
}

// translate tags
tagsMap := make(map[tag.Tag]bool, len(tags))
for _, s := range tags {
tagsMap[tag.Tag(strings.TrimPrefix(s, "-"))] = !strings.HasPrefix(s, "-")
envTags := os2.GetenvStrings(envTestTags, nil)
// always return false if there are non tags defined
if envTags == nil {
return false
}

if v, ok := tagsMap[v]; ok {
return v
// translate tags
envTagsMap := make(map[tag.Tag]bool, len(tags))
for _, s := range envTags {
envTagsMap[tag.Tag(strings.TrimPrefix(s, "-"))] = !strings.HasPrefix(s, "-")
}

return false
var (
anyInclude bool
anyExclude bool
)
for _, v := range tags {
if v, ok := envTagsMap[v]; ok && v {
anyInclude = true
} else if ok && !v {
anyExclude = true
}
}
return !(anyInclude && !anyExclude)
}
103 changes: 103 additions & 0 deletions testing/tag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package testing

import (
"os"
"testing"

"github.com/foomo/go/testing/tag"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_skipTags(t *testing.T) {
tests := []struct {
name string
env string
tags []tag.Tag
want bool
}{
{
name: "skip for nil",
env: "",
tags: nil,
want: true,
},
{
name: "skip for nil and short",
env: "short",
tags: nil,
want: true,
},
{
name: "skip for empty and short",
env: "short",
tags: []tag.Tag{},
want: true,
},
{
name: "skip for empty and short",
env: "short",
tags: []tag.Tag{},
want: true,
},
{
name: "skip for short and integration",
env: "short",
tags: []tag.Tag{tag.Integration, tag.Security},
want: true,
},
{
name: "skip for short and integration,security",
env: "short",
tags: []tag.Tag{tag.Integration, tag.Security},
want: true,
},
{
name: "don't skip for integration and integration",
env: "integration",
tags: []tag.Tag{tag.Integration},
want: false,
},
{
name: "don't skip for integration and integration,security",
env: "integration",
tags: []tag.Tag{tag.Integration, tag.Security},
want: false,
},
{
name: "don't skip for security and integration,security",
env: "security",
tags: []tag.Tag{tag.Integration, tag.Security},
want: false,
},
{
name: "skip for security and -integration,security",
env: "-integration,security",
tags: []tag.Tag{tag.Integration, tag.Security},
want: true,
},
{
name: "skip for security and integration,-security",
env: "integration,-security",
tags: []tag.Tag{tag.Integration, tag.Security},
want: true,
},
}

require.NoError(t, os.Setenv(envTestTags, "short,integration,security"))
assert.False(t, skipTags([]tag.Tag{tag.Integration, tag.Security}))

require.NoError(t, os.Setenv(envTestTags, "short,integration,-security"))
assert.True(t, skipTags([]tag.Tag{tag.Integration, tag.Security}))

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.env == "" {
require.NoError(t, os.Unsetenv(envTestTags))
} else {
require.NoError(t, os.Setenv(envTestTags, tt.env))
}
assert.Equalf(t, tt.want, skipTags(tt.tags), "skipTags(%v)", tt.tags)
})
}
}

0 comments on commit aa7f3b3

Please sign in to comment.