diff --git a/internal/api/main_test.go b/internal/api/main_test.go index c950486..8d181ef 100644 --- a/internal/api/main_test.go +++ b/internal/api/main_test.go @@ -5,8 +5,64 @@ import ( "log" "os" "testing" + + giteaSdk "gitea-sonarqube-pr-bot/internal/clients/gitea" + sqSdk "gitea-sonarqube-pr-bot/internal/clients/sonarqube" + "gitea-sonarqube-pr-bot/internal/settings" + webhook "gitea-sonarqube-pr-bot/internal/webhooks/sonarqube" + + "github.com/stretchr/testify/mock" ) +// Default SDK mocking +type HandlerPartialMock struct { + mock.Mock +} + +func (h *HandlerPartialMock) fetchDetails(w *webhook.Webhook) { + h.Called(w) +} + +type GiteaSdkMock struct { + mock.Mock +} + +func (h *GiteaSdkMock) PostComment(_ settings.GiteaRepository, _ int, _ string) error { + return nil +} + +func (h *GiteaSdkMock) DetermineHEAD(_ settings.GiteaRepository, _ int64) (string, error) { + return "", nil +} + +func (h *GiteaSdkMock) UpdateStatus(_ settings.GiteaRepository, _ string, _ giteaSdk.StatusDetails) error { + return nil +} + +type SQSdkMock struct { + mock.Mock +} + +func (h *SQSdkMock) GetMeasures(project string, branch string) (*sqSdk.MeasuresResponse, error) { + return &sqSdk.MeasuresResponse{}, nil +} + +func (h *SQSdkMock) GetPullRequestUrl(project string, index int64) string { + return "" +} + +func (h *SQSdkMock) GetPullRequest(project string, index int64) (*sqSdk.PullRequest, error) { + return nil, nil +} + +func (h *SQSdkMock) ComposeGiteaComment(data *sqSdk.CommentComposeData) (string, error) { + return "", nil +} + +func defaultMockPreparation(h *HandlerPartialMock) { + h.On("fetchDetails", mock.Anything).Return(nil) +} + // SETUP: mute logs func TestMain(m *testing.M) { log.SetOutput(ioutil.Discard) diff --git a/internal/api/sonarqube_test.go b/internal/api/sonarqube_test.go index cc6794b..a349731 100644 --- a/internal/api/sonarqube_test.go +++ b/internal/api/sonarqube_test.go @@ -6,63 +6,11 @@ import ( "net/http/httptest" "testing" - giteaSdk "gitea-sonarqube-pr-bot/internal/clients/gitea" - sqSdk "gitea-sonarqube-pr-bot/internal/clients/sonarqube" "gitea-sonarqube-pr-bot/internal/settings" - webhook "gitea-sonarqube-pr-bot/internal/webhooks/sonarqube" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" ) -type HandlerPartialMock struct { - mock.Mock -} - -func (h *HandlerPartialMock) fetchDetails(w *webhook.Webhook) { - h.Called(w) -} - -type GiteaSdkMock struct { - mock.Mock -} - -func (h *GiteaSdkMock) PostComment(_ settings.GiteaRepository, _ int, _ string) error { - return nil -} - -func (h *GiteaSdkMock) DetermineHEAD(_ settings.GiteaRepository, _ int64) (string, error) { - return "", nil -} - -func (h *GiteaSdkMock) UpdateStatus(_ settings.GiteaRepository, _ string, _ giteaSdk.StatusDetails) error { - return nil -} - -type SQSdkMock struct { - mock.Mock -} - -func (h *SQSdkMock) GetMeasures(project string, branch string) (*sqSdk.MeasuresResponse, error) { - return &sqSdk.MeasuresResponse{}, nil -} - -func (h *SQSdkMock) GetPullRequestUrl(project string, index int64) string { - return "" -} - -func (h *SQSdkMock) GetPullRequest(project string, index int64) (*sqSdk.PullRequest, error) { - return nil, nil -} - -func (h *SQSdkMock) ComposeGiteaComment(data *sqSdk.CommentComposeData) (string, error) { - return "", nil -} - -func defaultMockPreparation(h *HandlerPartialMock) { - h.On("fetchDetails", mock.Anything).Return(nil) -} - func withValidRequestData(t *testing.T, mockPreparation func(*HandlerPartialMock), jsonBody []byte) (*http.Request, *httptest.ResponseRecorder, http.HandlerFunc, *HandlerPartialMock) { partialMock := new(HandlerPartialMock) mockPreparation(partialMock)