2017-03-26 01:36:21 +02:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2017-04-15 19:00:29 +02:00
|
|
|
"io/ioutil"
|
2017-04-15 19:00:49 +02:00
|
|
|
"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())
|
|
|
|
}
|
|
|
|
|
2017-04-15 19:00:29 +02:00
|
|
|
func TestNotAGitFolder(t *testing.T) {
|
2017-07-23 21:27:46 +02:00
|
|
|
_, back := testlib.Mktmp(t)
|
2017-04-15 19:00:49 +02:00
|
|
|
defer back()
|
2017-04-15 18:05:54 +02:00
|
|
|
var ctx = &context.Context{
|
|
|
|
Config: config.Project{},
|
|
|
|
}
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.Error(t, Pipe{}.Run(ctx))
|
2017-04-15 18:05:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSingleCommit(t *testing.T) {
|
2017-07-23 21:27:46 +02:00
|
|
|
_, back := testlib.Mktmp(t)
|
2017-04-15 19:00:49 +02:00
|
|
|
defer back()
|
2017-07-23 21:42:09 +02:00
|
|
|
testlib.GitInit(t)
|
|
|
|
testlib.GitCommit(t, "commit1")
|
|
|
|
testlib.GitTag(t, "v0.0.1")
|
2017-04-15 18:05:54 +02:00
|
|
|
var ctx = &context.Context{
|
|
|
|
Config: config.Project{},
|
|
|
|
}
|
2017-08-20 21:50:34 +02:00
|
|
|
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.Equal(t, "v0.0.1", ctx.Git.CurrentTag)
|
2017-04-15 18:05:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewRepository(t *testing.T) {
|
2017-07-23 21:27:46 +02:00
|
|
|
_, back := testlib.Mktmp(t)
|
2017-04-15 19:00:49 +02:00
|
|
|
defer back()
|
2017-07-23 21:42:09 +02:00
|
|
|
testlib.GitInit(t)
|
2017-04-15 18:05:54 +02:00
|
|
|
var ctx = &context.Context{
|
|
|
|
Config: config.Project{},
|
|
|
|
}
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.Error(t, Pipe{}.Run(ctx))
|
2017-04-15 18:05:54 +02:00
|
|
|
}
|
|
|
|
|
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))
|
2017-09-27 00:24:49 +02:00
|
|
|
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,
|
|
|
|
}
|
2017-09-27 00:24:49 +02:00
|
|
|
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,
|
|
|
|
}
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.Error(t, Pipe{}.Run(ctx))
|
2017-04-29 12:49:22 +02:00
|
|
|
}
|
|
|
|
|
2017-04-15 18:05:54 +02:00
|
|
|
func TestInvalidTagFormat(t *testing.T) {
|
2017-07-23 21:27:46 +02:00
|
|
|
_, back := testlib.Mktmp(t)
|
2017-04-15 19:00:49 +02:00
|
|
|
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{
|
2017-04-18 18:10:13 +02:00
|
|
|
Config: config.Project{},
|
|
|
|
Validate: true,
|
2017-04-15 18:05:54 +02:00
|
|
|
}
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.EqualError(t, Pipe{}.Run(ctx), "sadasd is not in a valid version format")
|
|
|
|
assert.Equal(t, "sadasd", ctx.Git.CurrentTag)
|
2017-04-15 19:00:49 +02:00
|
|
|
}
|
|
|
|
|
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"))
|
2017-09-27 00:24:49 +02:00
|
|
|
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")
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, ioutil.WriteFile(dummy.Name(), []byte("lorem ipsum"), 0644))
|
2017-04-15 21:11:47 +02:00
|
|
|
var ctx = &context.Context{
|
2017-04-18 18:10:13 +02:00
|
|
|
Config: config.Project{},
|
|
|
|
Validate: true,
|
2017-04-15 21:11:47 +02:00
|
|
|
}
|
|
|
|
err = Pipe{}.Run(ctx)
|
2017-09-27 00:24:49 +02:00
|
|
|
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{
|
2017-04-18 18:10:13 +02:00
|
|
|
Config: config.Project{},
|
|
|
|
Validate: true,
|
2017-04-15 21:11:47 +02:00
|
|
|
}
|
|
|
|
err := Pipe{}.Run(ctx)
|
2017-09-27 00:24:49 +02:00
|
|
|
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,
|
|
|
|
}
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, Pipe{}.Run(ctx))
|
|
|
|
assert.Equal(t, "v0.0.2", ctx.Git.CurrentTag)
|
2017-04-22 15:46:58 +02:00
|
|
|
}
|
|
|
|
|
2017-04-18 18:10:13 +02:00
|
|
|
func TestNoValidate(t *testing.T) {
|
2017-07-23 21:27:46 +02:00
|
|
|
_, back := testlib.Mktmp(t)
|
2017-04-18 18:10:13 +02:00
|
|
|
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")
|
2017-04-18 18:10:13 +02:00
|
|
|
var ctx = &context.Context{
|
|
|
|
Config: config.Project{},
|
|
|
|
Validate: false,
|
|
|
|
}
|
2017-08-20 21:50:34 +02:00
|
|
|
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
|
2017-04-18 18:10:13 +02:00
|
|
|
}
|
|
|
|
|
2017-10-16 00:21:35 +02:00
|
|
|
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)
|
2017-10-16 00:21:35 +02:00
|
|
|
testlib.GitAdd(t)
|
|
|
|
testlib.GitCommit(t, "whatever")
|
2017-04-19 22:31:09 +02:00
|
|
|
var ctx = &context.Context{
|
2017-10-16 00:21:35 +02:00
|
|
|
Config: config.Project{},
|
|
|
|
Validate: true,
|
|
|
|
Snapshot: true,
|
2017-04-19 22:31:09 +02:00
|
|
|
}
|
2017-10-16 00:21:35 +02:00
|
|
|
assert.NoError(t, Pipe{}.Run(ctx))
|
2017-04-19 22:31:09 +02:00
|
|
|
}
|