Add missing tests for Gitea client
Signed-off-by: Steven Kriegler <sk.bunsenbrenner@gmail.com>
This commit is contained in:
parent
54aad1817b
commit
77a4943a63
8
Makefile
8
Makefile
|
@ -10,7 +10,8 @@ help:
|
||||||
@echo " - test Run full test suite"
|
@echo " - test Run full test suite"
|
||||||
@echo " - test p=./path/to/package Run test suite for specific package"
|
@echo " - test p=./path/to/package Run test suite for specific package"
|
||||||
@echo " - test\#SpecificTestName Run a specific"
|
@echo " - test\#SpecificTestName Run a specific"
|
||||||
@echo " - coverage Run full test suite and generates coverage report as HTML file"
|
@echo " - coverage Run full test suite and generate coverage report as HTML file"
|
||||||
|
@echo " - coverage p=./path/to/package Run test suite for specific package and generate coverage report as HTML file"
|
||||||
@echo " - helm-params Auto-generates 'Parameters' section of 'helm/README.md' based on comments in values.yaml"
|
@echo " - helm-params Auto-generates 'Parameters' section of 'helm/README.md' based on comments in values.yaml"
|
||||||
@echo " - helm-pack Prepares Helm Chart release artifacts for pushing to 'charts' branch"
|
@echo " - helm-pack Prepares Helm Chart release artifacts for pushing to 'charts' branch"
|
||||||
@echo " - dep Dependency maintenance (tidy, vendor, verify)"
|
@echo " - dep Dependency maintenance (tidy, vendor, verify)"
|
||||||
|
@ -44,8 +45,13 @@ test-ci:
|
||||||
go test -mod=vendor -coverprofile=cover.out -json ./... > test-report.out
|
go test -mod=vendor -coverprofile=cover.out -json ./... > test-report.out
|
||||||
|
|
||||||
coverage:
|
coverage:
|
||||||
|
ifdef p
|
||||||
|
go test -coverprofile=cover.out $(p)
|
||||||
|
go tool cover -html=cover.out -o cover.html
|
||||||
|
else
|
||||||
go test -coverprofile=cover.out ./...
|
go test -coverprofile=cover.out ./...
|
||||||
go tool cover -html=cover.out -o cover.html
|
go tool cover -html=cover.out -o cover.html
|
||||||
|
endif
|
||||||
|
|
||||||
helm-params:
|
helm-params:
|
||||||
npm install
|
npm install
|
||||||
|
|
|
@ -1,36 +1,198 @@
|
||||||
package gitea
|
package gitea
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"code.gitea.io/sdk/gitea"
|
||||||
"codeberg.org/justusbunsi/gitea-sonarqube-bot/internal/settings"
|
"codeberg.org/justusbunsi/gitea-sonarqube-bot/internal/settings"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SdkMock struct {
|
type SdkMock struct {
|
||||||
|
simulatedError error
|
||||||
|
mock.Mock
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *SdkMock) CreateIssueComment(owner, repo string, index int64, opt gitea.CreateIssueCommentOption) (*gitea.Comment, *gitea.Response, error) {
|
func (m *SdkMock) CreateIssueComment(owner, repo string, index int64, opt gitea.CreateIssueCommentOption) (*gitea.Comment, *gitea.Response, error) {
|
||||||
return nil, nil, nil
|
m.Called(owner, repo, index, opt)
|
||||||
|
return nil, nil, m.simulatedError
|
||||||
}
|
}
|
||||||
func (m *SdkMock) CreateStatus(owner, repo, sha string, opts gitea.CreateStatusOption) (*gitea.Status, *gitea.Response, error) {
|
func (m *SdkMock) CreateStatus(owner, repo, sha string, opts gitea.CreateStatusOption) (*gitea.Status, *gitea.Response, error) {
|
||||||
return nil, nil, nil
|
m.Called(owner, repo, sha, opts)
|
||||||
|
r := &gitea.Response{
|
||||||
|
Response: &http.Response{
|
||||||
|
StatusCode: http.StatusOK,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if m.simulatedError != nil {
|
||||||
|
r.StatusCode = http.StatusInternalServerError
|
||||||
|
}
|
||||||
|
return nil, r, m.simulatedError
|
||||||
}
|
}
|
||||||
func (m *SdkMock) GetPullRequest(owner, repo string, index int64) (*gitea.PullRequest, *gitea.Response, error) {
|
func (m *SdkMock) GetPullRequest(owner, repo string, index int64) (*gitea.PullRequest, *gitea.Response, error) {
|
||||||
return nil, nil, nil
|
m.Called(owner, repo, index)
|
||||||
|
return &gitea.PullRequest{
|
||||||
|
Head: &gitea.PRBranchInfo{
|
||||||
|
Sha: "a1aada0b7b19e58ae539b4812d960bca35ev78cb",
|
||||||
|
},
|
||||||
|
}, nil, m.simulatedError
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNew(t *testing.T) {
|
func TestNew(t *testing.T) {
|
||||||
config := &settings.GiteaConfig{
|
t.Run("Success", func(t *testing.T) {
|
||||||
Url: "http://example.com",
|
config := &settings.GiteaConfig{
|
||||||
Token: &settings.Token{
|
Url: "http://example.com",
|
||||||
Value: "test-token",
|
Token: &settings.Token{
|
||||||
},
|
Value: "test-token",
|
||||||
}
|
},
|
||||||
|
}
|
||||||
|
|
||||||
callback := func(url string, options ...gitea.ClientOption) (*SdkMock, error) {
|
callback := func(url string, options ...gitea.ClientOption) (*SdkMock, error) {
|
||||||
return &SdkMock{}, nil
|
return &SdkMock{}, nil
|
||||||
}
|
}
|
||||||
assert.IsType(t, &GiteaSdk{}, New(config, callback), "")
|
assert.IsType(t, &GiteaSdk{}, New(config, callback), "")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Initialization errors", func(t *testing.T) {
|
||||||
|
config := &settings.GiteaConfig{
|
||||||
|
Url: "http://example.com",
|
||||||
|
Token: &settings.Token{
|
||||||
|
Value: "test-token",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
callback := func(url string, options ...gitea.ClientOption) (*SdkMock, error) {
|
||||||
|
return nil, errors.New("Simulated initialization error")
|
||||||
|
}
|
||||||
|
assert.PanicsWithError(t, "cannot initialize Gitea client: Simulated initialization error", func() { New(config, callback) })
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDetermineHEAD(t *testing.T) {
|
||||||
|
t.Run("Success", func(t *testing.T) {
|
||||||
|
clientMock := &SdkMock{}
|
||||||
|
clientMock.On("GetPullRequest", "test-owner", "test-repo", int64(1)).Once()
|
||||||
|
|
||||||
|
sdk := GiteaSdk{
|
||||||
|
client: clientMock,
|
||||||
|
}
|
||||||
|
sha, err := sdk.DetermineHEAD(settings.GiteaRepository{
|
||||||
|
Owner: "test-owner",
|
||||||
|
Name: "test-repo",
|
||||||
|
}, 1)
|
||||||
|
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, "a1aada0b7b19e58ae539b4812d960bca35ev78cb", sha)
|
||||||
|
clientMock.AssertExpectations(t)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("API error", func(t *testing.T) {
|
||||||
|
clientMock := &SdkMock{
|
||||||
|
simulatedError: errors.New("Simulated error"),
|
||||||
|
}
|
||||||
|
clientMock.On("GetPullRequest", "test-owner", "test-repo", int64(1)).Once()
|
||||||
|
|
||||||
|
sdk := GiteaSdk{
|
||||||
|
client: clientMock,
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := sdk.DetermineHEAD(settings.GiteaRepository{
|
||||||
|
Owner: "test-owner",
|
||||||
|
Name: "test-repo",
|
||||||
|
}, 1)
|
||||||
|
|
||||||
|
assert.Errorf(t, err, "Simulated error")
|
||||||
|
clientMock.AssertExpectations(t)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUpdateStatus(t *testing.T) {
|
||||||
|
t.Run("Success", func(t *testing.T) {
|
||||||
|
clientMock := &SdkMock{}
|
||||||
|
clientMock.On("CreateStatus", "test-owner", "test-repo", "a1aada0b7b19e58ae539b4812d960bca35ev78cb", mock.Anything).Once()
|
||||||
|
sdk := GiteaSdk{
|
||||||
|
client: clientMock,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := sdk.UpdateStatus(settings.GiteaRepository{
|
||||||
|
Owner: "test-owner",
|
||||||
|
Name: "test-repo",
|
||||||
|
}, "a1aada0b7b19e58ae539b4812d960bca35ev78cb", StatusDetails{
|
||||||
|
Url: "http://example.com",
|
||||||
|
Message: "expected message",
|
||||||
|
State: StatusOK,
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Nil(t, err)
|
||||||
|
clientMock.AssertExpectations(t)
|
||||||
|
|
||||||
|
actualStatusOption := clientMock.Calls[0].Arguments[3].(gitea.CreateStatusOption)
|
||||||
|
assert.Equal(t, "http://example.com", actualStatusOption.TargetURL)
|
||||||
|
assert.Equal(t, "expected message", actualStatusOption.Description)
|
||||||
|
assert.Equal(t, gitea.StatusSuccess, actualStatusOption.State)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("API error", func(t *testing.T) {
|
||||||
|
clientMock := &SdkMock{
|
||||||
|
simulatedError: errors.New("Simulated error"),
|
||||||
|
}
|
||||||
|
clientMock.On("CreateStatus", "test-owner", "test-repo", "a1aada0b7b19e58ae539b4812d960bca35ev78cb", mock.Anything).Once()
|
||||||
|
sdk := GiteaSdk{
|
||||||
|
client: clientMock,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := sdk.UpdateStatus(settings.GiteaRepository{
|
||||||
|
Owner: "test-owner",
|
||||||
|
Name: "test-repo",
|
||||||
|
}, "a1aada0b7b19e58ae539b4812d960bca35ev78cb", StatusDetails{
|
||||||
|
Url: "http://example.com",
|
||||||
|
Message: "expected message",
|
||||||
|
State: StatusOK,
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Errorf(t, err, "Simulated error")
|
||||||
|
clientMock.AssertExpectations(t)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPostComment(t *testing.T) {
|
||||||
|
t.Run("Success", func(t *testing.T) {
|
||||||
|
clientMock := &SdkMock{}
|
||||||
|
clientMock.On("CreateIssueComment", "test-owner", "test-repo", int64(1), mock.Anything).Once()
|
||||||
|
sdk := GiteaSdk{
|
||||||
|
client: clientMock,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := sdk.PostComment(settings.GiteaRepository{
|
||||||
|
Owner: "test-owner",
|
||||||
|
Name: "test-repo",
|
||||||
|
}, 1, "test post comment")
|
||||||
|
|
||||||
|
assert.Nil(t, err)
|
||||||
|
clientMock.AssertExpectations(t)
|
||||||
|
|
||||||
|
actualCommentOption := clientMock.Calls[0].Arguments[3].(gitea.CreateIssueCommentOption)
|
||||||
|
assert.Equal(t, "test post comment", actualCommentOption.Body)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("API error", func(t *testing.T) {
|
||||||
|
clientMock := &SdkMock{
|
||||||
|
simulatedError: errors.New("Simulated error"),
|
||||||
|
}
|
||||||
|
clientMock.On("CreateIssueComment", "test-owner", "test-repo", int64(1), mock.Anything).Once()
|
||||||
|
sdk := GiteaSdk{
|
||||||
|
client: clientMock,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := sdk.PostComment(settings.GiteaRepository{
|
||||||
|
Owner: "test-owner",
|
||||||
|
Name: "test-repo",
|
||||||
|
}, 1, "test post comment")
|
||||||
|
|
||||||
|
assert.Errorf(t, err, "Simulated error")
|
||||||
|
clientMock.AssertExpectations(t)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue