github.com/goreleaser/goreleaser@v1.25.1/internal/git/git_test.go (about) 1 package git_test 2 3 import ( 4 "context" 5 "os" 6 "testing" 7 8 "github.com/goreleaser/goreleaser/internal/git" 9 "github.com/goreleaser/goreleaser/internal/testlib" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestGit(t *testing.T) { 14 ctx := context.Background() 15 out, err := git.Run(ctx, "status") 16 require.NoError(t, err) 17 require.NotEmpty(t, out) 18 19 out, err = git.Run(ctx, "command-that-dont-exist") 20 require.EqualError(t, err, "git: 'command-that-dont-exist' is not a git command. See 'git --help'.\n") 21 require.Empty(t, out) 22 } 23 24 func TestGitWarning(t *testing.T) { 25 ctx := context.Background() 26 testlib.Mktmp(t) 27 testlib.GitInit(t) 28 testlib.GitCommit(t, "foo") 29 testlib.GitBranch(t, "tags/1.2.2") 30 testlib.GitTag(t, "1.2.2") 31 testlib.GitCommit(t, "foobar") 32 testlib.GitBranch(t, "tags/1.2.3") 33 testlib.GitTag(t, "1.2.3") 34 testlib.GitTag(t, "nightly") 35 36 out, err := git.Run(ctx, "describe", "--tags", "--abbrev=0", "tags/1.2.3^") 37 require.NoError(t, err) 38 require.Equal(t, "1.2.2\n", out) 39 40 tags, err := git.CleanAllLines(git.Run(ctx, "tag", "--points-at", "HEAD", "--sort", "-version:refname")) 41 require.NoError(t, err) 42 require.ElementsMatch(t, []string{"1.2.3", "nightly"}, tags) 43 } 44 45 func TestRepo(t *testing.T) { 46 ctx := context.Background() 47 require.True(t, git.IsRepo(ctx), "goreleaser folder should be a git repo") 48 49 require.NoError(t, os.Chdir(os.TempDir())) 50 require.False(t, git.IsRepo(ctx), os.TempDir()+" folder should be a git repo") 51 } 52 53 func TestClean(t *testing.T) { 54 ctx := context.Background() 55 56 t.Run("success", func(t *testing.T) { 57 out, err := git.Clean("asdasd 'ssadas'\nadasd", nil) 58 require.NoError(t, err) 59 require.Equal(t, "asdasd ssadas", out) 60 }) 61 62 t.Run("error", func(t *testing.T) { 63 out, err := git.Clean(git.Run(ctx, "command-that-dont-exist")) 64 require.EqualError(t, err, "git: 'command-that-dont-exist' is not a git command. See 'git --help'.") 65 require.Empty(t, out) 66 }) 67 68 t.Run("all lines error", func(t *testing.T) { 69 out, err := git.CleanAllLines(git.Run(ctx, "command-that-dont-exist")) 70 require.EqualError(t, err, "git: 'command-that-dont-exist' is not a git command. See 'git --help'.") 71 require.Empty(t, out) 72 }) 73 }