2017-07-23 21:42:09 +02:00
|
|
|
package testlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2020-07-06 22:09:22 +02:00
|
|
|
"time"
|
2017-07-23 21:42:09 +02:00
|
|
|
|
2017-08-19 17:47:04 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/git"
|
2020-10-06 14:48:04 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-07-23 21:42:09 +02:00
|
|
|
)
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// GitInit inits a new git project.
|
2021-03-01 19:45:06 +02:00
|
|
|
func GitInit(tb testing.TB) {
|
|
|
|
tb.Helper()
|
2021-04-25 04:21:54 +02:00
|
|
|
out, err := fakeGit("init")
|
2021-03-01 19:45:06 +02:00
|
|
|
require.NoError(tb, err)
|
|
|
|
require.Contains(tb, out, "Initialized empty Git repository")
|
|
|
|
require.NoError(tb, err)
|
2021-04-25 04:21:54 +02:00
|
|
|
GitCheckoutBranch(tb, "main")
|
|
|
|
_, _ = fakeGit("branch", "-D", "master")
|
2017-07-23 21:42:09 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// GitRemoteAdd adds the given url as remote.
|
2021-03-01 19:45:06 +02:00
|
|
|
func GitRemoteAdd(tb testing.TB, url string) {
|
|
|
|
tb.Helper()
|
2017-07-23 21:42:09 +02:00
|
|
|
out, err := fakeGit("remote", "add", "origin", url)
|
2021-03-01 19:45:06 +02:00
|
|
|
require.NoError(tb, err)
|
|
|
|
require.Empty(tb, out)
|
2017-07-23 21:42:09 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// GitCommit creates a git commits.
|
2021-03-01 19:45:06 +02:00
|
|
|
func GitCommit(tb testing.TB, msg string) {
|
|
|
|
tb.Helper()
|
|
|
|
GitCommitWithDate(tb, msg, time.Time{})
|
2020-07-06 22:09:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GitCommitWithDate creates a git commit with a commit date.
|
2021-03-01 19:45:06 +02:00
|
|
|
func GitCommitWithDate(tb testing.TB, msg string, commitDate time.Time) {
|
|
|
|
tb.Helper()
|
2020-07-06 22:09:22 +02:00
|
|
|
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)
|
2021-03-01 19:45:06 +02:00
|
|
|
require.NoError(tb, err)
|
|
|
|
require.Contains(tb, out, "main", msg)
|
2017-07-23 21:42:09 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// GitTag creates a git tag.
|
2021-03-01 19:45:06 +02:00
|
|
|
func GitTag(tb testing.TB, tag string) {
|
|
|
|
tb.Helper()
|
2017-07-23 21:42:09 +02:00
|
|
|
out, err := fakeGit("tag", tag)
|
2021-03-01 19:45:06 +02:00
|
|
|
require.NoError(tb, err)
|
|
|
|
require.Empty(tb, out)
|
2017-07-23 21:42:09 +02:00
|
|
|
}
|
|
|
|
|
2020-08-14 15:12:55 +02:00
|
|
|
// GitBranch creates a git branch.
|
2021-03-01 19:45:06 +02:00
|
|
|
func GitBranch(tb testing.TB, branch string) {
|
|
|
|
tb.Helper()
|
2020-08-14 15:12:55 +02:00
|
|
|
out, err := fakeGit("branch", branch)
|
2021-03-01 19:45:06 +02:00
|
|
|
require.NoError(tb, err)
|
|
|
|
require.Empty(tb, out)
|
2020-08-14 15:12:55 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// GitAdd adds all files to stage.
|
2021-03-01 19:45:06 +02:00
|
|
|
func GitAdd(tb testing.TB) {
|
|
|
|
tb.Helper()
|
2017-08-19 17:47:04 +02:00
|
|
|
out, err := fakeGit("add", "-A")
|
2021-03-01 19:45:06 +02:00
|
|
|
require.NoError(tb, err)
|
|
|
|
require.Empty(tb, out)
|
2017-07-23 21:42:09 +02:00
|
|
|
}
|
|
|
|
|
2020-07-06 22:09:22 +02:00
|
|
|
func fakeGitEnv(env map[string]string, args ...string) (string, error) {
|
2021-03-01 19:45:06 +02:00
|
|
|
allArgs := []string{
|
2017-07-23 21:42:09 +02:00
|
|
|
"-c", "user.name='GoReleaser'",
|
|
|
|
"-c", "user.email='test@goreleaser.github.com'",
|
|
|
|
"-c", "commit.gpgSign=false",
|
2019-02-06 21:14:04 +02:00
|
|
|
"-c", "log.showSignature=false",
|
2017-07-23 21:42:09 +02:00
|
|
|
}
|
|
|
|
allArgs = append(allArgs, args...)
|
2020-07-06 22:09:22 +02:00
|
|
|
return git.RunEnv(env, allArgs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func fakeGit(args ...string) (string, error) {
|
|
|
|
return fakeGitEnv(nil, args...)
|
2017-07-23 21:42:09 +02:00
|
|
|
}
|
2018-10-05 21:18:39 +02:00
|
|
|
|
|
|
|
// GitCheckoutBranch allows us to change the active branch that we're using.
|
2021-03-01 19:45:06 +02:00
|
|
|
func GitCheckoutBranch(tb testing.TB, name string) {
|
|
|
|
tb.Helper()
|
2020-08-14 15:12:55 +02:00
|
|
|
out, err := fakeGit("checkout", "-b", name)
|
2021-03-01 19:45:06 +02:00
|
|
|
require.NoError(tb, err)
|
|
|
|
require.Empty(tb, out)
|
2018-10-05 21:18:39 +02:00
|
|
|
}
|