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"
|
|
|
|
|
2017-07-23 21:27:46 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/testlib"
|
2018-08-15 04:50:20 +02:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/config"
|
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
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) {
|
2017-12-02 23:53:19 +02:00
|
|
|
assert.NotEmpty(t, Pipe{}.String())
|
2017-03-26 01:40:45 +02:00
|
|
|
}
|
|
|
|
|
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{},
|
|
|
|
}
|
2018-02-26 01:17:45 +02:00
|
|
|
assert.EqualError(t, Pipe{}.Run(ctx), ErrNotRepository.Error())
|
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{},
|
|
|
|
}
|
2018-02-24 22:59:08 +02:00
|
|
|
assert.NoError(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{},
|
|
|
|
}
|
2018-02-24 22:59:08 +02:00
|
|
|
// TODO: improve this error handling
|
|
|
|
assert.Contains(t, Pipe{}.Run(ctx).Error(), `fatal: ambiguous argument 'HEAD'`)
|
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")
|
2018-02-24 22:59:08 +02:00
|
|
|
var ctx = context.New(config.Project{
|
|
|
|
Snapshot: config.Snapshot{
|
|
|
|
NameTemplate: "SNAPSHOT-{{.Commit}}",
|
2017-04-29 12:49:22 +02:00
|
|
|
},
|
2018-02-24 22:59:08 +02:00
|
|
|
})
|
|
|
|
ctx.Snapshot = true
|
2018-03-08 13:42:33 +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")
|
2018-02-24 22:59:08 +02:00
|
|
|
var ctx = context.New(config.Project{
|
|
|
|
Snapshot: config.Snapshot{
|
|
|
|
NameTemplate: "{{",
|
2017-04-29 12:49:22 +02:00
|
|
|
},
|
2018-02-24 22:59:08 +02:00
|
|
|
})
|
|
|
|
ctx.Snapshot = true
|
2018-07-09 08:08:06 +02:00
|
|
|
assert.EqualError(t, Pipe{}.Run(ctx), `failed to generate snapshot name: template: tmpl:1: unexpected unclosed action in command`)
|
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")
|
2018-02-24 22:59:08 +02:00
|
|
|
var ctx = context.New(config.Project{})
|
|
|
|
ctx.Snapshot = false
|
|
|
|
assert.EqualError(t, Pipe{}.Run(ctx), `git doesn't contain any tags. Either add a tag or use --snapshot`)
|
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")
|
2018-02-24 22:59:08 +02:00
|
|
|
var ctx = context.New(config.Project{})
|
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))
|
2018-03-08 13:42:33 +02:00
|
|
|
t.Run("all checks up", func(t *testing.T) {
|
|
|
|
err = Pipe{}.Run(context.New(config.Project{}))
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "git is currently in a dirty state:")
|
|
|
|
})
|
|
|
|
t.Run("skip validate is set", func(t *testing.T) {
|
|
|
|
ctx := context.New(config.Project{})
|
|
|
|
ctx.SkipValidate = true
|
|
|
|
err = Pipe{}.Run(ctx)
|
|
|
|
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
|
|
|
|
})
|
|
|
|
t.Run("snapshot", func(t *testing.T) {
|
|
|
|
ctx := context.New(config.Project{})
|
|
|
|
ctx.Snapshot = true
|
|
|
|
err = Pipe{}.Run(ctx)
|
|
|
|
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
|
|
|
|
})
|
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")
|
2018-02-24 22:59:08 +02:00
|
|
|
err := Pipe{}.Run(context.New(config.Project{}))
|
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")
|
2018-02-24 22:59:08 +02:00
|
|
|
var ctx = context.New(config.Project{})
|
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
|
|
|
}
|
|
|
|
|
2018-02-26 01:17:45 +02:00
|
|
|
func TestSnapshotNoTags(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")
|
2018-02-24 22:59:08 +02:00
|
|
|
var ctx = context.New(config.Project{})
|
|
|
|
ctx.Snapshot = true
|
2018-03-08 13:42:33 +02:00
|
|
|
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
|
2018-03-02 01:42:47 +02:00
|
|
|
assert.Equal(t, fakeInfo.CurrentTag, ctx.Git.CurrentTag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSnapshotNoCommits(t *testing.T) {
|
|
|
|
_, back := testlib.Mktmp(t)
|
|
|
|
defer back()
|
|
|
|
testlib.GitInit(t)
|
|
|
|
var ctx = context.New(config.Project{})
|
|
|
|
ctx.Snapshot = true
|
2018-03-08 13:42:33 +02:00
|
|
|
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
|
2018-03-02 01:42:47 +02:00
|
|
|
assert.Equal(t, fakeInfo, ctx.Git)
|
2017-04-19 22:31:09 +02:00
|
|
|
}
|
2018-02-24 22:59:08 +02:00
|
|
|
|
2018-02-26 01:17:45 +02:00
|
|
|
func TestSnapshotWithoutRepo(t *testing.T) {
|
|
|
|
_, back := testlib.Mktmp(t)
|
|
|
|
defer back()
|
|
|
|
var ctx = context.New(config.Project{})
|
|
|
|
ctx.Snapshot = true
|
2018-03-08 13:42:33 +02:00
|
|
|
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
|
2018-03-02 01:42:47 +02:00
|
|
|
assert.Equal(t, fakeInfo, ctx.Git)
|
2018-02-26 01:17:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSnapshotDirty(t *testing.T) {
|
|
|
|
folder, back := testlib.Mktmp(t)
|
|
|
|
defer back()
|
|
|
|
testlib.GitInit(t)
|
|
|
|
testlib.GitAdd(t)
|
|
|
|
testlib.GitCommit(t, "whatever")
|
|
|
|
testlib.GitTag(t, "v0.0.1")
|
|
|
|
assert.NoError(t, ioutil.WriteFile(filepath.Join(folder, "foo"), []byte("foobar"), 0644))
|
|
|
|
var ctx = context.New(config.Project{})
|
|
|
|
ctx.Snapshot = true
|
2018-03-08 13:42:33 +02:00
|
|
|
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
|
2018-02-26 01:17:45 +02:00
|
|
|
}
|
2018-03-02 14:20:27 +02:00
|
|
|
|
|
|
|
func TestShortCommitHash(t *testing.T) {
|
|
|
|
_, back := testlib.Mktmp(t)
|
|
|
|
defer back()
|
|
|
|
testlib.GitInit(t)
|
|
|
|
testlib.GitCommit(t, "first")
|
|
|
|
var ctx = context.New(config.Project{
|
|
|
|
Snapshot: config.Snapshot{
|
|
|
|
NameTemplate: "{{.Commit}}",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
ctx.Snapshot = true
|
|
|
|
ctx.Config.Git.ShortHash = true
|
2018-03-08 13:42:33 +02:00
|
|
|
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
|
2018-03-02 14:20:27 +02:00
|
|
|
assert.Len(t, ctx.Version, 7)
|
|
|
|
}
|
2018-08-21 04:18:43 +02:00
|
|
|
|
|
|
|
func TestGitNotInPath(t *testing.T) {
|
|
|
|
var path = os.Getenv("PATH")
|
|
|
|
defer func() {
|
|
|
|
assert.NoError(t, os.Setenv("PATH", path))
|
|
|
|
}()
|
|
|
|
assert.NoError(t, os.Setenv("PATH", ""))
|
|
|
|
assert.EqualError(t, Pipe{}.Run(context.New(config.Project{})), ErrNoGit.Error())
|
|
|
|
}
|