2020-07-09 22:40:37 +02:00
|
|
|
package git_test
|
2017-08-19 17:47:04 +02:00
|
|
|
|
|
|
|
import (
|
2022-04-12 13:35:19 +02:00
|
|
|
"context"
|
2017-10-16 19:43:26 +02:00
|
|
|
"os"
|
2017-08-19 17:47:04 +02:00
|
|
|
"testing"
|
|
|
|
|
2020-07-09 22:40:37 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/git"
|
2020-08-14 15:12:55 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/testlib"
|
2020-10-06 14:48:04 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-08-19 17:47:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGit(t *testing.T) {
|
2022-04-12 13:35:19 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
out, err := git.Run(ctx, "status")
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, out)
|
2017-08-19 17:47:04 +02:00
|
|
|
|
2022-04-12 13:35:19 +02:00
|
|
|
out, err = git.Run(ctx, "command-that-dont-exist")
|
2020-10-06 14:48:04 +02:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Empty(t, out)
|
|
|
|
require.Equal(
|
2017-09-27 00:24:49 +02:00
|
|
|
t,
|
2017-08-19 17:47:04 +02:00
|
|
|
"git: 'command-that-dont-exist' is not a git command. See 'git --help'.\n",
|
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
}
|
2017-10-16 19:43:26 +02:00
|
|
|
|
2020-08-14 15:12:55 +02:00
|
|
|
func TestGitWarning(t *testing.T) {
|
2022-04-12 13:35:19 +02:00
|
|
|
ctx := context.Background()
|
2020-12-12 18:27:35 +02:00
|
|
|
testlib.Mktmp(t)
|
2020-08-14 15:12:55 +02:00
|
|
|
testlib.GitInit(t)
|
|
|
|
testlib.GitCommit(t, "foo")
|
|
|
|
testlib.GitBranch(t, "tags/1.2.2")
|
|
|
|
testlib.GitTag(t, "1.2.2")
|
|
|
|
testlib.GitCommit(t, "foobar")
|
|
|
|
testlib.GitBranch(t, "tags/1.2.3")
|
|
|
|
testlib.GitTag(t, "1.2.3")
|
2022-10-14 04:59:20 +02:00
|
|
|
testlib.GitTag(t, "nightly")
|
2020-08-14 15:12:55 +02:00
|
|
|
|
2022-04-12 13:35:19 +02:00
|
|
|
out, err := git.Run(ctx, "describe", "--tags", "--abbrev=0", "tags/1.2.3^")
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "1.2.2\n", out)
|
2022-10-14 04:59:20 +02:00
|
|
|
|
|
|
|
tags, err := git.CleanAllLines(git.Run(ctx, "tag", "--points-at", "HEAD", "--sort", "-version:refname"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.ElementsMatch(t, []string{"1.2.3", "nightly"}, tags)
|
2020-08-14 15:12:55 +02:00
|
|
|
}
|
|
|
|
|
2017-10-16 19:43:26 +02:00
|
|
|
func TestRepo(t *testing.T) {
|
2022-04-12 13:35:19 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
require.True(t, git.IsRepo(ctx), "goreleaser folder should be a git repo")
|
2017-10-16 19:43:26 +02:00
|
|
|
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, os.Chdir(os.TempDir()))
|
2022-04-12 13:35:19 +02:00
|
|
|
require.False(t, git.IsRepo(ctx), os.TempDir()+" folder should be a git repo")
|
2017-10-16 19:43:26 +02:00
|
|
|
}
|
2017-10-16 00:47:52 +02:00
|
|
|
|
|
|
|
func TestClean(t *testing.T) {
|
2022-04-12 13:35:19 +02:00
|
|
|
ctx := context.Background()
|
2018-02-26 01:17:45 +02:00
|
|
|
|
2022-10-14 04:59:20 +02:00
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
out, err := git.Clean("asdasd 'ssadas'\nadasd", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "asdasd ssadas", out)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error", func(t *testing.T) {
|
|
|
|
out, err := git.Clean(git.Run(ctx, "command-that-dont-exist"))
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Empty(t, out)
|
|
|
|
require.Equal(
|
|
|
|
t,
|
|
|
|
"git: 'command-that-dont-exist' is not a git command. See 'git --help'.",
|
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("all lines error", func(t *testing.T) {
|
|
|
|
out, err := git.CleanAllLines(git.Run(ctx, "command-that-dont-exist"))
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Empty(t, out)
|
|
|
|
require.Equal(
|
|
|
|
t,
|
|
|
|
"git: 'command-that-dont-exist' is not a git command. See 'git --help'.",
|
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
})
|
2017-10-16 00:47:52 +02:00
|
|
|
}
|