Skip to content

Commit

Permalink
test: improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
aeddi committed Nov 11, 2024
1 parent 99256ed commit 78268ac
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 22 deletions.
61 changes: 61 additions & 0 deletions contribs/github-bot/condition/assignee_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package condition

import (
"context"
"testing"

"github.com/gnolang/gno/contribs/github-bot/client"
"github.com/gnolang/gno/contribs/github-bot/logger"
"github.com/gnolang/gno/contribs/github-bot/utils"
"github.com/migueleliasweb/go-github-mock/src/mock"

"github.com/google/go-github/v64/github"
"github.com/xlab/treeprint"
Expand Down Expand Up @@ -45,3 +49,60 @@ func TestAssignee(t *testing.T) {
})
}
}

func TestAssigneeInTeam(t *testing.T) {
t.Parallel()

members := []*github.User{
{Login: github.String("notTheRightOne")},
{Login: github.String("user")},
{Login: github.String("anotherOne")},
}

for _, testCase := range []struct {
name string
user string
members []*github.User
isMet bool
}{
{"empty assignee list", "user", []*github.User{}, false},
{"assignee list contains user", "user", members, true},
{"assignee list doesn't contain user", "user2", members, false},
} {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()

mockedHTTPClient := mock.NewMockedHTTPClient(
mock.WithRequestMatchPages(
mock.EndpointPattern{
Pattern: "/orgs/teams/team/members",
Method: "GET",
},
testCase.members,
),
)

gh := &client.GitHub{
Client: github.NewClient(mockedHTTPClient),
Ctx: context.Background(),
Logger: logger.NewNoopLogger(),
}

pr := &github.PullRequest{
Assignees: []*github.User{
{Login: github.String(testCase.user)},
},
}
details := treeprint.New()
condition := AssigneeInTeam(gh, "team")

if condition.IsMet(pr, details) != testCase.isMet {
t.Errorf("condition should have a met status: %t", testCase.isMet)
}
if !utils.TestLastNodeStatus(t, testCase.isMet, details) {
t.Errorf("condition details should have a status: %t", testCase.isMet)
}
})
}
}
6 changes: 3 additions & 3 deletions contribs/github-bot/condition/author_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ func TestAuthorInTeam(t *testing.T) {
members []*github.User
isMet bool
}{
{"empty assignee list", "user", []*github.User{}, false},
{"assignee list contains user", "user", members, true},
{"assignee list doesn't contain user", "user2", members, false},
{"empty member list", "user", []*github.User{}, false},
{"member list contains user", "user", members, true},
{"member list doesn't contain user", "user2", members, false},
} {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
Expand Down
16 changes: 10 additions & 6 deletions contribs/github-bot/requirement/assignee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ func TestAssignee(t *testing.T) {
name string
user string
assignees []*github.User
dryRun bool
exists bool
}{
{"empty assignee list", "user", []*github.User{}, false},
{"assignee list contains user", "user", assignees, true},
{"assignee list doesn't contain user", "user2", assignees, false},
{"empty assignee list", "user", []*github.User{}, false, false},
{"empty assignee list with dry-run", "user", []*github.User{}, true, false},
{"assignee list contains user", "user", assignees, false, true},
{"assignee list doesn't contain user", "user2", assignees, false, false},
{"assignee list doesn't contain user with dry-run", "user2", assignees, true, false},
} {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
Expand All @@ -54,19 +57,20 @@ func TestAssignee(t *testing.T) {
Client: github.NewClient(mockedHTTPClient),
Ctx: context.Background(),
Logger: logger.NewNoopLogger(),
DryRun: testCase.dryRun,
}

pr := &github.PullRequest{Assignees: testCase.assignees}
details := treeprint.New()
requirement := Assignee(gh, testCase.user)

if !requirement.IsSatisfied(pr, details) {
if !requirement.IsSatisfied(pr, details) && !testCase.dryRun {
t.Errorf("requirement should have a satisfied status: %t", true)
}
if !utils.TestLastNodeStatus(t, true, details) {
if !utils.TestLastNodeStatus(t, true, details) && !testCase.dryRun {
t.Errorf("requirement details should have a status: %t", true)
}
if !testCase.exists && !requested {
if !testCase.exists && !requested && !testCase.dryRun {
t.Errorf("requirement should have requested to create item")
}
})
Expand Down
6 changes: 3 additions & 3 deletions contribs/github-bot/requirement/author_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ func TestAuthorInTeam(t *testing.T) {
members []*github.User
isSatisfied bool
}{
{"empty assignee list", "user", []*github.User{}, false},
{"assignee list contains user", "user", members, true},
{"assignee list doesn't contain user", "user2", members, false},
{"empty member list", "user", []*github.User{}, false},
{"member list contains user", "user", members, true},
{"member list doesn't contain user", "user2", members, false},
} {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
Expand Down
27 changes: 17 additions & 10 deletions contribs/github-bot/requirement/label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,21 @@ func TestLabel(t *testing.T) {
name string
pattern string
labels []*github.Label
dryRun bool
exists bool
}{
{"empty label list", "label", []*github.Label{}, false},
{"label list contains exact match", "label", labels, true},
{"label list contains prefix match", "^lab", labels, true},
{"label list contains prefix doesn't match", "lab$", labels, false},
{"label list contains suffix match", "bel$", labels, true},
{"label list contains suffix doesn't match", "^bel", labels, false},
{"label list doesn't contains match", "baleb", labels, false},
{"empty label list", "label", []*github.Label{}, false, false},
{"empty label list with dry-run", "label", []*github.Label{}, true, false},
{"label list contains exact match", "label", labels, false, true},
{"label list contains prefix match", "^lab", labels, false, true},
{"label list contains prefix doesn't match", "lab$", labels, false, false},
{"label list contains prefix doesn't match with dry-run", "lab$", labels, true, false},
{"label list contains suffix match", "bel$", labels, false, true},
{"label list contains suffix match with dry-run", "bel$", labels, true, true},
{"label list contains suffix doesn't match", "^bel", labels, false, false},
{"label list contains suffix doesn't match with dry-run", "^bel", labels, true, false},
{"label list doesn't contains match", "baleb", labels, false, false},
{"label list doesn't contains match with dry-run", "baleb", labels, true, false},
} {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
Expand All @@ -58,19 +64,20 @@ func TestLabel(t *testing.T) {
Client: github.NewClient(mockedHTTPClient),
Ctx: context.Background(),
Logger: logger.NewNoopLogger(),
DryRun: testCase.dryRun,
}

pr := &github.PullRequest{Labels: testCase.labels}
details := treeprint.New()
requirement := Label(gh, testCase.pattern)

if !requirement.IsSatisfied(pr, details) {
if !requirement.IsSatisfied(pr, details) && !testCase.dryRun {
t.Errorf("requirement should have a satisfied status: %t", true)
}
if !utils.TestLastNodeStatus(t, true, details) {
if !utils.TestLastNodeStatus(t, true, details) && !testCase.dryRun {
t.Errorf("requirement details should have a status: %t", true)
}
if !testCase.exists && !requested {
if !testCase.exists && !requested && !testCase.dryRun {
t.Errorf("requirement should have requested to create item")
}
})
Expand Down

0 comments on commit 78268ac

Please sign in to comment.