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