2017-07-23 21:42:09 +02:00
|
|
|
package testlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-09-15 21:12:45 +02: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 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()
|
2021-11-28 05:22:44 +02:00
|
|
|
out, err := fakeGit("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
|
|
|
}
|
|
|
|
|
2021-12-06 04:23:15 +02:00
|
|
|
// GitAnnotatedTag creates an annotated tag.
|
|
|
|
func GitAnnotatedTag(tb testing.TB, tag, subject string) {
|
|
|
|
tb.Helper()
|
|
|
|
out, err := fakeGit("tag", "-a", tag, "-m", subject)
|
|
|
|
require.NoError(tb, err)
|
|
|
|
require.Empty(tb, out)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-28 05:22:44 +02:00
|
|
|
func fakeGit(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...)
|
2021-11-28 05:22:44 +02:00
|
|
|
return git.Run(allArgs...)
|
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
|
|
|
}
|