You've already forked goreleaser
mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-09-16 09:26:52 +02:00
git on testlib
This commit is contained in:
@@ -3,12 +3,12 @@ package goreleaserlib
|
|||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/goreleaser/goreleaser/config"
|
"github.com/goreleaser/goreleaser/config"
|
||||||
|
"github.com/goreleaser/goreleaser/internal/testlib"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
yaml "gopkg.in/yaml.v2"
|
yaml "gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
@@ -176,30 +176,17 @@ func setup(t *testing.T) (current string, back func()) {
|
|||||||
previous, err := os.Getwd()
|
previous, err := os.Getwd()
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
assert.NoError(os.Chdir(folder))
|
assert.NoError(os.Chdir(folder))
|
||||||
var gitCmds = [][]string{
|
|
||||||
{"init"},
|
|
||||||
{"config", "commit.gpgSign", "false"},
|
|
||||||
{"add", "-A"},
|
|
||||||
{"commit", "--allow-empty", "-m", "asdf"},
|
|
||||||
{"tag", "v0.0.1"},
|
|
||||||
{"commit", "--allow-empty", "-m", "asas89d"},
|
|
||||||
{"commit", "--allow-empty", "-m", "assssf"},
|
|
||||||
{"commit", "--allow-empty", "-m", "assd"},
|
|
||||||
{"tag", "v0.0.2"},
|
|
||||||
{"remote", "add", "origin", "git@github.com:goreleaser/fake.git"},
|
|
||||||
}
|
|
||||||
createGoreleaserYaml(t)
|
createGoreleaserYaml(t)
|
||||||
createMainGo(t)
|
createMainGo(t)
|
||||||
for _, cmd := range gitCmds {
|
testlib.GitInit(t)
|
||||||
var args = []string{
|
testlib.GitAdd(t)
|
||||||
"-c",
|
testlib.GitCommit(t, "asdf")
|
||||||
"user.name='GoReleaser'",
|
testlib.GitTag(t, "v0.0.1")
|
||||||
"-c",
|
testlib.GitCommit(t, "asas89d")
|
||||||
"user.email='test@goreleaser.github.com'",
|
testlib.GitCommit(t, "assssf")
|
||||||
}
|
testlib.GitCommit(t, "assd")
|
||||||
args = append(args, cmd...)
|
testlib.GitTag(t, "v0.0.2")
|
||||||
assert.NoError(exec.Command("git", args...).Run())
|
testlib.GitRemoteAdd(t, "git@github.com:goreleaser/fake.git")
|
||||||
}
|
|
||||||
return folder, func() {
|
return folder, func() {
|
||||||
assert.NoError(os.Chdir(previous))
|
assert.NoError(os.Chdir(previous))
|
||||||
}
|
}
|
||||||
|
69
internal/testlib/git.go
Normal file
69
internal/testlib/git.go
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
package testlib
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"os/exec"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GitInit inits a new git project
|
||||||
|
func GitInit(t *testing.T) {
|
||||||
|
var assert = assert.New(t)
|
||||||
|
out, err := git("init")
|
||||||
|
assert.NoError(err)
|
||||||
|
assert.Contains(out, "Initialized empty Git repository")
|
||||||
|
assert.NoError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GitRemoteAdd adds the given url as remote
|
||||||
|
func GitRemoteAdd(t *testing.T, url string) {
|
||||||
|
var assert = assert.New(t)
|
||||||
|
out, err := fakeGit("remote", "add", "origin", url)
|
||||||
|
assert.NoError(err)
|
||||||
|
assert.Empty(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GitCommit creates a git commits
|
||||||
|
func GitCommit(t *testing.T, msg string) {
|
||||||
|
var assert = assert.New(t)
|
||||||
|
out, err := fakeGit("commit", "--allow-empty", "-m", msg)
|
||||||
|
assert.NoError(err)
|
||||||
|
assert.Contains(out, "master", msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GitTag creates a git tag
|
||||||
|
func GitTag(t *testing.T, tag string) {
|
||||||
|
var assert = assert.New(t)
|
||||||
|
out, err := fakeGit("tag", tag)
|
||||||
|
assert.NoError(err)
|
||||||
|
assert.Empty(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GitAdd adds all files to stage
|
||||||
|
func GitAdd(t *testing.T) {
|
||||||
|
var assert = assert.New(t)
|
||||||
|
out, err := git("add", "-A")
|
||||||
|
assert.NoError(err)
|
||||||
|
assert.Empty(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func fakeGit(args ...string) (string, error) {
|
||||||
|
var allArgs = []string{
|
||||||
|
"-c", "user.name='GoReleaser'",
|
||||||
|
"-c", "user.email='test@goreleaser.github.com'",
|
||||||
|
"-c", "commit.gpgSign=false",
|
||||||
|
}
|
||||||
|
allArgs = append(allArgs, args...)
|
||||||
|
return git(allArgs...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func git(args ...string) (output string, err error) {
|
||||||
|
var cmd = exec.Command("git", args...)
|
||||||
|
bts, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return "", errors.New(string(bts))
|
||||||
|
}
|
||||||
|
return string(bts), err
|
||||||
|
}
|
15
internal/testlib/git_test.go
Normal file
15
internal/testlib/git_test.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package testlib
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGit(t *testing.T) {
|
||||||
|
_, back := Mktmp(t)
|
||||||
|
defer back()
|
||||||
|
GitInit(t)
|
||||||
|
GitAdd(t)
|
||||||
|
GitCommit(t, "commit1")
|
||||||
|
GitRemoteAdd(t, "git@github.com:goreleaser/nope.git")
|
||||||
|
GitTag(t, "v1.0.0")
|
||||||
|
}
|
@@ -31,9 +31,9 @@ func TestSingleCommit(t *testing.T) {
|
|||||||
var assert = assert.New(t)
|
var assert = assert.New(t)
|
||||||
_, back := testlib.Mktmp(t)
|
_, back := testlib.Mktmp(t)
|
||||||
defer back()
|
defer back()
|
||||||
gitInit(t)
|
testlib.GitInit(t)
|
||||||
gitCommit(t, "commit1")
|
testlib.GitCommit(t, "commit1")
|
||||||
gitTag(t, "v0.0.1")
|
testlib.GitTag(t, "v0.0.1")
|
||||||
var ctx = &context.Context{
|
var ctx = &context.Context{
|
||||||
Config: config.Project{},
|
Config: config.Project{},
|
||||||
}
|
}
|
||||||
@@ -45,7 +45,7 @@ func TestNewRepository(t *testing.T) {
|
|||||||
var assert = assert.New(t)
|
var assert = assert.New(t)
|
||||||
_, back := testlib.Mktmp(t)
|
_, back := testlib.Mktmp(t)
|
||||||
defer back()
|
defer back()
|
||||||
gitInit(t)
|
testlib.GitInit(t)
|
||||||
var ctx = &context.Context{
|
var ctx = &context.Context{
|
||||||
Config: config.Project{},
|
Config: config.Project{},
|
||||||
}
|
}
|
||||||
@@ -56,8 +56,8 @@ func TestNoTagsSnapshot(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
_, back := testlib.Mktmp(t)
|
_, back := testlib.Mktmp(t)
|
||||||
defer back()
|
defer back()
|
||||||
gitInit(t)
|
testlib.GitInit(t)
|
||||||
gitCommit(t, "first")
|
testlib.GitCommit(t, "first")
|
||||||
var ctx = &context.Context{
|
var ctx = &context.Context{
|
||||||
Config: config.Project{
|
Config: config.Project{
|
||||||
Snapshot: config.Snapshot{
|
Snapshot: config.Snapshot{
|
||||||
@@ -75,8 +75,8 @@ func TestNoTagsSnapshotInvalidTemplate(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
_, back := testlib.Mktmp(t)
|
_, back := testlib.Mktmp(t)
|
||||||
defer back()
|
defer back()
|
||||||
gitInit(t)
|
testlib.GitInit(t)
|
||||||
gitCommit(t, "first")
|
testlib.GitCommit(t, "first")
|
||||||
var ctx = &context.Context{
|
var ctx = &context.Context{
|
||||||
Config: config.Project{
|
Config: config.Project{
|
||||||
Snapshot: config.Snapshot{
|
Snapshot: config.Snapshot{
|
||||||
@@ -96,8 +96,8 @@ func TestNoTagsNoSnapshot(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
_, back := testlib.Mktmp(t)
|
_, back := testlib.Mktmp(t)
|
||||||
defer back()
|
defer back()
|
||||||
gitInit(t)
|
testlib.GitInit(t)
|
||||||
gitCommit(t, "first")
|
testlib.GitCommit(t, "first")
|
||||||
var ctx = &context.Context{
|
var ctx = &context.Context{
|
||||||
Config: config.Project{
|
Config: config.Project{
|
||||||
Snapshot: config.Snapshot{
|
Snapshot: config.Snapshot{
|
||||||
@@ -114,9 +114,9 @@ func TestInvalidTagFormat(t *testing.T) {
|
|||||||
var assert = assert.New(t)
|
var assert = assert.New(t)
|
||||||
_, back := testlib.Mktmp(t)
|
_, back := testlib.Mktmp(t)
|
||||||
defer back()
|
defer back()
|
||||||
gitInit(t)
|
testlib.GitInit(t)
|
||||||
gitCommit(t, "commit2")
|
testlib.GitCommit(t, "commit2")
|
||||||
gitTag(t, "sadasd")
|
testlib.GitTag(t, "sadasd")
|
||||||
var ctx = &context.Context{
|
var ctx = &context.Context{
|
||||||
Config: config.Project{},
|
Config: config.Project{},
|
||||||
Validate: true,
|
Validate: true,
|
||||||
@@ -129,12 +129,12 @@ func TestDirty(t *testing.T) {
|
|||||||
var assert = assert.New(t)
|
var assert = assert.New(t)
|
||||||
folder, back := testlib.Mktmp(t)
|
folder, back := testlib.Mktmp(t)
|
||||||
defer back()
|
defer back()
|
||||||
gitInit(t)
|
testlib.GitInit(t)
|
||||||
dummy, err := os.Create(filepath.Join(folder, "dummy"))
|
dummy, err := os.Create(filepath.Join(folder, "dummy"))
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
gitAdd(t)
|
testlib.GitAdd(t)
|
||||||
gitCommit(t, "commit2")
|
testlib.GitCommit(t, "commit2")
|
||||||
gitTag(t, "v0.0.1")
|
testlib.GitTag(t, "v0.0.1")
|
||||||
assert.NoError(ioutil.WriteFile(dummy.Name(), []byte("lorem ipsum"), 0644))
|
assert.NoError(ioutil.WriteFile(dummy.Name(), []byte("lorem ipsum"), 0644))
|
||||||
var ctx = &context.Context{
|
var ctx = &context.Context{
|
||||||
Config: config.Project{},
|
Config: config.Project{},
|
||||||
@@ -149,10 +149,10 @@ func TestTagIsNotLastCommit(t *testing.T) {
|
|||||||
var assert = assert.New(t)
|
var assert = assert.New(t)
|
||||||
_, back := testlib.Mktmp(t)
|
_, back := testlib.Mktmp(t)
|
||||||
defer back()
|
defer back()
|
||||||
gitInit(t)
|
testlib.GitInit(t)
|
||||||
gitCommit(t, "commit3")
|
testlib.GitCommit(t, "commit3")
|
||||||
gitTag(t, "v0.0.1")
|
testlib.GitTag(t, "v0.0.1")
|
||||||
gitCommit(t, "commit4")
|
testlib.GitCommit(t, "commit4")
|
||||||
var ctx = &context.Context{
|
var ctx = &context.Context{
|
||||||
Config: config.Project{},
|
Config: config.Project{},
|
||||||
Validate: true,
|
Validate: true,
|
||||||
@@ -166,11 +166,11 @@ func TestValidState(t *testing.T) {
|
|||||||
var assert = assert.New(t)
|
var assert = assert.New(t)
|
||||||
_, back := testlib.Mktmp(t)
|
_, back := testlib.Mktmp(t)
|
||||||
defer back()
|
defer back()
|
||||||
gitInit(t)
|
testlib.GitInit(t)
|
||||||
gitCommit(t, "commit3")
|
testlib.GitCommit(t, "commit3")
|
||||||
gitTag(t, "v0.0.1")
|
testlib.GitTag(t, "v0.0.1")
|
||||||
gitCommit(t, "commit4")
|
testlib.GitCommit(t, "commit4")
|
||||||
gitTag(t, "v0.0.2")
|
testlib.GitTag(t, "v0.0.2")
|
||||||
var ctx = &context.Context{
|
var ctx = &context.Context{
|
||||||
Config: config.Project{},
|
Config: config.Project{},
|
||||||
Validate: true,
|
Validate: true,
|
||||||
@@ -185,11 +185,11 @@ func TestNoValidate(t *testing.T) {
|
|||||||
var assert = assert.New(t)
|
var assert = assert.New(t)
|
||||||
_, back := testlib.Mktmp(t)
|
_, back := testlib.Mktmp(t)
|
||||||
defer back()
|
defer back()
|
||||||
gitInit(t)
|
testlib.GitInit(t)
|
||||||
gitAdd(t)
|
testlib.GitAdd(t)
|
||||||
gitCommit(t, "commit5")
|
testlib.GitCommit(t, "commit5")
|
||||||
gitTag(t, "v0.0.1")
|
testlib.GitTag(t, "v0.0.1")
|
||||||
gitCommit(t, "commit6")
|
testlib.GitCommit(t, "commit6")
|
||||||
var ctx = &context.Context{
|
var ctx = &context.Context{
|
||||||
Config: config.Project{},
|
Config: config.Project{},
|
||||||
Validate: false,
|
Validate: false,
|
||||||
@@ -201,12 +201,12 @@ func TestChangelog(t *testing.T) {
|
|||||||
var assert = assert.New(t)
|
var assert = assert.New(t)
|
||||||
_, back := testlib.Mktmp(t)
|
_, back := testlib.Mktmp(t)
|
||||||
defer back()
|
defer back()
|
||||||
gitInit(t)
|
testlib.GitInit(t)
|
||||||
gitCommit(t, "first")
|
testlib.GitCommit(t, "first")
|
||||||
gitTag(t, "v0.0.1")
|
testlib.GitTag(t, "v0.0.1")
|
||||||
gitCommit(t, "added feature 1")
|
testlib.GitCommit(t, "added feature 1")
|
||||||
gitCommit(t, "fixed bug 2")
|
testlib.GitCommit(t, "fixed bug 2")
|
||||||
gitTag(t, "v0.0.2")
|
testlib.GitTag(t, "v0.0.2")
|
||||||
var ctx = &context.Context{
|
var ctx = &context.Context{
|
||||||
Config: config.Project{},
|
Config: config.Project{},
|
||||||
}
|
}
|
||||||
@@ -222,7 +222,7 @@ func TestChangelogOfFirstRelease(t *testing.T) {
|
|||||||
var assert = assert.New(t)
|
var assert = assert.New(t)
|
||||||
_, back := testlib.Mktmp(t)
|
_, back := testlib.Mktmp(t)
|
||||||
defer back()
|
defer back()
|
||||||
gitInit(t)
|
testlib.GitInit(t)
|
||||||
var msgs = []string{
|
var msgs = []string{
|
||||||
"initial commit",
|
"initial commit",
|
||||||
"another one",
|
"another one",
|
||||||
@@ -230,9 +230,9 @@ func TestChangelogOfFirstRelease(t *testing.T) {
|
|||||||
"and finally this one",
|
"and finally this one",
|
||||||
}
|
}
|
||||||
for _, msg := range msgs {
|
for _, msg := range msgs {
|
||||||
gitCommit(t, msg)
|
testlib.GitCommit(t, msg)
|
||||||
}
|
}
|
||||||
gitTag(t, "v0.0.1")
|
testlib.GitTag(t, "v0.0.1")
|
||||||
var ctx = &context.Context{
|
var ctx = &context.Context{
|
||||||
Config: config.Project{},
|
Config: config.Project{},
|
||||||
}
|
}
|
||||||
@@ -248,9 +248,9 @@ func TestCustomReleaseNotes(t *testing.T) {
|
|||||||
var assert = assert.New(t)
|
var assert = assert.New(t)
|
||||||
_, back := testlib.Mktmp(t)
|
_, back := testlib.Mktmp(t)
|
||||||
defer back()
|
defer back()
|
||||||
gitInit(t)
|
testlib.GitInit(t)
|
||||||
gitCommit(t, "first")
|
testlib.GitCommit(t, "first")
|
||||||
gitTag(t, "v0.0.1")
|
testlib.GitTag(t, "v0.0.1")
|
||||||
var ctx = &context.Context{
|
var ctx = &context.Context{
|
||||||
Config: config.Project{},
|
Config: config.Project{},
|
||||||
ReleaseNotes: "custom",
|
ReleaseNotes: "custom",
|
||||||
@@ -259,48 +259,3 @@ func TestCustomReleaseNotes(t *testing.T) {
|
|||||||
assert.Equal("v0.0.1", ctx.Git.CurrentTag)
|
assert.Equal("v0.0.1", ctx.Git.CurrentTag)
|
||||||
assert.Equal(ctx.ReleaseNotes, "custom")
|
assert.Equal(ctx.ReleaseNotes, "custom")
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
func gitCommit(t *testing.T, msg string) {
|
|
||||||
var assert = assert.New(t)
|
|
||||||
out, err := fakeGit("commit", "--allow-empty", "-m", msg)
|
|
||||||
assert.NoError(err)
|
|
||||||
assert.Contains(out, "master", msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
func gitTag(t *testing.T, tag string) {
|
|
||||||
var assert = assert.New(t)
|
|
||||||
out, err := fakeGit("tag", tag)
|
|
||||||
assert.NoError(err)
|
|
||||||
assert.Empty(out)
|
|
||||||
}
|
|
||||||
|
|
||||||
func gitAdd(t *testing.T) {
|
|
||||||
var assert = assert.New(t)
|
|
||||||
out, err := git("add", "-A")
|
|
||||||
assert.NoError(err)
|
|
||||||
assert.Empty(out)
|
|
||||||
}
|
|
||||||
|
|
||||||
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...)
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user