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"
|
|
|
|
"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-04-15 18:05:54 +02:00
|
|
|
var assert = assert.New(t)
|
2017-04-15 19:00:49 +02:00
|
|
|
_, back := createAndChdir(t)
|
|
|
|
defer back()
|
2017-04-15 18:05:54 +02:00
|
|
|
var ctx = &context.Context{
|
|
|
|
Config: config.Project{},
|
|
|
|
}
|
2017-04-15 19:00:49 +02:00
|
|
|
assert.Error(Pipe{}.Run(ctx))
|
2017-04-15 18:05:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSingleCommit(t *testing.T) {
|
|
|
|
var assert = assert.New(t)
|
2017-04-15 19:00:49 +02:00
|
|
|
_, back := createAndChdir(t)
|
|
|
|
defer back()
|
2017-04-15 19:31:55 +02:00
|
|
|
gitInit(t)
|
|
|
|
gitCommit(t, "commit1")
|
|
|
|
gitTag(t, "v0.0.1")
|
2017-04-15 18:05:54 +02:00
|
|
|
var ctx = &context.Context{
|
|
|
|
Config: config.Project{},
|
|
|
|
}
|
2017-04-15 19:00:49 +02:00
|
|
|
assert.NoError(Pipe{}.Run(ctx))
|
2017-04-15 19:03:50 +02:00
|
|
|
assert.Equal("v0.0.1", ctx.Git.CurrentTag)
|
2017-04-15 18:05:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewRepository(t *testing.T) {
|
|
|
|
var assert = assert.New(t)
|
2017-04-15 19:00:49 +02:00
|
|
|
_, back := createAndChdir(t)
|
|
|
|
defer back()
|
2017-04-15 19:31:55 +02:00
|
|
|
gitInit(t)
|
2017-04-15 18:05:54 +02:00
|
|
|
var ctx = &context.Context{
|
|
|
|
Config: config.Project{},
|
|
|
|
}
|
2017-04-15 19:00:49 +02:00
|
|
|
assert.Error(Pipe{}.Run(ctx))
|
2017-04-15 18:05:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidTagFormat(t *testing.T) {
|
|
|
|
var assert = assert.New(t)
|
2017-04-15 19:00:49 +02:00
|
|
|
_, back := createAndChdir(t)
|
|
|
|
defer back()
|
2017-04-15 19:31:55 +02:00
|
|
|
gitInit(t)
|
|
|
|
gitCommit(t, "commit2")
|
|
|
|
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-04-15 19:00:49 +02:00
|
|
|
assert.EqualError(Pipe{}.Run(ctx), "sadasd is not in a valid version format")
|
2017-04-15 19:03:50 +02:00
|
|
|
assert.Equal("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) {
|
|
|
|
var assert = assert.New(t)
|
|
|
|
folder, back := createAndChdir(t)
|
|
|
|
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{
|
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)
|
|
|
|
assert.Error(err)
|
|
|
|
assert.Contains(err.Error(), "git is currently in a dirty state:")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTagIsNotLastCommit(t *testing.T) {
|
|
|
|
var assert = assert.New(t)
|
|
|
|
_, back := createAndChdir(t)
|
|
|
|
defer back()
|
|
|
|
gitInit(t)
|
|
|
|
gitCommit(t, "commit3")
|
|
|
|
gitTag(t, "v0.0.1")
|
|
|
|
gitCommit(t, "commit4")
|
|
|
|
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)
|
|
|
|
assert.Error(err)
|
2017-04-15 22:04:26 +02:00
|
|
|
assert.Contains(err.Error(), "git tag v0.0.1 was not made against commit")
|
2017-04-15 21:11:47 +02:00
|
|
|
}
|
|
|
|
|
2017-04-18 18:10:13 +02:00
|
|
|
func TestNoValidate(t *testing.T) {
|
|
|
|
var assert = assert.New(t)
|
|
|
|
_, back := createAndChdir(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 22:31:09 +02:00
|
|
|
func TestChangelog(t *testing.T) {
|
|
|
|
var assert = assert.New(t)
|
|
|
|
_, back := createAndChdir(t)
|
|
|
|
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")
|
|
|
|
assert.Contains(ctx.ReleaseNotes, "added feature 1")
|
|
|
|
assert.Contains(ctx.ReleaseNotes, "fixed bug 2")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCustomReleaseNotes(t *testing.T) {
|
|
|
|
var assert = assert.New(t)
|
|
|
|
_, back := createAndChdir(t)
|
|
|
|
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 19:31:55 +02:00
|
|
|
//
|
|
|
|
// helper functions
|
|
|
|
//
|
|
|
|
|
2017-04-15 19:00:49 +02:00
|
|
|
func createAndChdir(t *testing.T) (current string, back func()) {
|
|
|
|
var assert = assert.New(t)
|
2017-04-15 19:18:17 +02:00
|
|
|
folder, err := ioutil.TempDir("", "goreleasertest")
|
2017-04-15 19:00:49 +02:00
|
|
|
assert.NoError(err)
|
|
|
|
previous, err := os.Getwd()
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.NoError(os.Chdir(folder))
|
|
|
|
return folder, func() {
|
|
|
|
assert.NoError(os.Chdir(previous))
|
|
|
|
}
|
2017-04-15 18:05:54 +02:00
|
|
|
}
|
2017-04-15 19:31:55 +02:00
|
|
|
|
|
|
|
func gitInit(t *testing.T) {
|
|
|
|
var assert = assert.New(t)
|
|
|
|
out, err := git("init")
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Contains(out, "Initialized empty Git repository")
|
|
|
|
}
|
|
|
|
|
|
|
|
func gitCommit(t *testing.T, msg string) {
|
|
|
|
var assert = assert.New(t)
|
2017-04-15 19:38:54 +02:00
|
|
|
out, err := fakeGit("commit", "--allow-empty", "-m", msg)
|
2017-04-15 19:31:55 +02:00
|
|
|
assert.NoError(err)
|
|
|
|
assert.Contains(out, "master", msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func gitTag(t *testing.T, tag string) {
|
|
|
|
var assert = assert.New(t)
|
2017-04-15 19:38:54 +02:00
|
|
|
out, err := fakeGit("tag", tag)
|
2017-04-15 19:31:55 +02:00
|
|
|
assert.NoError(err)
|
|
|
|
assert.Empty(out)
|
|
|
|
}
|
|
|
|
|
2017-04-15 21:11:47 +02:00
|
|
|
func gitAdd(t *testing.T) {
|
2017-04-15 19:31:55 +02:00
|
|
|
var assert = assert.New(t)
|
2017-04-15 21:11:47 +02:00
|
|
|
out, err := git("add", "-A")
|
2017-04-15 19:31:55 +02:00
|
|
|
assert.NoError(err)
|
2017-04-15 21:11:47 +02:00
|
|
|
assert.Empty(out)
|
2017-04-15 19:31:55 +02:00
|
|
|
}
|
2017-04-15 19:38:54 +02: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...)
|
|
|
|
}
|