Fix failing tests

Signed-off-by: Steven Kriegler <61625851+justusbunsi@users.noreply.github.com>
This commit is contained in:
justusbunsi 2021-10-03 17:49:23 +02:00
parent 1dab92385f
commit 16f545f179
No known key found for this signature in database
GPG key ID: 990B348ECAC9C7DB
3 changed files with 26 additions and 11 deletions

1
.gitignore vendored
View file

@ -5,3 +5,4 @@
/gitea-sonarqube-bot /gitea-sonarqube-bot
/coverage.html /coverage.html
/*.log /*.log
/cover.out

View file

@ -3,9 +3,10 @@
## Table of Contents ## Table of Contents
- [Contribution Guidelines](#contribution-guidelines) - [Contribution Guidelines](#contribution-guidelines)
- [Setup development environment](#setup-development-environment) - [Table of Contents](#table-of-contents)
- [Testing](#testing) - [Setup development environment](#setup-development-environment)
- [Developer Certificate of Origin (DCO)](#developer-certificate-of-origin-dco) - [Testing](#testing)
- [Developer Certificate of Origin (DCO)](#developer-certificate-of-origin-dco)
## Setup development environment ## Setup development environment
@ -26,7 +27,11 @@ go build ./cmd/gitea-sonarqube-bot
## Testing ## Testing
```bash ```bash
# generic test execution
go test ./... go test ./...
# or with coverage report
go test -coverprofile cover.out ./...
``` ```
## Developer Certificate of Origin (DCO) ## Developer Certificate of Origin (DCO)

View file

@ -6,10 +6,11 @@ import (
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"gitea-sonarqube-pr-bot/internal/settings" "gitea-sonarqube-pr-bot/internal/settings"
webhook "gitea-sonarqube-pr-bot/internal/webhooks/sonarqube" webhook "gitea-sonarqube-pr-bot/internal/webhooks/sonarqube"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
) )
type HandlerPartialMock struct { type HandlerPartialMock struct {
@ -28,6 +29,14 @@ func (h *GiteaSdkMock) PostComment(_ settings.GiteaRepository, _ int, _ string)
return nil return nil
} }
type SQSdkMock struct {
mock.Mock
}
func (h *SQSdkMock) GetMeasures(project string, branch string) (string, error) {
return "", nil
}
func defaultMockPreparation(h *HandlerPartialMock) { func defaultMockPreparation(h *HandlerPartialMock) {
h.On("fetchDetails", mock.Anything).Return(nil) h.On("fetchDetails", mock.Anything).Return(nil)
} }
@ -36,7 +45,7 @@ func withValidRequestData(t *testing.T, mockPreparation func(*HandlerPartialMock
partialMock := new(HandlerPartialMock) partialMock := new(HandlerPartialMock)
mockPreparation(partialMock) mockPreparation(partialMock)
webhookHandler := NewSonarQubeWebhookHandler(new(GiteaSdkMock)) webhookHandler := NewSonarQubeWebhookHandler(new(GiteaSdkMock), new(SQSdkMock))
webhookHandler.fetchDetails = partialMock.fetchDetails webhookHandler.fetchDetails = partialMock.fetchDetails
req, err := http.NewRequest("POST", "/hooks/sonarqube", bytes.NewBuffer(jsonBody)) req, err := http.NewRequest("POST", "/hooks/sonarqube", bytes.NewBuffer(jsonBody))
@ -54,7 +63,7 @@ func withValidRequestData(t *testing.T, mockPreparation func(*HandlerPartialMock
func TestHandleSonarQubeWebhookProjectMapped(t *testing.T) { func TestHandleSonarQubeWebhookProjectMapped(t *testing.T) {
settings.Projects = []settings.Project{ settings.Projects = []settings.Project{
settings.Project{ settings.Project{
SonarQube: struct{Key string}{ SonarQube: struct{ Key string }{
Key: "pr-bot", Key: "pr-bot",
}, },
}, },
@ -69,7 +78,7 @@ func TestHandleSonarQubeWebhookProjectMapped(t *testing.T) {
func TestHandleSonarQubeWebhookProjectNotMapped(t *testing.T) { func TestHandleSonarQubeWebhookProjectNotMapped(t *testing.T) {
settings.Projects = []settings.Project{ settings.Projects = []settings.Project{
settings.Project{ settings.Project{
SonarQube: struct{Key string}{ SonarQube: struct{ Key string }{
Key: "another-project", Key: "another-project",
}, },
}, },
@ -84,7 +93,7 @@ func TestHandleSonarQubeWebhookProjectNotMapped(t *testing.T) {
func TestHandleSonarQubeWebhookInvalidJSONBody(t *testing.T) { func TestHandleSonarQubeWebhookInvalidJSONBody(t *testing.T) {
settings.Projects = []settings.Project{ settings.Projects = []settings.Project{
settings.Project{ settings.Project{
SonarQube: struct{Key string}{ SonarQube: struct{ Key string }{
Key: "pr-bot", Key: "pr-bot",
}, },
}, },
@ -100,7 +109,7 @@ func TestHandleSonarQubeWebhookInvalidJSONBody(t *testing.T) {
func TestHandleSonarQubeWebhookForPullRequest(t *testing.T) { func TestHandleSonarQubeWebhookForPullRequest(t *testing.T) {
settings.Projects = []settings.Project{ settings.Projects = []settings.Project{
settings.Project{ settings.Project{
SonarQube: struct{Key string}{ SonarQube: struct{ Key string }{
Key: "pr-bot", Key: "pr-bot",
}, },
}, },
@ -117,7 +126,7 @@ func TestHandleSonarQubeWebhookForPullRequest(t *testing.T) {
func TestHandleSonarQubeWebhookForBranch(t *testing.T) { func TestHandleSonarQubeWebhookForBranch(t *testing.T) {
settings.Projects = []settings.Project{ settings.Projects = []settings.Project{
settings.Project{ settings.Project{
SonarQube: struct{Key string}{ SonarQube: struct{ Key string }{
Key: "pr-bot", Key: "pr-bot",
}, },
}, },