Skip to content

Commit

Permalink
test: use wrapper interface and generated mocks (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoxys authored May 11, 2024
1 parent a196e5f commit 14e918c
Show file tree
Hide file tree
Showing 9 changed files with 523 additions and 154 deletions.
6 changes: 6 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
all: True
dir: "{{.PackageName}}/mocks"
outpkg: "mocks"
packages:
github.com/thegeeklab/wp-gitea-release/gitea:
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ IMPORT := github.com/thegeeklab/$(EXECUTABLE)

GO ?= go
CWD ?= $(shell pwd)
PACKAGES ?= $(shell go list ./...)
PACKAGES ?= $(shell go list ./... | grep -Ev '/mocks$$')
SOURCES ?= $(shell find . -name "*.go" -type f)

GOFUMPT_PACKAGE ?= mvdan.cc/gofumpt@$(GOFUMPT_PACKAGE_VERSION)
GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_PACKAGE_VERSION)
XGO_PACKAGE ?= src.techknowlogick.com/xgo@latest
GOTESTSUM_PACKAGE ?= gotest.tools/gotestsum@latest
MOCKERY_PACKAGE ?= github.com/vektra/mockery/v2@latest

XGO_VERSION := go-1.22.x
XGO_TARGETS ?= linux/amd64,linux/arm-6,linux/arm-7,linux/arm64
Expand Down Expand Up @@ -65,6 +66,7 @@ lint: golangci-lint
.PHONY: generate
generate:
$(GO) generate $(PACKAGES)
$(GO) run $(MOCKERY_PACKAGE)

.PHONY: test
test:
Expand Down
16 changes: 16 additions & 0 deletions gitea/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package gitea

import (
"io"

"code.gitea.io/sdk/gitea"
)

//nolint:lll
type APIClient interface {
ListReleases(owner, repo string, opt gitea.ListReleasesOptions) ([]*gitea.Release, *gitea.Response, error)
CreateRelease(owner, repo string, opt gitea.CreateReleaseOption) (*gitea.Release, *gitea.Response, error)
ListReleaseAttachments(user, repo string, release int64, opt gitea.ListReleaseAttachmentsOptions) ([]*gitea.Attachment, *gitea.Response, error)
CreateReleaseAttachment(user, repo string, release int64, file io.Reader, filename string) (*gitea.Attachment, *gitea.Response, error)
DeleteReleaseAttachment(user, repo string, release, id int64) (*gitea.Response, error)
}
56 changes: 31 additions & 25 deletions plugin/release.go → gitea/gitea.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package plugin
package gitea

import (
"errors"
"fmt"
"net/http"
"os"
"path"

Expand All @@ -15,17 +16,23 @@ var (
ErrFileExists = errors.New("asset file already exist")
)

type GiteaClient struct {
client *gitea.Client
Release *GiteaRelease
const (
FileExistsOverwrite FileExists = "overwrite"
FileExistsFail FileExists = "fail"
FileExistsSkip FileExists = "skip"
)

type Client struct {
client APIClient
Release *Release
}

type GiteaRelease struct {
client *gitea.Client
Opt GiteaReleaseOpt
type Release struct {
client APIClient
Opt ReleaseOpt
}

type GiteaReleaseOpt struct {
type ReleaseOpt struct {
Owner string
Repo string
Tag string
Expand All @@ -38,26 +45,25 @@ type GiteaReleaseOpt struct {

type FileExists string

const (
FileExistsOverwrite FileExists = "overwrite"
FileExistsFail FileExists = "fail"
FileExistsSkip FileExists = "skip"
)
// NewClient creates a new Client instance with the provided Gitea client.
func NewClient(url, key string, client *http.Client) (*Client, error) {
c, err := gitea.NewClient(url, gitea.SetToken(key), gitea.SetHTTPClient(client))
if err != nil {
return nil, err
}

// NewGiteaClient creates a new GiteaClient instance with the provided Gitea client.
func NewGiteaClient(client *gitea.Client) *GiteaClient {
return &GiteaClient{
client: client,
Release: &GiteaRelease{
client: client,
Opt: GiteaReleaseOpt{},
return &Client{
client: c,
Release: &Release{
client: c,
Opt: ReleaseOpt{},
},
}
}, nil
}

// Find retrieves the release with the specified tag name from the repository.
// If the release is not found, it returns an ErrReleaseNotFound error.
func (r *GiteaRelease) Find() (*gitea.Release, error) {
func (r *Release) Find() (*gitea.Release, error) {
releases, _, err := r.client.ListReleases(r.Opt.Owner, r.Opt.Repo, gitea.ListReleasesOptions{})
if err != nil {
return nil, err
Expand All @@ -76,7 +82,7 @@ func (r *GiteaRelease) Find() (*gitea.Release, error) {

// Create creates a new release on the Gitea repository with the specified options.
// It returns the created release or an error if the creation failed.
func (r *GiteaRelease) Create() (*gitea.Release, error) {
func (r *Release) Create() (*gitea.Release, error) {
opts := gitea.CreateReleaseOption{
TagName: r.Opt.Tag,
IsDraft: r.Opt.Draft,
Expand Down Expand Up @@ -104,7 +110,7 @@ func (r *GiteaRelease) Create() (*gitea.Release, error) {
// - "skip": skips uploading the file and logs a warning
//
// If there are no conflicts, it uploads the new files as attachments to the release.
func (r *GiteaRelease) AddAttachments(releaseID int64, files []string) error {
func (r *Release) AddAttachments(releaseID int64, files []string) error {
attachments, _, err := r.client.ListReleaseAttachments(
r.Opt.Owner,
r.Opt.Repo,
Expand Down Expand Up @@ -151,7 +157,7 @@ func (r *GiteaRelease) AddAttachments(releaseID int64, files []string) error {
return nil
}

func (r *GiteaRelease) uploadFile(releaseID int64, file string) error {
func (r *Release) uploadFile(releaseID int64, file string) error {
handle, err := os.Open(file)
if err != nil {
return fmt.Errorf("failed to read artifact: %s: %w", file, err)
Expand Down
Loading

0 comments on commit 14e918c

Please sign in to comment.