From 5d8b6f046a7975c8f5726b0631f2cef71d0f44a7 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Fri, 14 Aug 2020 10:12:55 -0300 Subject: [PATCH] fix: ignore git warnings (#1740) * fix: ignore git warnings Signed-off-by: Carlos Alexandro Becker * test: added test case Signed-off-by: Carlos Alexandro Becker * fix: failing test Signed-off-by: Carlos Alexandro Becker * fix: logs Signed-off-by: Carlos Alexandro Becker * fix: lint issues Signed-off-by: Carlos Alexandro Becker --- internal/git/git.go | 19 +++++++++++++++---- internal/git/git_test.go | 17 +++++++++++++++++ internal/testlib/git.go | 13 ++++++++++--- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/internal/git/git.go b/internal/git/git.go index 4e7210187..fb86b7c2d 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -2,6 +2,7 @@ package git import ( + "bytes" "errors" "os/exec" "strings" @@ -32,14 +33,24 @@ func RunEnv(env map[string]string, args ...string) (string, error) { } } + stdout := bytes.Buffer{} + stderr := bytes.Buffer{} + + cmd.Stdout = &stdout + cmd.Stderr = &stderr + log.WithField("args", args).Debug("running git") - bts, err := cmd.CombinedOutput() - log.WithField("output", string(bts)). + err := cmd.Run() + + log.WithField("stdout", stdout.String()). + WithField("stderr", stderr.String()). Debug("git result") + if err != nil { - return "", errors.New(string(bts)) + return "", errors.New(stderr.String()) } - return string(bts), nil + + return stdout.String(), nil } // Run runs a git command and returns its output or errors. diff --git a/internal/git/git_test.go b/internal/git/git_test.go index 2e3d0a5f9..170b11289 100644 --- a/internal/git/git_test.go +++ b/internal/git/git_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/goreleaser/goreleaser/internal/git" + "github.com/goreleaser/goreleaser/internal/testlib" "github.com/stretchr/testify/assert" ) @@ -23,6 +24,22 @@ func TestGit(t *testing.T) { ) } +func TestGitWarning(t *testing.T) { + _, back := testlib.Mktmp(t) + defer back() + 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") + + out, err := git.Run("describe", "--tags", "--abbrev=0", "tags/1.2.3^") + assert.NoError(t, err) + assert.Equal(t, "1.2.2\n", out) +} + func TestRepo(t *testing.T) { assert.True(t, git.IsRepo(), "goreleaser folder should be a git repo") diff --git a/internal/testlib/git.go b/internal/testlib/git.go index 4407c4722..a950224f5 100644 --- a/internal/testlib/git.go +++ b/internal/testlib/git.go @@ -48,6 +48,13 @@ func GitTag(t *testing.T, tag string) { assert.Empty(t, out) } +// GitBranch creates a git branch. +func GitBranch(t *testing.T, branch string) { + out, err := fakeGit("branch", branch) + assert.NoError(t, err) + assert.Empty(t, out) +} + // GitAdd adds all files to stage. func GitAdd(t *testing.T) { out, err := fakeGit("add", "-A") @@ -71,8 +78,8 @@ func fakeGit(args ...string) (string, error) { } // GitCheckoutBranch allows us to change the active branch that we're using. -func GitCheckoutBranch(t *testing.T, tag string) { - out, err := fakeGit("checkout", "-b", tag) +func GitCheckoutBranch(t *testing.T, name string) { + out, err := fakeGit("checkout", "-b", name) assert.NoError(t, err) - assert.Contains(t, out, tag) + assert.Empty(t, out) }