test(gitea): test approval behavior
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing

This commit is contained in:
Peter 2023-03-10 10:19:41 +01:00
parent 0409dd2fba
commit eee9a30503
Signed by: prskr
GPG key ID: C1DB5D2E8DB512F9
4 changed files with 156 additions and 6 deletions

View file

@ -6,6 +6,7 @@ import (
giteaSdk "code.gitea.io/sdk/gitea"
"github.com/spf13/cobra"
"golang.org/x/exp/slog"
"code.icb4dc0.de/prskr/nitter/nitters/gitea"
)
@ -118,7 +119,7 @@ func runGiteaPRNitting(cmd *cobra.Command, args []string) error {
return err
}
nitter := gitea.NewPRNitter(giteaClient, cfg)
nitter := gitea.NewPRNitter(slog.Default(), giteaClient, cfg)
return nitter.Report(report, issues)
}
@ -156,7 +157,7 @@ func runGiteaPRLintNitting(cmd *cobra.Command, _ []string) error {
return err
}
nitter := gitea.NewPRNitter(giteaClient, &cfg.GiteaPRConfig)
nitter := gitea.NewPRNitter(slog.Default(), giteaClient, &cfg.GiteaPRConfig)
return nitter.Report(report, issues)
}

View file

@ -0,0 +1,29 @@
package logging
import (
"io"
"testing"
"golang.org/x/exp/slog"
)
var _ io.Writer = (*testWriter)(nil)
func NewWriter(tb testing.TB) io.Writer {
tb.Helper()
return testWriter{tb: tb}
}
func NewLogger(tb testing.TB) *slog.Logger {
tb.Helper()
return slog.New(slog.NewTextHandler(NewWriter(tb)))
}
type testWriter struct {
tb testing.TB
}
func (t testWriter) Write(p []byte) (n int, err error) {
t.tb.Log(string(p))
return len(p), nil
}

View file

@ -32,16 +32,23 @@ func init() {
}
}
func NewPRNitter(cli Client, cfg *GiteaPRConfig) *prNitter {
func NewPRNitter(logger *slog.Logger, cli Client, cfg *GiteaPRConfig) *prNitter {
if logger == nil {
logger = slog.Default()
}
return &prNitter{
Client: cli,
logger: logger,
cfg: cfg,
}
}
type prNitter struct {
Client
cfg *GiteaPRConfig
logger *slog.Logger
cfg *GiteaPRConfig
}
func (p prNitter) Report(report *report.Data, issues []result.Issue) error {

View file

@ -5,10 +5,11 @@ import (
"testing"
giteasdk "code.gitea.io/sdk/gitea"
"github.com/golangci/golangci-lint/pkg/report"
"github.com/golangci/golangci-lint/pkg/result"
"github.com/stretchr/testify/mock"
"code.icb4dc0.de/prskr/nitter/internal/logging"
"code.icb4dc0.de/prskr/nitter/nitters/gitea"
"code.icb4dc0.de/prskr/nitter/nitters/gitea/mocks"
)
@ -40,7 +41,7 @@ func Test_PRNitter_Report_MockCreatorError_Error(t *testing.T) {
Return(nil, nil, nil).
Times(1)
p := gitea.NewPRNitter(client, &gitea.GiteaPRConfig{})
p := gitea.NewPRNitter(logging.NewLogger(t), client, new(gitea.GiteaPRConfig))
if err := p.Report(new(report.Data), nil); err == nil {
t.Error("expected error bot got none")
@ -48,3 +49,115 @@ func Test_PRNitter_Report_MockCreatorError_Error(t *testing.T) {
t.Errorf("Error %v was not expected", err)
}
}
func Test_PRNitter_RequestChangesIfIssues_ReviewStateRequestChanges(t *testing.T) {
t.Parallel()
client := mocks.NewClient(t)
client.
EXPECT().
CreatePullReview(mock.Anything, mock.Anything, mock.Anything, expectReviewState(giteasdk.ReviewStateRequestChanges)).
Return(nil, nil, nil).
Times(1)
client.
EXPECT().
GetMyUserInfo().
Return(&giteasdk.User{ID: 11}, nil, nil).
Times(1)
client.
EXPECT().
GetPullRequest(mock.Anything, mock.Anything, mock.Anything).
Return(&giteasdk.PullRequest{Poster: &giteasdk.User{ID: 42}}, nil, nil)
client.
EXPECT().
ListPullReviews(mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return(nil, nil, nil).
Times(1)
p := gitea.NewPRNitter(logging.NewLogger(t), client, &gitea.GiteaPRConfig{ReviewState: giteasdk.ReviewStateRequestChanges})
if err := p.Report(new(report.Data), make([]result.Issue, 1)); err != nil {
t.Errorf("Did not expect error but got %v", err)
}
}
func Test_PRNitter_ApproveOthersPR_ReviewStateApproved(t *testing.T) {
t.Parallel()
client := mocks.NewClient(t)
client.
EXPECT().
CreatePullReview(mock.Anything, mock.Anything, mock.Anything, expectReviewState(giteasdk.ReviewStateApproved)).
Return(nil, nil, nil).
Times(1)
client.
EXPECT().
GetMyUserInfo().
Return(&giteasdk.User{ID: 11}, nil, nil).
Times(1)
client.
EXPECT().
GetPullRequest(mock.Anything, mock.Anything, mock.Anything).
Return(&giteasdk.PullRequest{Poster: &giteasdk.User{ID: 42}}, nil, nil)
client.
EXPECT().
ListPullReviews(mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return(nil, nil, nil).
Times(1)
p := gitea.NewPRNitter(logging.NewLogger(t), client, new(gitea.GiteaPRConfig))
if err := p.Report(new(report.Data), nil); err != nil {
t.Errorf("Did not expect error but got %v", err)
}
}
func Test_PRNitter_DontApproveMyOwnPR_ReviewStateComment(t *testing.T) {
t.Parallel()
client := mocks.NewClient(t)
client.
EXPECT().
CreatePullReview(mock.Anything, mock.Anything, mock.Anything, expectReviewState(giteasdk.ReviewStateComment)).
Return(nil, nil, nil).
Times(1)
client.
EXPECT().
GetMyUserInfo().
Return(&giteasdk.User{ID: 11}, nil, nil).
Times(1)
client.
EXPECT().
GetPullRequest(mock.Anything, mock.Anything, mock.Anything).
Return(&giteasdk.PullRequest{Poster: &giteasdk.User{ID: 11}}, nil, nil)
client.
EXPECT().
ListPullReviews(mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return(nil, nil, nil).
Times(1)
p := gitea.NewPRNitter(logging.NewLogger(t), client, new(gitea.GiteaPRConfig))
if err := p.Report(new(report.Data), nil); err != nil {
t.Errorf("Did not expect error but got %v", err)
}
}
func match[T any](matcher func(arg T) bool) any {
return mock.MatchedBy(matcher)
}
func expectReviewState(expected giteasdk.ReviewStateType) any {
return match[giteasdk.CreatePullReviewOptions](func(arg giteasdk.CreatePullReviewOptions) bool {
return arg.State == expected
})
}