1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-11 14:39:28 +02:00

refactor: unifying client mocks (#2549)

* refactor: unifying client mocks

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>

* refactor: unifying client mocks

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
This commit is contained in:
Carlos Alexandro Becker 2021-10-03 13:24:20 -03:00 committed by GitHub
parent 57f79e91f8
commit c739724f12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 133 additions and 208 deletions

View File

@ -49,16 +49,18 @@ func New(ctx *context.Context) (Client, error) {
func newWithToken(ctx *context.Context, token string) (Client, error) {
log.WithField("type", ctx.TokenType).Debug("token type")
if ctx.TokenType == context.TokenTypeGitHub {
switch ctx.TokenType {
case context.TokenTypeGitHub:
return NewGitHub(ctx, token)
}
if ctx.TokenType == context.TokenTypeGitLab {
case context.TokenTypeGitLab:
return NewGitLab(ctx, token)
}
if ctx.TokenType == context.TokenTypeGitea {
case context.TokenTypeGitea:
return NewGitea(ctx, token)
case context.TokenTypeMock:
return NewMock(), nil
default:
return nil, fmt.Errorf("invalid client token type: %q", ctx.TokenType)
}
return nil, fmt.Errorf("invalid client token type: %q", ctx.TokenType)
}
func NewIfToken(ctx *context.Context, cli Client, token string) (Client, error) {

82
internal/client/mock.go Normal file
View File

@ -0,0 +1,82 @@
package client
import (
"errors"
"fmt"
"io"
"os"
"sync"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
)
func NewMock() *Mock {
return &Mock{}
}
type Mock struct {
CreatedFile bool
Content string
Path string
FailToCreateRelease bool
FailToUpload bool
CreatedRelease bool
UploadedFile bool
UploadedFileNames []string
UploadedFilePaths map[string]string
FailFirstUpload bool
Lock sync.Mutex
}
func (c *Mock) CloseMilestone(ctx *context.Context, repo Repo, title string) error {
return nil
}
func (c *Mock) GetDefaultBranch(ctx *context.Context, repo Repo) (string, error) {
return "", errors.New("Mock does not yet implement GetDefaultBranch")
}
func (c *Mock) CreateRelease(ctx *context.Context, body string) (string, error) {
if c.FailToCreateRelease {
return "", errors.New("release failed")
}
c.CreatedRelease = true
return "", nil
}
func (c *Mock) ReleaseURLTemplate(ctx *context.Context) (string, error) {
return "https://dummyhost/download/{{ .Tag }}/{{ .ArtifactName }}", nil
}
func (c *Mock) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo Repo, content []byte, path, msg string) error {
c.CreatedFile = true
c.Content = string(content)
c.Path = path
return nil
}
func (c *Mock) Upload(ctx *context.Context, releaseID string, artifact *artifact.Artifact, file *os.File) error {
c.Lock.Lock()
defer c.Lock.Unlock()
if c.UploadedFilePaths == nil {
c.UploadedFilePaths = map[string]string{}
}
// ensure file is read to better mimic real behavior
_, err := io.ReadAll(file)
if err != nil {
return fmt.Errorf("unexpected error: %w", err)
}
if c.FailToUpload {
return errors.New("upload failed")
}
if c.FailFirstUpload {
c.FailFirstUpload = false
return RetriableError{Err: errors.New("upload failed, should retry")}
}
c.UploadedFile = true
c.UploadedFileNames = append(c.UploadedFileNames, artifact.Name)
c.UploadedFilePaths[artifact.Name] = artifact.Path
return nil
}

View File

@ -1,7 +1,6 @@
package brew
import (
"errors"
"fmt"
"io/ioutil"
"os"
@ -298,7 +297,7 @@ func TestFullPipe(t *testing.T) {
f, err := os.Create(path)
require.NoError(t, err)
require.NoError(t, f.Close())
client := &DummyClient{}
client := client.NewMock()
distFile := filepath.Join(folder, name+".rb")
if tt.expectedRunError == "" {
@ -367,7 +366,7 @@ func TestRunPipeNameTemplate(t *testing.T) {
f, err := os.Create(path)
require.NoError(t, err)
require.NoError(t, f.Close())
client := &DummyClient{}
client := client.NewMock()
distFile := filepath.Join(folder, "foo_is_bar.rb")
require.NoError(t, runAll(ctx, client))
@ -446,7 +445,7 @@ func TestRunPipeMultipleBrewsWithSkip(t *testing.T) {
require.NoError(t, err)
require.NoError(t, f.Close())
cli := &DummyClient{}
cli := client.NewMock()
require.NoError(t, runAll(ctx, cli))
require.EqualError(t, publishAll(ctx, cli), `brew.skip_upload is set`)
require.True(t, cli.CreatedFile)
@ -567,7 +566,7 @@ func TestRunPipeForMultipleArmVersions(t *testing.T) {
require.NoError(t, f.Close())
}
client := &DummyClient{}
client := client.NewMock()
distFile := filepath.Join(folder, name+".rb")
require.NoError(t, runAll(ctx, client))
@ -596,7 +595,7 @@ func TestRunPipeNoBuilds(t *testing.T) {
},
},
}
client := &DummyClient{}
client := client.NewMock()
require.Equal(t, ErrNoArchivesFound, runAll(ctx, client))
require.False(t, client.CreatedFile)
}
@ -740,7 +739,7 @@ func TestRunPipeMultipleArchivesSameOsBuild(t *testing.T) {
},
})
}
client := &DummyClient{}
client := client.NewMock()
require.Equal(t, test.expectedError, runAll(ctx, client))
require.False(t, client.CreatedFile)
// clean the artifacts for the next run
@ -768,7 +767,7 @@ func TestRunPipeBinaryRelease(t *testing.T) {
Goarch: "amd64",
Type: artifact.Binary,
})
client := &DummyClient{}
client := client.NewMock()
require.Equal(t, ErrNoArchivesFound, runAll(ctx, client))
require.False(t, client.CreatedFile)
}
@ -805,7 +804,7 @@ func TestRunPipeNoUpload(t *testing.T) {
"Format": "tar.gz",
},
})
client := &DummyClient{}
client := client.NewMock()
assertNoPublish := func(t *testing.T) {
t.Helper()
@ -857,7 +856,7 @@ func TestRunEmptyTokenType(t *testing.T) {
"Format": "tar.gz",
},
})
client := &DummyClient{}
client := client.NewMock()
require.NoError(t, runAll(ctx, client))
}
@ -906,37 +905,6 @@ func TestGHFolder(t *testing.T) {
require.Equal(t, "fooo/bar.rb", buildFormulaPath("fooo", "bar.rb"))
}
type DummyClient struct {
CreatedFile bool
Content string
}
func (dc *DummyClient) CloseMilestone(ctx *context.Context, repo client.Repo, title string) error {
return nil
}
func (dc *DummyClient) GetDefaultBranch(ctx *context.Context, repo client.Repo) (string, error) {
return "", errors.New("DummyClient does not yet implement GetDefaultBranch")
}
func (dc *DummyClient) CreateRelease(ctx *context.Context, body string) (releaseID string, err error) {
return
}
func (dc *DummyClient) ReleaseURLTemplate(ctx *context.Context) (string, error) {
return "https://dummyhost/download/{{ .Tag }}/{{ .ArtifactName }}", nil
}
func (dc *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo client.Repo, content []byte, path, msg string) (err error) {
dc.CreatedFile = true
dc.Content = string(content)
return
}
func (dc *DummyClient) Upload(ctx *context.Context, releaseID string, artifact *artifact.Artifact, file *os.File) (err error) {
return
}
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
@ -957,6 +925,6 @@ func TestRunSkipNoName(t *testing.T) {
Brews: []config.Homebrew{{}},
})
client := &DummyClient{}
client := client.NewMock()
testlib.AssertSkipped(t, runAll(ctx, client))
}

View File

@ -1,7 +1,6 @@
package gofish
import (
"errors"
"fmt"
"io/ioutil"
"os"
@ -211,7 +210,7 @@ func TestFullPipe(t *testing.T) {
f, err := os.Create(path)
require.NoError(t, err)
require.NoError(t, f.Close())
client := &DummyClient{}
client := client.NewMock()
distFile := filepath.Join(folder, name+".lua")
require.NoError(t, runAll(ctx, client))
@ -275,7 +274,7 @@ func TestRunPipeNameTemplate(t *testing.T) {
f, err := os.Create(path)
require.NoError(t, err)
require.NoError(t, f.Close())
client := &DummyClient{}
client := client.NewMock()
distFile := filepath.Join(folder, "foo_is_bar.lua")
require.NoError(t, runAll(ctx, client))
@ -354,7 +353,7 @@ func TestRunPipeMultipleGoFishWithSkip(t *testing.T) {
require.NoError(t, err)
require.NoError(t, f.Close())
cli := &DummyClient{}
cli := client.NewMock()
require.NoError(t, runAll(ctx, cli))
require.EqualError(t, publishAll(ctx, cli), `rig.skip_upload is set`)
require.True(t, cli.CreatedFile)
@ -469,7 +468,7 @@ func TestRunPipeForMultipleArmVersions(t *testing.T) {
require.NoError(t, f.Close())
}
client := &DummyClient{}
client := client.NewMock()
distFile := filepath.Join(folder, name+".lua")
require.NoError(t, runAll(ctx, client))
@ -498,7 +497,7 @@ func TestRunPipeNoBuilds(t *testing.T) {
},
},
}
client := &DummyClient{}
client := client.NewMock()
require.Equal(t, ErrNoArchivesFound, runAll(ctx, client))
require.False(t, client.CreatedFile)
}
@ -642,7 +641,7 @@ func TestRunPipeMultipleArchivesSameOsBuild(t *testing.T) {
},
})
}
client := &DummyClient{}
client := client.NewMock()
require.Equal(t, test.expectedError, runAll(ctx, client))
require.False(t, client.CreatedFile)
// clean the artifacts for the next run
@ -670,7 +669,7 @@ func TestRunPipeBinaryRelease(t *testing.T) {
Goarch: "amd64",
Type: artifact.Binary,
})
client := &DummyClient{}
client := client.NewMock()
require.Equal(t, ErrNoArchivesFound, runAll(ctx, client))
require.False(t, client.CreatedFile)
}
@ -707,7 +706,7 @@ func TestRunPipeNoUpload(t *testing.T) {
"Format": "tar.gz",
},
})
client := &DummyClient{}
client := client.NewMock()
assertNoPublish := func(t *testing.T) {
t.Helper()
@ -758,7 +757,7 @@ func TestRunEmptyTokenType(t *testing.T) {
"Format": "tar.gz",
},
})
client := &DummyClient{}
client := client.NewMock()
require.NoError(t, runAll(ctx, client))
}
@ -806,37 +805,6 @@ func TestGHFolder(t *testing.T) {
require.Equal(t, "fooo/bar.lua", buildFoodPath("fooo", "bar.lua"))
}
type DummyClient struct {
CreatedFile bool
Content string
}
func (dc *DummyClient) CloseMilestone(ctx *context.Context, repo client.Repo, title string) error {
return nil
}
func (dc *DummyClient) CreateRelease(ctx *context.Context, body string) (releaseID string, err error) {
return
}
func (dc *DummyClient) ReleaseURLTemplate(ctx *context.Context) (string, error) {
return "https://dummyhost/download/{{ .Tag }}/{{ .ArtifactName }}", nil
}
func (dc *DummyClient) GetDefaultBranch(ctx *context.Context, repo client.Repo) (string, error) {
return "", errors.New("DummyClient does not yet implement GetDefaultBranch")
}
func (dc *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo client.Repo, content []byte, path, msg string) (err error) {
dc.CreatedFile = true
dc.Content = string(content)
return
}
func (dc *DummyClient) Upload(ctx *context.Context, releaseID string, artifact *artifact.Artifact, file *os.File) (err error) {
return
}
func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
@ -857,6 +825,6 @@ func TestRunSkipNoName(t *testing.T) {
Rigs: []config.GoFish{{}},
})
client := &DummyClient{}
client := client.NewMock()
testlib.AssertSkipped(t, runAll(ctx, client))
}

View File

@ -1,13 +1,9 @@
package release
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"github.com/goreleaser/goreleaser/internal/artifact"
@ -91,7 +87,7 @@ func TestRunPipeWithoutIDsThenDoesNotFilter(t *testing.T) {
"Format": "tar.gz",
},
})
client := &DummyClient{}
client := &client.Mock{}
require.NoError(t, doPublish(ctx, client))
require.True(t, client.CreatedRelease)
require.True(t, client.UploadedFile)
@ -164,7 +160,7 @@ func TestRunPipeWithIDsThenFilters(t *testing.T) {
"ID": "bar",
},
})
client := &DummyClient{}
client := &client.Mock{}
require.NoError(t, doPublish(ctx, client))
require.True(t, client.CreatedRelease)
require.True(t, client.UploadedFile)
@ -186,7 +182,7 @@ func TestRunPipeReleaseCreationFailed(t *testing.T) {
}
ctx := context.New(config)
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
client := &DummyClient{
client := &client.Mock{
FailToCreateRelease: true,
}
require.Error(t, doPublish(ctx, client))
@ -210,7 +206,7 @@ func TestRunPipeWithFileThatDontExist(t *testing.T) {
Name: "bin.tar.gz",
Path: "/nope/nope/nope",
})
client := &DummyClient{}
client := &client.Mock{}
require.Error(t, doPublish(ctx, client))
require.True(t, client.CreatedRelease)
require.False(t, client.UploadedFile)
@ -235,7 +231,7 @@ func TestRunPipeUploadFailure(t *testing.T) {
Name: "bin.tar.gz",
Path: tarfile.Name(),
})
client := &DummyClient{
client := &client.Mock{
FailToUpload: true,
}
require.EqualError(t, doPublish(ctx, client), "failed to upload bin.tar.gz after 1 tries: upload failed")
@ -258,7 +254,7 @@ func TestRunPipeExtraFileNotFound(t *testing.T) {
}
ctx := context.New(config)
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
client := &DummyClient{}
client := &client.Mock{}
require.EqualError(t, doPublish(ctx, client), "globbing failed for pattern ./nope: matching \"./nope\": file does not exist")
require.True(t, client.CreatedRelease)
require.False(t, client.UploadedFile)
@ -279,7 +275,7 @@ func TestRunPipeExtraOverride(t *testing.T) {
}
ctx := context.New(config)
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
client := &DummyClient{}
client := &client.Mock{}
require.NoError(t, doPublish(ctx, client))
require.True(t, client.CreatedRelease)
require.True(t, client.UploadedFile)
@ -306,7 +302,7 @@ func TestRunPipeUploadRetry(t *testing.T) {
Name: "bin.tar.gz",
Path: tarfile.Name(),
})
client := &DummyClient{
client := &client.Mock{
FailFirstUpload: true,
}
require.NoError(t, doPublish(ctx, client))
@ -523,62 +519,3 @@ func TestSkip(t *testing.T) {
require.False(t, Pipe{}.Skip(context.New(config.Project{})))
})
}
type DummyClient struct {
FailToCreateRelease bool
FailToUpload bool
CreatedRelease bool
UploadedFile bool
UploadedFileNames []string
UploadedFilePaths map[string]string
FailFirstUpload bool
Lock sync.Mutex
}
func (c *DummyClient) CloseMilestone(ctx *context.Context, repo client.Repo, title string) error {
return nil
}
func (c *DummyClient) CreateRelease(ctx *context.Context, body string) (releaseID string, err error) {
if c.FailToCreateRelease {
return "", errors.New("release failed")
}
c.CreatedRelease = true
return
}
func (c *DummyClient) ReleaseURLTemplate(ctx *context.Context) (string, error) {
return "", nil
}
func (c *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo client.Repo, content []byte, path, msg string) (err error) {
return
}
func (c *DummyClient) GetDefaultBranch(ctx *context.Context, repo client.Repo) (string, error) {
return "", errors.New("DummyClient does not yet implement GetDefaultBranch")
}
func (c *DummyClient) Upload(ctx *context.Context, releaseID string, artifact *artifact.Artifact, file *os.File) error {
c.Lock.Lock()
defer c.Lock.Unlock()
if c.UploadedFilePaths == nil {
c.UploadedFilePaths = map[string]string{}
}
// ensure file is read to better mimic real behavior
_, err := io.ReadAll(file)
if err != nil {
return fmt.Errorf("unexpected error: %w", err)
}
if c.FailToUpload {
return errors.New("upload failed")
}
if c.FailFirstUpload {
c.FailFirstUpload = false
return client.RetriableError{Err: errors.New("upload failed, should retry")}
}
c.UploadedFile = true
c.UploadedFileNames = append(c.UploadedFileNames, artifact.Name)
c.UploadedFilePaths[artifact.Name] = artifact.Path
return nil
}

View File

@ -2,7 +2,6 @@ package scoop
import (
ctx "context"
"errors"
"os"
"path/filepath"
"testing"
@ -63,7 +62,7 @@ func Test_doRun(t *testing.T) {
type args struct {
ctx *context.Context
client *DummyClient
client *client.Mock
}
type asserter func(*testing.T, args)
@ -127,7 +126,7 @@ func Test_doRun(t *testing.T) {
},
},
},
&DummyClient{},
client.NewMock(),
},
[]*artifact.Artifact{
{Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Path: file},
@ -175,7 +174,7 @@ func Test_doRun(t *testing.T) {
},
},
},
&DummyClient{},
client.NewMock(),
},
[]*artifact.Artifact{
{
@ -237,7 +236,7 @@ func Test_doRun(t *testing.T) {
},
},
},
&DummyClient{},
client.NewMock(),
},
[]*artifact.Artifact{
{Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Path: file},
@ -285,7 +284,7 @@ func Test_doRun(t *testing.T) {
},
},
},
&DummyClient{},
client.NewMock(),
},
[]*artifact.Artifact{
{
@ -341,7 +340,7 @@ func Test_doRun(t *testing.T) {
},
},
},
&DummyClient{},
client.NewMock(),
},
[]*artifact.Artifact{
{
@ -396,7 +395,7 @@ func Test_doRun(t *testing.T) {
},
},
},
&DummyClient{},
client.NewMock(),
},
[]*artifact.Artifact{
{Name: "foo_1.0.1_linux_amd64.tar.gz", Goos: "linux", Goarch: "amd64"},
@ -438,7 +437,7 @@ func Test_doRun(t *testing.T) {
},
},
},
&DummyClient{},
client.NewMock(),
},
[]*artifact.Artifact{
{Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Path: file},
@ -490,7 +489,7 @@ func Test_doRun(t *testing.T) {
},
},
},
&DummyClient{},
client.NewMock(),
},
[]*artifact.Artifact{
{Name: "foo_1.0.1-pre.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Path: file},
@ -536,7 +535,7 @@ func Test_doRun(t *testing.T) {
},
},
},
&DummyClient{},
client.NewMock(),
},
[]*artifact.Artifact{
{Name: "foo_1.0.1-pre.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Path: file},
@ -578,7 +577,7 @@ func Test_doRun(t *testing.T) {
},
},
},
&DummyClient{},
client.NewMock(),
},
[]*artifact.Artifact{
{Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Path: file},
@ -620,7 +619,7 @@ func Test_doRun(t *testing.T) {
},
},
},
&DummyClient{},
client.NewMock(),
},
[]*artifact.Artifact{
{Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Path: file},
@ -940,7 +939,7 @@ func TestRunPipeScoopWithSkipUpload(t *testing.T) {
require.NoError(t, err)
require.NoError(t, f.Close())
cli := &DummyClient{}
cli := client.NewMock()
require.NoError(t, doRun(ctx, cli))
require.EqualError(t, doPublish(ctx, cli), `scoop.skip_upload is true`)
@ -1036,36 +1035,3 @@ func TestSkip(t *testing.T) {
require.False(t, Pipe{}.Skip(ctx))
})
}
type DummyClient struct {
CreatedFile bool
Content string
Path string
}
func (dc *DummyClient) CloseMilestone(ctx *context.Context, repo client.Repo, title string) error {
return nil
}
func (dc *DummyClient) CreateRelease(ctx *context.Context, body string) (releaseID string, err error) {
return
}
func (dc *DummyClient) ReleaseURLTemplate(ctx *context.Context) (string, error) {
return "", nil
}
func (dc *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo client.Repo, content []byte, path, msg string) (err error) {
dc.CreatedFile = true
dc.Content = string(content)
dc.Path = path
return
}
func (dc *DummyClient) GetDefaultBranch(ctx *context.Context, repo client.Repo) (string, error) {
return "", errors.New("DummyClient does not yet implement GetDefaultBranch")
}
func (dc *DummyClient) Upload(ctx *context.Context, releaseID string, artifact *artifact.Artifact, file *os.File) (err error) {
return
}

View File

@ -59,6 +59,8 @@ const (
TokenTypeGitLab TokenType = "gitlab"
// TokenTypeGitea defines gitea as type of the token.
TokenTypeGitea TokenType = "gitea"
// TokenTypeMock is a mock token type used in tests.
TokenTypeMock = "mock"
)
// Context carries along some data through the pipes.