2020-07-09 22:40:37 +02:00
|
|
|
package milestone
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
|
|
|
"github.com/goreleaser/goreleaser/internal/client"
|
|
|
|
"github.com/goreleaser/goreleaser/internal/testlib"
|
|
|
|
"github.com/goreleaser/goreleaser/pkg/config"
|
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
2020-10-06 14:48:04 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-07-09 22:40:37 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDefaultWithRepoConfig(t *testing.T) {
|
2020-12-12 18:27:35 +02:00
|
|
|
testlib.Mktmp(t)
|
2020-07-09 22:40:37 +02:00
|
|
|
testlib.GitInit(t)
|
|
|
|
testlib.GitRemoteAdd(t, "git@github.com:githubowner/githubrepo.git")
|
|
|
|
|
2021-04-25 19:20:49 +02:00
|
|
|
ctx := &context.Context{
|
2020-07-09 22:40:37 +02:00
|
|
|
Config: config.Project{
|
|
|
|
Milestones: []config.Milestone{
|
|
|
|
{
|
|
|
|
Repo: config.Repo{
|
|
|
|
Name: "configrepo",
|
|
|
|
Owner: "configowner",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx.TokenType = context.TokenTypeGitHub
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, Pipe{}.Default(ctx))
|
|
|
|
require.Equal(t, "configrepo", ctx.Config.Milestones[0].Repo.Name)
|
|
|
|
require.Equal(t, "configowner", ctx.Config.Milestones[0].Repo.Owner)
|
2020-07-09 22:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultWithRepoRemote(t *testing.T) {
|
2020-12-12 18:27:35 +02:00
|
|
|
testlib.Mktmp(t)
|
2020-07-09 22:40:37 +02:00
|
|
|
testlib.GitInit(t)
|
|
|
|
testlib.GitRemoteAdd(t, "git@github.com:githubowner/githubrepo.git")
|
|
|
|
|
2021-04-25 19:20:49 +02:00
|
|
|
ctx := context.New(config.Project{})
|
2020-07-09 22:40:37 +02:00
|
|
|
ctx.TokenType = context.TokenTypeGitHub
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, Pipe{}.Default(ctx))
|
|
|
|
require.Equal(t, "githubrepo", ctx.Config.Milestones[0].Repo.Name)
|
|
|
|
require.Equal(t, "githubowner", ctx.Config.Milestones[0].Repo.Owner)
|
2020-07-09 22:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultWithNameTemplate(t *testing.T) {
|
2021-04-25 19:20:49 +02:00
|
|
|
ctx := &context.Context{
|
2020-07-09 22:40:37 +02:00
|
|
|
Config: config.Project{
|
|
|
|
Milestones: []config.Milestone{
|
|
|
|
{
|
|
|
|
NameTemplate: "confignametemplate",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, Pipe{}.Default(ctx))
|
|
|
|
require.Equal(t, "confignametemplate", ctx.Config.Milestones[0].NameTemplate)
|
2020-07-09 22:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultWithoutGitRepo(t *testing.T) {
|
2020-12-12 18:27:35 +02:00
|
|
|
testlib.Mktmp(t)
|
2021-04-25 19:20:49 +02:00
|
|
|
ctx := &context.Context{
|
2020-07-09 22:40:37 +02:00
|
|
|
Config: config.Project{},
|
|
|
|
}
|
|
|
|
ctx.TokenType = context.TokenTypeGitHub
|
2020-10-06 14:48:04 +02:00
|
|
|
require.EqualError(t, Pipe{}.Default(ctx), "current folder is not a git repository")
|
|
|
|
require.Empty(t, ctx.Config.Milestones[0].Repo.String())
|
2020-07-09 22:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultWithoutGitRepoOrigin(t *testing.T) {
|
2020-12-12 18:27:35 +02:00
|
|
|
testlib.Mktmp(t)
|
2021-04-25 19:20:49 +02:00
|
|
|
ctx := &context.Context{
|
2020-07-09 22:40:37 +02:00
|
|
|
Config: config.Project{},
|
|
|
|
}
|
|
|
|
ctx.TokenType = context.TokenTypeGitHub
|
|
|
|
testlib.GitInit(t)
|
2021-09-15 21:12:45 +02:00
|
|
|
require.EqualError(t, Pipe{}.Default(ctx), "no remote configured to list refs from")
|
2020-10-06 14:48:04 +02:00
|
|
|
require.Empty(t, ctx.Config.Milestones[0].Repo.String())
|
2020-07-09 22:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultWithoutGitRepoSnapshot(t *testing.T) {
|
2020-12-12 18:27:35 +02:00
|
|
|
testlib.Mktmp(t)
|
2021-04-25 19:20:49 +02:00
|
|
|
ctx := &context.Context{
|
2020-07-09 22:40:37 +02:00
|
|
|
Config: config.Project{},
|
|
|
|
}
|
|
|
|
ctx.TokenType = context.TokenTypeGitHub
|
|
|
|
ctx.Snapshot = true
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, Pipe{}.Default(ctx))
|
|
|
|
require.Empty(t, ctx.Config.Milestones[0].Repo.String())
|
2020-07-09 22:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultWithoutNameTemplate(t *testing.T) {
|
2021-04-25 19:20:49 +02:00
|
|
|
ctx := &context.Context{
|
2020-07-09 22:40:37 +02:00
|
|
|
Config: config.Project{
|
|
|
|
Milestones: []config.Milestone{},
|
|
|
|
},
|
|
|
|
}
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, Pipe{}.Default(ctx))
|
|
|
|
require.Equal(t, "{{ .Tag }}", ctx.Config.Milestones[0].NameTemplate)
|
2020-07-09 22:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestString(t *testing.T) {
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NotEmpty(t, Pipe{}.String())
|
2020-07-09 22:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPublishCloseDisabled(t *testing.T) {
|
2021-04-25 19:20:49 +02:00
|
|
|
ctx := context.New(config.Project{
|
2020-07-09 22:40:37 +02:00
|
|
|
Milestones: []config.Milestone{
|
|
|
|
{
|
|
|
|
Close: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
client := &DummyClient{}
|
|
|
|
testlib.AssertSkipped(t, doPublish(ctx, client))
|
2020-10-06 14:48:04 +02:00
|
|
|
require.Equal(t, "", client.ClosedMilestone)
|
2020-07-09 22:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPublishCloseEnabled(t *testing.T) {
|
2021-04-25 19:20:49 +02:00
|
|
|
ctx := context.New(config.Project{
|
2020-07-09 22:40:37 +02:00
|
|
|
Milestones: []config.Milestone{
|
|
|
|
{
|
|
|
|
Close: true,
|
|
|
|
NameTemplate: defaultNameTemplate,
|
|
|
|
Repo: config.Repo{
|
|
|
|
Name: "configrepo",
|
|
|
|
Owner: "configowner",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
ctx.Git.CurrentTag = "v1.0.0"
|
|
|
|
client := &DummyClient{}
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, doPublish(ctx, client))
|
|
|
|
require.Equal(t, "v1.0.0", client.ClosedMilestone)
|
2020-07-09 22:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPublishCloseError(t *testing.T) {
|
2021-04-25 19:20:49 +02:00
|
|
|
config := config.Project{
|
2020-07-09 22:40:37 +02:00
|
|
|
Milestones: []config.Milestone{
|
|
|
|
{
|
|
|
|
Close: true,
|
|
|
|
NameTemplate: defaultNameTemplate,
|
|
|
|
Repo: config.Repo{
|
|
|
|
Name: "configrepo",
|
|
|
|
Owner: "configowner",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-04-25 19:20:49 +02:00
|
|
|
ctx := context.New(config)
|
2020-07-09 22:40:37 +02:00
|
|
|
ctx.Git.CurrentTag = "v1.0.0"
|
|
|
|
client := &DummyClient{
|
|
|
|
FailToCloseMilestone: true,
|
|
|
|
}
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, doPublish(ctx, client))
|
|
|
|
require.Equal(t, "", client.ClosedMilestone)
|
2020-07-09 22:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPublishCloseFailOnError(t *testing.T) {
|
2021-04-25 19:20:49 +02:00
|
|
|
config := config.Project{
|
2020-07-09 22:40:37 +02:00
|
|
|
Milestones: []config.Milestone{
|
|
|
|
{
|
|
|
|
Close: true,
|
|
|
|
FailOnError: true,
|
|
|
|
NameTemplate: defaultNameTemplate,
|
|
|
|
Repo: config.Repo{
|
|
|
|
Name: "configrepo",
|
|
|
|
Owner: "configowner",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-04-25 19:20:49 +02:00
|
|
|
ctx := context.New(config)
|
2020-07-09 22:40:37 +02:00
|
|
|
ctx.Git.CurrentTag = "v1.0.0"
|
|
|
|
client := &DummyClient{
|
|
|
|
FailToCloseMilestone: true,
|
|
|
|
}
|
2020-10-06 14:48:04 +02:00
|
|
|
require.Error(t, doPublish(ctx, client))
|
|
|
|
require.Equal(t, "", client.ClosedMilestone)
|
2020-07-09 22:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type DummyClient struct {
|
|
|
|
ClosedMilestone string
|
|
|
|
FailToCloseMilestone bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DummyClient) CloseMilestone(ctx *context.Context, repo client.Repo, title string) error {
|
|
|
|
if c.FailToCloseMilestone {
|
|
|
|
return errors.New("milestone failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ClosedMilestone = title
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DummyClient) CreateRelease(ctx *context.Context, body string) (string, error) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DummyClient) Upload(ctx *context.Context, releaseID string, artifact *artifact.Artifact, file *os.File) error {
|
|
|
|
return nil
|
|
|
|
}
|