1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-24 04:16:27 +02:00

86 lines
2.2 KiB
Go
Raw Normal View History

2017-07-23 16:42:09 -03:00
package testlib
import (
"testing"
"time"
2017-07-23 16:42:09 -03:00
2017-08-19 12:47:04 -03:00
"github.com/goreleaser/goreleaser/internal/git"
"github.com/stretchr/testify/require"
2017-07-23 16:42:09 -03:00
)
// GitInit inits a new git project.
func GitInit(t testing.TB) {
out, err := fakeGit("init", "-b", "main")
require.NoError(t, err)
require.Contains(t, out, "Initialized empty Git repository")
require.NoError(t, err)
2017-07-23 16:42:09 -03:00
}
// GitRemoteAdd adds the given url as remote.
func GitRemoteAdd(t testing.TB, url string) {
2017-07-23 16:42:09 -03:00
out, err := fakeGit("remote", "add", "origin", url)
require.NoError(t, err)
require.Empty(t, out)
2017-07-23 16:42:09 -03:00
}
// GitCommit creates a git commits.
func GitCommit(t testing.TB, msg string) {
GitCommitWithDate(t, msg, time.Time{})
}
// GitCommitWithDate creates a git commit with a commit date.
func GitCommitWithDate(t testing.TB, msg string, commitDate time.Time) {
env := (map[string]string)(nil)
if !commitDate.IsZero() {
env = map[string]string{
"GIT_COMMITTER_DATE": commitDate.Format(time.RFC1123Z),
}
}
out, err := fakeGitEnv(env, "commit", "--allow-empty", "-m", msg)
require.NoError(t, err)
require.Contains(t, out, "main", msg)
2017-07-23 16:42:09 -03:00
}
// GitTag creates a git tag.
func GitTag(t testing.TB, tag string) {
2017-07-23 16:42:09 -03:00
out, err := fakeGit("tag", tag)
require.NoError(t, err)
require.Empty(t, out)
2017-07-23 16:42:09 -03:00
}
// GitBranch creates a git branch.
func GitBranch(t testing.TB, branch string) {
out, err := fakeGit("branch", branch)
require.NoError(t, err)
require.Empty(t, out)
}
// GitAdd adds all files to stage.
func GitAdd(t testing.TB) {
2017-08-19 12:47:04 -03:00
out, err := fakeGit("add", "-A")
require.NoError(t, err)
require.Empty(t, out)
2017-07-23 16:42:09 -03:00
}
func fakeGitEnv(env map[string]string, args ...string) (string, error) {
2017-07-23 16:42:09 -03:00
var allArgs = []string{
"-c", "user.name='GoReleaser'",
"-c", "user.email='test@goreleaser.github.com'",
"-c", "commit.gpgSign=false",
"-c", "log.showSignature=false",
2017-07-23 16:42:09 -03:00
}
allArgs = append(allArgs, args...)
return git.RunEnv(env, allArgs...)
}
func fakeGit(args ...string) (string, error) {
return fakeGitEnv(nil, args...)
2017-07-23 16:42:09 -03:00
}
// GitCheckoutBranch allows us to change the active branch that we're using.
func GitCheckoutBranch(t testing.TB, name string) {
out, err := fakeGit("checkout", "-b", name)
require.NoError(t, err)
require.Empty(t, out)
}