1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-24 04:16:27 +02:00
goreleaser/pipeline/git/git_test.go

200 lines
4.6 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) {
2017-07-23 16:27:46 -03:00
_, back := testlib.Mktmp(t)
defer back()
var ctx = &context.Context{
Config: config.Project{},
}
assert.Error(t, Pipe{}.Run(ctx))
}
func TestSingleCommit(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.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))
assert.Equal(t, "v0.0.1", ctx.Git.CurrentTag)
}
func TestNewRepository(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)
var ctx = &context.Context{
Config: config.Project{},
}
assert.Error(t, Pipe{}.Run(ctx))
}
2017-04-29 12:49:22 +02:00
func TestNoTagsSnapshot(t *testing.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))
assert.Contains(t, ctx.Version, "SNAPSHOT-")
2017-04-29 12:49:22 +02:00
}
func TestNoTagsSnapshotInvalidTemplate(t *testing.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(t, Pipe{}.Run(ctx))
2017-04-29 12:49:22 +02:00
}
// 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) {
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(t, Pipe{}.Run(ctx))
2017-04-29 12:49:22 +02:00
}
func TestInvalidTagFormat(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.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(t, Pipe{}.Run(ctx), "sadasd is not in a valid version format")
assert.Equal(t, "sadasd", ctx.Git.CurrentTag)
}
2017-04-15 16:11:47 -03:00
func TestDirty(t *testing.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(t, err)
2017-07-23 16:42:09 -03:00
testlib.GitAdd(t)
testlib.GitCommit(t, "commit2")
testlib.GitTag(t, "v0.0.1")
assert.NoError(t, ioutil.WriteFile(dummy.Name(), []byte("lorem ipsum"), 0644))
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(t, err)
assert.Contains(t, err.Error(), "git is currently in a dirty state:")
2017-04-15 16:11:47 -03:00
}
func TestTagIsNotLastCommit(t *testing.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(t, err)
assert.Contains(t, 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) {
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(t, Pipe{}.Run(ctx))
assert.Equal(t, "v0.0.2", ctx.Git.CurrentTag)
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))
}
func TestSnapshot(t *testing.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.GitAdd(t)
testlib.GitCommit(t, "whatever")
2017-04-19 17:31:09 -03:00
var ctx = &context.Context{
Config: config.Project{},
Validate: true,
Snapshot: true,
2017-04-19 17:31:09 -03:00
}
assert.NoError(t, Pipe{}.Run(ctx))
2017-04-19 17:31:09 -03:00
}