diff --git a/contribs/github-bot/condition/assignee_test.go b/contribs/github-bot/condition/assignee_test.go index 527c479ffb0..f0090ffb249 100644 --- a/contribs/github-bot/condition/assignee_test.go +++ b/contribs/github-bot/condition/assignee_test.go @@ -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" @@ -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) + } + }) + } +} diff --git a/contribs/github-bot/condition/author_test.go b/contribs/github-bot/condition/author_test.go index 607045e4068..d93a50ba89a 100644 --- a/contribs/github-bot/condition/author_test.go +++ b/contribs/github-bot/condition/author_test.go @@ -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) { diff --git a/contribs/github-bot/requirement/assignee_test.go b/contribs/github-bot/requirement/assignee_test.go index 9f277f2c31f..d6e10a86b63 100644 --- a/contribs/github-bot/requirement/assignee_test.go +++ b/contribs/github-bot/requirement/assignee_test.go @@ -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) { @@ -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") } }) diff --git a/contribs/github-bot/requirement/author_test.go b/contribs/github-bot/requirement/author_test.go index d32f5fb4257..b318de9a80f 100644 --- a/contribs/github-bot/requirement/author_test.go +++ b/contribs/github-bot/requirement/author_test.go @@ -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) { diff --git a/contribs/github-bot/requirement/label_test.go b/contribs/github-bot/requirement/label_test.go index f012b746cee..28927125c31 100644 --- a/contribs/github-bot/requirement/label_test.go +++ b/contribs/github-bot/requirement/label_test.go @@ -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) { @@ -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") } })