2016-12-29 11:21:50 -02:00
|
|
|
package release
|
|
|
|
|
|
|
|
import (
|
2017-03-26 15:56:35 -03:00
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-02-24 17:31:07 -03:00
|
|
|
"sync"
|
2016-12-30 12:03:43 -02:00
|
|
|
"testing"
|
2017-01-02 10:47:19 -02:00
|
|
|
|
2017-12-17 21:26:03 -02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
2017-08-20 16:50:34 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/testlib"
|
2018-08-14 23:50:20 -03:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/config"
|
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
2018-12-28 19:17:40 -02:00
|
|
|
"github.com/pkg/errors"
|
2017-01-02 10:47:19 -02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-12-29 11:21:50 -02:00
|
|
|
)
|
|
|
|
|
2017-03-25 20:46:02 -03:00
|
|
|
func TestPipeDescription(t *testing.T) {
|
2017-12-02 19:53:19 -02:00
|
|
|
assert.NotEmpty(t, Pipe{}.String())
|
2017-03-25 20:43:42 -03:00
|
|
|
}
|
2017-03-26 15:56:35 -03:00
|
|
|
|
|
|
|
func TestRunPipe(t *testing.T) {
|
2017-04-15 14:18:17 -03:00
|
|
|
folder, err := ioutil.TempDir("", "goreleasertest")
|
2017-09-26 19:24:49 -03:00
|
|
|
assert.NoError(t, err)
|
2017-04-14 12:18:59 -03:00
|
|
|
tarfile, err := os.Create(filepath.Join(folder, "bin.tar.gz"))
|
2017-09-26 19:24:49 -03:00
|
|
|
assert.NoError(t, err)
|
2017-04-14 12:18:59 -03:00
|
|
|
debfile, err := os.Create(filepath.Join(folder, "bin.deb"))
|
2017-09-26 19:24:49 -03:00
|
|
|
assert.NoError(t, err)
|
2017-07-15 16:49:52 -03:00
|
|
|
var config = config.Project{
|
|
|
|
Dist: folder,
|
|
|
|
Release: config.Release{
|
|
|
|
GitHub: config.Repo{
|
|
|
|
Owner: "test",
|
|
|
|
Name: "test",
|
2017-03-26 15:56:35 -03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-07-15 16:49:52 -03:00
|
|
|
var ctx = context.New(config)
|
|
|
|
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
|
2017-12-17 21:26:03 -02:00
|
|
|
ctx.Artifacts.Add(artifact.Artifact{
|
|
|
|
Type: artifact.UploadableArchive,
|
|
|
|
Name: "bin.tar.gz",
|
|
|
|
Path: tarfile.Name(),
|
|
|
|
})
|
|
|
|
ctx.Artifacts.Add(artifact.Artifact{
|
|
|
|
Type: artifact.LinuxPackage,
|
|
|
|
Name: "bin.deb",
|
|
|
|
Path: debfile.Name(),
|
|
|
|
})
|
2017-03-26 15:56:35 -03:00
|
|
|
client := &DummyClient{}
|
2018-10-16 20:39:10 -03:00
|
|
|
assert.NoError(t, doPublish(ctx, client))
|
2017-09-26 19:24:49 -03:00
|
|
|
assert.True(t, client.CreatedRelease)
|
|
|
|
assert.True(t, client.UploadedFile)
|
|
|
|
assert.Contains(t, client.UploadedFileNames, "bin.deb")
|
|
|
|
assert.Contains(t, client.UploadedFileNames, "bin.tar.gz")
|
2017-03-26 15:56:35 -03:00
|
|
|
}
|
|
|
|
|
2017-04-15 12:18:09 -03:00
|
|
|
func TestRunPipeReleaseCreationFailed(t *testing.T) {
|
2017-07-15 16:49:52 -03:00
|
|
|
var config = config.Project{
|
|
|
|
Release: config.Release{
|
|
|
|
GitHub: config.Repo{
|
|
|
|
Owner: "test",
|
|
|
|
Name: "test",
|
2017-04-15 12:18:09 -03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-07-15 16:49:52 -03:00
|
|
|
var ctx = context.New(config)
|
|
|
|
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
|
2017-04-15 12:18:09 -03:00
|
|
|
client := &DummyClient{
|
|
|
|
FailToCreateRelease: true,
|
|
|
|
}
|
2018-10-16 20:39:10 -03:00
|
|
|
assert.Error(t, doPublish(ctx, client))
|
2017-09-26 19:24:49 -03:00
|
|
|
assert.False(t, client.CreatedRelease)
|
|
|
|
assert.False(t, client.UploadedFile)
|
2017-04-15 12:18:09 -03:00
|
|
|
}
|
|
|
|
|
2017-04-14 15:53:36 -03:00
|
|
|
func TestRunPipeWithFileThatDontExist(t *testing.T) {
|
2017-07-15 16:49:52 -03:00
|
|
|
var config = config.Project{
|
|
|
|
Release: config.Release{
|
|
|
|
GitHub: config.Repo{
|
|
|
|
Owner: "test",
|
|
|
|
Name: "test",
|
2017-04-14 15:53:36 -03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-07-15 16:49:52 -03:00
|
|
|
var ctx = context.New(config)
|
|
|
|
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
|
2017-12-17 21:26:03 -02:00
|
|
|
ctx.Artifacts.Add(artifact.Artifact{
|
|
|
|
Type: artifact.UploadableArchive,
|
|
|
|
Name: "bin.tar.gz",
|
|
|
|
Path: "/nope/nope/nope",
|
|
|
|
})
|
2017-04-14 15:53:36 -03:00
|
|
|
client := &DummyClient{}
|
2018-10-16 20:39:10 -03:00
|
|
|
assert.Error(t, doPublish(ctx, client))
|
2017-09-26 19:24:49 -03:00
|
|
|
assert.True(t, client.CreatedRelease)
|
|
|
|
assert.False(t, client.UploadedFile)
|
2017-04-14 15:53:36 -03:00
|
|
|
}
|
|
|
|
|
2017-04-15 12:18:09 -03:00
|
|
|
func TestRunPipeUploadFailure(t *testing.T) {
|
2017-04-15 14:18:17 -03:00
|
|
|
folder, err := ioutil.TempDir("", "goreleasertest")
|
2017-09-26 19:24:49 -03:00
|
|
|
assert.NoError(t, err)
|
2017-04-15 12:18:09 -03:00
|
|
|
tarfile, err := os.Create(filepath.Join(folder, "bin.tar.gz"))
|
2017-09-26 19:24:49 -03:00
|
|
|
assert.NoError(t, err)
|
2017-07-15 16:49:52 -03:00
|
|
|
var config = config.Project{
|
|
|
|
Release: config.Release{
|
|
|
|
GitHub: config.Repo{
|
|
|
|
Owner: "test",
|
|
|
|
Name: "test",
|
2017-04-15 12:18:09 -03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-07-15 16:49:52 -03:00
|
|
|
var ctx = context.New(config)
|
|
|
|
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
|
2017-12-17 21:26:03 -02:00
|
|
|
ctx.Artifacts.Add(artifact.Artifact{
|
|
|
|
Type: artifact.UploadableArchive,
|
|
|
|
Name: "bin.tar.gz",
|
|
|
|
Path: tarfile.Name(),
|
|
|
|
})
|
2017-04-15 12:18:09 -03:00
|
|
|
client := &DummyClient{
|
|
|
|
FailToUpload: true,
|
|
|
|
}
|
2018-12-28 19:17:40 -02:00
|
|
|
assert.EqualError(t, doPublish(ctx, client), "failed to upload bin.tar.gz after 10 retries: upload failed")
|
2017-09-26 19:24:49 -03:00
|
|
|
assert.True(t, client.CreatedRelease)
|
|
|
|
assert.False(t, client.UploadedFile)
|
2017-04-15 12:18:09 -03:00
|
|
|
}
|
|
|
|
|
2018-12-28 19:17:40 -02:00
|
|
|
func TestRunPipeUploadRetry(t *testing.T) {
|
|
|
|
folder, err := ioutil.TempDir("", "goreleasertest")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
tarfile, err := os.Create(filepath.Join(folder, "bin.tar.gz"))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
var config = config.Project{
|
|
|
|
Release: config.Release{
|
|
|
|
GitHub: config.Repo{
|
|
|
|
Owner: "test",
|
|
|
|
Name: "test",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var ctx = context.New(config)
|
|
|
|
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
|
|
|
|
ctx.Artifacts.Add(artifact.Artifact{
|
|
|
|
Type: artifact.UploadableArchive,
|
|
|
|
Name: "bin.tar.gz",
|
|
|
|
Path: tarfile.Name(),
|
|
|
|
})
|
|
|
|
client := &DummyClient{
|
|
|
|
FailFirstUpload: true,
|
|
|
|
}
|
|
|
|
assert.NoError(t, doPublish(ctx, client))
|
|
|
|
assert.True(t, client.CreatedRelease)
|
|
|
|
assert.True(t, client.UploadedFile)
|
|
|
|
}
|
|
|
|
|
2018-04-24 20:36:05 -07:00
|
|
|
func TestPipeDisabled(t *testing.T) {
|
|
|
|
var ctx = context.New(config.Project{
|
|
|
|
Release: config.Release{
|
|
|
|
Disable: true,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
client := &DummyClient{}
|
2018-10-16 20:39:10 -03:00
|
|
|
testlib.AssertSkipped(t, doPublish(ctx, client))
|
2017-09-26 19:24:49 -03:00
|
|
|
assert.False(t, client.CreatedRelease)
|
|
|
|
assert.False(t, client.UploadedFile)
|
2017-04-18 13:10:13 -03:00
|
|
|
}
|
|
|
|
|
2017-12-02 23:21:13 -02:00
|
|
|
func TestDefault(t *testing.T) {
|
|
|
|
_, back := testlib.Mktmp(t)
|
|
|
|
defer back()
|
|
|
|
testlib.GitInit(t)
|
|
|
|
testlib.GitRemoteAdd(t, "git@github.com:goreleaser/goreleaser.git")
|
|
|
|
|
2018-04-24 20:57:19 -07:00
|
|
|
var ctx = context.New(config.Project{})
|
2017-12-02 23:21:13 -02:00
|
|
|
assert.NoError(t, Pipe{}.Default(ctx))
|
|
|
|
assert.Equal(t, "goreleaser", ctx.Config.Release.GitHub.Name)
|
|
|
|
assert.Equal(t, "goreleaser", ctx.Config.Release.GitHub.Owner)
|
|
|
|
}
|
|
|
|
|
2018-11-29 19:42:14 +01:00
|
|
|
func TestDefaultPreReleaseAuto(t *testing.T) {
|
|
|
|
_, back := testlib.Mktmp(t)
|
|
|
|
defer back()
|
|
|
|
testlib.GitInit(t)
|
|
|
|
testlib.GitRemoteAdd(t, "git@github.com:goreleaser/goreleaser.git")
|
|
|
|
|
|
|
|
t.Run("auto-release", func(t *testing.T) {
|
|
|
|
var ctx = context.New(config.Project{
|
|
|
|
Release: config.Release{
|
|
|
|
Prerelease: "auto",
|
|
|
|
},
|
|
|
|
})
|
2019-01-19 16:57:58 -02:00
|
|
|
ctx.Semver = context.Semver{
|
|
|
|
Major: 1,
|
|
|
|
Minor: 0,
|
|
|
|
Patch: 0,
|
|
|
|
}
|
2018-11-29 19:42:14 +01:00
|
|
|
assert.NoError(t, Pipe{}.Default(ctx))
|
|
|
|
assert.Equal(t, false, ctx.PreRelease)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("auto-rc", func(t *testing.T) {
|
|
|
|
var ctx = context.New(config.Project{
|
|
|
|
Release: config.Release{
|
|
|
|
Prerelease: "auto",
|
|
|
|
},
|
|
|
|
})
|
2019-01-19 16:57:58 -02:00
|
|
|
ctx.Semver = context.Semver{
|
|
|
|
Major: 1,
|
|
|
|
Minor: 0,
|
|
|
|
Patch: 0,
|
|
|
|
Prerelease: "rc1",
|
|
|
|
}
|
2018-11-29 19:42:14 +01:00
|
|
|
assert.NoError(t, Pipe{}.Default(ctx))
|
|
|
|
assert.Equal(t, true, ctx.PreRelease)
|
|
|
|
})
|
2019-01-10 17:08:08 -02:00
|
|
|
|
2019-01-19 16:57:58 -02:00
|
|
|
t.Run("auto-rc-github-setup", func(t *testing.T) {
|
2019-01-10 17:08:08 -02:00
|
|
|
var ctx = context.New(config.Project{
|
|
|
|
Release: config.Release{
|
|
|
|
GitHub: config.Repo{
|
|
|
|
Name: "foo",
|
|
|
|
Owner: "foo",
|
|
|
|
},
|
|
|
|
Prerelease: "auto",
|
|
|
|
},
|
|
|
|
})
|
2019-01-19 16:57:58 -02:00
|
|
|
ctx.Semver = context.Semver{
|
|
|
|
Major: 1,
|
|
|
|
Minor: 0,
|
|
|
|
Patch: 0,
|
|
|
|
Prerelease: "rc1",
|
|
|
|
}
|
2019-01-10 17:08:08 -02:00
|
|
|
assert.NoError(t, Pipe{}.Default(ctx))
|
|
|
|
assert.Equal(t, true, ctx.PreRelease)
|
|
|
|
})
|
2018-11-29 19:42:14 +01:00
|
|
|
}
|
|
|
|
|
2018-04-24 20:57:19 -07:00
|
|
|
func TestDefaultPipeDisabled(t *testing.T) {
|
|
|
|
_, back := testlib.Mktmp(t)
|
|
|
|
defer back()
|
|
|
|
testlib.GitInit(t)
|
|
|
|
testlib.GitRemoteAdd(t, "git@github.com:goreleaser/goreleaser.git")
|
|
|
|
|
|
|
|
var ctx = context.New(config.Project{
|
|
|
|
Release: config.Release{
|
|
|
|
Disable: true,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NoError(t, Pipe{}.Default(ctx))
|
2018-11-03 15:18:04 -03:00
|
|
|
assert.Equal(t, "goreleaser", ctx.Config.Release.GitHub.Name)
|
|
|
|
assert.Equal(t, "goreleaser", ctx.Config.Release.GitHub.Owner)
|
2018-04-24 20:57:19 -07:00
|
|
|
}
|
|
|
|
|
2017-12-02 23:21:13 -02:00
|
|
|
func TestDefaultFilled(t *testing.T) {
|
|
|
|
_, back := testlib.Mktmp(t)
|
|
|
|
defer back()
|
|
|
|
testlib.GitInit(t)
|
|
|
|
testlib.GitRemoteAdd(t, "git@github.com:goreleaser/goreleaser.git")
|
|
|
|
|
|
|
|
var ctx = &context.Context{
|
|
|
|
Config: config.Project{
|
|
|
|
Release: config.Release{
|
|
|
|
GitHub: config.Repo{
|
|
|
|
Name: "foo",
|
|
|
|
Owner: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.NoError(t, Pipe{}.Default(ctx))
|
|
|
|
assert.Equal(t, "foo", ctx.Config.Release.GitHub.Name)
|
|
|
|
assert.Equal(t, "bar", ctx.Config.Release.GitHub.Owner)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultNotAGitRepo(t *testing.T) {
|
|
|
|
_, back := testlib.Mktmp(t)
|
|
|
|
defer back()
|
2018-02-25 20:17:45 -03:00
|
|
|
var ctx = &context.Context{
|
|
|
|
Config: config.Project{},
|
|
|
|
}
|
|
|
|
assert.EqualError(t, Pipe{}.Default(ctx), "current folder is not a git repository")
|
|
|
|
assert.Empty(t, ctx.Config.Release.GitHub.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultGitRepoWithoutOrigin(t *testing.T) {
|
|
|
|
_, back := testlib.Mktmp(t)
|
|
|
|
defer back()
|
|
|
|
var ctx = &context.Context{
|
|
|
|
Config: config.Project{},
|
|
|
|
}
|
2017-12-02 23:21:13 -02:00
|
|
|
testlib.GitInit(t)
|
2018-02-25 20:17:45 -03:00
|
|
|
assert.EqualError(t, Pipe{}.Default(ctx), "repository doesn't have an `origin` remote")
|
|
|
|
assert.Empty(t, ctx.Config.Release.GitHub.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultNotAGitRepoSnapshot(t *testing.T) {
|
|
|
|
_, back := testlib.Mktmp(t)
|
|
|
|
defer back()
|
2017-12-02 23:21:13 -02:00
|
|
|
var ctx = &context.Context{
|
|
|
|
Config: config.Project{},
|
|
|
|
}
|
2018-02-25 20:17:45 -03:00
|
|
|
ctx.Snapshot = true
|
|
|
|
assert.NoError(t, Pipe{}.Default(ctx))
|
2017-12-02 23:21:13 -02:00
|
|
|
assert.Empty(t, ctx.Config.Release.GitHub.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultGitRepoWithoutRemote(t *testing.T) {
|
|
|
|
_, back := testlib.Mktmp(t)
|
|
|
|
defer back()
|
|
|
|
var ctx = &context.Context{
|
|
|
|
Config: config.Project{},
|
|
|
|
}
|
|
|
|
assert.Error(t, Pipe{}.Default(ctx))
|
|
|
|
assert.Empty(t, ctx.Config.Release.GitHub.String())
|
|
|
|
}
|
|
|
|
|
2017-03-26 15:56:35 -03:00
|
|
|
type DummyClient struct {
|
2017-04-15 12:18:09 -03:00
|
|
|
FailToCreateRelease bool
|
|
|
|
FailToUpload bool
|
|
|
|
CreatedRelease bool
|
|
|
|
UploadedFile bool
|
2017-07-04 21:28:21 -03:00
|
|
|
UploadedFileNames []string
|
2018-12-28 19:17:40 -02:00
|
|
|
FailFirstUpload bool
|
2018-02-24 17:31:07 -03:00
|
|
|
Lock sync.Mutex
|
2017-03-26 15:56:35 -03:00
|
|
|
}
|
|
|
|
|
2018-01-26 15:46:42 -02:00
|
|
|
func (client *DummyClient) CreateRelease(ctx *context.Context, body string) (releaseID int64, err error) {
|
2017-04-15 12:18:09 -03:00
|
|
|
if client.FailToCreateRelease {
|
|
|
|
return 0, errors.New("release failed")
|
|
|
|
}
|
2017-03-26 15:56:35 -03:00
|
|
|
client.CreatedRelease = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-10 14:13:00 -03:00
|
|
|
func (client *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo config.Repo, content bytes.Buffer, path, msg string) (err error) {
|
2017-03-26 15:56:35 -03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-28 19:17:40 -02:00
|
|
|
func (client *DummyClient) Upload(ctx *context.Context, releaseID int64, name string, file *os.File) error {
|
2018-02-24 17:31:07 -03:00
|
|
|
client.Lock.Lock()
|
|
|
|
defer client.Lock.Unlock()
|
2018-12-28 19:17:40 -02:00
|
|
|
// ensure file is read to better mimic real behavior
|
|
|
|
_, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "unexpected error")
|
|
|
|
}
|
2017-04-15 12:18:09 -03:00
|
|
|
if client.FailToUpload {
|
|
|
|
return errors.New("upload failed")
|
|
|
|
}
|
2018-12-28 19:17:40 -02:00
|
|
|
if client.FailFirstUpload {
|
|
|
|
client.FailFirstUpload = false
|
|
|
|
return errors.New("upload failed, should retry")
|
|
|
|
}
|
2017-03-26 15:56:35 -03:00
|
|
|
client.UploadedFile = true
|
2017-07-04 21:28:21 -03:00
|
|
|
client.UploadedFileNames = append(client.UploadedFileNames, name)
|
2018-12-28 19:17:40 -02:00
|
|
|
return nil
|
2017-03-26 15:56:35 -03:00
|
|
|
}
|