1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-26 04:22:05 +02:00
goreleaser/pipeline/git/git_test.go

261 lines
6.3 KiB
Go
Raw Normal View History

2017-03-25 20:36:21 -03:00
package git
import (
"io/ioutil"
"os"
2017-04-15 16:11:47 -03:00
"path/filepath"
2017-03-25 20:36:21 -03:00
"testing"
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
2017-07-23 16:27:46 -03:00
"github.com/goreleaser/goreleaser/internal/testlib"
2017-05-01 10:39:57 -03:00
"github.com/goreleaser/goreleaser/pipeline/defaults"
2017-03-25 20:36:21 -03:00
"github.com/stretchr/testify/assert"
)
2017-03-25 20:40:45 -03:00
func TestDescription(t *testing.T) {
assert.NotEmpty(t, Pipe{}.Description())
}
func TestNotAGitFolder(t *testing.T) {
var assert = assert.New(t)
2017-07-23 16:27:46 -03:00
_, back := testlib.Mktmp(t)
defer back()
var ctx = &context.Context{
Config: config.Project{},
}
assert.Error(Pipe{}.Run(ctx))
}
func TestSingleCommit(t *testing.T) {
var assert = assert.New(t)
2017-07-23 16:27:46 -03:00
_, back := testlib.Mktmp(t)
defer back()
2017-07-23 16:42:09 -03:00
testlib.GitInit(t)
testlib.GitCommit(t, "commit1")
testlib.GitTag(t, "v0.0.1")
var ctx = &context.Context{
Config: config.Project{},
}
2017-08-20 16:50:34 -03:00
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
2017-04-15 14:03:50 -03:00
assert.Equal("v0.0.1", ctx.Git.CurrentTag)
}
func TestNewRepository(t *testing.T) {
var assert = assert.New(t)
2017-07-23 16:27:46 -03:00
_, back := testlib.Mktmp(t)
defer back()
2017-07-23 16:42:09 -03:00
testlib.GitInit(t)
var ctx = &context.Context{
Config: config.Project{},
}
assert.Error(Pipe{}.Run(ctx))
}
2017-04-29 12:49:22 +02:00
func TestNoTagsSnapshot(t *testing.T) {
assert := assert.New(t)
2017-07-23 16:27:46 -03:00
_, back := testlib.Mktmp(t)
2017-04-29 12:49:22 +02:00
defer back()
2017-07-23 16:42:09 -03:00
testlib.GitInit(t)
testlib.GitCommit(t, "first")
2017-04-29 12:49:22 +02:00
var ctx = &context.Context{
Config: config.Project{
2017-05-01 10:39:57 -03:00
Snapshot: config.Snapshot{
NameTemplate: defaults.SnapshotNameTemplate,
},
2017-04-29 12:49:22 +02:00
},
Snapshot: true,
Publish: false,
}
2017-08-20 16:50:34 -03:00
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
2017-04-29 12:49:22 +02:00
assert.Contains(ctx.Version, "SNAPSHOT-")
}
func TestNoTagsSnapshotInvalidTemplate(t *testing.T) {
assert := assert.New(t)
2017-07-23 16:27:46 -03:00
_, back := testlib.Mktmp(t)
2017-04-29 12:49:22 +02:00
defer back()
2017-07-23 16:42:09 -03:00
testlib.GitInit(t)
testlib.GitCommit(t, "first")
2017-04-29 12:49:22 +02:00
var ctx = &context.Context{
Config: config.Project{
2017-05-01 10:39:57 -03:00
Snapshot: config.Snapshot{
NameTemplate: "{{",
},
2017-04-29 12:49:22 +02:00
},
Snapshot: true,
Publish: false,
}
assert.Error(Pipe{}.Run(ctx))
}
// TestNoTagsNoSnapshot covers the situation where a repository
// only contains simple commits and no tags. In this case you have
// to set the --snapshot flag otherwise an error is returned.
func TestNoTagsNoSnapshot(t *testing.T) {
assert := assert.New(t)
2017-07-23 16:27:46 -03:00
_, back := testlib.Mktmp(t)
2017-04-29 12:49:22 +02:00
defer back()
2017-07-23 16:42:09 -03:00
testlib.GitInit(t)
testlib.GitCommit(t, "first")
2017-04-29 12:49:22 +02:00
var ctx = &context.Context{
Config: config.Project{
2017-05-01 10:39:57 -03:00
Snapshot: config.Snapshot{
NameTemplate: defaults.SnapshotNameTemplate,
},
2017-04-29 12:49:22 +02:00
},
Snapshot: false,
Publish: false,
}
assert.Error(Pipe{}.Run(ctx))
}
func TestInvalidTagFormat(t *testing.T) {
var assert = assert.New(t)
2017-07-23 16:27:46 -03:00
_, back := testlib.Mktmp(t)
defer back()
2017-07-23 16:42:09 -03:00
testlib.GitInit(t)
testlib.GitCommit(t, "commit2")
testlib.GitTag(t, "sadasd")
2017-04-15 13:36:48 -03:00
var ctx = &context.Context{
Config: config.Project{},
Validate: true,
}
assert.EqualError(Pipe{}.Run(ctx), "sadasd is not in a valid version format")
2017-04-15 14:03:50 -03:00
assert.Equal("sadasd", ctx.Git.CurrentTag)
}
2017-04-15 16:11:47 -03:00
func TestDirty(t *testing.T) {
var assert = assert.New(t)
2017-07-23 16:27:46 -03:00
folder, back := testlib.Mktmp(t)
2017-04-15 16:11:47 -03:00
defer back()
2017-07-23 16:42:09 -03:00
testlib.GitInit(t)
2017-04-15 16:11:47 -03:00
dummy, err := os.Create(filepath.Join(folder, "dummy"))
assert.NoError(err)
2017-07-23 16:42:09 -03:00
testlib.GitAdd(t)
testlib.GitCommit(t, "commit2")
testlib.GitTag(t, "v0.0.1")
2017-04-15 16:11:47 -03:00
assert.NoError(ioutil.WriteFile(dummy.Name(), []byte("lorem ipsum"), 0644))
var ctx = &context.Context{
Config: config.Project{},
Validate: true,
2017-04-15 16:11:47 -03:00
}
err = Pipe{}.Run(ctx)
assert.Error(err)
assert.Contains(err.Error(), "git is currently in a dirty state:")
}
func TestTagIsNotLastCommit(t *testing.T) {
var assert = assert.New(t)
2017-07-23 16:27:46 -03:00
_, back := testlib.Mktmp(t)
2017-04-15 16:11:47 -03:00
defer back()
2017-07-23 16:42:09 -03:00
testlib.GitInit(t)
testlib.GitCommit(t, "commit3")
testlib.GitTag(t, "v0.0.1")
testlib.GitCommit(t, "commit4")
2017-04-15 16:11:47 -03:00
var ctx = &context.Context{
Config: config.Project{},
Validate: true,
2017-04-15 16:11:47 -03:00
}
err := Pipe{}.Run(ctx)
assert.Error(err)
2017-04-15 17:04:26 -03:00
assert.Contains(err.Error(), "git tag v0.0.1 was not made against commit")
2017-04-15 16:11:47 -03:00
}
2017-04-22 10:46:58 -03:00
func TestValidState(t *testing.T) {
var assert = assert.New(t)
2017-07-23 16:27:46 -03:00
_, back := testlib.Mktmp(t)
2017-04-22 10:46:58 -03:00
defer back()
2017-07-23 16:42:09 -03:00
testlib.GitInit(t)
testlib.GitCommit(t, "commit3")
testlib.GitTag(t, "v0.0.1")
testlib.GitCommit(t, "commit4")
testlib.GitTag(t, "v0.0.2")
2017-04-22 10:46:58 -03:00
var ctx = &context.Context{
Config: config.Project{},
Validate: true,
}
assert.NoError(Pipe{}.Run(ctx))
2017-07-13 22:23:31 -03:00
assert.Equal("v0.0.2", ctx.Git.CurrentTag)
assert.NotContains("commit4", ctx.ReleaseNotes)
2017-07-13 22:22:44 -03:00
assert.NotContains("commit3", ctx.ReleaseNotes)
2017-04-22 10:46:58 -03:00
}
func TestNoValidate(t *testing.T) {
2017-07-23 16:27:46 -03:00
_, back := testlib.Mktmp(t)
defer back()
2017-07-23 16:42:09 -03:00
testlib.GitInit(t)
testlib.GitAdd(t)
testlib.GitCommit(t, "commit5")
testlib.GitTag(t, "v0.0.1")
testlib.GitCommit(t, "commit6")
var ctx = &context.Context{
Config: config.Project{},
Validate: false,
}
2017-08-20 16:50:34 -03:00
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
}
2017-04-19 17:31:09 -03:00
func TestChangelog(t *testing.T) {
var assert = assert.New(t)
2017-07-23 16:27:46 -03:00
_, back := testlib.Mktmp(t)
2017-04-19 17:31:09 -03:00
defer back()
2017-07-23 16:42:09 -03:00
testlib.GitInit(t)
testlib.GitCommit(t, "first")
testlib.GitTag(t, "v0.0.1")
testlib.GitCommit(t, "added feature 1")
testlib.GitCommit(t, "fixed bug 2")
testlib.GitTag(t, "v0.0.2")
2017-04-19 17:31:09 -03:00
var ctx = &context.Context{
Config: config.Project{},
}
2017-08-20 16:50:34 -03:00
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
2017-04-19 17:31:09 -03:00
assert.Equal("v0.0.2", ctx.Git.CurrentTag)
assert.Contains(ctx.ReleaseNotes, "## Changelog")
2017-04-23 16:33:44 -03:00
assert.NotContains(ctx.ReleaseNotes, "first")
2017-04-19 17:31:09 -03:00
assert.Contains(ctx.ReleaseNotes, "added feature 1")
assert.Contains(ctx.ReleaseNotes, "fixed bug 2")
}
2017-04-23 16:33:44 -03:00
func TestChangelogOfFirstRelease(t *testing.T) {
var assert = assert.New(t)
2017-07-23 16:27:46 -03:00
_, back := testlib.Mktmp(t)
2017-04-23 16:33:44 -03:00
defer back()
2017-07-23 16:42:09 -03:00
testlib.GitInit(t)
2017-04-23 16:33:44 -03:00
var msgs = []string{
"initial commit",
"another one",
"one more",
"and finally this one",
}
for _, msg := range msgs {
2017-07-23 16:42:09 -03:00
testlib.GitCommit(t, msg)
2017-04-23 16:33:44 -03:00
}
2017-07-23 16:42:09 -03:00
testlib.GitTag(t, "v0.0.1")
2017-04-23 16:33:44 -03:00
var ctx = &context.Context{
Config: config.Project{},
}
2017-08-20 16:50:34 -03:00
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
2017-04-23 16:33:44 -03:00
assert.Equal("v0.0.1", ctx.Git.CurrentTag)
assert.Contains(ctx.ReleaseNotes, "## Changelog")
for _, msg := range msgs {
assert.Contains(ctx.ReleaseNotes, msg)
}
}
2017-04-19 17:31:09 -03:00
func TestCustomReleaseNotes(t *testing.T) {
var assert = assert.New(t)
2017-07-23 16:27:46 -03:00
_, back := testlib.Mktmp(t)
2017-04-19 17:31:09 -03:00
defer back()
2017-07-23 16:42:09 -03:00
testlib.GitInit(t)
testlib.GitCommit(t, "first")
testlib.GitTag(t, "v0.0.1")
2017-04-19 17:31:09 -03:00
var ctx = &context.Context{
Config: config.Project{},
ReleaseNotes: "custom",
}
2017-08-20 16:50:34 -03:00
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
2017-04-19 17:31:09 -03:00
assert.Equal("v0.0.1", ctx.Git.CurrentTag)
assert.Equal(ctx.ReleaseNotes, "custom")
}