1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-16 03:52:12 +02:00

git on testlib

This commit is contained in:
Carlos Alexandro Becker 2017-07-23 16:42:09 -03:00
parent 57753af876
commit f643b33bda
No known key found for this signature in database
GPG Key ID: E61E2F7DC14AB940
4 changed files with 137 additions and 111 deletions

View File

@ -3,12 +3,12 @@ package goreleaserlib
import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"testing"
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/stretchr/testify/assert"
yaml "gopkg.in/yaml.v2"
)
@ -176,30 +176,17 @@ func setup(t *testing.T) (current string, back func()) {
previous, err := os.Getwd()
assert.NoError(err)
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)
createMainGo(t)
for _, cmd := range gitCmds {
var args = []string{
"-c",
"user.name='GoReleaser'",
"-c",
"user.email='test@goreleaser.github.com'",
}
args = append(args, cmd...)
assert.NoError(exec.Command("git", args...).Run())
}
testlib.GitInit(t)
testlib.GitAdd(t)
testlib.GitCommit(t, "asdf")
testlib.GitTag(t, "v0.0.1")
testlib.GitCommit(t, "asas89d")
testlib.GitCommit(t, "assssf")
testlib.GitCommit(t, "assd")
testlib.GitTag(t, "v0.0.2")
testlib.GitRemoteAdd(t, "git@github.com:goreleaser/fake.git")
return folder, func() {
assert.NoError(os.Chdir(previous))
}

69
internal/testlib/git.go Normal file
View 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
}

View 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")
}

View File

@ -31,9 +31,9 @@ func TestSingleCommit(t *testing.T) {
var assert = assert.New(t)
_, back := testlib.Mktmp(t)
defer back()
gitInit(t)
gitCommit(t, "commit1")
gitTag(t, "v0.0.1")
testlib.GitInit(t)
testlib.GitCommit(t, "commit1")
testlib.GitTag(t, "v0.0.1")
var ctx = &context.Context{
Config: config.Project{},
}
@ -45,7 +45,7 @@ func TestNewRepository(t *testing.T) {
var assert = assert.New(t)
_, back := testlib.Mktmp(t)
defer back()
gitInit(t)
testlib.GitInit(t)
var ctx = &context.Context{
Config: config.Project{},
}
@ -56,8 +56,8 @@ func TestNoTagsSnapshot(t *testing.T) {
assert := assert.New(t)
_, back := testlib.Mktmp(t)
defer back()
gitInit(t)
gitCommit(t, "first")
testlib.GitInit(t)
testlib.GitCommit(t, "first")
var ctx = &context.Context{
Config: config.Project{
Snapshot: config.Snapshot{
@ -75,8 +75,8 @@ func TestNoTagsSnapshotInvalidTemplate(t *testing.T) {
assert := assert.New(t)
_, back := testlib.Mktmp(t)
defer back()
gitInit(t)
gitCommit(t, "first")
testlib.GitInit(t)
testlib.GitCommit(t, "first")
var ctx = &context.Context{
Config: config.Project{
Snapshot: config.Snapshot{
@ -96,8 +96,8 @@ func TestNoTagsNoSnapshot(t *testing.T) {
assert := assert.New(t)
_, back := testlib.Mktmp(t)
defer back()
gitInit(t)
gitCommit(t, "first")
testlib.GitInit(t)
testlib.GitCommit(t, "first")
var ctx = &context.Context{
Config: config.Project{
Snapshot: config.Snapshot{
@ -114,9 +114,9 @@ func TestInvalidTagFormat(t *testing.T) {
var assert = assert.New(t)
_, back := testlib.Mktmp(t)
defer back()
gitInit(t)
gitCommit(t, "commit2")
gitTag(t, "sadasd")
testlib.GitInit(t)
testlib.GitCommit(t, "commit2")
testlib.GitTag(t, "sadasd")
var ctx = &context.Context{
Config: config.Project{},
Validate: true,
@ -129,12 +129,12 @@ func TestDirty(t *testing.T) {
var assert = assert.New(t)
folder, back := testlib.Mktmp(t)
defer back()
gitInit(t)
testlib.GitInit(t)
dummy, err := os.Create(filepath.Join(folder, "dummy"))
assert.NoError(err)
gitAdd(t)
gitCommit(t, "commit2")
gitTag(t, "v0.0.1")
testlib.GitAdd(t)
testlib.GitCommit(t, "commit2")
testlib.GitTag(t, "v0.0.1")
assert.NoError(ioutil.WriteFile(dummy.Name(), []byte("lorem ipsum"), 0644))
var ctx = &context.Context{
Config: config.Project{},
@ -149,10 +149,10 @@ func TestTagIsNotLastCommit(t *testing.T) {
var assert = assert.New(t)
_, back := testlib.Mktmp(t)
defer back()
gitInit(t)
gitCommit(t, "commit3")
gitTag(t, "v0.0.1")
gitCommit(t, "commit4")
testlib.GitInit(t)
testlib.GitCommit(t, "commit3")
testlib.GitTag(t, "v0.0.1")
testlib.GitCommit(t, "commit4")
var ctx = &context.Context{
Config: config.Project{},
Validate: true,
@ -166,11 +166,11 @@ func TestValidState(t *testing.T) {
var assert = assert.New(t)
_, back := testlib.Mktmp(t)
defer back()
gitInit(t)
gitCommit(t, "commit3")
gitTag(t, "v0.0.1")
gitCommit(t, "commit4")
gitTag(t, "v0.0.2")
testlib.GitInit(t)
testlib.GitCommit(t, "commit3")
testlib.GitTag(t, "v0.0.1")
testlib.GitCommit(t, "commit4")
testlib.GitTag(t, "v0.0.2")
var ctx = &context.Context{
Config: config.Project{},
Validate: true,
@ -185,11 +185,11 @@ func TestNoValidate(t *testing.T) {
var assert = assert.New(t)
_, back := testlib.Mktmp(t)
defer back()
gitInit(t)
gitAdd(t)
gitCommit(t, "commit5")
gitTag(t, "v0.0.1")
gitCommit(t, "commit6")
testlib.GitInit(t)
testlib.GitAdd(t)
testlib.GitCommit(t, "commit5")
testlib.GitTag(t, "v0.0.1")
testlib.GitCommit(t, "commit6")
var ctx = &context.Context{
Config: config.Project{},
Validate: false,
@ -201,12 +201,12 @@ func TestChangelog(t *testing.T) {
var assert = assert.New(t)
_, back := testlib.Mktmp(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")
testlib.GitInit(t)
testlib.GitCommit(t, "first")
testlib.GitTag(t, "v0.0.1")
testlib.GitCommit(t, "added feature 1")
testlib.GitCommit(t, "fixed bug 2")
testlib.GitTag(t, "v0.0.2")
var ctx = &context.Context{
Config: config.Project{},
}
@ -222,7 +222,7 @@ func TestChangelogOfFirstRelease(t *testing.T) {
var assert = assert.New(t)
_, back := testlib.Mktmp(t)
defer back()
gitInit(t)
testlib.GitInit(t)
var msgs = []string{
"initial commit",
"another one",
@ -230,9 +230,9 @@ func TestChangelogOfFirstRelease(t *testing.T) {
"and finally this one",
}
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{
Config: config.Project{},
}
@ -248,9 +248,9 @@ func TestCustomReleaseNotes(t *testing.T) {
var assert = assert.New(t)
_, back := testlib.Mktmp(t)
defer back()
gitInit(t)
gitCommit(t, "first")
gitTag(t, "v0.0.1")
testlib.GitInit(t)
testlib.GitCommit(t, "first")
testlib.GitTag(t, "v0.0.1")
var ctx = &context.Context{
Config: config.Project{},
ReleaseNotes: "custom",
@ -259,48 +259,3 @@ func TestCustomReleaseNotes(t *testing.T) {
assert.Equal("v0.0.1", ctx.Git.CurrentTag)
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...)
}