From ca5724f5f73ac9e69819947374b7a56bc140f37c Mon Sep 17 00:00:00 2001 From: k1LoW Date: Wed, 18 Oct 2023 11:35:18 +0900 Subject: [PATCH] Consider vPrefix when retrieving the latest semver tag. --- tagpr.go | 7 +++++-- tagpr_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/tagpr.go b/tagpr.go index 3c27b86..1ae3823 100644 --- a/tagpr.go +++ b/tagpr.go @@ -42,8 +42,11 @@ type tagpr struct { func (tp *tagpr) latestSemverTag() string { vers := (&gitsemvers.Semvers{GitPath: tp.gitPath}).VersionStrings() - if len(vers) > 0 { - return vers[0] + vPrefix := (tp.cfg.vPrefix != nil && *tp.cfg.vPrefix) + for _, v := range vers { + if strings.HasPrefix(v, "v") == vPrefix { + return v + } } return "" } diff --git a/tagpr_test.go b/tagpr_test.go index 6ca4685..e8fe2a5 100644 --- a/tagpr_test.go +++ b/tagpr_test.go @@ -310,6 +310,35 @@ func TestGeneratenNextLabels(t *testing.T) { } } +func TestLatestSemverTag(t *testing.T) { + tests := []struct { + name string + vPrefix bool + wantVer bool + }{ + {"github.com/Songmu/tagpr has a semver tag with 'v' prefix", true, true}, + {"github.com/Songmu/tagpr has no semver tag without 'v' prefix", false, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tp, err := newTagPR(context.Background(), &commander{ + gitPath: "git", outStream: os.Stdout, errStream: os.Stderr, dir: "."}, + ) + if err != nil { + t.Error(err) + return + } + tp.cfg = &config{ + vPrefix: &tt.vPrefix, + } + got := tp.latestSemverTag() + if (got != "") != tt.wantVer { + t.Errorf("got: %s, wantVer: %t", got, tt.wantVer) + } + }) + } +} + func newGithubLabel(name *string) *github.Label { return &github.Label{ ID: new(int64),