1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/pipeline/git/git_test.go

200 lines
4.6 KiB
Go
Raw Normal View History

2017-03-26 01:36:21 +02:00
package git
import (
"io/ioutil"
"os"
2017-04-15 21:11:47 +02:00
"path/filepath"
2017-03-26 01:36:21 +02:00
"testing"
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
2017-07-23 21:27:46 +02:00
"github.com/goreleaser/goreleaser/internal/testlib"
2017-05-01 15:39:57 +02:00
"github.com/goreleaser/goreleaser/pipeline/defaults"
2017-03-26 01:36:21 +02:00
"github.com/stretchr/testify/assert"
)
2017-03-26 01:40:45 +02:00
func TestDescription(t *testing.T) {
assert.NotEmpty(t, Pipe{}.Description())
}
func TestNotAGitFolder(t *testing.T) {
2017-07-23 21:27:46 +02: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 21:27:46 +02:00
_, back := testlib.Mktmp(t)
defer back()
2017-07-23 21:42:09 +02:00
testlib.GitInit(t)
testlib.GitCommit(t, "commit1")
testlib.GitTag(t, "v0.0.1")
var ctx = &context.Context{
Config: config.Project{},
}
2017-08-20 21:50:34 +02:00
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
assert.Equal(t, "v0.0.1", ctx.Git.CurrentTag)
}
func TestNewRepository(t *testing.T) {
2017-07-23 21:27:46 +02:00
_, back := testlib.Mktmp(t)
defer back()
2017-07-23 21:42:09 +02: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 21:27:46 +02:00
_, back := testlib.Mktmp(t)
2017-04-29 12:49:22 +02:00
defer back()
2017-07-23 21:42:09 +02: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 15:39:57 +02:00
Snapshot: config.Snapshot{
NameTemplate: defaults.SnapshotNameTemplate,
},
2017-04-29 12:49:22 +02:00
},
Snapshot: true,
Publish: false,
}
2017-08-20 21:50:34 +02: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 21:27:46 +02:00
_, back := testlib.Mktmp(t)
2017-04-29 12:49:22 +02:00
defer back()
2017-07-23 21:42:09 +02: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 15:39:57 +02: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 21:27:46 +02:00
_, back := testlib.Mktmp(t)
2017-04-29 12:49:22 +02:00
defer back()
2017-07-23 21:42:09 +02: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 15:39:57 +02: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 21:27:46 +02:00
_, back := testlib.Mktmp(t)
defer back()
2017-07-23 21:42:09 +02:00
testlib.GitInit(t)
testlib.GitCommit(t, "commit2")
testlib.GitTag(t, "sadasd")
2017-04-15 18:36:48 +02: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 21:11:47 +02:00
func TestDirty(t *testing.T) {
2017-07-23 21:27:46 +02:00
folder, back := testlib.Mktmp(t)
2017-04-15 21:11:47 +02:00
defer back()
2017-07-23 21:42:09 +02:00
testlib.GitInit(t)
2017-04-15 21:11:47 +02:00
dummy, err := os.Create(filepath.Join(folder, "dummy"))
assert.NoError(t, err)
2017-07-23 21:42:09 +02: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 21:11:47 +02:00
var ctx = &context.Context{
Config: config.Project{},
Validate: true,
2017-04-15 21:11:47 +02:00
}
err = Pipe{}.Run(ctx)
assert.Error(t, err)
assert.Contains(t, err.Error(), "git is currently in a dirty state:")
2017-04-15 21:11:47 +02:00
}
func TestTagIsNotLastCommit(t *testing.T) {
2017-07-23 21:27:46 +02:00
_, back := testlib.Mktmp(t)
2017-04-15 21:11:47 +02:00
defer back()
2017-07-23 21:42:09 +02:00
testlib.GitInit(t)
testlib.GitCommit(t, "commit3")
testlib.GitTag(t, "v0.0.1")
testlib.GitCommit(t, "commit4")
2017-04-15 21:11:47 +02:00
var ctx = &context.Context{
Config: config.Project{},
Validate: true,
2017-04-15 21:11:47 +02: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 21:11:47 +02:00
}
2017-04-22 15:46:58 +02:00
func TestValidState(t *testing.T) {
2017-07-23 21:27:46 +02:00
_, back := testlib.Mktmp(t)
2017-04-22 15:46:58 +02:00
defer back()
2017-07-23 21:42:09 +02: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 15:46:58 +02: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 15:46:58 +02:00
}
func TestNoValidate(t *testing.T) {
2017-07-23 21:27:46 +02:00
_, back := testlib.Mktmp(t)
defer back()
2017-07-23 21:42:09 +02: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 21:50:34 +02:00
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
}
func TestSnapshot(t *testing.T) {
2017-07-23 21:27:46 +02:00
_, back := testlib.Mktmp(t)
2017-04-19 22:31:09 +02:00
defer back()
2017-07-23 21:42:09 +02:00
testlib.GitInit(t)
testlib.GitAdd(t)
testlib.GitCommit(t, "whatever")
2017-04-19 22:31:09 +02:00
var ctx = &context.Context{
Config: config.Project{},
Validate: true,
Snapshot: true,
2017-04-19 22:31:09 +02:00
}
assert.NoError(t, Pipe{}.Run(ctx))
2017-04-19 22:31:09 +02:00
}