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

307 lines
6.8 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-04-15 14:31:55 -03:00
gitInit(t)
gitCommit(t, "commit1")
gitTag(t, "v0.0.1")
var ctx = &context.Context{
Config: config.Project{},
}
assert.NoError(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-04-15 14:31:55 -03:00
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()
gitInit(t)
gitCommit(t, "first")
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,
}
assert.NoError(Pipe{}.Run(ctx))
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()
gitInit(t)
gitCommit(t, "first")
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()
gitInit(t)
gitCommit(t, "first")
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-04-15 14:31:55 -03:00
gitInit(t)
gitCommit(t, "commit2")
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()
gitInit(t)
dummy, err := os.Create(filepath.Join(folder, "dummy"))
assert.NoError(err)
gitAdd(t)
gitCommit(t, "commit2")
gitTag(t, "v0.0.1")
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()
gitInit(t)
gitCommit(t, "commit3")
gitTag(t, "v0.0.1")
gitCommit(t, "commit4")
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()
gitInit(t)
gitCommit(t, "commit3")
gitTag(t, "v0.0.1")
gitCommit(t, "commit4")
gitTag(t, "v0.0.2")
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) {
var assert = assert.New(t)
2017-07-23 16:27:46 -03:00
_, back := testlib.Mktmp(t)
defer back()
gitInit(t)
gitAdd(t)
gitCommit(t, "commit5")
gitTag(t, "v0.0.1")
gitCommit(t, "commit6")
var ctx = &context.Context{
Config: config.Project{},
Validate: false,
}
assert.NoError(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()
gitInit(t)
gitCommit(t, "first")
gitTag(t, "v0.0.1")
gitCommit(t, "added feature 1")
gitCommit(t, "fixed bug 2")
gitTag(t, "v0.0.2")
var ctx = &context.Context{
Config: config.Project{},
}
assert.NoError(Pipe{}.Run(ctx))
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()
gitInit(t)
var msgs = []string{
"initial commit",
"another one",
"one more",
"and finally this one",
}
for _, msg := range msgs {
gitCommit(t, msg)
}
gitTag(t, "v0.0.1")
var ctx = &context.Context{
Config: config.Project{},
}
assert.NoError(Pipe{}.Run(ctx))
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()
gitInit(t)
gitCommit(t, "first")
gitTag(t, "v0.0.1")
var ctx = &context.Context{
Config: config.Project{},
ReleaseNotes: "custom",
}
assert.NoError(Pipe{}.Run(ctx))
assert.Equal("v0.0.1", ctx.Git.CurrentTag)
assert.Equal(ctx.ReleaseNotes, "custom")
}
2017-04-15 14:31:55 -03:00
//
// helper functions
//
func gitInit(t *testing.T) {
var assert = assert.New(t)
out, err := git("init")
assert.NoError(err)
assert.Contains(out, "Initialized empty Git repository")
_, err = git("config", "commit.gpgSign", "false")
assert.NoError(err)
2017-04-15 14:31:55 -03:00
}
func gitCommit(t *testing.T, msg string) {
var assert = assert.New(t)
2017-04-15 14:38:54 -03:00
out, err := fakeGit("commit", "--allow-empty", "-m", msg)
2017-04-15 14:31:55 -03:00
assert.NoError(err)
assert.Contains(out, "master", msg)
}
func gitTag(t *testing.T, tag string) {
var assert = assert.New(t)
2017-04-15 14:38:54 -03:00
out, err := fakeGit("tag", tag)
2017-04-15 14:31:55 -03:00
assert.NoError(err)
assert.Empty(out)
}
2017-04-15 16:11:47 -03:00
func gitAdd(t *testing.T) {
2017-04-15 14:31:55 -03:00
var assert = assert.New(t)
2017-04-15 16:11:47 -03:00
out, err := git("add", "-A")
2017-04-15 14:31:55 -03:00
assert.NoError(err)
2017-04-15 16:11:47 -03:00
assert.Empty(out)
2017-04-15 14:31:55 -03:00
}
2017-04-15 14:38:54 -03:00
func fakeGit(args ...string) (string, error) {
var allArgs = []string{
"-c",
"user.name='GoReleaser'",
"-c",
"user.email='test@goreleaser.github.com'",
}
allArgs = append(allArgs, args...)
return git(allArgs...)
}