From b1588211654f16a6e55d534cc757b70fcc6bcb07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Henrique=20Guard=C3=A3o=20Gandarez?= Date: Thu, 13 May 2021 19:33:22 -0300 Subject: [PATCH] Do not check for errors in Latesttag and AncestorTag --- cmd/generate/generate.go | 14 ++++---------- cmd/generate/generate_test.go | 16 ++++++++-------- pkg/git/git.go | 20 ++++++++++---------- pkg/git/git_test.go | 12 ++++-------- 4 files changed, 26 insertions(+), 36 deletions(-) diff --git a/cmd/generate/generate.go b/cmd/generate/generate.go index aa0c0b5..cebf204 100644 --- a/cmd/generate/generate.go +++ b/cmd/generate/generate.go @@ -26,8 +26,8 @@ const tagDefault = "0.0.0" type gitClient interface { CurrentBranch() (string, error) IsRepo() bool - LatestTag() (string, error) - AncestorTag(include, exclude string) (string, error) + LatestTag() string + AncestorTag(include, exclude string) string SourceBranch(commitHash string) (string, error) } @@ -88,10 +88,7 @@ func Tag(params Params, gc gitClient) (Result, error) { log.Debugf("method: %q, version: %q", method, version) - latestTag, err := gc.LatestTag() - if err != nil { - return Result{}, fmt.Errorf("failed getting latest tag: %s", err) - } + latestTag := gc.LatestTag() var ( tag *semver.Version @@ -190,10 +187,7 @@ func Tag(params Params, gc gitClient) (Result, error) { finalTag = params.Prefix + tag.FinalizeVersion() } - ancestorTag, err = gc.AncestorTag(includePattern, excludePattern) - if err != nil { - return Result{}, fmt.Errorf("failed getting ancestor tag: %s", err) - } + ancestorTag = gc.AncestorTag(includePattern, excludePattern) return Result{ PreviousTag: previousTag, diff --git a/cmd/generate/generate_test.go b/cmd/generate/generate_test.go index 6f706de..f1130cb 100644 --- a/cmd/generate/generate_test.go +++ b/cmd/generate/generate_test.go @@ -278,9 +278,9 @@ type gitClientMock struct { CurrentBranchFnInvoked int IsRepoFn func() bool IsRepoFnInvoked int - LatestTagFn func() (string, error) + LatestTagFn func() string LatestTagFnInvoked int - AncestorTagFn func(include, exclude string) (string, error) + AncestorTagFn func(include, exclude string) string AncestorTagFnInvoked int SourceBranchFn func(commitHash string) (string, error) SourceBranchFnInvoked int @@ -294,11 +294,11 @@ func initGitClientMock(t *testing.T, latestTag, ancestorTag, currentBranch, sour IsRepoFn: func() bool { return true }, - LatestTagFn: func() (string, error) { - return latestTag, nil + LatestTagFn: func() string { + return latestTag }, - AncestorTagFn: func(include, exclude string) (string, error) { - return ancestorTag, nil + AncestorTagFn: func(include, exclude string) string { + return ancestorTag }, SourceBranchFn: func(commitHash string) (string, error) { assert.Equal(t, expectedCommitHash, commitHash) @@ -316,12 +316,12 @@ func (m *gitClientMock) IsRepo() bool { return m.IsRepoFn() } -func (m *gitClientMock) LatestTag() (string, error) { +func (m *gitClientMock) LatestTag() string { m.LatestTagFnInvoked += 1 return m.LatestTagFn() } -func (m *gitClientMock) AncestorTag(include, exclude string) (string, error) { +func (m *gitClientMock) AncestorTag(include, exclude string) string { m.AncestorTagFnInvoked += 1 return m.AncestorTagFn(include, exclude) } diff --git a/pkg/git/git.go b/pkg/git/git.go index 227ff1d..5a993f8 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -124,19 +124,19 @@ func (c *Client) SourceBranch(commitHash string) (string, error) { } // LatestTag returns the latest tag if found. -func (c *Client) LatestTag() (string, error) { - result, err := c.Clean(c.Run("-C", c.repoDir, "tag", "--points-at", "HEAD", "--sort", "-version:creatordate")) - if err != nil || result == "" { - return c.Clean(c.Run("-C", c.repoDir, "describe", "--tags", "--abbrev=0")) +func (c *Client) LatestTag() string { + result, _ := c.Clean(c.Run("-C", c.repoDir, "tag", "--points-at", "HEAD", "--sort", "-version:creatordate")) + if result == "" { + result, _ = c.Clean(c.Run("-C", c.repoDir, "describe", "--tags", "--abbrev=0")) } - return result, err + return result } // AncestorTag returns the previous tag that matches specific pattern if found. -func (c *Client) AncestorTag(include, exclude string) (string, error) { - result, err := c.Clean(c.Run("-C", c.repoDir, "describe", "--tags", "--abbrev=0", "--match", include, "--exclude", exclude)) - if err != nil || result == "" { - return c.Clean(c.Run("-C", c.repoDir, "rev-list", "--max-parents=0", "HEAD")) +func (c *Client) AncestorTag(include, exclude string) string { + result, _ := c.Clean(c.Run("-C", c.repoDir, "describe", "--tags", "--abbrev=0", "--match", include, "--exclude", exclude)) + if result == "" { + result, _ = c.Clean(c.Run("-C", c.repoDir, "rev-list", "--max-parents=0", "HEAD")) } - return result, err + return result } diff --git a/pkg/git/git_test.go b/pkg/git/git_test.go index ab7fd97..3aa33a3 100644 --- a/pkg/git/git_test.go +++ b/pkg/git/git_test.go @@ -113,8 +113,7 @@ func TestLatestTag(t *testing.T) { return "v2.4.79", nil } - value, err := gc.LatestTag() - require.NoError(t, err) + value := gc.LatestTag() assert.Equal(t, "v2.4.79", value) } @@ -138,8 +137,7 @@ func TestLatestTag_NoTagFound(t *testing.T) { return "", nil } - value, err := gc.LatestTag() - require.NoError(t, err) + value := gc.LatestTag() assert.Empty(t, value) } @@ -171,8 +169,7 @@ func TestAncestorTag(t *testing.T) { return test.ExpectedTag, nil } - value, err := gc.AncestorTag(test.IncludePattern, test.ExcludePattern) - require.NoError(t, err) + value := gc.AncestorTag(test.IncludePattern, test.ExcludePattern) assert.Equal(t, test.ExpectedTag, value) }) @@ -198,8 +195,7 @@ func TestAncestorTag_NoTagFound(t *testing.T) { return "", nil } - value, err := gc.AncestorTag("", "") - require.NoError(t, err) + value := gc.AncestorTag("", "") assert.Empty(t, value) }