Skip to content

Commit

Permalink
fix(testing): improve use cases
Browse files Browse the repository at this point in the history
  • Loading branch information
franklinkim committed Aug 3, 2023
1 parent 3948237 commit d46dd48
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
29 changes: 22 additions & 7 deletions testing/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"strings"
"testing"

os2 "github.com/foomo/go/os"
osx "github.com/foomo/go/os"
"github.com/foomo/go/testing/tag"
)

Expand Down Expand Up @@ -50,28 +50,43 @@ func skipTags(tags []tag.Tag) bool {
return true
}

envTags := os2.GetenvStringSlice(envTestTags, nil)
envTags := osx.GetenvStringSlice(envTestTags, nil)
// always return false if there are non tags defined
if envTags == nil {
return false
}

// translate tags
allExclude := true
envTagsMap := make(map[tag.Tag]bool, len(tags))
for _, s := range envTags {
envTagsMap[tag.Tag(strings.TrimPrefix(s, "-"))] = !strings.HasPrefix(s, "-")
include := !strings.HasPrefix(s, "-")
envTagsMap[tag.Tag(strings.TrimPrefix(s, "-"))] = include
if include {
allExclude = false
}
}

var (
anyFound bool
anyInclude bool
anyExclude bool
)
for _, v := range tags {
if v, ok := envTagsMap[v]; ok && v {
anyInclude = true
} else if ok && !v {
anyExclude = true
include, ok := envTagsMap[v]
if ok {
anyFound = true
if include {
anyInclude = true
} else {
anyExclude = true
}
}
}

if allExclude && !anyFound {
return false
}

return !(anyInclude && !anyExclude)
}
26 changes: 13 additions & 13 deletions testing/tag_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testing
package testing //nolint:testpackage

import (
"os"
Expand Down Expand Up @@ -41,13 +41,13 @@ func Test_skipTags(t *testing.T) {
want: true,
},
{
name: "skip for short and integration",
name: "skip for integration,security and integration",
env: "short",
tags: []tag.Tag{tag.Integration, tag.Security},
want: true,
},
{
name: "skip for short and integration,security",
name: "skip for integration,security and short",
env: "short",
tags: []tag.Tag{tag.Integration, tag.Security},
want: true,
Expand All @@ -59,37 +59,37 @@ func Test_skipTags(t *testing.T) {
want: false,
},
{
name: "don't skip for integration and integration,security",
name: "don't skip for integration,security and integration",
env: "integration",
tags: []tag.Tag{tag.Integration, tag.Security},
want: false,
},
{
name: "don't skip for security and integration,security",
name: "don't skip for integration,security and security",
env: "security",
tags: []tag.Tag{tag.Integration, tag.Security},
want: false,
},
{
name: "skip for security and -integration,security",
name: "skip for integration,security and -integration,security",
env: "-integration,security",
tags: []tag.Tag{tag.Integration, tag.Security},
want: true,
},
{
name: "skip for security and integration,-security",
name: "skip for integration,security and integration,-security",
env: "integration,-security",
tags: []tag.Tag{tag.Integration, tag.Security},
want: true,
},
{
name: "don't skip for integration,security and -short",
env: "-short",
tags: []tag.Tag{tag.Integration, tag.Security},
want: false,
},
}

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 == "" {
Expand Down

0 comments on commit d46dd48

Please sign in to comment.